Skip to content

Commit 6db29bf

Browse files
migrate unit tests for Groovy to separate Groovy file
- testing all Groovy functionality doesn't belong as an inner class - it was awkard to load Groovy scripts via Java (leftover from previous codebase where Groovy wasn't part of build, only a runtime library) - establish convention for each language to have a suite of tests in /src/test
1 parent 1f48ae1 commit 6db29bf

File tree

3 files changed

+241
-240
lines changed

3 files changed

+241
-240
lines changed

language-adaptors/rxjava-groovy/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
apply plugin: 'java'
2+
apply plugin: 'groovy'
23
apply plugin: 'eclipse'
34
apply plugin: 'idea'
45

56
dependencies {
67
compile project(':rxjava-core')
7-
provided 'org.codehaus.groovy:groovy-all:2.+'
8+
groovy 'org.codehaus.groovy:groovy-all:2.+'
89
provided 'junit:junit:4.10'
910
provided 'org.mockito:mockito-core:1.8.5'
1011
}

language-adaptors/rxjava-groovy/src/main/java/rx/lang/groovy/GroovyAdaptor.java

Lines changed: 0 additions & 239 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,7 @@
1515
*/
1616
package rx.lang.groovy;
1717

18-
import static org.mockito.Matchers.*;
19-
import static org.mockito.Mockito.*;
20-
import groovy.lang.Binding;
2118
import groovy.lang.Closure;
22-
import groovy.lang.GroovyClassLoader;
23-
24-
import java.util.Arrays;
25-
26-
import org.codehaus.groovy.runtime.InvokerHelper;
27-
import org.junit.Before;
28-
import org.junit.Test;
29-
import org.mockito.Mock;
30-
import org.mockito.MockitoAnnotations;
31-
32-
import rx.Notification;
33-
import rx.Observable;
34-
import rx.Observer;
35-
import rx.Subscription;
36-
import rx.util.functions.Func1;
3719
import rx.util.functions.FunctionLanguageAdaptor;
3820

