Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: edfdaa7308fd9826e893d31d6dcff98de4766056 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*******************************************************************************
 * Copyright (c) 2004, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.tests.services.datalocation;

import java.io.*;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.core.runtime.Platform;
import org.eclipse.osgi.storagemanager.ManagedOutputStream;
import org.eclipse.osgi.storagemanager.StorageManager;
import org.eclipse.osgi.tests.OSGiTest;

public class StreamManagerTests extends OSGiTest {
	StorageManager manager1;
	StorageManager manager2;
	File base;
	String reliableFile;

	/**
	 * Constructs a test case with the given name.
	 */
	public StreamManagerTests(String name) {
		super(name);
	}

	public static Test suite() {
		return new TestSuite(StreamManagerTests.class);
	}

	protected void setUp() throws Exception {
		super.setUp();
		base = new File(Platform.getConfigurationLocation().getURL().getPath(), "StreamManagerTests");
		manager1 = null;
		manager2 = null;
		reliableFile = System.getProperty("osgi.useReliableFiles");
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		if (manager1 != null)
			manager1.close();
		if (manager2 != null)
			manager2.close();
		rm(base);
		if (reliableFile == null)
			System.getProperties().remove("osgi.useReliableFiles");
		else
			System.setProperty("osgi.useReliableFiles", reliableFile);
	}

	private void rm(File file) {
		if (file.isDirectory()) {
			File[] list = file.listFiles();
			if (list != null) {
				for (int idx = 0; idx < list.length; idx++) {
					rm(list[idx]);
				}
			}
		}
		file.delete();
	}

	private String getInputStreamContents(InputStream is) throws IOException {
		StringBuffer sb = new StringBuffer();
		byte[] data = new byte[64];
		int len;
		try {
			while ((len = is.read(data)) != -1) {
				sb.append(new String(data, 0, len));
			}
		} finally {
			is.close();
		}
		return sb.toString();
	}

	void writeToFile(File file, String str) throws IOException {
		FileOutputStream fos = new FileOutputStream(file);
		try {
			fos.write(str.getBytes());
			fos.flush();
			fos.getFD().sync();
		} finally {
			fos.close();
		}
	}

	/**
	 * This tests that FM will keep a backup version of a reliableFile and that
	 * corrupting the reliableFile will recover the previous contents.
	 * 
	 */
	public void testReliableFile() {
		String fileName = "testReliableFile.txt";
		File file1 = new File(base, fileName + ".1");
		File file2 = new File(base, fileName + ".2");
		File file3 = new File(base, fileName + ".3");
		String contents1 = "test reliable file cOntents #1";
		String contents2 = "test reliable file cOntents #2";
		try {
			System.setProperty("osgi.useReliableFiles", "true"); // force reliable files
			manager1 = new StorageManager(base, null);
			manager1.open(true);
			ManagedOutputStream fmos = manager1.getOutputStream(fileName);
			assertNotNull(fmos);
			fmos.write(contents1.getBytes());
			fmos.close();

			// Write verion2 of the file
			fmos = manager1.getOutputStream(fileName);
			assertNotNull(fmos);
			fmos.write(contents2.getBytes());
			fmos.close();
			assertTrue(file1.exists());
			assertTrue(file2.exists());
			assertTrue(!file3.exists());
			manager1.close();
			manager1 = null;

			//now, open new manager, verify file contents are #2
			System.setProperty("osgi.useReliableFiles", "true"); // force reliable files
			manager2 = new StorageManager(base, null);
			manager2.open(true);
			InputStream is = manager2.getInputStream(fileName);
			assertNotNull(is);
			assertEquals(contents2, getInputStreamContents(is));
			manager2.close();
			manager2 = null;

			// need to sleep, FAT32 doesn't have too fine granularity in timestamps
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {/*ignore*/
			}
			//now, corrupt version 2 of the file
			RandomAccessFile raf = new RandomAccessFile(file2, "rw");
			raf.seek(20);
			raf.write('0'); // change 'O' to '0'
			raf.close();

			System.setProperty("osgi.useReliableFiles", "true"); // force reliable files
			manager1 = new StorageManager(base, null);
			manager1.open(true);

			//request any valid stream available
			is = manager1.getInputStream(fileName);
			assertNotNull(is);
			assertEquals(contents1, getInputStreamContents(is));

			//now request only the primary file
			try {
				InputStream[] isSet = manager1.getInputStreamSet(new String[] {fileName});
				for (int i = 0; i < isSet.length; i++) {
					if (isSet[i] != null)
						isSet[i].close();
				}
				fail("getInputStreamSet was successful");
			} catch (IOException e) {
				//good
			}

			//now, corrupt version 1 of the file
			raf = new RandomAccessFile(file1, "rw");
			raf.seek(20);
			raf.write('0'); // change 'O' to '0'
			raf.close();

			// get input stream should fail
			try {
				is = manager1.getInputStream(fileName);
				fail("get input stream succedded");
			} catch (IOException e) {
				//good
			}
			manager1.close();
			manager1 = null;
		} catch (IOException e) {
			fail("unexepected exception", e);
		} finally {
			System.setProperty("osgi.useReliableFiles", "false"); // force reliable files off
		}
	}

