| 1 |
/* |
| 2 |
* Created on Jan 25, 2009 |
| 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 @2009 the original author or authors. |
| 14 |
*/ |
| 15 |
package org.fest.reflect.innerclass; |
| 16 |
|
| 17 |
import static org.fest.reflect.core.Reflection.constructor; |
| 18 |
import static org.fest.reflect.core.Reflection.field; |
| 19 |
import static org.fest.reflect.core.Reflection.method; |
| 20 |
import static org.fest.reflect.util.ExpectedFailures.expectIllegalArgumentException; |
| 21 |
import static org.fest.reflect.util.ExpectedFailures.expectNullPointerException; |
| 22 |
import static org.fest.reflect.util.ExpectedFailures.expectReflectionError; |
| 23 |
|
| 24 |
import static org.junit.Assert.assertEquals; |
| 25 |
import static org.junit.Assert.assertTrue; |
| 26 |
|
| 27 |
import org.junit.Test; |
| 28 |
|
| 29 |
import org.fest.test.CodeToTest; |
| 30 |
|
| 31 |
/** |
| 32 |
* Tests for the fluent interface for inner classes. |
| 33 |
* |
| 34 |
* @author Alex Ruiz |
| 35 |
*/ |
| 36 |
public class StaticInnerClass_Test { |
| 37 |
|
| 38 |
@Test |
| 39 |
public void should_throw_error_if_static_inner_class_name_is_null() { |
| 40 |
expectNullPointerException("The name of the static inner class to access should not be null").on(new CodeToTest() { |
| 41 |
public void run() { |
| 42 |
StaticInnerClassName.startStaticInnerClassAccess(null); |
| 43 |
} |
| 44 |
}); |
| 45 |
} |
| 46 |
|
| 47 |
@Test |
| 48 |
public void should_throw_error_if_static_inner_class_name_is_empty() { |
| 49 |
expectIllegalArgumentException("The name of the static inner class to access should not be empty").on(new CodeToTest() { |
| 50 |
public void run() { |
| 51 |
StaticInnerClassName.startStaticInnerClassAccess(""); |
| 52 |
} |
| 53 |
}); |
| 54 |
} |
| 55 |
|
| 56 |
@Test |
| 57 |
public void should_throw_error_if_declaring_class_is_null() { |
| 58 |
expectNullPointerException("The declaring class should not be null").on(new CodeToTest() { |
| 59 |
public void run() { |
| 60 |
StaticInnerClassName.startStaticInnerClassAccess("Hello").in(null); |
| 61 |
} |
| 62 |
}); |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void should_see_static_inner_class() { |
| 67 |
Class<?> innerClass = StaticInnerClassName.startStaticInnerClassAccess("PrivateInnerClass").in(OuterClass.class).get(); |
| 68 |
assertTrue(innerClass.getName().contains("PrivateInnerClass")); |
| 69 |
// make sure we really got the inner classes by creating a new instance and accessing its fields and methods. |
| 70 |
Object leia = constructor().withParameterTypes(String.class).in(innerClass).newInstance("Leia"); |
| 71 |
assertEquals("Leia", field("name").ofType(String.class).in(leia).get()); |
| 72 |
assertEquals("Leia", method("name").withReturnType(String.class).in(leia).invoke()); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void should_return_null_if_static_inner_class_does_not_exist() { |
| 77 |
String msg = "The static inner class <SomeInnerClass> cannot be found in org.fest.reflect.innerclass.OuterClass"; |
| 78 |
expectReflectionError(msg).on(new CodeToTest() { |
| 79 |
public void run() { |
| 80 |
StaticInnerClassName.startStaticInnerClassAccess("SomeInnerClass").in(OuterClass.class).get(); |
| 81 |
} |
| 82 |
}); |
| 83 |
} |
| 84 |
} |