Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 93262 | Differences between
and this patch

Collapse All | Expand All

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

Return to bug 93262