	public void testBigReliableFile() {
		String fileName = getName() + ".txt";
		try {
			System.setProperty("osgi.useReliableFiles", "true"); // force reliable files
			manager1 = new StorageManager(base, null);
			manager1.open(true);
			ManagedOutputStream fmos = manager1.getOutputStream(fileName);
			assertNotNull(fmos);
			DataOutputStream bufferedOut = new DataOutputStream(new BufferedOutputStream(fmos));
			// 200 K of integers (200 * 1024 / 4)
			for (int i = 0; i < (200 * 1024 / 4); i++)
				bufferedOut.writeInt(i);
			bufferedOut.close();
			manager1.close();
			manager1 = null;

			//now, open new manager, verify file contents are there
			System.setProperty("osgi.useReliableFiles", "true"); // force reliable files
			manager2 = new StorageManager(base, null);
			manager2.open(true);
			InputStream is = manager2.getInputStream(fileName);
			assertNotNull(is);
			DataInputStream bufferedIn = new DataInputStream(new BufferedInputStream(is));
			for (int i = 0; i < (200 * 1024 / 4); i++)
				assertEquals("Wrong content found", i, bufferedIn.readInt());
			manager2.close();
			manager2 = null;

		} catch (IOException e) {
			fail("unexepected exception", e);
		}
	}

	/**
	 * This tests if migration from a prior (non-ReliableFile) .fileTable
	 * to the current .fileTable is correct.
	 *
	 */
	public void testMigration() {
		File testDir = new File(base, "testMigrationManager");
		File managerDir = new File(testDir, ".manager");
		String fileName = "testMigration.txt";
		File file2 = new File(testDir, fileName + ".2");
		File file5 = new File(testDir, fileName + ".5");
		File fileTable = new File(managerDir, ".fileTable");
		File fileTable1 = new File(managerDir, ".fileTable.1");
		File fileTable2 = new File(managerDir, ".fileTable.2");
		File fileTable3 = new File(managerDir, ".fileTable.3");
		String contents1 = "test reliable file contents #1";
		String contents2 = "test reliable file contents #2";
		String contents3 = "test reliable file contents #3";
		try {
			// create a .fileTable and a normal file
			managerDir.mkdirs();
			writeToFile(fileTable, "#safe table\n" + fileName + "=2\n");
			writeToFile(file2, contents1);
			manager1 = new StorageManager(testDir, null);
			manager1.open(true);
			File test = manager1.lookup(fileName, false);
			assertNotNull(test);
			assertTrue(test.exists());

			// update a new file
			File testFile = manager1.createTempFile(fileName);
			writeToFile(testFile, contents2);
			manager1.update(new String[] {fileName}, new String[] {testFile.getName()});
			// again
			testFile = manager1.createTempFile(fileName);
			writeToFile(testFile, contents3);
			manager1.update(new String[] {fileName}, new String[] {testFile.getName()});
			// again
			testFile = manager1.createTempFile(fileName);
			writeToFile(testFile, contents1);
			manager1.update(new String[] {fileName}, new String[] {testFile.getName()});
			manager1.close();
			manager1 = null;

			String[] files = managerDir.list();
			assertEquals(4, files.length);
			//original file never gets deleted
			assertTrue(fileTable.exists());
			//behaves like a reliableFile?
			assertFalse(fileTable1.exists());
			assertTrue(fileTable2.exists());
			assertTrue(fileTable3.exists());
			//files are as expected?
			files = testDir.list();
			assertEquals(2, files.length);
			assertTrue(file5.exists());

			manager2 = new StorageManager(testDir, null);
			manager2.open(true);
			testFile = manager2.lookup(fileName, false);
			assertNotNull(testFile);
			assertTrue(testFile.exists());
			assertTrue(testFile.getName().endsWith(".5"));
			manager2.close();
			manager2 = null;

		} catch (IOException e) {
			fail("unexepected exception", e);
		}
	}

