jtulach@248
|
1 |
package org.apidesign.anagram.gui;
|
jtulach@248
|
2 |
|
jtulach@248
|
3 |
import org.apidesign.anagram.api.Scrambler;
|
jtulach@248
|
4 |
import org.apidesign.anagram.api.WordLibrary;
|
jtulach@250
|
5 |
import org.junit.Before;
|
jtulach@248
|
6 |
import static org.junit.Assert.*;
|
jtulach@248
|
7 |
import org.junit.Test;
|
jtulach@248
|
8 |
import org.netbeans.junit.MockServices;
|
jtulach@248
|
9 |
|
jtulach@248
|
10 |
public abstract class AnagramsTestBase {
|
jtulach@248
|
11 |
|
jtulach@248
|
12 |
protected abstract Anagrams create();
|
jtulach@248
|
13 |
|
jtulach@250
|
14 |
@Before
|
jtulach@250
|
15 |
public void cleanUpServices() {
|
jtulach@250
|
16 |
MockServices.setServices();
|
jtulach@250
|
17 |
}
|
jtulach@250
|
18 |
|
jtulach@248
|
19 |
@Test public void testInjectionOfServices() throws Exception {
|
jtulach@248
|
20 |
Anagrams ui = create();
|
jtulach@248
|
21 |
|
jtulach@248
|
22 |
assertNull("No scrambler injected yet", ui.getScrambler());
|
jtulach@248
|
23 |
assertNull("No scrambler injected yet", ui.getWordLibrary());
|
jtulach@248
|
24 |
|
jtulach@248
|
25 |
MockServices.setServices(
|
jtulach@248
|
26 |
ReversingMockScrambler.class, SingleMockLibrary.class
|
jtulach@248
|
27 |
);
|
jtulach@248
|
28 |
|
jtulach@248
|
29 |
Scrambler s = ui.getScrambler();
|
jtulach@248
|
30 |
assertNotNull("Now we have scrambler", s);
|
jtulach@248
|
31 |
assertEquals(
|
jtulach@248
|
32 |
"It is the mock one", ReversingMockScrambler.class, s.getClass()
|
jtulach@248
|
33 |
);
|
jtulach@248
|
34 |
WordLibrary l = ui.getWordLibrary();
|
jtulach@248
|
35 |
assertNotNull("Now we have library", l);
|
jtulach@248
|
36 |
assertEquals(
|
jtulach@248
|
37 |
"It is the mock one", SingleMockLibrary.class, l.getClass()
|
jtulach@248
|
38 |
);
|
jtulach@249
|
39 |
|
jtulach@249
|
40 |
ui.display();
|
jtulach@249
|
41 |
|
jtulach@249
|
42 |
assertEquals(
|
jtulach@249
|
43 |
"The word from SingleMockLibrary is taken",
|
jtulach@249
|
44 |
"Hello World!", ui.getOriginalWord()
|
jtulach@249
|
45 |
);
|
jtulach@249
|
46 |
assertEquals(
|
jtulach@249
|
47 |
"The word is rotated using ReversingMockScrambler",
|
jtulach@249
|
48 |
"!dlroW olleH", ui.getScrambledWord()
|
jtulach@249
|
49 |
);
|
jtulach@248
|
50 |
}
|
jtulach@248
|
51 |
|
jtulach@249
|
52 |
public static final class ReversingMockScrambler implements Scrambler {
|
jtulach@249
|
53 |
public String scramble(String word) {
|
jtulach@249
|
54 |
return new StringBuilder(word).reverse().toString();
|
jtulach@249
|
55 |
}
|
jtulach@248
|
56 |
}
|
jtulach@248
|
57 |
|
jtulach@249
|
58 |
public static final class SingleMockLibrary implements WordLibrary {
|
jtulach@249
|
59 |
public String[] getWords() {
|
jtulach@249
|
60 |
return new String[] { "Hello World!" };
|
jtulach@249
|
61 |
}
|
jtulach@248
|
62 |
|
jtulach@248
|
63 |
}
|
jtulach@248
|
64 |
}
|