77
88/**
99 * Copyright 2008, 2009, 2010, 2011 Viera K. Proulx, Matthias Felleisen
10- * This program is distributed under the terms of the
10+ * This program is distributed under the terms of the
1111 * GNU Lesser General Public License (LGPL)
1212 */
1313
2222 * The main method instantiates this class and invokes the
2323 * <CODE>{@link Tester Tester}</CODE> on this new instance.
2424 * </P>
25- *
25+ *
2626 * @author Viera K. Proulx, Matthias Felleisen, Weston Jossey
27- * @since 3 March 2008, 16 October 2008, 20 December 2008, 13 May 2009,
27+ * @since 3 March 2008, 16 October 2008, 20 December 2008, 13 May 2009,
2828 * 9 February 2010, 18 February 2011
29- *
29+ *
3030 */
3131public class Main {
3232 /**
3333 * <p>Method that creates an instance of the <code>Tester</code> and an
3434 * instance of the class that defines the tests, invokes the test evaluation
3535 * by the <code>Tester</code>, and reports the results.</p>
36- * <p>The method scans all classes for <code>@Example</code> annotation
37- * and looks in those classes for methods annotated with the
36+ * <p>The method scans all classes for <code>@Example</code> annotation
37+ * and looks in those classes for methods annotated with the
3838 * <code>@TestMethod</code> annotation. Tests for each class are evaluated
3939 * and reported separately/consecutively.</p>
40- * <p>If there are no annotated classes the class <code>Examples</code> or
40+ * <p>If there are no annotated classes the class <code>Examples</code> or
4141 * the class whose name is given as the argument is the only test class.</p>
42- *
43- * @param argv
44- * [optional] the name of the class that defines the tests
45- * @throws Exception
42+ *
43+ * @param argv [optional] the name of the class that defines the tests
44+ * @throws Exception
4645 */
4746 public static void main (String argv []) throws Exception {
4847 //pool = Executors.newCachedThreadPool(); //For concurrency
4948 //Instrumentor inst = new Instrumentor();
50- //Set<String> instrumentedClasses =
49+ //Set<String> instrumentedClasses =
5150 // new AnnotationScanner(tester.cobertura.Instrument.class).scan();
52-
51+
5352 //if(instrumentedClasses != null){
5453 // inst.instrumentClasses();
5554 //}
5655
5756 boolean noTests = true ; // set to false if Annotations found test to run
58-
57+
5958 // set up the scanner to look for '@Example' class annotation
6059 AnnotationScanner scanner = new AnnotationScanner (
6160 tester .Example .class );
6261 Set <String > classes = null ;
63-
62+
6463 // Find all classes annotated with '@Example' and in each run all tests
6564 try {
6665 classes = scanner .scan ();
67-
66+
6867 // get the name of the class that defines the tests
6968 if (classes != null ){
7069 for (String clazz : classes ){
@@ -81,7 +80,7 @@ public static void main(String argv[]) throws Exception {
8180 }finally {
8281 //if(instrumentedClasses != null){
8382 // inst.generateReports();
84- //}
83+ //}
8584 //System.out.println("Done with the annotation scan");
8685 }
8786
@@ -91,7 +90,7 @@ public static void main(String argv[]) throws Exception {
9190 classname = "Examples" ;
9291 else
9392 classname = argv [0 ];
94-
93+
9594 // System.out.println("Class name for tester.Main to use is: " + classname);
9695
9796 // to represent an instance of the class that defines the tests
@@ -103,25 +102,25 @@ public static void main(String argv[]) throws Exception {
103102 // Note: if we already ran other tests, do not report errors here
104103 try {
105104 // get the specified 'Examples' class
106- examples = Class . forName (classname );
105+ examples = Reflector . classForName (classname );
107106
108107 //System.out.println("Tests are defined in the class " + examples.getName());
109-
108+
110109 if (examples == null && noTests )
111110 throw new ClassNotFoundException ("could not find the class " + classname );
112111 // else
113112 // System.out.println("found class " + examples.getName());
114113
115114 // create an instance of this class
116- try {
117- Constructor <?> construct = examples
118- .getDeclaredConstructor ();
119- construct . setAccessible ( true );
120- o = construct .newInstance ();
115+ try {
116+ Constructor <?> constructor = examples
117+ .getDeclaredConstructor ();
118+ Reflector . ensureIsAccessible ( constructor );
119+ o = constructor .newInstance ();
121120
122121 System .out .println ("\n \n ---------------------------------\n " + "" +
123122 "Tests for the class: " + classname );
124-
123+
125124 // handle all exceptions when new instance cannot be created
126125 } catch (Throwable exception ) {
127126
@@ -134,13 +133,13 @@ public static void main(String argv[]) throws Exception {
134133 excName = exception .getClass ().getName ();
135134 excMessage = exception .getMessage ();
136135 }
137-
136+
138137 if (excName .equals ("java.lang.NoSuchMethodException" )){
139138 System .out .println ("no default constructor: "
140139 + excMessage );
141140 } else if (excName .equals ("java.lang.InvocationTargetException" )){
142141 System .out .println (
143- "Invocation -- Exception thrown by the constructor: \n " +
142+ "Invocation -- Exception thrown by the constructor: \n " +
144143 "Exception: " + excName + "\n Message: " +
145144 excMessage );
146145 } else if (excName .equals ("java.lang.InstantiationException" )){
@@ -150,13 +149,13 @@ public static void main(String argv[]) throws Exception {
150149 System .out .println (
151150 "Exception: " + excName + "\n Message: " +
152151 excMessage );
153- } else
152+ } else
154153 System .out .println (
155- "Invocation -- Exception thrown by the constructor: \n " +
154+ "Invocation -- Exception thrown by the constructor: \n " +
156155 "Exception: " + excName + "\n Message: " +
157- excMessage );
158- }
159-
156+ excMessage );
157+ }
158+
160159 // run tests if the instance was successfully constructed
161160 if (o != null ) {
162161 Tester t = new Tester ();
@@ -173,19 +172,19 @@ public static void main(String argv[]) throws Exception {
173172 "constructor that does not fail during instantiation." );
174173 }
175174 }
176-
175+
177176 // report error if the class with the given classname does not exist
178177 catch (ClassNotFoundException c ) {
179178 if (noTests ){
180179 if (classname != null )
181- System .err .println (classname + " class doesn't exist.\n " +
180+ System .err .println (classname + " class doesn't exist.\n " +
182181 "Please check to make sure that the argment that gives the name" +
183182 " of your examples class has the correct spelling." );
184183 else
185184 System .err .println (c .getMessage ());
186185 }
187- }
188-
186+ }
187+
189188 // finish up
190189 finally {
191190 // System.out.println("done");
0 commit comments