| 1 |
/* |
| 2 |
* Created on May 17, 2007 |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the |
| 5 |
* License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" |
| 10 |
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language |
| 11 |
* governing permissions and limitations under the License. |
| 12 |
* |
| 13 |
* Copyright @2007-2009 the original author or authors. |
| 14 |
*/ |
| 15 |
package org.fest.reflect.constructor; |
| 16 |
|
| 17 |
import static org.fest.reflect.util.ExpectedFailures.expectIllegalArgumentException; |
| 18 |
import static org.fest.reflect.util.ExpectedFailures.expectNullPointerException; |
| 19 |
|
| 20 |
import static org.junit.Assert.assertEquals; |
| 21 |
import static org.junit.Assert.assertNotNull; |
| 22 |
import static org.junit.Assert.assertNull; |
| 23 |
import static org.junit.Assert.assertTrue; |
| 24 |
import static org.junit.Assert.fail; |
| 25 |
|
| 26 |
import java.lang.reflect.Constructor; |
| 27 |
|
| 28 |
import org.junit.Test; |
| 29 |
|
| 30 |
import org.fest.reflect.Person; |
| 31 |
import org.fest.reflect.exception.ReflectionError; |
| 32 |
import org.fest.test.CodeToTest; |
| 33 |
|
| 34 |
/** |
| 35 |
* Tests for the fluent interface for constructors. |
| 36 |
* |
| 37 |
* @author Alex Ruiz |
| 38 |
* @author Yvonne Wang |
| 39 |
*/ |
| 40 |
public class Constructor_Test { |
| 41 |
|
| 42 |
@Test |
| 43 |
public void should_create_new_instance_with_default_constructor() { |
| 44 |
Person person = TargetType.startConstructorAccess().in(Person.class).newInstance(); |
| 45 |
assertNotNull(person); |
| 46 |
assertNull(person.getName()); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void should_create_new_instance_using_given_constructor_parameters() { |
| 51 |
Person person = TargetType.startConstructorAccess().withParameterTypes(String.class).in(Person.class).newInstance("Yoda"); |
| 52 |
assertNotNull(person); |
| 53 |
assertEquals("Yoda", person.getName()); |
| 54 |
} |
| 55 |
|
| 56 |
@Test |
| 57 |
public void should_return_real_constructor() { |
| 58 |
Constructor<Person> constructor = TargetType.startConstructorAccess().withParameterTypes(String.class).in(Person.class) |
| 59 |
.info(); |
| 60 |
assertNotNull(constructor); |
| 61 |
Class<?>[] parameterTypes = constructor.getParameterTypes(); |
| 62 |
assertEquals(1, parameterTypes.length); |
| 63 |
assertEquals(String.class, parameterTypes[0]); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void should_throw_error_if_parameter_type_array_is_null() { |
| 68 |
expectNullPointerException("The array of parameter types should not be null").on(new CodeToTest() { |
| 69 |
public void run() { |
| 70 |
TargetType.startConstructorAccess().withParameterTypes((Class<?>[]) null).in(Person.class).info(); |
| 71 |
} |
| 72 |
}); |
| 73 |
} |
| 74 |
|
| 75 |
@Test(expected = ReflectionError.class) |
| 76 |
public void should_throw_error_if_constructor_was_not_found() { |
| 77 |
Class<Integer> illegalType = Integer.class; |
| 78 |
TargetType.startConstructorAccess().withParameterTypes(illegalType).in(Person.class); |
| 79 |
} |
| 80 |
|
| 81 |
@Test(expected = IllegalArgumentException.class) |
| 82 |
public void should_throw_error_if_instance_was_not_created() { |
| 83 |
int illegalArg = 8; |
| 84 |
TargetType.startConstructorAccess().withParameterTypes(String.class).in(Person.class).newInstance(illegalArg); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void should_rethrow_RuntimeException_thrown_by_constructor() { |
| 89 |
expectIllegalArgumentException("The name of a person cannot be a number").on(new CodeToTest() { |
| 90 |
public void run() { |
| 91 |
TargetType.startConstructorAccess().withParameterTypes(int.class).in(Person.class).newInstance(8); |
| 92 |
} |
| 93 |
}); |
| 94 |
} |
| 95 |
|
| 96 |
@Test |
| 97 |
public void should_wrap_with_a_ReflectionError_the_checked_Exception_thrown_by_constructor() { |
| 98 |
try { |
| 99 |
TargetType.startConstructorAccess().withParameterTypes(Person.class).in(Person.class).newInstance(new Person()); |
| 100 |
fail("Expecting an ReflectionError"); |
| 101 |
} catch (ReflectionError e) { |
| 102 |
Throwable cause = e.getCause(); |
| 103 |
assertTrue(cause instanceof Exception); |
| 104 |
assertEquals("A person cannot be created from another person", cause.getMessage()); |
| 105 |
} |
| 106 |
} |
| 107 |
} |