|
1 /** |
|
2 * |
|
3 */ |
|
4 package org.junit.experimental.theories.internal; |
|
5 |
|
6 import java.lang.reflect.Array; |
|
7 import java.lang.reflect.Field; |
|
8 import java.lang.reflect.InvocationTargetException; |
|
9 import java.lang.reflect.Method; |
|
10 import java.lang.reflect.Modifier; |
|
11 import java.util.ArrayList; |
|
12 import java.util.List; |
|
13 |
|
14 import org.junit.experimental.theories.DataPoint; |
|
15 import org.junit.experimental.theories.DataPoints; |
|
16 import org.junit.experimental.theories.ParameterSignature; |
|
17 import org.junit.experimental.theories.ParameterSupplier; |
|
18 import org.junit.experimental.theories.PotentialAssignment; |
|
19 import org.junit.runners.model.FrameworkMethod; |
|
20 import org.junit.runners.model.TestClass; |
|
21 |
|
22 public class AllMembersSupplier extends ParameterSupplier { |
|
23 static class MethodParameterValue extends PotentialAssignment { |
|
24 private final FrameworkMethod fMethod; |
|
25 |
|
26 private MethodParameterValue(FrameworkMethod dataPointMethod) { |
|
27 fMethod= dataPointMethod; |
|
28 } |
|
29 |
|
30 @Override |
|
31 public Object getValue() throws CouldNotGenerateValueException { |
|
32 try { |
|
33 return fMethod.invokeExplosively(null); |
|
34 } catch (IllegalArgumentException e) { |
|
35 throw new RuntimeException( |
|
36 "unexpected: argument length is checked"); |
|
37 } catch (IllegalAccessException e) { |
|
38 throw new RuntimeException( |
|
39 "unexpected: getMethods returned an inaccessible method"); |
|
40 } catch (Throwable e) { |
|
41 throw new CouldNotGenerateValueException(); |
|
42 // do nothing, just look for more values |
|
43 } |
|
44 } |
|
45 |
|
46 @Override |
|
47 public String getDescription() throws CouldNotGenerateValueException { |
|
48 return fMethod.getName(); |
|
49 } |
|
50 } |
|
51 |
|
52 private final TestClass fClass; |
|
53 |
|
54 public AllMembersSupplier(TestClass type) { |
|
55 fClass= type; |
|
56 } |
|
57 |
|
58 @Override |
|
59 public List<PotentialAssignment> getValueSources(ParameterSignature sig) { |
|
60 List<PotentialAssignment> list= new ArrayList<PotentialAssignment>(); |
|
61 |
|
62 addFields(sig, list); |
|
63 addSinglePointMethods(sig, list); |
|
64 addMultiPointMethods(list); |
|
65 |
|
66 return list; |
|
67 } |
|
68 |
|
69 private void addMultiPointMethods(List<PotentialAssignment> list) { |
|
70 for (FrameworkMethod dataPointsMethod : fClass |
|
71 .getAnnotatedMethods(DataPoints.class)) |
|
72 try { |
|
73 addArrayValues(dataPointsMethod.getName(), list, dataPointsMethod.invokeExplosively(null)); |
|
74 } catch (Throwable e) { |
|
75 // ignore and move on |
|
76 } |
|
77 } |
|
78 |
|
79 private void addSinglePointMethods(ParameterSignature sig, |
|
80 List<PotentialAssignment> list) { |
|
81 for (FrameworkMethod dataPointMethod : fClass |
|
82 .getAnnotatedMethods(DataPoint.class)) { |
|
83 Class<?> type= sig.getType(); |
|
84 if ((dataPointMethod.producesType(type))) |
|
85 list.add(new MethodParameterValue(dataPointMethod)); |
|
86 } |
|
87 } |
|
88 |
|
89 private void addFields(ParameterSignature sig, |
|
90 List<PotentialAssignment> list) { |
|
91 for (final Field field : fClass.getJavaClass().getFields()) { |
|
92 if (Modifier.isStatic(field.getModifiers())) { |
|
93 Class<?> type= field.getType(); |
|
94 if (sig.canAcceptArrayType(type) |
|
95 && field.getAnnotation(DataPoints.class) != null) { |
|
96 addArrayValues(field.getName(), list, getStaticFieldValue(field)); |
|
97 } else if (sig.canAcceptType(type)) { |
|
98 list.add(PotentialAssignment |
|
99 .forValue(field.getName(), getStaticFieldValue(field))); |
|
100 } |
|
101 } |
|
102 } |
|
103 } |
|
104 |
|
105 private void addArrayValues(String name, List<PotentialAssignment> list, Object array) { |
|
106 for (int i= 0; i < Array.getLength(array); i++) |
|
107 list.add(PotentialAssignment.forValue(name + "[" + i + "]", Array.get(array, i))); |
|
108 } |
|
109 |
|
110 private Object getStaticFieldValue(final Field field) { |
|
111 try { |
|
112 return field.get(null); |
|
113 } catch (IllegalArgumentException e) { |
|
114 throw new RuntimeException( |
|
115 "unexpected: field from getClass doesn't exist on object"); |
|
116 } catch (IllegalAccessException e) { |
|
117 throw new RuntimeException( |
|
118 "unexpected: getFields returned an inaccessible field"); |
|
119 } |
|
120 } |
|
121 } |