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.util.ArrayList;
19import java.util.Collection;
20import java.util.HashMap;
21import java.util.Iterator;
22import java.util.List;
23import java.util.Map;
24
25import org.objenesis.Objenesis;
26
27/**
28 * <b>Technology Compatibility Kit</b> (TCK) for {@link Objenesis}s.
29 * <p/>
30 * This TCK accepts a set of candidate classes (class it attempts to instantiate) and a set of
31 * Objenesis implementations. It then tries instantiating every candidate with every Objenesis
32 * implementations, reporting the results to a {@link Reporter}.
33 * <h3>Example usage</h3>
34 *
35 * <pre>
36 * TCK tck = new TCK();
37 * // register candidate classes.
38 * tck.registerCandidate(SomeClass.class, &quot;A basic class&quot;);
39 * tck.registerCandidate(SomeEvil.class, &quot;Something evil&quot;);
40 * tck.registerCandidate(NotEvil.class, &quot;Something nice&quot;);
41 * // register Objenesis instances.
42 * tck.registerObjenesisInstance(new ObjenesisStd(), &quot;Objenesis&quot;);
43 * tck.registerObjenesisInstance(new ObjenesisSerializaer(), &quot;Objenesis for serialization&quot;);
44 * // go!
45 * Reporter reporter = new TextReporter(System.out, System.err);
46 * tck.runTests(reporter);
47 * </pre>
48 *
49 * @author Joe Walnes
50 * @see org.objenesis.instantiator.ObjectInstantiator
51 * @see Reporter
52 * @see Main
53 */
54public class TCK {
55
56   private final List objenesisInstances = new ArrayList();
57   private final List candidates = new ArrayList();
58   private final Map descriptions = new HashMap();
59
60   /**
61    * Register a candidate class to attempt to instantiate.
62    *
63    * @param candidateClass Class to attempt to instantiate
64    * @param description Description of the class
65    */
66   public void registerCandidate(Class candidateClass, String description) {
67      candidates.add(candidateClass);
68      descriptions.put(candidateClass, description);
69   }
70
71   /**
72    * Register an Objenesis instance to use when attempting to instantiate a class.
73    *
74    * @param objenesis Tested Objenesis instance
75    * @param description Description of the Objenesis instance
76    */
77   public void registerObjenesisInstance(Objenesis objenesis, String description) {
78      objenesisInstances.add(objenesis);
79      descriptions.put(objenesis, description);
80   }
81
82   /**
83    * Run all TCK tests.
84    *
85    * @param reporter Where to report the results of the test to.
86    */
87   public void runTests(Reporter reporter) {
88      reporter.startTests(describePlatform(), findAllDescriptions(candidates, descriptions),
89         findAllDescriptions(objenesisInstances, descriptions));
90
91      for(Iterator i = candidates.iterator(); i.hasNext();) {
92         Class candidateClass = (Class) i.next();
93         String candidateDescription = (String) descriptions.get(candidateClass);
94
95         for(Iterator j = objenesisInstances.iterator(); j.hasNext();) {
96            Objenesis objenesis = (Objenesis) j.next();
97
98            String objenesisDescription = (String) descriptions.get(objenesis);
99
100            reporter.startTest(candidateDescription, objenesisDescription);
101
102            runTest(reporter, candidateClass, objenesis, candidateDescription);
103
104            reporter.endTest();
105         }
106      }
107      reporter.endTests();
108   }
109
110   private void runTest(Reporter reporter, Class candidate, Objenesis objenesis,
111      String candidateDescription) {
112      try {
113         Object instance = objenesis.newInstance(candidate);
114         boolean success = instance != null && instance.getClass() == candidate;
115         reporter.result(success);
116      }
117      catch(Exception e) {
118         reporter.exception(e);
119      }
120   }
121
122   private Collection findAllDescriptions(List keys, Map descriptions) {
123      List results = new ArrayList(keys.size());
124      for(int i = 0; i < keys.size(); i++) {
125         results.add(descriptions.get(keys.get(i)));
126      }
127      return results;
128   }
129
130   /**
131    * Describes the platform. Outputs Java version and vendor. To change this behavior, override
132    * this method.
133    *
134    * @return Description of the current platform
135    */
136   protected String describePlatform() {
137      return "Java " + System.getProperty("java.specification.version") + " ("
138         + System.getProperty("java.vm.vendor") + " " + System.getProperty("java.vm.name") + " "
139         + System.getProperty("java.vm.version") + " " + System.getProperty("java.runtime.version")
140         + ")";
141   }
142
143}
144