1/**
2 * Copyright 2006-2013 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.objenesis.tck;
17
18import java.io.IOException;
19import java.io.Serializable;
20
21import org.objenesis.Objenesis;
22import org.objenesis.ObjenesisSerializer;
23import org.objenesis.ObjenesisStd;
24
25/**
26 * Command line launcher for Technology Compatibility Kit (TCK).
27 *
28 * @author Joe Walnes
29 * @see TCK
30 */
31public class Main {
32
33   private static class MockSuperClass {
34      private final boolean superConstructorCalled;
35
36      public MockSuperClass() {
37         superConstructorCalled = true;
38      }
39
40      public boolean isSuperConstructorCalled() {
41         return superConstructorCalled;
42      }
43   }
44
45   private static class MockClass extends MockSuperClass implements Serializable {
46      private static final long serialVersionUID = 1L;
47
48      private final boolean constructorCalled;
49
50      public MockClass() {
51         constructorCalled = true;
52      }
53
54      public boolean isConstructorCalled() {
55         return constructorCalled;
56      }
57   }
58
59   /**
60    * Main class of the TCK. Can also be called as a normal method from an application server.
61    *
62    * @param args No parameters are required
63    * @throws IOException When the TCK fails to read properties' files.
64    */
65   public static void main(String[] args) throws IOException {
66
67      TextReporter reporter = new TextReporter(System.out, System.err);
68
69      runTest(new ObjenesisStd(), reporter, "Objenesis std", "candidates/candidates.properties");
70      runTest(new ObjenesisSerializer(), reporter, "Objenesis serializer",
71         "candidates/serializable-candidates.properties");
72
73      boolean result = runParentConstructorTest();
74
75      reporter.printResult(result);
76   }
77
78   private static boolean runParentConstructorTest() {
79      try {
80         Object result = new ObjenesisSerializer().newInstance(MockClass.class);
81         MockClass mockObject = (MockClass) result;
82         return mockObject.isSuperConstructorCalled() && !mockObject.isConstructorCalled();
83      }
84      catch(Exception e) {
85         System.err.println("--- Not serializable parent constructor called ---");
86         e.printStackTrace(System.err);
87         return false;
88      }
89   }
90
91   private static void runTest(Objenesis objenesis, Reporter reporter, String description,
92      String candidates) throws IOException {
93      TCK tck = new TCK();
94      tck.registerObjenesisInstance(objenesis, description);
95
96      CandidateLoader candidateLoader = new CandidateLoader(tck, Main.class.getClassLoader(),
97         new CandidateLoader.LoggingErrorHandler(System.err));
98
99      candidateLoader.loadFromResource(Main.class, candidates);
100
101      tck.runTests(reporter);
102   }
103
104}
105