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