Skip to content

Commit 13b3327

Browse files
committed
Merge pull request #791 from stefanbirkner/member-improvement
Improve FrameworkMember, FrameworkMethod and FrameworkField
2 parents 21898b1 + fbb1efe commit 13b3327

File tree

6 files changed

+111
-26
lines changed

6 files changed

+111
-26
lines changed

src/main/java/org/junit/runners/model/FrameworkField.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.lang.annotation.Annotation;
44
import java.lang.reflect.Field;
5-
import java.lang.reflect.Modifier;
65

76
import org.junit.runners.BlockJUnit4ClassRunner;
87

@@ -16,6 +15,10 @@ public class FrameworkField extends FrameworkMember<FrameworkField> {
1615
private final Field fField;
1716

1817
FrameworkField(Field field) {
18+
if (field == null) {
19+
throw new NullPointerException(
20+
"FrameworkField cannot be created without an underlying field.");
21+
}
1922
fField = field;
2023
}
2124

@@ -29,21 +32,14 @@ public Annotation[] getAnnotations() {
2932
return fField.getAnnotations();
3033
}
3134

32-
@Override
33-
public boolean isPublic() {
34-
int modifiers = fField.getModifiers();
35-
return Modifier.isPublic(modifiers);
36-
}
37-
3835
@Override
3936
public boolean isShadowedBy(FrameworkField otherMember) {
4037
return otherMember.getName().equals(getName());
4138
}
4239

4340
@Override
44-
public boolean isStatic() {
45-
int modifiers = fField.getModifiers();
46-
return Modifier.isStatic(modifiers);
41+
protected int getModifiers() {
42+
return fField.getModifiers();
4743
}
4844

4945
/**
@@ -73,4 +69,9 @@ public Class<?> getDeclaringClass() {
7369
public Object get(Object target) throws IllegalArgumentException, IllegalAccessException {
7470
return fField.get(target);
7571
}
72+
73+
@Override
74+
public String toString() {
75+
return fField.toString();
76+
}
7677
}

src/main/java/org/junit/runners/model/FrameworkMember.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.junit.runners.model;
22

33
import java.lang.annotation.Annotation;
4+
import java.lang.reflect.Modifier;
45
import java.util.List;
56

67
/**
@@ -25,9 +26,21 @@ boolean isShadowedBy(List<T> members) {
2526
return false;
2627
}
2728

28-
public abstract boolean isPublic();
29+
protected abstract int getModifiers();
2930

30-
public abstract boolean isStatic();
31+
/**
32+
* Returns true if this member is static, false if not.
33+
*/
34+
public boolean isStatic() {
35+
return Modifier.isStatic(getModifiers());
36+
}
37+
38+
/**
39+
* Returns true if this member is public, false if not.
40+
*/
41+
public boolean isPublic() {
42+
return Modifier.isPublic(getModifiers());
43+
}
3144

3245
public abstract String getName();
3346

src/main/java/org/junit/runners/model/FrameworkMethod.java

+12-14
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@
1919
* @since 4.5
2020
*/
2121
public class FrameworkMethod extends FrameworkMember<FrameworkMethod> {
22-
final Method fMethod;
22+
private final Method fMethod;
2323

2424
/**
2525
* Returns a new {@code FrameworkMethod} for {@code method}
2626
*/
2727
public FrameworkMethod(Method method) {
28+
if (method == null) {
29+
throw new NullPointerException(
30+
"FrameworkMethod cannot be created without an underlying method.");
31+
}
2832
fMethod = method;
2933
}
3034

@@ -99,22 +103,11 @@ public void validatePublicVoid(boolean isStatic, List<Throwable> errors) {
99103
}
100104
}
101105

102-
/**
103-
* Returns true if this method is static, false if not
104-
*/
105106
@Override
106-
public boolean isStatic() {
107-
return Modifier.isStatic(fMethod.getModifiers());
107+
protected int getModifiers() {
108+
return fMethod.getModifiers();
108109
}
109110

110-
/**
111-
* Returns true if this method is public, false if not
112-
*/
113-
@Override
114-
public boolean isPublic() {
115-
return Modifier.isPublic(fMethod.getModifiers());
116-
}
117-
118111
/**
119112
* Returns the return type of the method
120113
*/
@@ -209,4 +202,9 @@ public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
209202
public List<ParameterSignature> getParameterSignatures() {
210203
return ParameterSignature.signatures(fMethod);
211204
}
205+
206+
@Override
207+
public String toString() {
208+
return fMethod.toString();
209+
}
212210
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.junit.runners.model;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.rules.ExpectedException.none;
5+
6+
import java.lang.reflect.Field;
7+
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
import org.junit.rules.ExpectedException;
11+
12+
public class FrameworkFieldTest {
13+
@Rule
14+
public final ExpectedException thrown = none();
15+
16+
@Test
17+
public void cannotBeCreatedWithoutUnderlyingField() {
18+
thrown.expect(NullPointerException.class);
19+
thrown.expectMessage("FrameworkField cannot be created without an underlying field.");
20+
new FrameworkField(null);
21+
}
22+
23+
@Test
24+
public void hasToStringWhichPrintsFieldName() throws Exception {
25+
Field field = ClassWithDummyField.class.getField("dummyField");
26+
FrameworkField frameworkField = new FrameworkField(field);
27+
assertTrue(frameworkField.toString().contains("dummyField"));
28+
}
29+
30+
private static class ClassWithDummyField {
31+
@SuppressWarnings("unused")
32+
public final int dummyField = 0;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.junit.runners.model;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.rules.ExpectedException.none;
5+
6+
import java.lang.reflect.Method;
7+
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
import org.junit.rules.ExpectedException;
11+
12+
public class FrameworkMethodTest {
13+
@Rule
14+
public final ExpectedException thrown = none();
15+
16+
@Test
17+
public void cannotBeCreatedWithoutUnderlyingField() {
18+
thrown.expect(NullPointerException.class);
19+
thrown.expectMessage("FrameworkMethod cannot be created without an underlying method.");
20+
new FrameworkMethod(null);
21+
}
22+
23+
@Test
24+
public void hasToStringWhichPrintsMethodName() throws Exception {
25+
Method method = ClassWithDummyMethod.class.getMethod("dummyMethod");
26+
FrameworkMethod frameworkMethod = new FrameworkMethod(method);
27+
assertTrue(frameworkMethod.toString().contains("dummyMethod"));
28+
}
29+
30+
private static class ClassWithDummyMethod {
31+
@SuppressWarnings("unused")
32+
public void dummyMethod() {
33+
}
34+
}
35+
}

src/test/java/org/junit/tests/AllTests.java

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.junit.runner.notification.SynchronizedRunListenerTest;
1616
import org.junit.runners.Suite;
1717
import org.junit.runners.Suite.SuiteClasses;
18+
import org.junit.runners.model.FrameworkFieldTest;
19+
import org.junit.runners.model.FrameworkMethodTest;
1820
import org.junit.tests.assertion.AssertionTest;
1921
import org.junit.tests.assertion.ComparisonFailureTest;
2022
import org.junit.tests.assertion.MultipleFailureExceptionTest;
@@ -196,6 +198,8 @@
196198
JUnitCommandLineParseResultTest.class,
197199
FilterFactoriesTest.class,
198200
CategoryFilterFactoryTest.class,
201+
FrameworkFieldTest.class,
202+
FrameworkMethodTest.class,
199203
JUnitCoreTest.class
200204
})
201205
public class AllTests {

0 commit comments

Comments
 (0)