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.Collection;
19
20import junit.framework.TestCase;
21
22import org.objenesis.Objenesis;
23import org.objenesis.instantiator.ObjectInstantiator;
24
25/**
26 * @author Joe Walnes
27 * @author Henri Tremblay
28 */
29public class TCKTest extends TestCase {
30
31   public static class StubbedInstantiator1 implements Objenesis {
32      public Object newInstance(Class clazz) {
33         return null;
34      }
35
36      public ObjectInstantiator getInstantiatorOf(Class clazz) {
37         return null;
38      }
39   }
40
41   public static class StubbedInstantiator2 implements Objenesis {
42      public Object newInstance(Class clazz) {
43         return null;
44      }
45
46      public ObjectInstantiator getInstantiatorOf(Class clazz) {
47         return null;
48      }
49   }
50
51   public void testReportsAllCandidatesAndInstantiatorCombinationsToReporter() {
52      // Given... a TCK with some candidate classes: A, B and C.
53      TCK tck = new TCK();
54
55      tck.registerCandidate(CandidateA.class, "Candidate A");
56      tck.registerCandidate(CandidateB.class, "Candidate B");
57      tck.registerCandidate(CandidateC.class, "Candidate C");
58
59      // And... two ObjectInstantiators registered
60      tck.registerObjenesisInstance(new StubbedInstantiator1(), "Instantiator1");
61      tck.registerObjenesisInstance(new StubbedInstantiator2(), "Instantiator2");
62
63      // When... the TCK tests are run
64      Reporter reporter = new RecordingReporter();
65      tck.runTests(reporter);
66
67      // Expect... the reporter to have received a sequence of calls
68      // notifying it of what the TCK is doing.
69      assertEquals("" + "startTests()\n" + "startTest('Candidate A', 'Instantiator1')\n"
70         + "result(false)\n" + "endTest()\n" + "startTest('Candidate A', 'Instantiator2')\n"
71         + "result(false)\n" + "endTest()\n" + "startTest('Candidate B', 'Instantiator1')\n"
72         + "result(false)\n" + "endTest()\n" + "startTest('Candidate B', 'Instantiator2')\n"
73         + "result(false)\n" + "endTest()\n" + "startTest('Candidate C', 'Instantiator1')\n"
74         + "result(false)\n" + "endTest()\n" + "startTest('Candidate C', 'Instantiator2')\n"
75         + "result(false)\n" + "endTest()\n" + "endTests()\n", reporter.toString());
76   }
77
78   public void testReportsSuccessIfCandidateCanBeInstantiated() {
79      // Given... a TCK with some candidate classes: A, B and C.
80      TCK tck = new TCK();
81
82      tck.registerCandidate(CandidateA.class, "Candidate A");
83      tck.registerCandidate(CandidateB.class, "Candidate B");
84
85      // And... a single ObjectInsantiator registered that can instantiate
86      // A but not B.
87      tck.registerObjenesisInstance(new SelectiveInstantiator(), "instantiator");
88
89      // When... the TCK tests are run
90      Reporter reporter = new RecordingReporter();
91      tck.runTests(reporter);
92
93      // Expect... the reporter to be notified that A succeeded
94      // but B failed.
95      assertEquals("" + "startTests()\n" + "startTest('Candidate A', 'instantiator')\n" + // A
96         "result(true)\n" + // true
97         "endTest()\n" + "startTest('Candidate B', 'instantiator')\n" + // B
98         "result(false)\n" + // false
99         "endTest()\n" + "endTests()\n", reporter.toString());
100   }
101
102   // Some sample classes used for testing.
103
104   public static class SelectiveInstantiator implements Objenesis {
105      public Object newInstance(Class clazz) {
106         return clazz == CandidateA.class ? new CandidateA() : null;
107      }
108
109      public ObjectInstantiator getInstantiatorOf(Class clazz) {
110         return null;
111      }
112   }
113
114   public static class CandidateA {
115   }
116
117   public static class CandidateB {
118   }
119
120   public static class CandidateC {
121   }
122
123   /**
124    * A poor man's mock. Using a recording test double to verify interactions between the TCK and
125    * the Recorder. <p/> Note: This test case could be simplified by using a mock object library.
126    * However, I wanted to simplify dependencies - particularly as in the future, the mock libraries
127    * may depend on objenesis - getting into an awkward cyclical dependency situation. -Joe.
128    */
129   private static class RecordingReporter implements Reporter {
130
131      private final StringBuffer log = new StringBuffer();
132
133      public void startTests(String platformDescription, Collection allCandidates,
134         Collection allInstantiators) {
135         log.append("startTests()\n");
136      }
137
138      public void startTest(String candidateDescription, String objenesisDescription) {
139         log.append("startTest('").append(candidateDescription).append("', '").append(
140            objenesisDescription).append("')\n");
141      }
142
143      public void result(boolean instantiatedObject) {
144         log.append("result(").append(instantiatedObject).append(")\n");
145      }
146
147      public void exception(Exception exception) {
148         log.append("exception()\n");
149      }
150
151      public void endTest() {
152         log.append("endTest()\n");
153      }
154
155      public void endTests() {
156         log.append("endTests()\n");
157      }
158
159      public String toString() {
160         return log.toString();
161      }
162   }
163}
164