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.ByteArrayOutputStream; 19import java.io.PrintStream; 20import java.io.Serializable; 21import java.util.Collection; 22 23import junit.framework.TestCase; 24 25import org.objenesis.ObjenesisSerializer; 26import org.objenesis.ObjenesisStd; 27 28/** 29 * Integration test for Objenesis. Should pass successfully on every supported JVM for all Objenesis 30 * interface implementation. 31 * 32 * @author Henri Tremblay 33 */ 34public class ObjenesisTest extends TestCase { 35 36 public static class ErrorHandler implements CandidateLoader.ErrorHandler { 37 public void classNotFound(String name) { 38 fail("Class not found : " + name); 39 } 40 } 41 42 public static class JUnitReporter implements Reporter { 43 44 private String currentObjenesis; 45 46 private String currentCandidate; 47 48 public void startTests(String platformDescription, Collection allCandidates, 49 Collection allInstantiators) { 50 } 51 52 public void startTest(String candidateDescription, String objenesisDescription) { 53 currentCandidate = candidateDescription; 54 currentObjenesis = objenesisDescription; 55 } 56 57 public void endObjenesis(String description) { 58 } 59 60 public void endTests() { 61 } 62 63 public void exception(Exception exception) { 64 ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 65 PrintStream out = new PrintStream(buffer); 66 out.println("Exception when instantiating " + currentCandidate + " with " 67 + currentObjenesis + ": "); 68 exception.printStackTrace(out); 69 fail(buffer.toString()); 70 } 71 72 public void result(boolean instantiatedObject) { 73 assertTrue("Instantiating " + currentCandidate + " with " + currentObjenesis + " failed", 74 instantiatedObject); 75 } 76 77 public void endTest() { 78 } 79 } 80 81 static class MockSuperClass { 82 private final boolean superConstructorCalled; 83 public MockSuperClass() { 84 superConstructorCalled = true; 85 } 86 public boolean isSuperConstructorCalled() { 87 return superConstructorCalled; 88 } 89 } 90 91 static class MockClass extends MockSuperClass implements Serializable { 92 private static final long serialVersionUID = 1L; 93 private final boolean constructorCalled; 94 public MockClass() { 95 super(); 96 constructorCalled = true; 97 } 98 public boolean isConstructorCalled() { 99 return constructorCalled; 100 } 101 } 102 103 private TCK tck = null; 104 105 private CandidateLoader candidateLoader = null; 106 107 protected void setUp() throws Exception { 108 super.setUp(); 109 110 tck = new TCK(); 111 112 candidateLoader = new CandidateLoader(tck, getClass().getClassLoader(), new ErrorHandler()); 113 } 114 115 protected void tearDown() throws Exception { 116 candidateLoader = null; 117 tck = null; 118 super.tearDown(); 119 } 120 121 public void testObjenesisStd() throws Exception { 122 candidateLoader.loadFromResource(getClass(), "candidates/candidates.properties"); 123 tck.registerObjenesisInstance(new ObjenesisStd(), "Objenesis standard"); 124 tck.runTests(new JUnitReporter()); 125 } 126 127 public void testObjenesisSerializer() throws Exception { 128 candidateLoader.loadFromResource(getClass(), "candidates/serializable-candidates.properties"); 129 tck.registerObjenesisInstance(new ObjenesisSerializer(), "Objenesis serializer"); 130 tck.runTests(new JUnitReporter()); 131 } 132 133 public void testObjenesisSerializerParentConstructorCalled() throws Exception { 134 Object result = new ObjenesisSerializer().newInstance(MockClass.class); 135 assertEquals(MockClass.class, result.getClass()); 136 MockClass mockObject = (MockClass) result; 137 assertTrue(mockObject.isSuperConstructorCalled()); 138 assertFalse(mockObject.isConstructorCalled()); 139 } 140} 141