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