001/*
002 * Copyright (c) 2012, 2013, Werner Keil, Credit Suisse (Anatole Tresch). Licensed under the Apache
003 * License, Version 2.0 (the "License"); you may not use this file except in compliance with the
004 * License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
005 * Unless required by applicable law or agreed to in writing, software distributed under the License
006 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
007 * or implied. See the License for the specific language governing permissions and limitations under
008 * the License. Contributors: Anatole Tresch - initial version.
009 */
010package org.javamoney.tck;
011
012import org.javamoney.tck.tests.*;
013import org.javamoney.tck.tests.conversion.ConvertingAmountsTest;
014import org.javamoney.tck.tests.conversion.ExchangeRatesAndRateProvidersTest;
015import org.javamoney.tck.tests.conversion.MonetaryConversionsTest;
016import org.javamoney.tck.tests.conversion.ProviderChainsTest;
017import org.javamoney.tck.tests.format.FormattingMonetaryAmountsTest;
018import org.jboss.test.audit.annotations.SpecAssertion;
019import org.testng.ITestResult;
020import org.testng.TestListenerAdapter;
021import org.testng.TestNG;
022import org.testng.annotations.Test;
023import org.testng.reporters.VerboseReporter;
024import org.testng.xml.XmlClass;
025import org.testng.xml.XmlSuite;
026import org.testng.xml.XmlTest;
027
028import java.io.*;
029import java.lang.reflect.Method;
030import java.util.ArrayList;
031import java.util.List;
032
033/**
034 * Main class for executing the JSR 354 TCK.
035 * Created by Anatole on 12.06.2014.
036 */
037public class TCKRunner extends XmlSuite{
038    public TCKRunner(){
039        setName("JSR354-TCK 1.0");
040        XmlTest test = new XmlTest(this);
041        test.setName("TCK/Test Setup");
042        List<XmlClass> classes = new ArrayList<>();
043        classes.add(new XmlClass(TCKTestSetup.class));
044        classes.add(new XmlClass(ModellingCurrenciesTest.class));
045        classes.add(new XmlClass(ModellingMonetaryAmountsTest.class));
046        classes.add(new XmlClass(CreatingMonetaryAmountsTest.class));
047        classes.add(new XmlClass(ExternalizingNumericValueTest.class));
048        classes.add(new XmlClass(FunctionalExtensionPointsTest.class));
049        classes.add(new XmlClass(AccessingCurrenciesAmountsRoundingsTest.class));
050        classes.add(new XmlClass(MonetaryConversionsTest.class));
051        classes.add(new XmlClass(ExchangeRatesAndRateProvidersTest.class));
052        classes.add(new XmlClass(ConvertingAmountsTest.class));
053        classes.add(new XmlClass(ProviderChainsTest.class));
054        classes.add(new XmlClass(FormattingMonetaryAmountsTest.class));
055        test.setXmlClasses(classes);
056    }
057
058    public static void main(String... args){
059        System.out.println("-- JSR 354 TCK started --");
060        List<XmlSuite> suites = new ArrayList<>();
061        suites.add(new TCKRunner());
062        TestNG tng = new TestNG();
063        tng.setXmlSuites(suites);
064        tng.setOutputDirectory("./tck-results");
065//        tng.addListener(new VerboseReporter());
066        File file = new File(System.getProperty("java.io.tmpdir"), "tck-results.txt");
067        TCKReporter rep = new TCKReporter(file);
068        System.out.println("Writing to file " + file.getAbsolutePath() + " ...");
069        tng.addListener(rep);
070        tng.run();
071        rep.writeSummary();
072        System.out.println("-- JSR 354 TCK finished --");
073    }
074
075    public static final class TCKReporter extends TestListenerAdapter{
076        private int count = 0;
077        private int skipped = 0;
078        private int failed = 0;
079        private int success = 0;
080
081        private StringWriter internalBuffer = new StringWriter(3000);
082        private FileWriter w;
083
084        public TCKReporter(File file){
085            try{
086                if(!file.exists()){
087                    file.createNewFile();
088                }
089                w = new FileWriter(file);
090                w.write("*****************************************************************************************\n");
091                w.write("**** JSR 354 - Money & Currency, Technical Compatibility Kit, version 1.0\n");
092                w.write("*****************************************************************************************\n\n");
093                w.write("Executed on " + new java.util.Date() +"\n\n" );
094
095                // System.out:
096                internalBuffer.write("*****************************************************************************************\n");
097                internalBuffer.write("**** JSR 354 - Money & Currency, Technical Compatibility Kit, version 1.0\n");
098                internalBuffer.write("*****************************************************************************************\n\n");
099                internalBuffer.write("Executed on " + new java.util.Date() + "\n\n");
100            }
101            catch(IOException e){
102                e.printStackTrace();
103                System.exit(-1);
104            }
105        }
106
107        @Override
108        public void onTestFailure(ITestResult tr){
109            failed++;
110            String location = tr.getTestClass().getRealClass().getSimpleName()+'#'+tr.getMethod().getMethodName();
111            try{
112                Method realTestMethod = tr.getMethod().getMethod();
113                Test testAnnot = realTestMethod.getAnnotation(Test.class);
114                if(testAnnot!=null && testAnnot.description()!=null && !testAnnot.description().isEmpty()){
115                    if(tr.getThrowable()!=null){
116                        StringWriter sw = new StringWriter();
117                        PrintWriter w = new PrintWriter(sw);
118                        tr.getThrowable().printStackTrace(w);
119                        w.flush();
120                        log("[FAILED]  " + testAnnot.description() + "("+location+"):\n"+sw.toString());
121                    }
122                    else{
123                        log("[FAILED]  " + testAnnot.description()+ "("+location+")");
124                    }
125                }
126                else{
127
128                    if(tr.getThrowable()!=null){
129                        StringWriter sw = new StringWriter();
130                        PrintWriter w = new PrintWriter(sw);
131                        tr.getThrowable().printStackTrace(w);
132                        w.flush();
133                        log("[FAILED]  " + location + ":\n"+sw.toString());
134                    }
135                    else{
136                        log("[FAILED]  " + location);
137                    }
138                }
139            }
140            catch(IOException e){
141                throw new IllegalStateException("IO Error", e);
142            }
143        }
144
145        @Override
146        public void onTestSkipped(ITestResult tr){
147            skipped++;
148            String location = tr.getTestClass().getRealClass().getSimpleName()+'#'+tr.getMethod().getMethodName();
149            try{
150                Method realTestMethod = tr.getMethod().getMethod();
151                Test specAssert = realTestMethod.getAnnotation(Test.class);
152                if(specAssert!=null && specAssert.description()!=null && !specAssert.description().isEmpty()){
153                    log("[SKIPPED] " + specAssert.description()+ "("+location+")");
154                }
155                else{
156                    log("[SKIPPED] " + location);
157                }
158            }
159            catch(IOException e){
160                throw new IllegalStateException("IO Error", e);
161            }
162        }
163
164        @Override
165        public void onTestSuccess(ITestResult tr){
166            success++;
167            String location = tr.getTestClass().getRealClass().getSimpleName()+'#'+tr.getMethod().getMethodName();
168            try{
169                Method realTestMethod = tr.getMethod().getMethod();
170                Test specAssert = realTestMethod.getAnnotation(Test.class);
171                if(specAssert!=null && specAssert.description()!=null && !specAssert.description().isEmpty()){
172                    log("[SUCCESS] " + specAssert.description()+ "("+location+")");
173                }
174                else{
175                    log("[SUCCESS] " + location);
176                }
177            }
178            catch(IOException e){
179                throw new IllegalStateException("IO Error", e);
180            }
181        }
182
183        private void log(String text) throws IOException {
184            count++;
185            w.write(text);
186            w.write('\n');
187            internalBuffer.write(text);
188            internalBuffer.write('\n');
189        }
190
191        public void writeSummary(){
192            try{
193                log("\nJSR 354 TCP version 1.0 Summary");
194                log("-------------------------------");
195                log("\nTOTAL TESTS EXECUTED : " + count);
196                log("TOTAL TESTS SKIPPED  : " + skipped);
197                log("TOTAL TESTS SUCCESS  : " + success);
198                log("TOTAL TESTS FAILED   : " + failed);
199                w.flush();
200                w.close();
201                internalBuffer.flush();
202                System.out.println();
203                System.out.println(internalBuffer);
204            }
205            catch(IOException e){
206                throw new IllegalStateException("IO Error", e);
207            }
208        }
209    }
210
211
212}