Skip to content

Commit 6606ff1

Browse files
committed
Test to check the metric validation exceptions.
1 parent 0fecad9 commit 6606ff1

1 file changed

Lines changed: 172 additions & 0 deletions

File tree

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package nl.rug.jbi.jsm.core;
2+
3+
import nl.rug.jbi.jsm.core.calculator.*;
4+
import nl.rug.jbi.jsm.core.event.Subscribe;
5+
import nl.rug.jbi.jsm.core.event.UsingProducer;
6+
import nl.rug.jbi.jsm.core.pipeline.MetricPreparationException;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.junit.runners.JUnit4;
11+
12+
import java.util.EnumSet;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
@RunWith(JUnit4.class)
17+
public class MetricValidationTest {
18+
private JSMCore core = null;
19+
20+
@Before
21+
public void setUp() throws Exception {
22+
this.core = new JSMCore();
23+
}
24+
25+
@Test(expected = MetricPreparationException.class)
26+
public void testProducerDirectRegistration() throws MetricPreparationException {
27+
//Direct registration of a producer is forbidden.
28+
this.core.registerMetric(new ProducerMetric(MetricScope.CLASS, MetricScope.CLASS) {
29+
@Override
30+
public List<Produce> getProduce(Map<String, MetricState> states, int invalidMembers) {
31+
throw new UnsupportedOperationException();
32+
}
33+
34+
@Override
35+
public Class getProducedClass() {
36+
throw new UnsupportedOperationException();
37+
}
38+
});
39+
}
40+
41+
@Test(expected = MetricPreparationException.class)
42+
public void testIllegalScopeOrder() throws MetricPreparationException {
43+
//Results can either be of the execution scope or a future scope, but not a previous scope.
44+
this.core.registerMetric(new SharedMetric(MetricScope.COLLECTION) {
45+
@Override
46+
public List<MetricResult> getResults(Map<String, MetricState> states, int invalidMembers) {
47+
throw new UnsupportedOperationException();
48+
}
49+
50+
@Override
51+
public EnumSet<MetricScope> getResultScopes() {
52+
//Class gets evaluated before collection, so collection cannot be produced by class.
53+
return EnumSet.of(MetricScope.CLASS);
54+
}
55+
});
56+
}
57+
58+
@Test(expected = MetricPreparationException.class)
59+
public void testMissingData() throws MetricPreparationException {
60+
//Tests case where a class needs data that is neither provided by the class visitor, nor a producer.
61+
this.core.registerMetric(new SharedMetric(MetricScope.CLASS) {
62+
@Subscribe
63+
public void handleData(final MetricState state, final Object randomObj) {
64+
65+
}
66+
67+
@Override
68+
public List<MetricResult> getResults(Map<String, MetricState> states, int invalidMembers) {
69+
throw new UnsupportedOperationException();
70+
}
71+
});
72+
}
73+
74+
@Test(expected = MetricPreparationException.class)
75+
public void testInvalidListenerArgCount() throws MetricPreparationException {
76+
//A Subscribed method needs to have 2 parameters
77+
this.core.registerMetric(new SharedMetric(MetricScope.CLASS) {
78+
@Subscribe
79+
public void handleData(final MetricState state) {
80+
81+
}
82+
83+
@Override
84+
public List<MetricResult> getResults(Map<String, MetricState> states, int invalidMembers) {
85+
throw new UnsupportedOperationException();
86+
}
87+
});
88+
}
89+
90+
@Test(expected = MetricPreparationException.class)
91+
public void testWrongFirstParameter() throws MetricPreparationException {
92+
//The first parameter needs to be MetricState
93+
this.core.registerMetric(new SharedMetric(MetricScope.CLASS) {
94+
@Subscribe
95+
public void handleData(final Object randomObj, final MetricState state) {
96+
97+
}
98+
99+
@Override
100+
public List<MetricResult> getResults(Map<String, MetricState> states, int invalidMembers) {
101+
throw new UnsupportedOperationException();
102+
}
103+
});
104+
}
105+
106+
@Test(expected = MetricPreparationException.class)
107+
public void testListenerPrivate() throws MetricPreparationException {
108+
//Listener methods need to be public
109+
this.core.registerMetric(new SharedMetric(MetricScope.CLASS) {
110+
@Subscribe
111+
void handleData(final Object randomObj, final MetricState state) {
112+
113+
}
114+
115+
@Override
116+
public List<MetricResult> getResults(Map<String, MetricState> states, int invalidMembers) {
117+
throw new UnsupportedOperationException();
118+
}
119+
});
120+
}
121+
122+
@Test(expected = MetricPreparationException.class)
123+
public void testProducerProduceMismatch() throws MetricPreparationException {
124+
//If a producer is declared, the second parameter needs to match the produce of the producer
125+
this.core.registerMetric(new SharedMetric(MetricScope.CLASS) {
126+
@Subscribe
127+
@UsingProducer(TestProducer.class)
128+
public void handleData(final MetricState state, final Integer one) {
129+
130+
}
131+
132+
@Override
133+
public List<MetricResult> getResults(Map<String, MetricState> states, int invalidMembers) {
134+
throw new UnsupportedOperationException();
135+
}
136+
});
137+
}
138+
139+
@Test(expected = MetricPreparationException.class)
140+
public void testProducerScopeMistmatch() throws MetricPreparationException {
141+
//If a producer is declared, the produce scope needs to match the metric scope.
142+
this.core.registerMetric(new SharedMetric(MetricScope.COLLECTION) {
143+
@Subscribe
144+
@UsingProducer(TestProducer.class)
145+
public void handleData(final MetricState state, final Object one) {
146+
//The metric calculates for COLLECTION, but the producer is for CLASS
147+
}
148+
149+
@Override
150+
public List<MetricResult> getResults(Map<String, MetricState> states, int invalidMembers) {
151+
throw new UnsupportedOperationException();
152+
}
153+
});
154+
}
155+
156+
public static class TestProducer extends ProducerMetric {
157+
158+
public TestProducer() {
159+
super(MetricScope.CLASS, MetricScope.CLASS);
160+
}
161+
162+
@Override
163+
public List<Produce> getProduce(Map<String, MetricState> states, int invalidMembers) {
164+
throw new UnsupportedOperationException();
165+
}
166+
167+
@Override
168+
public Class getProducedClass() {
169+
return Object.class;
170+
}
171+
}
172+
}

0 commit comments

Comments
 (0)