	/**
	 * This tests that an output stream abort behave as expected.
	 *
	 */
	public void testAbort() {
		testAbort(true);
		testAbort(false);
	}

	private void testAbort(boolean reliable) {
		String fileName;
		if (reliable)
			fileName = "abortFileReliable.txt";
		else
			fileName = "abortFileStd.txt";
		File file1 = new File(base, fileName + ".1");
		File file2 = new File(base, fileName + ".2");
		File file3 = new File(base, fileName + ".3");
		String contents1 = "test reliable file contents #1";
		String contents2 = "test reliable file contents #2";
		try {
			System.setProperty("osgi.useReliableFiles", "true"); // force reliable files
			manager1 = new StorageManager(base, null);
			manager1.open(true);
			//create version 1
			ManagedOutputStream smos = manager1.getOutputStream(fileName);
			smos.write(contents1.getBytes());
			smos.close();

			//start creating version 2
			smos = manager1.getOutputStream(fileName);
			smos.write(contents2.getBytes());
			smos.abort();
			smos.close(); // shouldn't cause exception, check!

			// now see if we're still on version #1
			assertEquals(1, manager1.getId(fileName));
			// check contents also
			InputStream is = manager1.getInputStream(fileName);
			assertNotNull(is);
			assertEquals(contents1, getInputStreamContents(is));
			manager1.close();
			manager1 = null;

			// open a new manager & check the same thing to ensure the database is correct
			System.setProperty("osgi.useReliableFiles", "true"); // force reliable files
			manager2 = new StorageManager(base, null);
			manager2.open(true);
			//create version 1
			// now see if we're still on version #1
			assertEquals(1, manager2.getId(fileName));
			// check contents also
			is = manager2.getInputStream(fileName);
			assertNotNull(is);
			assertEquals(contents1, getInputStreamContents(is));
			manager2.close();
			manager2 = null;
			assertTrue(file1.exists());
			assertFalse(file2.exists());
			assertFalse(file3.exists());
		} catch (IOException e) {
			fail("unexepected exception", e);
		}
	}

	/**
	 * This tests if getting an output stream-set work properly.
	 *
	 */
	public void testGetOutputStreamSet() {
		testGetOutputStreamSet(true);
		testGetOutputStreamSet(false);
	}