3921
public class GroovyAdaptor implements FunctionLanguageAdaptor {
@@ -46,225 +28,4 @@ public Object call(Object function, Object[] args) {
4628
public Class<?>[] getFunctionClass() {
4729
return new Class<?>[] { Closure.class };
4830
}
49-
50-
public static class UnitTest {
51-
52-
@Mock
53-
ScriptAssertion assertion;
54-
55-
@Mock
56-
Observer<Integer> w;
57-
58-
@Before
59-
public void before() {
60-
MockitoAnnotations.initMocks(this);
61-
}
62-
63-
@Test
64-
public void testCreateViaGroovy() {
65-
runGroovyScript("o.create({it.onNext('hello');it.onCompleted();}).subscribe({ result -> a.received(result)});");
66-
verify(assertion, times(1)).received("hello");
67-
}
68-
69-
@Test
70-
public void testFilterViaGroovy() {
71-
runGroovyScript("o.filter(o.toObservable(1, 2, 3), {it >= 2}).subscribe({ result -> a.received(result)});");
72-
verify(assertion, times(0)).received(1);
73-
verify(assertion, times(1)).received(2);
74-
verify(assertion, times(1)).received(3);
75-
}
76-
77-
@Test
78-
public void testLast() {
79-
String script = "mockApiCall.getObservable().last().subscribe({ result -> a.received(result)});";
80-
runGroovyScript(script);
81-
verify(assertion, times(1)).received("hello_1");
82-
}
83-
84-
@Test
85-
public void testMap() {
86-
String script = "mockApiCall.getObservable().map({v -> 'say' + v}).subscribe({ result -> a.received(result)});";
87-
runGroovyScript(script);
88-
verify(assertion, times(1)).received("sayhello_1");
89-
}
90-
91-
@Test
92-
public void testMapViaGroovy() {
93-
runGroovyScript("o.map(o.toObservable(1, 2, 3), {'hello_' + it}).subscribe({ result -> a.received(result)});");
94-
verify(assertion, times(1)).received("hello_" + 1);
95-
verify(assertion, times(1)).received("hello_" + 2);
96-
verify(assertion, times(1)).received("hello_" + 3);
97-
}
98-
99-
@Test
100-
public void testMaterializeViaGroovy() {
101-
runGroovyScript("o.materialize(o.toObservable(1, 2, 3)).subscribe({ result -> a.received(result)});");
102-
// we expect 4 onNext calls: 3 for 1, 2, 3 ObservableNotification.OnNext and 1 for ObservableNotification.OnCompleted
103-
verify(assertion, times(4)).received(any(Notification.class));
104-
verify(assertion, times(0)).error(any(Exception.class));
105-
}
106-
107-
@Test
108-
public void testMergeDelayErrorViaGroovy() {
109-
runGroovyScript("o.mergeDelayError(o.toObservable(1, 2, 3), o.merge(o.toObservable(6), o.error(new NullPointerException()), o.toObservable(7)), o.toObservable(4, 5)).subscribe({ result -> a.received(result)}, { exception -> a.error(exception)});");
110-
verify(assertion, times(1)).received(1);
111-
verify(assertion, times(1)).received(2);
112-
verify(assertion, times(1)).received(3);
113-
verify(assertion, times(1)).received(4);
114-
verify(assertion, times(1)).received(5);
115-
verify(assertion, times(1)).received(6);
116-
verify(assertion, times(0)).received(7);
117-
verify(assertion, times(1)).error(any(NullPointerException.class));
118-
}
119-
120-
@Test
121-
public void testMergeViaGroovy() {
122-
runGroovyScript("o.merge(o.toObservable(1, 2, 3), o.merge(o.toObservable(6), o.error(new NullPointerException()), o.toObservable(7)), o.toObservable(4, 5)).subscribe({ result -> a.received(result)}, { exception -> a.error(exception)});");
123-
// executing synchronously so we can deterministically know what order things will come
124-
verify(assertion, times(1)).received(1);
125-
verify(assertion, times(1)).received(2);
126-
verify(assertion, times(1)).received(3);
127-
verify(assertion, times(0)).received(4); // the NPE will cause this sequence to be skipped
128-
verify(assertion, times(0)).received(5); // the NPE will cause this sequence to be skipped
129-
verify(assertion, times(1)).received(6); // this comes before the NPE so should exist
130-
verify(assertion, times(0)).received(7);// this comes in the sequence after the NPE
131-
verify(assertion, times(1)).error(any(NullPointerException.class));
132-
}
133-
134-
@Test
135-
public void testScriptWithMaterialize() {
136-
String script = "mockApiCall.getObservable().materialize().subscribe({ result -> a.received(result)});";
137-
runGroovyScript(script);
138-
// 2 times: once for hello_1 and once for onCompleted
139-
verify(assertion, times(2)).received(any(Notification.class));
140-
}
141-
142-
@Test
143-
public void testScriptWithMerge() {
144-
String script = "o.merge(mockApiCall.getObservable(), mockApiCall.getObservable()).subscribe({ result -> a.received(result)});";
145-
runGroovyScript(script);
146-
verify(assertion, times(1)).received("hello_1");
147-
verify(assertion, times(1)).received("hello_2");
148-
}
149-
150-
@Test
151-
public void testScriptWithOnNext() {
152-
String script = "mockApiCall.getObservable().subscribe({ result -> a.received(result)})";
153-
runGroovyScript(script);
154-
verify(assertion).received("hello_1");
155-
}
156-
157-
@Test
158-
public void testSkipTakeViaGroovy() {
159-
runGroovyScript("o.skip(o.toObservable(1, 2, 3), 1).take(1).subscribe({ result -> a.received(result)});");
160-
verify(assertion, times(0)).received(1);
161-
verify(assertion, times(1)).received(2);
162-
verify(assertion, times(0)).received(3);
163-
}
164-
165-
@Test
166-
public void testSkipViaGroovy() {
167-
runGroovyScript("o.skip(o.toObservable(1, 2, 3), 2).subscribe({ result -> a.received(result)});");
168-
verify(assertion, times(0)).received(1);
169-
verify(assertion, times(0)).received(2);
170-
verify(assertion, times(1)).received(3);
171-
}
172-
173-
@Test
174-
public void testTakeViaGroovy() {
175-
runGroovyScript("o.take(o.toObservable(1, 2, 3), 2).subscribe({ result -> a.received(result)});");
176-
verify(assertion, times(1)).received(1);
177-
verify(assertion, times(1)).received(2);
178-
verify(assertion, times(0)).received(3);
179-
}
180-
181-
@Test
182-
public void testToSortedList() {
183-
runGroovyScript("mockApiCall.getNumbers().toSortedList().subscribe({ result -> a.received(result)});");
184-
verify(assertion, times(1)).received(Arrays.asList(1, 2, 3, 4, 5));
185-
}
186-
187-
@Test
188-
public void testToSortedListStatic() {
189-
runGroovyScript("o.toSortedList(o.toObservable(1, 3, 2, 5, 4)).subscribe({ result -> a.received(result)});");
190-
verify(assertion, times(1)).received(Arrays.asList(1, 2, 3, 4, 5));
191-
}
192-
193-
@Test
194-
public void testToSortedListWithFunction() {
195-
runGroovyScript("mockApiCall.getNumbers().toSortedList({a, b -> a - b}).subscribe({ result -> a.received(result)});");
196-
verify(assertion, times(1)).received(Arrays.asList(1, 2, 3, 4, 5));
197-
}
198-
199-
@Test
200-
public void testToSortedListWithFunctionStatic() {
201-
runGroovyScript("o.toSortedList(o.toObservable(1, 3, 2, 5, 4), {a, b -> a - b}).subscribe({ result -> a.received(result)});");
202-
verify(assertion, times(1)).received(Arrays.asList(1, 2, 3, 4, 5));
203-
}
204-
205-
private void runGroovyScript(String script) {
206-
ClassLoader parent = getClass().getClassLoader();
207-
@SuppressWarnings("resource")
208-
GroovyClassLoader loader = new GroovyClassLoader(parent);
209-
210-
Binding binding = new Binding();
211-
binding.setVariable("mockApiCall", new TestFactory());
212-
binding.setVariable("a", assertion);
213-
binding.setVariable("o", rx.Observable.class);
214-
215-
/* parse the script and execute it */
216-
InvokerHelper.createScript(loader.parseClass(script), binding).run();
217-
}
218-
219-
private static interface ScriptAssertion {
220-
public void error(Exception o);
221-
222-
public void received(Object o);
223-
}
224-
225-
private static class TestFactory {
226-
int counter = 1;
227-
228-
@SuppressWarnings("unused")
229-
public Observable<Integer> getNumbers() {
230-
return Observable.toObservable(1, 3, 2, 5, 4);
231-
}
232-
233-
@SuppressWarnings("unused")
234-
public TestObservable getObservable() {
235-
return new TestObservable(counter++);
236-
}
237-
}
238-
239-
private static class TestObservable extends Observable<String> {
240-
private final int count;
241-
242-
public TestObservable(int count) {
243-
super(new Func1<Observer<String>, Subscription>() {
244-
245-
@Override
246-
public Subscription call(Observer<String> t1) {
247-
// do nothing, override subscribe for test
248-
return null;
249-
}
250-
});
251-
this.count = count;
252-
}
253-
254-
public Subscription subscribe(Observer<String> observer) {
255-
256-
observer.onNext("hello_" + count);
257-
observer.onCompleted();
258-
259-
return new Subscription() {
260-
261-
public void unsubscribe() {
262-
// unregister ... will never be called here since we are executing synchronously
263-
}
264-
265-
};
266-
}
267-
}
268-
}
269-
27031
}

0 commit comments

Comments
 (0)