|
Added
Link Here
|
| 1 |
package org.eclipse.ui.tests.rcp; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.Reader; |
| 5 |
import java.io.StringReader; |
| 6 |
import java.io.StringWriter; |
| 7 |
|
| 8 |
import junit.framework.TestCase; |
| 9 |
|
| 10 |
import org.eclipse.ui.IMemento; |
| 11 |
import org.eclipse.ui.WorkbenchException; |
| 12 |
import org.eclipse.ui.XMLMemento; |
| 13 |
|
| 14 |
/** |
| 15 |
* Testing XMLMemento. Emphasis is on ensuring that the 3.1 version behaves just |
| 16 |
* like the 3.0.1 version. |
| 17 |
* |
| 18 |
* @since 3.1 |
| 19 |
* |
| 20 |
*/ |
| 21 |
public class XMLMementoTest extends TestCase { |
| 22 |
|
| 23 |
private static final String[] TEST_STRINGS = { "value", |
| 24 |
" value with spaces ", "value.with.many.dots", |
| 25 |
"\nvalue\nwith\nnewlines\n", "value_with_underscores", |
| 26 |
"value<with<lessthan", "value>with>greaterthan", |
| 27 |
"value&with&ersand", "value\"with\"quote", "value#with#hash", |
| 28 |
"\tvalue\twith\ttab\t", "\rvalue\rwith\rreturn\r", "", }; |
| 29 |
|
| 30 |
/* |
| 31 |
* Class under test for XMLMemento createReadRoot(Reader) |
| 32 |
*/ |
| 33 |
public void testCreateReadRootReaderExceptionCases() { |
| 34 |
try { |
| 35 |
XMLMemento.createReadRoot(new StringReader("Invalid format")); |
| 36 |
fail("should throw WorkbenchException because of invalid format"); |
| 37 |
} catch (WorkbenchException e) { |
| 38 |
// expected |
| 39 |
} |
| 40 |
try { |
| 41 |
XMLMemento.createReadRoot(new StringReader( |
| 42 |
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>")); |
| 43 |
fail("should throw WorkbenchException because there is no element"); |
| 44 |
} catch (WorkbenchException e) { |
| 45 |
// expected |
| 46 |
} |
| 47 |
try { |
| 48 |
XMLMemento.createReadRoot(new Reader() { |
| 49 |
|
| 50 |
public void close() throws IOException { |
| 51 |
throw new IOException(); |
| 52 |
} |
| 53 |
|
| 54 |
public int read(char[] arg0, int arg1, int arg2) |
| 55 |
throws IOException { |
| 56 |
throw new IOException(); |
| 57 |
} |
| 58 |
}); |
| 59 |
fail("should throw WorkbenchException because of IOException"); |
| 60 |
} catch (WorkbenchException e) { |
| 61 |
// expected |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public void testCreateReadRootReader() throws WorkbenchException { |
| 66 |
XMLMemento memento = XMLMemento |
| 67 |
.createReadRoot(new StringReader( |
| 68 |
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><simple>some text data</simple>")); |
| 69 |
assertEquals("some text data", memento.getTextData()); |
| 70 |
} |
| 71 |
|
| 72 |
/* |
| 73 |
* Class under test for XMLMemento createReadRoot(Reader, String) |
| 74 |
*/ |
| 75 |
public void testCreateReadRootReaderString() { |
| 76 |
// TODO - I don't know how to test this. The method is not called by |
| 77 |
// anyone as of 2005/04/05. |
| 78 |
} |
| 79 |
|
| 80 |
public void testCreateWriteRoot() { |
| 81 |
String[] rootTypes = { "type", "type.with.dots", |
| 82 |
"type_with_underscores" }; |
| 83 |
for (int i = 0; i < rootTypes.length; i++) { |
| 84 |
String type = rootTypes[i]; |
| 85 |
XMLMemento memento = XMLMemento.createWriteRoot(type); |
| 86 |
assertNotNull(memento); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
public void testSpacesInRootAreIllegal() { |
| 91 |
try { |
| 92 |
XMLMemento.createWriteRoot("with space"); |
| 93 |
fail("should fail"); |
| 94 |
} catch (Exception e) { |
| 95 |
// expected |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public void testSpacesInKeysAreIllegal() { |
| 100 |
XMLMemento memento = XMLMemento.createWriteRoot("foo"); |
| 101 |
try { |
| 102 |
memento.createChild("with space", "bar"); |
| 103 |
fail("should fail"); |
| 104 |
} catch (Exception e) { |
| 105 |
// expected |
| 106 |
} |
| 107 |
try { |
| 108 |
memento.putString("with space", "bar"); |
| 109 |
fail("should fail"); |
| 110 |
} catch (Exception e) { |
| 111 |
// expected |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public void testCopyChild() throws WorkbenchException, IOException { |
| 116 |
|
| 117 |
testPutAndGet(new MementoChecker() { |
| 118 |
|
| 119 |
public void prepareAndCheckBeforeSerialization( |
| 120 |
XMLMemento mementoToSerialize) { |
| 121 |
IMemento child = mementoToSerialize.createChild("c", "i"); |
| 122 |
fillMemento(child); |
| 123 |
IMemento copiedChild = mementoToSerialize.copyChild(child); |
| 124 |
assertEquals("i", copiedChild.getID()); |
| 125 |
checkMemento(copiedChild, true); |
| 126 |
} |
| 127 |
|
| 128 |
public void checkAfterDeserialization(XMLMemento deserializedMemento) { |
| 129 |
IMemento child = deserializedMemento.getChild("c"); |
| 130 |
checkMemento(child, true); |
| 131 |
IMemento[] children = deserializedMemento.getChildren("c"); |
| 132 |
assertEquals(2, children.length); |
| 133 |
assertEquals("i", children[0].getID()); |
| 134 |
checkMemento(children[0], true); |
| 135 |
assertEquals("i", children[1].getID()); |
| 136 |
checkMemento(children[1], true); |
| 137 |
} |
| 138 |
}); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Helper method to fill a memento to be checked later by checkMemento. |
| 143 |
* |
| 144 |
* @param memento |
| 145 |
*/ |
| 146 |
private void fillMemento(IMemento memento) { |
| 147 |
memento.putFloat("floatKey", 0.4f); |
| 148 |
memento.putInteger("integerKey", 324765); |
| 149 |
memento.putString("stringKey", "a string"); |
| 150 |
memento.putTextData("some text data"); |
| 151 |
memento.createChild("child1"); |
| 152 |
memento.createChild("child2", "child2id1"); |
| 153 |
memento.createChild("child2", "child2id2"); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Helper method to check if the values set by fillMemento are still there. |
| 158 |
* The boolean parameter is needed because in some cases |
| 159 |
* (IMememento#putMemento), the text data gets lost. |
| 160 |
* |
| 161 |
* @param memento |
| 162 |
* @param checkForTextData |
| 163 |
*/ |
| 164 |
protected void checkMemento(IMemento memento, boolean checkForTextData) { |
| 165 |
assertEquals(0.4f, memento.getFloat("floatKey").floatValue(), 0.0f); |
| 166 |
assertEquals(324765, memento.getInteger("integerKey").intValue()); |
| 167 |
assertEquals("a string", memento.getString("stringKey")); |
| 168 |
if (checkForTextData) { |
| 169 |
assertEquals("some text data", memento.getTextData()); |
| 170 |
} |
| 171 |
IMemento child1 = memento.getChild("child1"); |
| 172 |
assertNotNull(child1); |
| 173 |
IMemento child2 = memento.getChild("child2"); |
| 174 |
assertNotNull(child2); |
| 175 |
assertEquals("child2id1", child2.getID()); |
| 176 |
IMemento[] children = memento.getChildren("child2"); |
| 177 |
assertNotNull(children); |
| 178 |
assertEquals(2, children.length); |
| 179 |
assertEquals("child2id1", children[0].getID()); |
| 180 |
assertEquals("child2id2", children[1].getID()); |
| 181 |
} |
| 182 |
|
| 183 |
public void testCreateAndGetChild() throws WorkbenchException, IOException { |
| 184 |
final String type1 = "type1"; |
| 185 |
final String type2 = "type2"; |
| 186 |
final String id = "id"; |
| 187 |
|
| 188 |
testPutAndGet(new MementoChecker() { |
| 189 |
|
| 190 |
public void prepareAndCheckBeforeSerialization( |
| 191 |
XMLMemento mementoToSerialize) { |
| 192 |
// check that nothing is there yet |
| 193 |
assertEquals(null, mementoToSerialize.getChild(type1)); |
| 194 |
assertEquals(null, mementoToSerialize.getChild(type2)); |
| 195 |
|
| 196 |
// creation without ID |
| 197 |
IMemento child1 = mementoToSerialize.createChild(type1); |
| 198 |
assertNotNull(child1); |
| 199 |
assertNotNull(mementoToSerialize.getChild(type1)); |
| 200 |
|
| 201 |
// creation with ID |
| 202 |
IMemento child2 = mementoToSerialize.createChild(type2, id); |
| 203 |
assertNotNull(child2); |
| 204 |
assertNotNull(mementoToSerialize.getChild(type2)); |
| 205 |
assertEquals(id, child2.getID()); |
| 206 |
} |
| 207 |
|
| 208 |
public void checkAfterDeserialization(XMLMemento deserializedMemento) { |
| 209 |
IMemento child1 = deserializedMemento.getChild(type1); |
| 210 |
assertNotNull(child1); |
| 211 |
IMemento child2 = deserializedMemento.getChild(type2); |
| 212 |
assertNotNull(child2); |
| 213 |
assertEquals(id, child2.getID()); |
| 214 |
} |
| 215 |
}); |
| 216 |
} |
| 217 |
|
| 218 |
public void testGetChildren() throws WorkbenchException, IOException { |
| 219 |
final String type = "type"; |
| 220 |
final String id1 = "id"; |
| 221 |
final String id2 = "id2"; |
| 222 |
|
| 223 |
testPutAndGet(new MementoChecker() { |
| 224 |
|
| 225 |
public void prepareAndCheckBeforeSerialization( |
| 226 |
XMLMemento mementoToSerialize) { |
| 227 |
// check that nothing is there yet |
| 228 |
assertEquals(null, mementoToSerialize.getChild(type)); |
| 229 |
|
| 230 |
IMemento child1 = mementoToSerialize.createChild(type, id1); |
| 231 |
assertNotNull(child1); |
| 232 |
assertNotNull(mementoToSerialize.getChild(type)); |
| 233 |
assertEquals(id1, child1.getID()); |
| 234 |
|
| 235 |
// second child with the same type |
| 236 |
IMemento child2 = mementoToSerialize.createChild(type, id2); |
| 237 |
assertNotNull(child2); |
| 238 |
assertEquals(2, mementoToSerialize.getChildren(type).length); |
| 239 |
assertEquals(id2, child2.getID()); |
| 240 |
} |
| 241 |
|
| 242 |
public void checkAfterDeserialization(XMLMemento deserializedMemento) { |
| 243 |
IMemento[] children = deserializedMemento.getChildren(type); |
| 244 |
assertNotNull(children); |
| 245 |
assertEquals(2, children.length); |
| 246 |
|
| 247 |
// this checks that the order is maintained. |
| 248 |
// the spec does not promise this, but clients |
| 249 |
// may rely on the current implementation behaviour. |
| 250 |
assertEquals(id1, children[0].getID()); |
| 251 |
assertEquals(id2, children[1].getID()); |
| 252 |
} |
| 253 |
}); |
| 254 |
} |
| 255 |
|
| 256 |
public void testGetID() throws WorkbenchException, IOException { |
| 257 |
final String type = "type"; |
| 258 |
|
| 259 |
String[] ids = { "id", "", "id.with.many.dots", "id_with_underscores", |
| 260 |
"id<with<lessthan", "id>with>greaterthan", "id&with&ersand", |
| 261 |
"id\"with\"quote", "id#with#hash" }; |
| 262 |
|
| 263 |
for (int i = 0; i < ids.length; i++) { |
| 264 |
final String id = ids[i]; |
| 265 |
|
| 266 |
testPutAndGet(new MementoChecker() { |
| 267 |
|
| 268 |
public void prepareAndCheckBeforeSerialization( |
| 269 |
XMLMemento mementoToSerialize) { |
| 270 |
assertEquals(null, mementoToSerialize.getChild(type)); |
| 271 |
IMemento child = mementoToSerialize.createChild(type, id); |
| 272 |
assertEquals(id, child.getID()); |
| 273 |
} |
| 274 |
|
| 275 |
public void checkAfterDeserialization( |
| 276 |
XMLMemento deserializedMemento) { |
| 277 |
IMemento child = deserializedMemento.getChild(type); |
| 278 |
assertNotNull(child); |
| 279 |
assertEquals(id, child.getID()); |
| 280 |
} |
| 281 |
}); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
public void testPutAndGetFloat() throws WorkbenchException, IOException { |
| 286 |
final String key = "key"; |
| 287 |
|
| 288 |
final Float[] values = new Float[] { new Float(-3.1415), new Float(1), |
| 289 |
new Float(0), new Float(4554.45235), |
| 290 |
new Float(Float.MAX_VALUE), new Float(Float.MIN_VALUE), |
| 291 |
new Float(Float.NaN), new Float(Float.POSITIVE_INFINITY), |
| 292 |
new Float(Float.NEGATIVE_INFINITY) }; |
| 293 |
|
| 294 |
for (int i = 0; i < values.length; i++) { |
| 295 |
final Float value = values[i]; |
| 296 |
testPutAndGet(new MementoChecker() { |
| 297 |
|
| 298 |
public void prepareAndCheckBeforeSerialization( |
| 299 |
XMLMemento mementoToSerialize) { |
| 300 |
assertEquals(null, mementoToSerialize.getFloat(key)); |
| 301 |
mementoToSerialize.putFloat(key, value.floatValue()); |
| 302 |
assertEquals(value, mementoToSerialize.getFloat(key)); |
| 303 |
} |
| 304 |
|
| 305 |
public void checkAfterDeserialization( |
| 306 |
XMLMemento deserializedMemento) { |
| 307 |
assertEquals(value, deserializedMemento.getFloat(key)); |
| 308 |
} |
| 309 |
}); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
public void testPutAndGetInteger() throws WorkbenchException, IOException { |
| 314 |
final String key = "key"; |
| 315 |
|
| 316 |
Integer[] values = new Integer[] { new Integer(36254), new Integer(0), |
| 317 |
new Integer(1), new Integer(-36254), |
| 318 |
new Integer(Integer.MAX_VALUE), new Integer(Integer.MIN_VALUE) }; |
| 319 |
|
| 320 |
for (int i = 0; i < values.length; i++) { |
| 321 |
final Integer value = values[i]; |
| 322 |
|
| 323 |
testPutAndGet(new MementoChecker() { |
| 324 |
|
| 325 |
public void prepareAndCheckBeforeSerialization( |
| 326 |
XMLMemento mementoToSerialize) { |
| 327 |
assertEquals(null, mementoToSerialize.getInteger(key)); |
| 328 |
mementoToSerialize.putInteger(key, value.intValue()); |
| 329 |
assertEquals(value, mementoToSerialize.getInteger(key)); |
| 330 |
} |
| 331 |
|
| 332 |
public void checkAfterDeserialization( |
| 333 |
XMLMemento deserializedMemento) { |
| 334 |
assertEquals(value, deserializedMemento.getInteger(key)); |
| 335 |
} |
| 336 |
}); |
| 337 |
} |
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
public void testPutMemento() throws WorkbenchException, IOException { |
| 342 |
testPutAndGet(new MementoChecker() { |
| 343 |
|
| 344 |
public void prepareAndCheckBeforeSerialization( |
| 345 |
XMLMemento mementoToSerialize) { |
| 346 |
mementoToSerialize.putTextData("unchanged text data"); |
| 347 |
mementoToSerialize.putString("neverlost", "retained value"); |
| 348 |
|
| 349 |
IMemento aMemento = XMLMemento.createWriteRoot("foo"); |
| 350 |
fillMemento(aMemento); |
| 351 |
|
| 352 |
// note that this does not copy the text data: |
| 353 |
mementoToSerialize.putMemento(aMemento); |
| 354 |
|
| 355 |
// do not check for text data: |
| 356 |
checkMemento(mementoToSerialize, false); |
| 357 |
|
| 358 |
assertEquals("unchanged text data", mementoToSerialize |
| 359 |
.getTextData()); |
| 360 |
assertEquals("retained value", mementoToSerialize |
| 361 |
.getString("neverlost")); |
| 362 |
} |
| 363 |
|
| 364 |
public void checkAfterDeserialization(XMLMemento deserializedMemento) { |
| 365 |
// do not check for text data: |
| 366 |
checkMemento(deserializedMemento, false); |
| 367 |
|
| 368 |
assertEquals("unchanged text data", deserializedMemento |
| 369 |
.getTextData()); |
| 370 |
assertEquals("retained value", deserializedMemento |
| 371 |
.getString("neverlost")); |
| 372 |
} |
| 373 |
}); |
| 374 |
} |
| 375 |
|
| 376 |
public void testPutAndGetString() throws IOException, WorkbenchException { |
| 377 |
final String key = "key"; |
| 378 |
String[] values = TEST_STRINGS; |
| 379 |
|
| 380 |
for (int i = 0; i < values.length; i++) { |
| 381 |
final String value = values[i]; |
| 382 |
|
| 383 |
testPutAndGet(new MementoChecker() { |
| 384 |
|
| 385 |
public void prepareAndCheckBeforeSerialization( |
| 386 |
XMLMemento mementoToSerialize) { |
| 387 |
assertEquals(null, mementoToSerialize.getString(key)); |
| 388 |
String helper = value; |
| 389 |
mementoToSerialize.putString(key, value); |
| 390 |
assertEquals(value, mementoToSerialize.getString(key)); |
| 391 |
helper.toString(); |
| 392 |
} |
| 393 |
|
| 394 |
public void checkAfterDeserialization( |
| 395 |
XMLMemento deserializedMemento) { |
| 396 |
assertEquals(value, deserializedMemento.getString(key)); |
| 397 |
} |
| 398 |
}); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
public void testPutAndGetTextData() throws WorkbenchException, IOException { |
| 403 |
String[] values = TEST_STRINGS; |
| 404 |
|
| 405 |
for (int i = 0; i < values.length; i++) { |
| 406 |
final String data = values[i]; |
| 407 |
testPutAndGet(new MementoChecker() { |
| 408 |
|
| 409 |
public void prepareAndCheckBeforeSerialization( |
| 410 |
XMLMemento mementoToSerialize) { |
| 411 |
assertEquals(null, mementoToSerialize.getTextData()); |
| 412 |
mementoToSerialize.putTextData(data); |
| 413 |
assertEquals(data, mementoToSerialize.getTextData()); |
| 414 |
} |
| 415 |
|
| 416 |
public void checkAfterDeserialization( |
| 417 |
XMLMemento deserializedMemento) { |
| 418 |
if (data.equals("")) { |
| 419 |
// this comes back as null... |
| 420 |
assertEquals(null, deserializedMemento.getTextData()); |
| 421 |
} else { |
| 422 |
assertEquals(data, deserializedMemento.getTextData()); |
| 423 |
} |
| 424 |
} |
| 425 |
}); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
public void testLegalKeys() throws WorkbenchException, IOException { |
| 430 |
String[] legalKeys = { "value", "value.with.many.dots", |
| 431 |
"value_with_underscores" }; |
| 432 |
|
| 433 |
for (int i = 0; i < legalKeys.length; i++) { |
| 434 |
final String key = legalKeys[i]; |
| 435 |
testPutAndGet(new MementoChecker() { |
| 436 |
|
| 437 |
public void prepareAndCheckBeforeSerialization( |
| 438 |
XMLMemento mementoToSerialize) { |
| 439 |
assertEquals(null, mementoToSerialize.getString(key)); |
| 440 |
try { |
| 441 |
mementoToSerialize.putString(key, "some string"); |
| 442 |
} catch (RuntimeException ex) { |
| 443 |
System.out.println("offending key: '" + key + "'"); |
| 444 |
throw ex; |
| 445 |
} |
| 446 |
assertEquals("some string", mementoToSerialize |
| 447 |
.getString(key)); |
| 448 |
} |
| 449 |
|
| 450 |
public void checkAfterDeserialization( |
| 451 |
XMLMemento deserializedMemento) { |
| 452 |
assertEquals("some string", deserializedMemento |
| 453 |
.getString(key)); |
| 454 |
} |
| 455 |
}); |
| 456 |
} |
| 457 |
|
| 458 |
} |
| 459 |
|
| 460 |
public void testIllegalKeys() { |
| 461 |
String[] illegalKeys = { "", " ", " key", "key ", "key key", "\t", |
| 462 |
"\tkey", "key\t", "key\tkey", "\n", "\nkey", "key\n", |
| 463 |
"key\nkey", "key<with<lessthan", "key>with>greaterthan", |
| 464 |
"key&with&ersand", "key#with#hash", "key\"with\"quote", "\"" }; |
| 465 |
|
| 466 |
for (int i = 0; i < illegalKeys.length; i++) { |
| 467 |
final String key = illegalKeys[i]; |
| 468 |
XMLMemento memento = XMLMemento.createWriteRoot("foo"); |
| 469 |
try { |
| 470 |
memento.putString(key, "some string"); |
| 471 |
fail("putString with illegal key should fail"); |
| 472 |
} catch (Exception ex) { |
| 473 |
// expected |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
public void testPutTextDataWithChildren() throws WorkbenchException, |
| 479 |
IOException { |
| 480 |
final String textData = "\n\tThis is\ntext data\n\t\twith newlines and \ttabs\t\n\t "; |
| 481 |
testPutAndGet(new MementoChecker() { |
| 482 |
|
| 483 |
public void prepareAndCheckBeforeSerialization( |
| 484 |
XMLMemento mementoToSerialize) { |
| 485 |
mementoToSerialize.createChild("type", "id"); |
| 486 |
mementoToSerialize.putTextData(textData); |
| 487 |
mementoToSerialize.createChild("type", "id"); |
| 488 |
mementoToSerialize.createChild("type", "id"); |
| 489 |
assertEquals(textData, mementoToSerialize.getTextData()); |
| 490 |
} |
| 491 |
|
| 492 |
public void checkAfterDeserialization(XMLMemento deserializedMemento) { |
| 493 |
assertEquals(textData, deserializedMemento.getTextData()); |
| 494 |
} |
| 495 |
}); |
| 496 |
} |
| 497 |
|
| 498 |
private static interface MementoChecker { |
| 499 |
void prepareAndCheckBeforeSerialization(XMLMemento mementoToSerialize); |
| 500 |
|
| 501 |
void checkAfterDeserialization(XMLMemento deserializedMemento); |
| 502 |
} |
| 503 |
|
| 504 |
private void testPutAndGet(MementoChecker mementoChecker) |
| 505 |
throws IOException, WorkbenchException { |
| 506 |
XMLMemento mementoToSerialize = XMLMemento |
| 507 |
.createWriteRoot("XMLMementoTest"); |
| 508 |
|
| 509 |
mementoChecker.prepareAndCheckBeforeSerialization(mementoToSerialize); |
| 510 |
|
| 511 |
StringWriter writer = new StringWriter(); |
| 512 |
mementoToSerialize.save(writer); |
| 513 |
writer.close(); |
| 514 |
|
| 515 |
StringReader reader = new StringReader(writer.getBuffer().toString()); |
| 516 |
XMLMemento deserializedMemento = XMLMemento.createReadRoot(reader); |
| 517 |
|
| 518 |
mementoChecker.checkAfterDeserialization(deserializedMemento); |
| 519 |
} |
| 520 |
|
| 521 |
} |