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