| 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 |
|
|---|
| 17 | @BeforeClass
|
|---|
| 18 | public static void initTestCase() {
|
|---|
| 19 | JOSMFixture.createUnitTestFixture().init()
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | @Test
|
|---|
| 23 | public void test_SingletonAccess() {
|
|---|
| 24 |
|
|---|
| 25 | JosmUserIdentityManager im = JosmUserIdentityManager.getInstance()
|
|---|
| 26 |
|
|---|
| 27 | // created ?
|
|---|
| 28 | assert im != null
|
|---|
| 29 |
|
|---|
| 30 | JosmUserIdentityManager im2 = JosmUserIdentityManager.getInstance()
|
|---|
| 31 |
|
|---|
| 32 | // 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")
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /**
|
|---|
| 118 | * Preferences include neither an url nor a user name => we have an anonymous user
|
|---|
| 119 | */
|
|---|
| 120 | @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()
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Preferences include neither an url nor a user name => we have an anonymous user
|
|---|
| 138 | */
|
|---|
| 139 | @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
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 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()
|
|---|
| 168 |
|
|---|
| 169 | // for this test we disable the listener
|
|---|
| 170 | Main.pref.removePreferenceChangeListener im
|
|---|
| 171 |
|
|---|
| 172 | try {
|
|---|
| 173 | // 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
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 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()
|
|---|
| 196 |
|
|---|
| 197 | // for this test we disable the listener
|
|---|
| 198 | Main.pref.removePreferenceChangeListener im
|
|---|
| 199 |
|
|---|
| 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 im
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 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()
|
|---|
| 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()
|
|---|
| 326 | }
|
|---|
| 327 | }
|
|---|