- Timestamp:
- 2017-04-14T22:58:46+02:00 (8 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/User.java
r10841 r11909 70 70 71 71 if (lastUser != null && lastUser.getId() == uid) { 72 lastUser.setPreferredName(name); 72 73 return lastUser; 73 74 } -
trunk/test/unit/org/openstreetmap/josm/gui/JosmUserIdentityManagerTest.java
r11900 r11909 1 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 2 package org.openstreetmap.josm.gui; 3 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertFalse; 6 import static org.junit.Assert.assertNotNull; 7 import static org.junit.Assert.assertNull; 8 import static org.junit.Assert.assertSame; 9 import static org.junit.Assert.assertTrue; 10 11 import org.junit.BeforeClass; 12 import org.junit.Test; 13 import org.openstreetmap.josm.JOSMFixture; 14 import org.openstreetmap.josm.Main; 15 import org.openstreetmap.josm.data.osm.User; 16 import org.openstreetmap.josm.data.osm.UserInfo; 17 18 /** 19 * Unit tests of {@link JosmUserIdentityManager} class. 20 */ 21 public class JosmUserIdentityManagerTest { 22 23 /** 24 * Setup test. 25 */ 17 26 @BeforeClass 18 27 public static void initTestCase() { 19 JOSMFixture.createUnitTestFixture().init() 20 } 21 22 @Test 23 public void test_SingletonAccess() { 24 25 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() 28 JOSMFixture.createUnitTestFixture().init(); 29 } 30 31 private static UserInfo newUserInfo() { 32 return newUserInfo(1, "a description"); 33 } 34 35 private static UserInfo newUserInfo(int id, String description) { 36 UserInfo userInfo = new UserInfo(); 37 userInfo.setId(id); 38 userInfo.setDescription(description); 39 return userInfo; 40 } 41 42 /** 43 * Test singleton access. 44 */ 45 @Test 46 public void testSingletonAccess() { 47 48 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance(); 26 49 27 50 // created ? 28 assert im != null29 30 JosmUserIdentityManager im2 = JosmUserIdentityManager.getInstance() 51 assertNotNull(im); 52 53 JosmUserIdentityManager im2 = JosmUserIdentityManager.getInstance(); 31 54 32 55 // only one instance 33 assert im == im2 34 } 35 36 @Test 37 public void test_setAnonymous() { 38 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() 39 40 im.setPartiallyIdentified "test" 41 im.setAnonymous() 42 43 assert im.isAnonymous() 44 assert ! im.isPartiallyIdentified() 45 assert ! im.isFullyIdentified() 46 47 assert im.getUserId() == 0 48 assert im.getUserName() == null 49 assert im.getUserInfo() == null 50 assert im.asUser() == User.anonymous 51 } 52 53 @Test 54 public void test_setPartiallyIdentified() { 55 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() 56 57 im.setPartiallyIdentified "test" 58 59 shouldFail(IllegalArgumentException) { 60 im.setPartiallyIdentified null 61 } 62 63 shouldFail(IllegalArgumentException) { 64 im.setPartiallyIdentified "" 65 } 66 67 shouldFail(IllegalArgumentException) { 68 im.setPartiallyIdentified " \t " 69 } 70 71 im.setPartiallyIdentified "test" 72 73 assert ! im.isAnonymous() 74 assert im.isPartiallyIdentified() 75 assert ! im.isFullyIdentified() 76 77 assert im.getUserId() == 0 78 assert im.getUserName() == "test" 79 assert im.getUserInfo() == null 80 assert im.asUser() == new User(0, "test") 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 assert im.asUser() == new User(1, "test") 56 assertSame(im, im2); 57 } 58 59 /** 60 * Unit test of {@link JosmUserIdentityManager#setAnonymous}. 61 */ 62 @Test 63 public void testSetAnonymous() { 64 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance(); 65 66 im.setPartiallyIdentified("test"); 67 im.setAnonymous(); 68 69 assertTrue(im.isAnonymous()); 70 assertFalse(im.isPartiallyIdentified()); 71 assertFalse(im.isFullyIdentified()); 72 73 assertEquals(0, im.getUserId()); 74 assertNull(im.getUserName()); 75 assertNull(im.getUserInfo()); 76 assertSame(User.getAnonymous(), im.asUser()); 77 } 78 79 /** 80 * Unit test of {@link JosmUserIdentityManager#setPartiallyIdentified} - nominal case. 81 */ 82 @Test 83 public void testSetPartiallyIdentified() { 84 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance(); 85 im.setPartiallyIdentified("test"); 86 87 assertFalse(im.isAnonymous()); 88 assertTrue(im.isPartiallyIdentified()); 89 assertFalse(im.isFullyIdentified()); 90 91 assertEquals(0, im.getUserId()); 92 assertEquals("test", im.getUserName()); 93 assertNull(im.getUserInfo()); 94 User usr = im.asUser(); 95 assertEquals(0, usr.getId()); 96 assertEquals("test", usr.getName()); 97 } 98 99 /** 100 * Unit test of {@link JosmUserIdentityManager#setPartiallyIdentified} - null case. 101 */ 102 @Test(expected = IllegalArgumentException.class) 103 public void testSetPartiallyIdentifiedNull() { 104 JosmUserIdentityManager.getInstance().setPartiallyIdentified(null); 105 } 106 107 /** 108 * Unit test of {@link JosmUserIdentityManager#setPartiallyIdentified} - empty case. 109 */ 110 @Test(expected = IllegalArgumentException.class) 111 public void testSetPartiallyIdentifiedEmpty() { 112 JosmUserIdentityManager.getInstance().setPartiallyIdentified(""); 113 } 114 115 /** 116 * Unit test of {@link JosmUserIdentityManager#setPartiallyIdentified} - blank case. 117 */ 118 @Test(expected = IllegalArgumentException.class) 119 public void testSetPartiallyIdentifiedBlank() { 120 JosmUserIdentityManager.getInstance().setPartiallyIdentified(" \t "); 121 } 122 123 /** 124 * Unit test of {@link JosmUserIdentityManager#setFullyIdentified} - nominal case. 125 */ 126 @Test 127 public void testSetFullyIdentified() { 128 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance(); 129 130 UserInfo userInfo = newUserInfo(); 131 132 im.setFullyIdentified("test", userInfo); 133 134 assertFalse(im.isAnonymous()); 135 assertFalse(im.isPartiallyIdentified()); 136 assertTrue(im.isFullyIdentified()); 137 138 assertEquals(1, im.getUserId()); 139 assertEquals("test", im.getUserName()); 140 assertEquals(userInfo, im.getUserInfo()); 141 User usr = im.asUser(); 142 assertEquals(1, usr.getId()); 143 assertEquals("test", usr.getName()); 144 } 145 146 /** 147 * Unit test of {@link JosmUserIdentityManager#setFullyIdentified} - null name case. 148 */ 149 @Test(expected = IllegalArgumentException.class) 150 public void testSetFullyIdentifiedNullName() { 151 JosmUserIdentityManager.getInstance().setFullyIdentified(null, newUserInfo()); 152 } 153 154 /** 155 * Unit test of {@link JosmUserIdentityManager#setFullyIdentified} - empty name case. 156 */ 157 @Test(expected = IllegalArgumentException.class) 158 public void testSetFullyIdentifiedEmptyName() { 159 JosmUserIdentityManager.getInstance().setFullyIdentified("", newUserInfo()); 160 } 161 162 /** 163 * Unit test of {@link JosmUserIdentityManager#setFullyIdentified} - blank name case. 164 */ 165 @Test(expected = IllegalArgumentException.class) 166 public void testSetFullyIdentifiedBlankName() { 167 JosmUserIdentityManager.getInstance().setFullyIdentified(" \t ", newUserInfo()); 168 } 169 170 /** 171 * Unit test of {@link JosmUserIdentityManager#setFullyIdentified} - null info case. 172 */ 173 @Test(expected = IllegalArgumentException.class) 174 public void testSetFullyIdentifiedNullInfo() { 175 JosmUserIdentityManager.getInstance().setFullyIdentified("test", null); 115 176 } 116 177 … … 119 180 */ 120 181 @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() 182 public void testInitFromPreferences1() { 183 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance(); 184 185 // reset it 186 im.setAnonymous(); 187 188 // for this test we disable the listener 189 Main.pref.removePreferenceChangeListener(im); 190 191 try { 192 Main.pref.put("osm-server.url", null); 193 Main.pref.put("osm-server.username", null); 194 195 im.initFromPreferences(); 196 197 assertTrue(im.isAnonymous()); 198 } finally { 199 Main.pref.addPreferenceChangeListener(im); 200 } 134 201 } 135 202 … … 138 205 */ 139 206 @Test 140 public void initFromPreferences_2() { 141 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() 142 143 // reset it 144 im.@userName = null 145 im.@userInfo = null 146 147 // for this test we disable the listener 148 Main.pref.removePreferenceChangeListener im 149 150 try { 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 } finally { 158 Main.pref.addPreferenceChangeListener im 207 public void testInitFromPreferences2() { 208 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance(); 209 210 // reset it 211 im.setAnonymous(); 212 213 // for this test we disable the listener 214 Main.pref.removePreferenceChangeListener(im); 215 216 try { 217 Main.pref.put("osm-server.url", "http://api.openstreetmap.org"); 218 Main.pref.put("osm-server.username", null); 219 220 im.initFromPreferences(); 221 222 assertTrue(im.isAnonymous()); 223 } finally { 224 Main.pref.addPreferenceChangeListener(im); 159 225 } 160 226 } … … 164 230 */ 165 231 @Test 166 public void initFromPreferences_3() {167 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() 168 169 // for this test we disable the listener 170 Main.pref.removePreferenceChangeListener im232 public void testInitFromPreferences3() { 233 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance(); 234 235 // for this test we disable the listener 236 Main.pref.removePreferenceChangeListener(im); 171 237 172 238 try { 173 239 // reset it 174 im.@userName = null 175 im.@userInfo = null 176 177 Main.pref.put "osm-server.url", "http://api.openstreetmap.org" 178 Main.pref.put "osm-server.username", "test" 179 180 im.initFromPreferences() 181 182 assert im.isPartiallyIdentified() 183 } finally { 184 Main.pref.addPreferenceChangeListener im 240 im.setAnonymous(); 241 242 Main.pref.put("osm-server.url", "http://api.openstreetmap.org"); 243 Main.pref.put("osm-server.username", "test"); 244 245 im.initFromPreferences(); 246 247 assertTrue(im.isPartiallyIdentified()); 248 } finally { 249 Main.pref.addPreferenceChangeListener(im); 185 250 } 186 251 } … … 192 257 */ 193 258 @Test 194 public void initFromPreferences_4() {195 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() 196 197 // for this test we disable the listener 198 Main.pref.removePreferenceChangeListener im199 200 try { 201 im.setFullyIdentified "test1", new UserInfo(id: 1)202 203 Main.pref.put "osm-server.url", "http://api.openstreetmap.org"204 Main.pref.put "osm-server.username", "test2"205 206 im.initFromPreferences() 207 208 assert im.isPartiallyIdentified()209 } finally { 210 Main.pref.addPreferenceChangeListener im259 public void testInitFromPreferences4() { 260 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance(); 261 262 // for this test we disable the listener 263 Main.pref.removePreferenceChangeListener(im); 264 265 try { 266 im.setFullyIdentified("test1", newUserInfo()); 267 268 Main.pref.put("osm-server.url", "http://api.openstreetmap.org"); 269 Main.pref.put("osm-server.username", "test2"); 270 271 im.initFromPreferences(); 272 273 assertTrue(im.isPartiallyIdentified()); 274 } finally { 275 Main.pref.addPreferenceChangeListener(im); 211 276 } 212 277 } … … 218 283 */ 219 284 @Test 220 public void initFromPreferences_5() { 221 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() 222 223 // for this test we disable the listener 224 Main.pref.removePreferenceChangeListener im 225 226 try { 227 im.setFullyIdentified "test1", new UserInfo(id: 1) 228 229 Main.pref.put "osm-server.url", "http://api.openstreetmap.org" 230 Main.pref.put "osm-server.username", "test1" 231 232 im.initFromPreferences() 233 234 assert im.isFullyIdentified() 235 } finally { 236 Main.pref.addPreferenceChangeListener im 237 } 238 } 239 240 @Test 241 public void apiUrlChanged() { 242 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() 243 244 // reset it 245 im.@userName = null 246 im.@userInfo = null 247 248 Main.pref.put "osm-server.url", "http://api.openstreetmap.org" 249 assert im.isAnonymous() 250 251 Main.pref.put "osm-server.url", null 252 assert im.isAnonymous() 253 254 // reset it 255 im.@userName = "test" 256 im.@userInfo = null 257 258 Main.pref.put "osm-server.url", "http://api.openstreetmap.org" 259 assert im.isPartiallyIdentified() 260 assert im.getUserName() == "test" 261 262 Main.pref.put "osm-server.url", null 263 assert im.isAnonymous() 264 265 // reset it 266 im.@userName = "test" 267 im.@userInfo = new UserInfo(id:1) 268 269 Main.pref.put "osm-server.url", "http://api.openstreetmap.org" 270 assert im.isPartiallyIdentified() 271 assert im.getUserName() == "test" 272 273 // reset it 274 im.@userName = "test" 275 im.@userInfo = new UserInfo(id:1) 276 277 278 Main.pref.put "osm-server.url", null 279 assert im.isAnonymous() 280 } 281 282 @Test 283 public void userNameChanged() { 284 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance() 285 286 // reset it 287 im.@userName = null 288 im.@userInfo = null 289 290 Main.pref.put "osm-server.username", "test" 291 assert im.isPartiallyIdentified() 292 assert im.getUserName() == "test" 293 294 Main.pref.put "osm-server.username", null 295 assert im.isAnonymous() 296 assert im.asUser() == User.anonymous 297 298 // reset it 299 im.@userName = "test1" 300 im.@userInfo = null 301 302 Main.pref.put "osm-server.username", "test2" 303 assert im.isPartiallyIdentified() 304 assert im.getUserName() == "test2" 305 assert im.asUser() == new User(0, "test2") 306 307 Main.pref.put "osm-server.username", null 308 assert im.isAnonymous() 309 310 // reset it 311 im.@userName = "test1" 312 im.@userInfo = new UserInfo(id:1) 313 314 Main.pref.put "osm-server.username", "test2" 315 assert im.isPartiallyIdentified() 316 assert im.getUserName() == "test2" 317 assert im.asUser() == new User(0, "test2") 318 319 // reset it 320 im.@userName = "test1" 321 im.@userInfo = new UserInfo(id:1) 322 323 324 Main.pref.put "osm-server.username", null 325 assert im.isAnonymous() 285 public void testInitFromPreferences5() { 286 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance(); 287 288 // for this test we disable the listener 289 Main.pref.removePreferenceChangeListener(im); 290 291 try { 292 im.setFullyIdentified("test1", new UserInfo()); 293 294 Main.pref.put("osm-server.url", "http://api.openstreetmap.org"); 295 Main.pref.put("osm-server.username", "test1"); 296 297 im.initFromPreferences(); 298 299 assertTrue(im.isFullyIdentified()); 300 } finally { 301 Main.pref.addPreferenceChangeListener(im); 302 } 303 } 304 305 @Test 306 public void testApiUrlChanged() { 307 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance(); 308 309 // reset it 310 im.setAnonymous(); 311 312 Main.pref.put("osm-server.url", "http://api.openstreetmap.org"); 313 assertTrue(im.isAnonymous()); 314 315 Main.pref.put("osm-server.url", null); 316 assertTrue(im.isAnonymous()); 317 318 // reset it 319 im.setPartiallyIdentified("test"); 320 321 Main.pref.put("osm-server.url", "http://api.openstreetmap.org"); 322 assertTrue(im.isPartiallyIdentified()); 323 assertEquals("test", im.getUserName()); 324 325 Main.pref.put("osm-server.url", null); 326 assertTrue(im.isAnonymous()); 327 328 // reset it 329 im.setFullyIdentified("test", newUserInfo()); 330 331 Main.pref.put("osm-server.url", "http://api.openstreetmap.org"); 332 assertTrue(im.isPartiallyIdentified()); 333 assertEquals("test", im.getUserName()); 334 335 // reset it 336 im.setFullyIdentified("test", newUserInfo()); 337 338 Main.pref.put("osm-server.url", null); 339 assertTrue(im.isAnonymous()); 340 } 341 342 @Test 343 //@Ignore 344 public void testUserNameChanged() { 345 JosmUserIdentityManager im = JosmUserIdentityManager.getInstance(); 346 347 // reset it 348 im.setAnonymous(); 349 350 Main.pref.put("osm-server.username", "test"); 351 assertTrue(im.isPartiallyIdentified()); 352 assertEquals("test", im.getUserName()); 353 354 Main.pref.put("osm-server.username", null); 355 assertTrue(im.isAnonymous()); 356 assertEquals(User.getAnonymous(), im.asUser()); 357 358 // reset it 359 im.setPartiallyIdentified("test1"); 360 361 Main.pref.put("osm-server.username", "test2"); 362 assertTrue(im.isPartiallyIdentified()); 363 assertEquals("test2", im.getUserName()); 364 User usr = im.asUser(); 365 assertEquals(0, usr.getId()); 366 assertEquals("test2", usr.getName()); 367 368 Main.pref.put("osm-server.username", null); 369 assertTrue(im.isAnonymous()); 370 371 // reset it 372 im.setFullyIdentified("test1", newUserInfo()); 373 374 Main.pref.put("osm-server.username", "test2"); 375 assertTrue(im.isPartiallyIdentified()); 376 assertEquals("test2", im.getUserName()); 377 usr = im.asUser(); 378 assertEquals(0, usr.getId()); 379 assertEquals("test2", usr.getName()); 380 381 // reset it 382 im.setFullyIdentified("test1", newUserInfo()); 383 384 Main.pref.put("osm-server.username", null); 385 assertTrue(im.isAnonymous()); 326 386 } 327 387 }
Note:
See TracChangeset
for help on using the changeset viewer.