	private void testGetOutputStreamSet(boolean reliable) {
		File mgrDir;
		if (reliable)
			mgrDir = new File(base, "getSetReliable");
		else
			mgrDir = new File(base, "getSetStd");
		String fileName1 = "testSet1.txt";
		String fileName2 = "testSet2.txt";
		File file1_1 = new File(mgrDir, fileName1 + ".1");
		File file1_2 = new File(mgrDir, fileName1 + ".2");
		File file2_1 = new File(mgrDir, fileName2 + ".1");
		File file2_2 = new File(mgrDir, fileName2 + ".2");
		String contents1 = "test reliable file contents #1";
		String contents2 = "test reliable file contents #2";
		try {
			System.setProperty("osgi.useReliableFiles", reliable ? "true" : "false"); // force reliable files
			manager1 = new StorageManager(mgrDir, null);
			manager1.open(true);
			ManagedOutputStream[] outs = manager1.getOutputStreamSet(new String[] {fileName1, fileName2});
			assertNotNull(outs);
			assertEquals(2, outs.length);

			outs[0].write(contents1.getBytes());
			outs[1].write(contents2.getBytes());
			outs[1].close();
			assertFalse(file1_1.exists());
			assertFalse(file2_1.exists());
			outs[0].close();
			assertTrue(file1_1.exists());
			assertTrue(file2_1.exists());

			outs = manager1.getOutputStreamSet(new String[] {fileName1, fileName2});
			assertNotNull(outs);

			outs[0].write("new data #1".getBytes());
			outs[1].write("new data #2".getBytes());
			outs[0].close();
			assertFalse(file1_2.exists());
			assertFalse(file2_2.exists());
			outs[1].close();
			assertTrue(file1_2.exists());
			assertTrue(file2_2.exists());
			manager1.close();
			manager1 = null;

			if (reliable) {
				// verify FM thinks they are reliable
				assertTrue(file1_1.exists());
				assertTrue(file2_1.exists());
			} else {
				// verify FM thinks they are not reliable
				assertFalse(file1_1.exists());
				assertFalse(file2_1.exists());
			}

		} catch (IOException e) {
			fail("unexepected exception", e);
		}
	}

	/**
	 * This tests if aborting a managed stream-set works as expected
	 *
	 */
	public void testAbortStreamSet() {
		testAbortSet(true);
		testAbortSet(false);
	}

	private void testAbortSet(boolean reliable) {
		File mgrDir;
		if (reliable)
			mgrDir = new File(base, "abortSetReliable");
		else
			mgrDir = new File(base, "abortSetStd");
		String fileName1 = "test1.txt";
		String fileName2 = "test2.txt";
		String fileName3 = "test3.txt";
		String fileName4 = "test4.txt";
		String contents1 = "test reliable file contents #1";
		String contents2 = "test reliable file contents #2";
		try {
			mgrDir.mkdirs();
			String[] list = mgrDir.list();
			assertEquals(0, list.length);
			System.setProperty("osgi.useReliableFiles", reliable ? "true" : "false"); // force reliable files
			manager1 = new StorageManager(mgrDir, null);
			manager1.open(true);
			ManagedOutputStream[] outs = manager1.getOutputStreamSet(new String[] {fileName1, fileName2, fileName3, fileName4});
			assertNotNull(outs);

			outs[0].write(contents1.getBytes());
			outs[1].write(contents2.getBytes());
			outs[2].write(contents2.getBytes());
			outs[3].write(contents1.getBytes());
			//sanity check
			list = mgrDir.list();
			assertEquals(5, list.length);
			outs[2].close();
			outs[1].abort();
			outs[0].close(); //noop
			outs[3].close(); //noop
			outs[2].close(); //noop
			outs[1].close(); //noop
			list = mgrDir.list();
			assertEquals(1, list.length);
			assertNull(manager1.lookup(fileName1, false));
			assertNull(manager1.lookup(fileName2, false));
			assertNull(manager1.lookup(fileName3, false));
			assertNull(manager1.lookup(fileName4, false));
			manager1.close();
			manager1 = null;

			// open a new manager & check the same thing to ensure the database is correct
			System.setProperty("osgi.useReliableFiles", reliable ? "true" : "false"); // force reliable files
			manager2 = new StorageManager(mgrDir, null);
			manager2.open(true);
			//create version 1
			assertNull(manager2.lookup(fileName1, false));
			assertNull(manager2.lookup(fileName2, false));
			assertNull(manager2.lookup(fileName3, false));
			assertNull(manager2.lookup(fileName4, false));
			list = mgrDir.list();
			assertEquals(1, list.length);
			manager2.close();
			manager2 = null;

		} catch (IOException e) {
			fail("unexepected exception", e);
		}
	}

}

Back to the top