1 |
alfonx |
1097 |
package skrueger.i8n; |
2 |
|
|
|
3 |
|
|
import static org.junit.Assert.assertEquals; |
4 |
|
|
import static org.junit.Assert.assertFalse; |
5 |
|
|
import static org.junit.Assert.assertTrue; |
6 |
|
|
|
7 |
|
|
import java.awt.event.ActionEvent; |
8 |
|
|
import java.awt.event.ActionListener; |
9 |
|
|
import java.beans.PropertyChangeEvent; |
10 |
|
|
import java.beans.PropertyChangeListener; |
11 |
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
12 |
|
|
|
13 |
|
|
import org.junit.After; |
14 |
|
|
import org.junit.Before; |
15 |
|
|
import org.junit.Test; |
16 |
|
|
|
17 |
|
|
public class TranslationTest { |
18 |
|
|
|
19 |
|
|
@Before |
20 |
|
|
public void setUp() throws Exception { |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
@After |
24 |
|
|
public void tearDown() throws Exception { |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
@Test |
28 |
|
|
public void testCopyToAndEquals() { |
29 |
|
|
Translation t = new Translation(); |
30 |
|
|
Translation t2 = new Translation(); |
31 |
|
|
t.put("de", "berg"); |
32 |
|
|
t.put("en", "mountain"); |
33 |
|
|
t.copyTo(t2); |
34 |
|
|
assertEquals(t2.keySet(), t.keySet()); |
35 |
|
|
assertEquals(t2.toOneLine(), t.toOneLine()); |
36 |
|
|
assertEquals(t2, t); |
37 |
|
|
|
38 |
|
|
assertEquals(t.copyTo(null), t); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
@Test |
42 |
|
|
public void testCopyAndClone() { |
43 |
|
|
Translation t = new Translation(); |
44 |
|
|
t.put("de", "berg"); |
45 |
|
|
t.put("en", "mountain"); |
46 |
|
|
Translation t2 = t.copy(); |
47 |
|
|
|
48 |
|
|
assertEquals(t2, t); |
49 |
|
|
|
50 |
|
|
Translation t3 = t2.clone(); |
51 |
|
|
assertEquals(t3, t); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
@Test |
55 |
|
|
public void testActiveLangChanges() { |
56 |
|
|
Translation t = new Translation(); |
57 |
|
|
final AtomicBoolean calledAl = new AtomicBoolean(false); |
58 |
|
|
final AtomicBoolean calledL = new AtomicBoolean(false); |
59 |
|
|
final AtomicBoolean calledC = new AtomicBoolean(false); |
60 |
|
|
|
61 |
|
|
Translation.addActiveLangChangeListener(new PropertyChangeListener() { |
62 |
|
|
|
63 |
|
|
@Override |
64 |
|
|
public void propertyChange(PropertyChangeEvent evt) { |
65 |
|
|
calledAl.set(true); |
66 |
|
|
} |
67 |
|
|
}); |
68 |
|
|
|
69 |
|
|
Translation.addLocaleChangeListener(new PropertyChangeListener() { |
70 |
|
|
|
71 |
|
|
@Override |
72 |
|
|
public void propertyChange(PropertyChangeEvent evt) { |
73 |
|
|
calledL.set(true); |
74 |
|
|
} |
75 |
|
|
}); |
76 |
|
|
|
77 |
|
|
t.addTranslationChangeListener(new ActionListener() { |
78 |
|
|
|
79 |
|
|
@Override |
80 |
|
|
public void actionPerformed(ActionEvent e) { |
81 |
|
|
calledC.set(true); |
82 |
|
|
} |
83 |
|
|
}); |
84 |
|
|
|
85 |
|
|
// Change the content |
86 |
|
|
t.put("de", "change"); |
87 |
|
|
assertFalse(calledAl.get()); |
88 |
|
|
assertFalse(calledL.get()); |
89 |
|
|
assertTrue(calledC.get()); |
90 |
|
|
|
91 |
|
|
// Change active language by default |
92 |
|
|
calledAl.set(false); |
93 |
|
|
calledL.set(false); |
94 |
|
|
calledC.set(false); |
95 |
|
|
Translation.setActiveLang("en"); |
96 |
|
|
assertTrue(calledAl.get()); |
97 |
|
|
assertTrue(calledL.get()); |
98 |
|
|
assertFalse(calledC.get()); |
99 |
|
|
|
100 |
|
|
calledAl.set(false); |
101 |
|
|
calledL.set(false); |
102 |
|
|
Translation.setActiveLang("ar", false); |
103 |
|
|
assertTrue(calledAl.get()); |
104 |
|
|
assertFalse(calledL.get()); |
105 |
|
|
assertFalse(calledC.get()); |
106 |
|
|
|
107 |
|
|
calledAl.set(false); |
108 |
|
|
Translation.setActiveLang("ar", false); |
109 |
|
|
assertFalse( |
110 |
|
|
"if the same language is set again, it should not trigger events", |
111 |
|
|
calledAl.get()); |
112 |
|
|
assertFalse(calledC.get()); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
@Test |
116 |
|
|
public void testToString() { |
117 |
|
|
Translation t = new Translation(); |
118 |
|
|
t.put("de", "de"); |
119 |
|
|
t.put("en", "en"); |
120 |
|
|
t.put("fr", "fr"); |
121 |
|
|
t.put("ru", "ru"); |
122 |
|
|
|
123 |
|
|
Translation.setActiveLang("de"); |
124 |
|
|
assertEquals("de", t.toString()); |
125 |
|
|
Translation.setActiveLang("ru"); |
126 |
|
|
assertEquals("ru", t.toString()); |
127 |
|
|
} |
128 |
|
|
} |