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