1 /** 2 * Copyright 2006-2017 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 */ 16 package org.objenesis; 17 18 import org.junit.Rule; 19 import org.junit.Test; 20 import org.junit.rules.ExpectedException; 21 import org.objenesis.instantiator.ObjectInstantiator; 22 import org.objenesis.strategy.InstantiatorStrategy; 23 24 import static org.junit.Assert.*; 25 26 /** 27 * @author Henri Tremblay 28 */ 29 public class ObjenesisTest { 30 31 @Rule 32 public ExpectedException expectedException = ExpectedException.none(); 33 34 @Test testObjenesis()35 public final void testObjenesis() { 36 Objenesis o = new ObjenesisStd(); 37 assertEquals( 38 "org.objenesis.ObjenesisStd using org.objenesis.strategy.StdInstantiatorStrategy with caching", 39 o.toString()); 40 } 41 42 @Test testObjenesis_WithoutCache()43 public final void testObjenesis_WithoutCache() { 44 Objenesis o = new ObjenesisStd(false); 45 assertEquals( 46 "org.objenesis.ObjenesisStd using org.objenesis.strategy.StdInstantiatorStrategy without caching", 47 o.toString()); 48 49 assertEquals(o.getInstantiatorOf(getClass()).newInstance().getClass(), getClass()); 50 } 51 52 @Test testNewInstance()53 public final void testNewInstance() { 54 Objenesis o = new ObjenesisStd(); 55 assertEquals(getClass(), o.newInstance(getClass()).getClass()); 56 } 57 58 @Test testGetInstantiatorOf()59 public final void testGetInstantiatorOf() { 60 Objenesis o = new ObjenesisStd(); 61 ObjectInstantiator<?> i1 = o.getInstantiatorOf(getClass()); 62 // Test instance creation 63 assertEquals(getClass(), i1.newInstance().getClass()); 64 65 // Test caching 66 ObjectInstantiator<?> i2 = o.getInstantiatorOf(getClass()); 67 assertSame(i1, i2); 68 } 69 70 @Test testGetInstantiatorOf_primitive()71 public final void testGetInstantiatorOf_primitive() { 72 Objenesis o = new ObjenesisStd(); 73 expectedException.expect(IllegalArgumentException.class); 74 o.getInstantiatorOf(long.class); 75 } 76 77 @Test testToString()78 public final void testToString() { 79 Objenesis o = new ObjenesisStd() {}; 80 assertEquals( 81 "org.objenesis.ObjenesisTest$1 using org.objenesis.strategy.StdInstantiatorStrategy with caching", 82 o.toString()); 83 } 84 } 85 86 class MyStrategy implements InstantiatorStrategy { newInstantiatorOf(Class<T> type)87 public <T> ObjectInstantiator<T> newInstantiatorOf(Class<T> type) { 88 return null; 89 } 90 } 91