| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui
|
|---|
| 3 |
|
|---|
| 4 | import org.openstreetmap.josm.data.osm.User;
|
|---|
| 5 |
|
|---|
| 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 |
|
|---|
| 16 | final shouldFail = new GroovyTestCase().&shouldFail
|
|---|
| 17 |
|
|---|
| 18 | @BeforeClass
|
|---|
| 19 | public static void initTestCase() {
|
|---|
| 20 | JOSMFixture.createUnitTestFixture().init()
|
|---|
| 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 | assert im.asUser() == User.anonymous
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | @Test
|
|---|
| 58 | public void test_setPartiallyIdentified() {
|
|---|
| 59 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance()
|
|---|
| 60 |
|
|---|
| 61 | im.setPartiallyIdentified "test"
|
|---|
| 62 |
|
|---|
| 63 | shouldFail(IllegalArgumentException) {
|
|---|
| 64 | im.setPartiallyIdentified null
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | shouldFail(IllegalArgumentException) {
|
|---|
| 68 | im.setPartiallyIdentified ""
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | shouldFail(IllegalArgumentException) {
|
|---|
| 72 | im.setPartiallyIdentified " \t "
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | im.setPartiallyIdentified "test"
|
|---|
| 76 |
|
|---|
| 77 | assert ! im.isAnonymous()
|
|---|
| 78 | assert im.isPartiallyIdentified()
|
|---|
| 79 | assert ! im.isFullyIdentified()
|
|---|
| 80 |
|
|---|
| 81 | assert im.getUserId() == 0
|
|---|
| 82 | assert im.getUserName() == "test"
|
|---|
| 83 | assert im.getUserInfo() == null
|
|---|
| 84 | assert im.asUser() == new User(0, "test")
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 | @Test
|
|---|
| 89 | public void test_setFullyIdentified() {
|
|---|
| 90 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance()
|
|---|
| 91 |
|
|---|
| 92 | UserInfo userInfo = new UserInfo(id: 1, description: "a description")
|
|---|
| 93 |
|
|---|
| 94 | im.setFullyIdentified "test", userInfo
|
|---|
| 95 |
|
|---|
| 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 | }
|
|---|
| 108 |
|
|---|
| 109 | im.setFullyIdentified "test", userInfo
|
|---|
| 110 |
|
|---|
| 111 | assert ! im.isAnonymous()
|
|---|
| 112 | assert ! im.isPartiallyIdentified()
|
|---|
| 113 | assert im.isFullyIdentified()
|
|---|
| 114 |
|
|---|
| 115 | assert im.getUserId() == 1
|
|---|
| 116 | assert im.getUserName() == "test"
|
|---|
| 117 | assert im.getUserInfo() == userInfo
|
|---|
| 118 | assert im.asUser() == new User(1, "test")
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 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()
|
|---|
| 128 |
|
|---|
| 129 | // reset it
|
|---|
| 130 | im.@userName = null
|
|---|
| 131 | im.@userInfo = null
|
|---|
| 132 |
|
|---|
| 133 | Main.pref.put "osm-server.url", null
|
|---|
| 134 | Main.pref.put "osm-server.username", null
|
|---|
| 135 |
|
|---|
| 136 | im.initFromPreferences()
|
|---|
| 137 |
|
|---|
| 138 | assert im.isAnonymous()
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 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()
|
|---|
| 148 |
|
|---|
| 149 | // reset it
|
|---|
| 150 | im.@userName = null
|
|---|
| 151 | im.@userInfo = null
|
|---|
| 152 |
|
|---|
| 153 | // for this test we disable the listener
|
|---|
| 154 | Main.pref.removePreferenceChangeListener im
|
|---|
| 155 |
|
|---|
| 156 | Main.pref.put "osm-server.url", "http://api.openstreetmap.org"
|
|---|
| 157 | Main.pref.put "osm-server.username", null
|
|---|
| 158 |
|
|---|
| 159 | im.initFromPreferences()
|
|---|
| 160 |
|
|---|
| 161 | assert im.isAnonymous()
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 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()
|
|---|
| 170 |
|
|---|
| 171 | // for this test we disable the listener
|
|---|
| 172 | Main.pref.removePreferenceChangeListener im
|
|---|
| 173 |
|
|---|
| 174 | // reset it
|
|---|
| 175 | im.@userName = null
|
|---|
| 176 | im.@userInfo = null
|
|---|
| 177 |
|
|---|
| 178 | Main.pref.put "osm-server.url", "http://api.openstreetmap.org"
|
|---|
| 179 | Main.pref.put "osm-server.username", "test"
|
|---|
| 180 |
|
|---|
| 181 | im.initFromPreferences()
|
|---|
| 182 |
|
|---|
| 183 | assert im.isPartiallyIdentified()
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 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()
|
|---|
| 194 |
|
|---|
| 195 | // for this test we disable the listener
|
|---|
| 196 | Main.pref.removePreferenceChangeListener im
|
|---|
| 197 |
|
|---|
| 198 | im.setFullyIdentified "test1", new UserInfo(id: 1)
|
|---|
| 199 |
|
|---|
| 200 | Main.pref.put "osm-server.url", "http://api.openstreetmap.org"
|
|---|
| 201 | Main.pref.put "osm-server.username", "test2"
|
|---|
| 202 |
|
|---|
| 203 | im.initFromPreferences()
|
|---|
| 204 |
|
|---|
| 205 | assert im.isPartiallyIdentified()
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 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()
|
|---|
| 216 |
|
|---|
| 217 | // for this test we disable the listener
|
|---|
| 218 | Main.pref.removePreferenceChangeListener im
|
|---|
| 219 |
|
|---|
| 220 | im.setFullyIdentified "test1", new UserInfo(id: 1)
|
|---|
| 221 |
|
|---|
| 222 | Main.pref.put "osm-server.url", "http://api.openstreetmap.org"
|
|---|
| 223 | Main.pref.put "osm-server.username", "test1"
|
|---|
| 224 |
|
|---|
| 225 | im.initFromPreferences()
|
|---|
| 226 |
|
|---|
| 227 | assert im.isFullyIdentified()
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | @Test
|
|---|
| 231 | public void apiUrlChanged() {
|
|---|
| 232 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance()
|
|---|
| 233 |
|
|---|
| 234 | // make sure im is a preference change listener
|
|---|
| 235 | Main.pref.addPreferenceChangeListener im
|
|---|
| 236 |
|
|---|
| 237 | // reset it
|
|---|
| 238 | im.@userName = null
|
|---|
| 239 | im.@userInfo = null
|
|---|
| 240 |
|
|---|
| 241 | Main.pref.put "osm-server.url", "http://api.openstreetmap.org"
|
|---|
| 242 | assert im.isAnonymous()
|
|---|
| 243 |
|
|---|
| 244 | Main.pref.put "osm-server.url", null
|
|---|
| 245 | assert im.isAnonymous()
|
|---|
| 246 |
|
|---|
| 247 | // reset it
|
|---|
| 248 | im.@userName = "test"
|
|---|
| 249 | im.@userInfo = null
|
|---|
| 250 |
|
|---|
| 251 | Main.pref.put "osm-server.url", "http://api.openstreetmap.org"
|
|---|
| 252 | assert im.isPartiallyIdentified()
|
|---|
| 253 | assert im.getUserName() == "test"
|
|---|
| 254 |
|
|---|
| 255 | Main.pref.put "osm-server.url", null
|
|---|
| 256 | assert im.isAnonymous()
|
|---|
| 257 |
|
|---|
| 258 | // reset it
|
|---|
| 259 | im.@userName = "test"
|
|---|
| 260 | im.@userInfo = new UserInfo(id:1)
|
|---|
| 261 |
|
|---|
| 262 | Main.pref.put "osm-server.url", "http://api.openstreetmap.org"
|
|---|
| 263 | assert im.isPartiallyIdentified()
|
|---|
| 264 | assert im.getUserName() == "test"
|
|---|
| 265 |
|
|---|
| 266 | // reset it
|
|---|
| 267 | im.@userName = "test"
|
|---|
| 268 | im.@userInfo = new UserInfo(id:1)
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 | Main.pref.put "osm-server.url", null
|
|---|
| 272 | assert im.isAnonymous()
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | @Test
|
|---|
| 276 | public void userNameChanged() {
|
|---|
| 277 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance()
|
|---|
| 278 |
|
|---|
| 279 | // make sure im is a preference change listener
|
|---|
| 280 | Main.pref.addPreferenceChangeListener im
|
|---|
| 281 |
|
|---|
| 282 | // reset it
|
|---|
| 283 | im.@userName = null
|
|---|
| 284 | im.@userInfo = null
|
|---|
| 285 |
|
|---|
| 286 | Main.pref.put "osm-server.username", "test"
|
|---|
| 287 | assert im.isPartiallyIdentified()
|
|---|
| 288 | assert im.getUserName() == "test"
|
|---|
| 289 |
|
|---|
| 290 | Main.pref.put "osm-server.username", null
|
|---|
| 291 | assert im.isAnonymous()
|
|---|
| 292 | assert im.asUser() == User.anonymous
|
|---|
| 293 |
|
|---|
| 294 | // reset it
|
|---|
| 295 | im.@userName = "test1"
|
|---|
| 296 | im.@userInfo = null
|
|---|
| 297 |
|
|---|
| 298 | Main.pref.put "osm-server.username", "test2"
|
|---|
| 299 | assert im.isPartiallyIdentified()
|
|---|
| 300 | assert im.getUserName() == "test2"
|
|---|
| 301 | assert im.asUser() == new User(0, "test2")
|
|---|
| 302 |
|
|---|
| 303 | Main.pref.put "osm-server.username", null
|
|---|
| 304 | assert im.isAnonymous()
|
|---|
| 305 |
|
|---|
| 306 | // reset it
|
|---|
| 307 | im.@userName = "test1"
|
|---|
| 308 | im.@userInfo = new UserInfo(id:1)
|
|---|
| 309 |
|
|---|
| 310 | Main.pref.put "osm-server.username", "test2"
|
|---|
| 311 | assert im.isPartiallyIdentified()
|
|---|
| 312 | assert im.getUserName() == "test2"
|
|---|
| 313 | assert im.asUser() == new User(0, "test2")
|
|---|
| 314 |
|
|---|
| 315 | // reset it
|
|---|
| 316 | im.@userName = "test1"
|
|---|
| 317 | im.@userInfo = new UserInfo(id:1)
|
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 | Main.pref.put "osm-server.username", null
|
|---|
| 321 | assert im.isAnonymous()
|
|---|
| 322 | }
|
|---|
| 323 | }
|
|---|