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