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