1 | // License: GPL. For details, see LICENSE file. |
---|
2 | package org.openstreetmap.josm.gui; |
---|
3 | |
---|
4 | import static org.junit.Assert.* |
---|
5 | |
---|
6 | import org.junit.BeforeClass |
---|
7 | import org.junit.Test |
---|
8 | import org.openstreetmap.josm.JOSMFixture |
---|
9 | import org.openstreetmap.josm.Main |
---|
10 | import org.openstreetmap.josm.data.osm.UserInfo |
---|
11 | |
---|
12 | class JosmUserIdentityManagerTest { |
---|
13 | |
---|
14 | final shouldFail = new GroovyTestCase().&shouldFail |
---|
15 | |
---|
16 | @BeforeClass |
---|
17 | public static void initTestCase() { |
---|
18 | JOSMFixture.createUnitTestFixture().init() |
---|
19 | } |
---|
20 | |
---|
21 | @Test |
---|
22 | public void test_SingletonAccess() { |
---|
23 | |
---|
24 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() |
---|
25 | |
---|
26 | // created ? |
---|
27 | assert im != null |
---|
28 | |
---|
29 | // registered as listener ? |
---|
30 | assert Main.pref.@listeners.contains(im) |
---|
31 | |
---|
32 | JosmUserIdentityManager im2 = JosmUserIdentityManager.getInstance() |
---|
33 | |
---|
34 | // only one instance |
---|
35 | assert im == im2 |
---|
36 | } |
---|
37 | |
---|
38 | @Test |
---|
39 | public void test_setAnonymouse() { |
---|
40 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() |
---|
41 | |
---|
42 | im.setPartiallyIdentified "test" |
---|
43 | im.setAnonymous() |
---|
44 | |
---|
45 | assert im.isAnonymous() |
---|
46 | assert ! im.isPartiallyIdentified() |
---|
47 | assert ! im.isFullyIdentified() |
---|
48 | |
---|
49 | assert im.getUserId() == 0 |
---|
50 | assert im.getUserName() == null |
---|
51 | assert im.getUserInfo() == null |
---|
52 | } |
---|
53 | |
---|
54 | @Test |
---|
55 | public void test_setPartiallyIdentified() { |
---|
56 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() |
---|
57 | |
---|
58 | im.setPartiallyIdentified "test" |
---|
59 | |
---|
60 | shouldFail(IllegalArgumentException) { |
---|
61 | im.setPartiallyIdentified null |
---|
62 | } |
---|
63 | |
---|
64 | shouldFail(IllegalArgumentException) { |
---|
65 | im.setPartiallyIdentified "" |
---|
66 | } |
---|
67 | |
---|
68 | shouldFail(IllegalArgumentException) { |
---|
69 | im.setPartiallyIdentified " \t " |
---|
70 | } |
---|
71 | |
---|
72 | im.setPartiallyIdentified "test" |
---|
73 | |
---|
74 | assert ! im.isAnonymous() |
---|
75 | assert im.isPartiallyIdentified() |
---|
76 | assert ! im.isFullyIdentified() |
---|
77 | |
---|
78 | assert im.getUserId() == 0 |
---|
79 | assert im.getUserName() == "test" |
---|
80 | assert im.getUserInfo() == null |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | @Test |
---|
85 | public void test_setFullyIdentified() { |
---|
86 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() |
---|
87 | |
---|
88 | UserInfo userInfo = new UserInfo(id: 1, description: "a description") |
---|
89 | |
---|
90 | im.setFullyIdentified "test", userInfo |
---|
91 | |
---|
92 | shouldFail(IllegalArgumentException) { |
---|
93 | im.setFullyIdentified null, userInfo |
---|
94 | } |
---|
95 | shouldFail(IllegalArgumentException) { |
---|
96 | im.setFullyIdentified "", userInfo |
---|
97 | } |
---|
98 | shouldFail(IllegalArgumentException) { |
---|
99 | im.setFullyIdentified " \t ", userInfo |
---|
100 | } |
---|
101 | shouldFail(IllegalArgumentException) { |
---|
102 | im.setFullyIdentified "test", null |
---|
103 | } |
---|
104 | |
---|
105 | im.setFullyIdentified "test", userInfo |
---|
106 | |
---|
107 | assert ! im.isAnonymous() |
---|
108 | assert ! im.isPartiallyIdentified() |
---|
109 | assert im.isFullyIdentified() |
---|
110 | |
---|
111 | assert im.getUserId() == 1 |
---|
112 | assert im.getUserName() == "test" |
---|
113 | assert im.getUserInfo() == userInfo |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Preferences include neither an url nor a user name => we have |
---|
118 | * an anonymous user |
---|
119 | */ |
---|
120 | @Test |
---|
121 | public void initFromPreferences_1() { |
---|
122 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() |
---|
123 | |
---|
124 | // reset it |
---|
125 | im.@userName = null |
---|
126 | im.@userInfo = null |
---|
127 | |
---|
128 | Main.pref.put "osm-server.url", null |
---|
129 | Main.pref.put "osm-server.username", null |
---|
130 | |
---|
131 | im.initFromPreferences() |
---|
132 | |
---|
133 | assert im.isAnonymous() |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Preferences include neither an url nor a user name => we have |
---|
138 | * an annoymous user |
---|
139 | */ |
---|
140 | @Test |
---|
141 | public void initFromPreferences_2() { |
---|
142 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() |
---|
143 | |
---|
144 | // reset it |
---|
145 | im.@userName = null |
---|
146 | im.@userInfo = null |
---|
147 | |
---|
148 | // for this test we disable the listener |
---|
149 | Main.pref.removePreferenceChangeListener im |
---|
150 | |
---|
151 | Main.pref.put "osm-server.url", "http://api.openstreetmap.org" |
---|
152 | Main.pref.put "osm-server.username", null |
---|
153 | |
---|
154 | im.initFromPreferences() |
---|
155 | |
---|
156 | assert im.isAnonymous() |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * Preferences include an user name => we have a partially identified user |
---|
161 | */ |
---|
162 | @Test |
---|
163 | public void initFromPreferences_3() { |
---|
164 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() |
---|
165 | |
---|
166 | // for this test we disable the listener |
---|
167 | Main.pref.removePreferenceChangeListener im |
---|
168 | |
---|
169 | // reset it |
---|
170 | im.@userName = null |
---|
171 | im.@userInfo = null |
---|
172 | |
---|
173 | Main.pref.put "osm-server.url", "http://api.openstreetmap.org" |
---|
174 | Main.pref.put "osm-server.username", "test" |
---|
175 | |
---|
176 | im.initFromPreferences() |
---|
177 | |
---|
178 | assert im.isPartiallyIdentified() |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * Preferences include an user name which is different from the current |
---|
183 | * user name and we are currently fully identifed => josm user becomes |
---|
184 | * partially identified |
---|
185 | */ |
---|
186 | @Test |
---|
187 | public void initFromPreferences_4() { |
---|
188 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() |
---|
189 | |
---|
190 | // for this test we disable the listener |
---|
191 | Main.pref.removePreferenceChangeListener im |
---|
192 | |
---|
193 | im.setFullyIdentified "test1", new UserInfo(id: 1) |
---|
194 | |
---|
195 | Main.pref.put "osm-server.url", "http://api.openstreetmap.org" |
---|
196 | Main.pref.put "osm-server.username", "test2" |
---|
197 | |
---|
198 | im.initFromPreferences() |
---|
199 | |
---|
200 | assert im.isPartiallyIdentified() |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Preferences include an user name which is the same as the current |
---|
205 | * user name and we are currently fully identifed => josm user remains |
---|
206 | * fully identified |
---|
207 | */ |
---|
208 | @Test |
---|
209 | public void initFromPreferences_5() { |
---|
210 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() |
---|
211 | |
---|
212 | // for this test we disable the listener |
---|
213 | Main.pref.removePreferenceChangeListener im |
---|
214 | |
---|
215 | im.setFullyIdentified "test1", new UserInfo(id: 1) |
---|
216 | |
---|
217 | Main.pref.put "osm-server.url", "http://api.openstreetmap.org" |
---|
218 | Main.pref.put "osm-server.username", "test1" |
---|
219 | |
---|
220 | im.initFromPreferences() |
---|
221 | |
---|
222 | assert im.isFullyIdentified() |
---|
223 | } |
---|
224 | |
---|
225 | @Test |
---|
226 | public void apiUrlChanged() { |
---|
227 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() |
---|
228 | |
---|
229 | // make sure im is a preference change listener |
---|
230 | Main.pref.addPreferenceChangeListener im |
---|
231 | |
---|
232 | // reset it |
---|
233 | im.@userName = null |
---|
234 | im.@userInfo = null |
---|
235 | |
---|
236 | Main.pref.put "osm-server.url", "http://api.openstreetmap.org" |
---|
237 | assert im.isAnonymous() |
---|
238 | |
---|
239 | Main.pref.put "osm-server.url", null |
---|
240 | assert im.isAnonymous() |
---|
241 | |
---|
242 | // reset it |
---|
243 | im.@userName = "test" |
---|
244 | im.@userInfo = null |
---|
245 | |
---|
246 | Main.pref.put "osm-server.url", "http://api.openstreetmap.org" |
---|
247 | assert im.isPartiallyIdentified() |
---|
248 | assert im.getUserName() == "test" |
---|
249 | |
---|
250 | Main.pref.put "osm-server.url", null |
---|
251 | assert im.isAnonymous() |
---|
252 | |
---|
253 | // reset it |
---|
254 | im.@userName = "test" |
---|
255 | im.@userInfo = new UserInfo(id:1) |
---|
256 | |
---|
257 | Main.pref.put "osm-server.url", "http://api.openstreetmap.org" |
---|
258 | assert im.isPartiallyIdentified() |
---|
259 | assert im.getUserName() == "test" |
---|
260 | |
---|
261 | // reset it |
---|
262 | im.@userName = "test" |
---|
263 | im.@userInfo = new UserInfo(id:1) |
---|
264 | |
---|
265 | |
---|
266 | Main.pref.put "osm-server.url", null |
---|
267 | assert im.isAnonymous() |
---|
268 | } |
---|
269 | |
---|
270 | @Test |
---|
271 | public void userNameChanged() { |
---|
272 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() |
---|
273 | |
---|
274 | // make sure im is a preference change listener |
---|
275 | Main.pref.addPreferenceChangeListener im |
---|
276 | |
---|
277 | // reset it |
---|
278 | im.@userName = null |
---|
279 | im.@userInfo = null |
---|
280 | |
---|
281 | Main.pref.put "osm-server.username", "test" |
---|
282 | assert im.isPartiallyIdentified() |
---|
283 | assert im.getUserName() == "test" |
---|
284 | |
---|
285 | Main.pref.put "osm-server.username", null |
---|
286 | assert im.isAnonymous() |
---|
287 | |
---|
288 | // reset it |
---|
289 | im.@userName = "test1" |
---|
290 | im.@userInfo = null |
---|
291 | |
---|
292 | Main.pref.put "osm-server.username", "test2" |
---|
293 | assert im.isPartiallyIdentified() |
---|
294 | assert im.getUserName() == "test2" |
---|
295 | |
---|
296 | Main.pref.put "osm-server.username", null |
---|
297 | assert im.isAnonymous() |
---|
298 | |
---|
299 | // reset it |
---|
300 | im.@userName = "test1" |
---|
301 | im.@userInfo = new UserInfo(id:1) |
---|
302 | |
---|
303 | Main.pref.put "osm-server.username", "test2" |
---|
304 | assert im.isPartiallyIdentified() |
---|
305 | assert im.getUserName() == "test2" |
---|
306 | |
---|
307 | // reset it |
---|
308 | im.@userName = "test1" |
---|
309 | im.@userInfo = new UserInfo(id:1) |
---|
310 | |
---|
311 | |
---|
312 | Main.pref.put "osm-server.username", null |
---|
313 | assert im.isAnonymous() |
---|
314 | } |
---|
315 | } |
---|