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 259981 | Differences between
and this patch

Collapse All | Expand All

(-)supplement/src/org/eclipse/osgi/storagemanager/StorageManager.java (-10 / +22 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2010 IBM Corporation and others.
2
 * Copyright (c) 2004, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 87-100 Link Here
87
	private static final int FILETYPE_STANDARD = 0;
87
	private static final int FILETYPE_STANDARD = 0;
88
	private static final int FILETYPE_RELIABLEFILE = 1;
88
	private static final int FILETYPE_RELIABLEFILE = 1;
89
	private static final SecureAction secure = AccessController.doPrivileged(SecureAction.createSecureAction());
89
	private static final SecureAction secure = AccessController.doPrivileged(SecureAction.createSecureAction());
90
	private static final boolean tempCleanup = Boolean.valueOf(secure.getProperty("osgi.embedded.cleanTempFiles")).booleanValue(); //$NON-NLS-1$
91
	private static final boolean openCleanup = Boolean.valueOf(secure.getProperty("osgi.embedded.cleanupOnOpen")).booleanValue(); //$NON-NLS-1$
92
	private static final String MANAGER_FOLDER = ".manager"; //$NON-NLS-1$
90
	private static final String MANAGER_FOLDER = ".manager"; //$NON-NLS-1$
93
	private static final String TABLE_FILE = ".fileTable"; //$NON-NLS-1$
91
	private static final String TABLE_FILE = ".fileTable"; //$NON-NLS-1$
94
	private static final String LOCK_FILE = ".fileTableLock"; //$NON-NLS-1$
92
	private static final String LOCK_FILE = ".fileTableLock"; //$NON-NLS-1$
95
	private static final int MAX_LOCK_WAIT = 5000; // 5 seconds 
93
	private static final int MAX_LOCK_WAIT = 5000; // 5 seconds 
96
	// this should be static but the tests expect to be able to create new managers after changing this setting dynamically
94
	// these should be static but the tests expect to be able to create new managers after changing this setting dynamically
97
	private final boolean useReliableFiles = Boolean.valueOf(secure.getProperty("osgi.useReliableFiles")).booleanValue(); //$NON-NLS-1$
95
	private final boolean useReliableFiles = Boolean.valueOf(secure.getProperty("osgi.useReliableFiles")).booleanValue(); //$NON-NLS-1$
96
	private final boolean tempCleanup = Boolean.valueOf(secure.getProperty("osgi.embedded.cleanTempFiles")).booleanValue(); //$NON-NLS-1$
97
	private final boolean openCleanup = Boolean.valueOf(secure.getProperty("osgi.embedded.cleanupOnOpen")).booleanValue(); //$NON-NLS-1$
98
98
99
	private class Entry {
99
	private class Entry {
100
		int readId;
100
		int readId;
Lines 554-559 Link Here
554
			if (error)
554
			if (error)
555
				fileStream.abort();
555
				fileStream.abort();
556
		}
556
		}
557
		// bug 259981 we should clean up
558
		if (openCleanup) {
559
			try {
560
				cleanup(false);
561
			} catch (IOException ex) {
562
				// If IOException is thrown from our custom method.
563
				// log and swallow for now.
564
				System.out.println("Unexpected IOException is thrown inside cleanupWithLock. Please look below for stacktrace");
565
				ex.printStackTrace(System.out);
566
			}
567
		}
557
		tableStamp = ReliableFile.lastModifiedVersion(tableFile);
568
		tableStamp = ReliableFile.lastModifiedVersion(tableFile);
558
	}
569
	}
559
570
Lines 584-594 Link Here
584
	 * This removal is only done if the instance of eclipse calling this method is the last instance using this storage manager.
595
	 * This removal is only done if the instance of eclipse calling this method is the last instance using this storage manager.
585
	 * @throws IOException
596
	 * @throws IOException
586
	 */
597
	 */
587
	private void cleanup() throws IOException {
598
	private void cleanup(boolean doLock) throws IOException {
588
		if (readOnly)
599
		if (readOnly)
589
			return;
600
			return;
590
		//Lock first, so someone else can not start while we're in the middle of cleanup
601
		//Lock first, so someone else can not start while we're in the middle of cleanup
591
		if (!lock(true))
602
		if (doLock && !lock(true))
592
			throw new IOException(EclipseAdaptorMsg.fileManager_cannotLock);
603
			throw new IOException(EclipseAdaptorMsg.fileManager_cannotLock);
593
		try {
604
		try {
594
			//Iterate through the temp files and delete them all, except the one representing this storage manager.
605
			//Iterate through the temp files and delete them all, except the one representing this storage manager.
Lines 636-642 Link Here
636
				}
647
				}
637
			}
648
			}
638
		} finally {
649
		} finally {
639
			release();
650
			if (doLock)
651
				release();
640
		}
652
		}
641
	}
653
	}
642
654
Lines 662-668 Link Here
662
		if (readOnly)
674
		if (readOnly)
663
			return;
675
			return;
664
		try {
676
		try {
665
			cleanup();
677
			cleanup(true);
666
		} catch (IOException e) {
678
		} catch (IOException e) {
667
			//Ignore and close.
679
			//Ignore and close.
668
		}
680
		}
Lines 680-691 Link Here
680
	 * @throws IOException if an error occurred opening the storage manager
692
	 * @throws IOException if an error occurred opening the storage manager
681
	 */
693
	 */
682
	public void open(boolean wait) throws IOException {
694
	public void open(boolean wait) throws IOException {
683
		if (openCleanup)
684
			cleanup();
685
		if (!readOnly) {
695
		if (!readOnly) {
686
			managerRoot.mkdirs();
696
			managerRoot.mkdirs();
687
			if (!managerRoot.exists())
697
			if (!managerRoot.exists())
688
				throw new IOException(EclipseAdaptorMsg.fileManager_cannotLock);
698
				throw new IOException(EclipseAdaptorMsg.fileManager_cannotLock);
699
			if (openCleanup)
700
				cleanup(true);
689
			boolean locked = lock(wait);
701
			boolean locked = lock(wait);
690
			if (!locked && wait)
702
			if (!locked && wait)
691
				throw new IOException(EclipseAdaptorMsg.fileManager_cannotLock);
703
				throw new IOException(EclipseAdaptorMsg.fileManager_cannotLock);
(-)src/org/eclipse/osgi/tests/services/datalocation/FileManagerTests.java (-68 / +113 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2006 IBM Corporation and others.
2
 * Copyright (c) 2004, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 11-17 Link Here
11
package org.eclipse.osgi.tests.services.datalocation;
11
package org.eclipse.osgi.tests.services.datalocation;
12
12
13
import java.io.*;
13
import java.io.*;
14
15
import junit.framework.Test;
14
import junit.framework.Test;
16
import junit.framework.TestSuite;
15
import junit.framework.TestSuite;
17
import org.eclipse.core.runtime.Platform;
16
import org.eclipse.core.runtime.Platform;
Lines 54-68 Link Here
54
		if (file.isDirectory()) {
53
		if (file.isDirectory()) {
55
			File[] list = file.listFiles();
54
			File[] list = file.listFiles();
56
			if (list != null) {
55
			if (list != null) {
57
				for (int idx=0; idx<list.length; idx++) {
56
				for (int idx = 0; idx < list.length; idx++) {
58
					rm(list[idx]);
57
					rm(list[idx]);
59
				}
58
				}
60
			}
59
			}
61
		}
60
		}
62
		file.delete();
61
		file.delete();
63
	}
62
	}
64
	
63
65
	
66
	/**
64
	/**
67
	 * Open a filemanager in readonly and ensure files are
65
	 * Open a filemanager in readonly and ensure files are
68
	 * not created.
66
	 * not created.
Lines 76-82 Link Here
76
		manager1 = new StorageManager(testDir, null, true);
74
		manager1 = new StorageManager(testDir, null, true);
77
		try {
75
		try {
78
			manager1.open(true);
76
			manager1.open(true);
79
		} catch(IOException e) {
77
		} catch (IOException e) {
80
			fail("unexpected exception", e);
78
			fail("unexpected exception", e);
81
		}
79
		}
82
		files = testDir.list();
80
		files = testDir.list();
Lines 84-104 Link Here
84
		try {
82
		try {
85
			manager1.add(fileName);
83
			manager1.add(fileName);
86
			fail("add succedded");
84
			fail("add succedded");
87
		} catch(IOException e) {
85
		} catch (IOException e) {
88
			//good
86
			//good
89
		}
87
		}
90
		
88
91
		try {
89
		try {
92
			manager1.lookup(fileName, true);
90
			manager1.lookup(fileName, true);
93
			fail("lookup succedded");
91
			fail("lookup succedded");
94
		} catch(IOException e) {
92
		} catch (IOException e) {
95
			//good
93
			//good
96
		}
94
		}
97
		
95
98
		try {
96
		try {
99
			manager1.createTempFile(fileName);
97
			manager1.createTempFile(fileName);
100
			fail("create temp file succedded");
98
			fail("create temp file succedded");
101
		} catch(IOException e) {
99
		} catch (IOException e) {
102
			//good
100
			//good
103
		}
101
		}
104
		files = testDir.list();
102
		files = testDir.list();
Lines 116-127 Link Here
116
			manager2.update(new String[] {fileName}, new String[] {tmpFile.getName()});
114
			manager2.update(new String[] {fileName}, new String[] {tmpFile.getName()});
117
			manager2.close();
115
			manager2.close();
118
			manager2 = null;
116
			manager2 = null;
119
		} catch(IOException e) {
117
		} catch (IOException e) {
120
			fail("unexpected exception", e);
118
			fail("unexpected exception", e);
121
		}		
119
		}
122
	}
120
	}
123
	
121
124
	
125
	void writeToFile(File file, String str) throws IOException {
122
	void writeToFile(File file, String str) throws IOException {
126
		FileOutputStream fos = new FileOutputStream(file);
123
		FileOutputStream fos = new FileOutputStream(file);
127
		try {
124
		try {
Lines 133-139 Link Here
133
		}
130
		}
134
	}
131
	}
135
132
136
	
137
	private String getInputStreamContents(InputStream is) throws IOException {
133
	private String getInputStreamContents(InputStream is) throws IOException {
138
		StringBuffer sb = new StringBuffer();
134
		StringBuffer sb = new StringBuffer();
139
		byte[] data = new byte[64];
135
		byte[] data = new byte[64];
Lines 141-154 Link Here
141
		try {
137
		try {
142
			while ((len = is.read(data)) != -1) {
138
			while ((len = is.read(data)) != -1) {
143
				sb.append(new String(data, 0, len));
139
				sb.append(new String(data, 0, len));
144
			} 
140
			}
145
		} finally {
141
		} finally {
146
			is.close();
142
			is.close();
147
		}
143
		}
148
		return sb.toString();
144
		return sb.toString();
149
	}
145
	}
150
	
146
151
	
152
	/**
147
	/**
153
	 * This tests a FM update where the a specific file is managed with a 
148
	 * This tests a FM update where the a specific file is managed with a 
154
	 * revision number of (n) and an update is requested while there already
149
	 * revision number of (n) and an update is requested while there already
Lines 161-170 Link Here
161
		manager1 = new StorageManager(base, null);
156
		manager1 = new StorageManager(base, null);
162
		try {
157
		try {
163
			manager1.open(false);
158
			manager1.open(false);
164
			File file1 = new File(base, testFile+".1");
159
			File file1 = new File(base, testFile + ".1");
165
			File file2 = new File(base, testFile+".2");
160
			File file2 = new File(base, testFile + ".2");
166
			File file3 = new File(base, testFile+".3");
161
			File file3 = new File(base, testFile + ".3");
167
			File file4 = new File(base, testFile+".4");
162
			File file4 = new File(base, testFile + ".4");
168
			if (file1.exists() || file2.exists() || file3.exists() || file4.exists()) {
163
			if (file1.exists() || file2.exists() || file3.exists() || file4.exists()) {
169
				fail("Test files already exists.");
164
				fail("Test files already exists.");
170
				return;
165
				return;
Lines 178-184 Link Here
178
				fail("Failed to create a single test file");
173
				fail("Failed to create a single test file");
179
				return;
174
				return;
180
			}
175
			}
181
			
176
182
			// create file version (2) outside filemanager
177
			// create file version (2) outside filemanager
183
			writeToFile(file2, "file2 exists");
178
			writeToFile(file2, "file2 exists");
184
179
Lines 190-196 Link Here
190
				fail("Failed to skip existing filemanager file.");
185
				fail("Failed to skip existing filemanager file.");
191
				return;
186
				return;
192
			}
187
			}
193
			
188
194
			// open a new manager, ensure a lookup results in file version (3)
189
			// open a new manager, ensure a lookup results in file version (3)
195
			manager2 = new StorageManager(base, null);
190
			manager2 = new StorageManager(base, null);
196
			manager2.open(true);
191
			manager2.open(true);
Lines 203-220 Link Here
203
			assertTrue(file.exists());
198
			assertTrue(file.exists());
204
			FileInputStream fis = new FileInputStream(file);
199
			FileInputStream fis = new FileInputStream(file);
205
			assertEquals(getInputStreamContents(fis), "file 3 contents");
200
			assertEquals(getInputStreamContents(fis), "file 3 contents");
206
			
201
207
			manager2.close();
202
			manager2.close();
208
			manager2=null;
203
			manager2 = null;
209
			
204
210
			manager1.close();
205
			manager1.close();
211
			manager1=null;
206
			manager1 = null;
212
		} catch(IOException e) {
207
		} catch (IOException e) {
213
			fail("unexpected exception", e);
208
			fail("unexpected exception", e);
214
		}
209
		}
215
	}
210
	}
216
211
217
	
218
	/**
212
	/**
219
	 * This tests that FM apis throw exceptions if FM has not yet been opened
213
	 * This tests that FM apis throw exceptions if FM has not yet been opened
220
	 * or if FM has been closed.  
214
	 * or if FM has been closed.  
Lines 231-247 Link Here
231
			this.writeToFile(tmpFile, "File exists");
225
			this.writeToFile(tmpFile, "File exists");
232
			manager1.update(new String[] {permanentFile}, new String[] {tmpFile.getName()});
226
			manager1.update(new String[] {permanentFile}, new String[] {tmpFile.getName()});
233
			manager1.add(scratchFile);
227
			manager1.add(scratchFile);
234
		} catch(IOException e) {
228
		} catch (IOException e) {
235
			fail("unexpected exception", e);
229
			fail("unexpected exception", e);
236
		}
230
		}
237
		
231
238
		// create a new manager, and try making calls
232
		// create a new manager, and try making calls
239
		manager2 = new StorageManager(base, null);
233
		manager2 = new StorageManager(base, null);
240
		checkOpen(false, permanentFile, scratchFile);
234
		checkOpen(false, permanentFile, scratchFile);
241
		// open the manager, try again
235
		// open the manager, try again
242
		try {
236
		try {
243
			manager2.open(true);
237
			manager2.open(true);
244
		} catch(IOException e) {
238
		} catch (IOException e) {
245
			fail("unexpected exception", e);
239
			fail("unexpected exception", e);
246
		}
240
		}
247
		checkOpen(true, permanentFile, scratchFile);
241
		checkOpen(true, permanentFile, scratchFile);
Lines 249-255 Link Here
249
		manager2.close();
243
		manager2.close();
250
		checkOpen(false, permanentFile, scratchFile);
244
		checkOpen(false, permanentFile, scratchFile);
251
		manager2 = null;
245
		manager2 = null;
252
		
246
253
		manager1.close();
247
		manager1.close();
254
		manager1 = null;
248
		manager1 = null;
255
	}
249
	}
Lines 261-267 Link Here
261
			if (!open)
255
			if (!open)
262
				fail("add did not fail.");
256
				fail("add did not fail.");
263
			manager2.remove("failFile");
257
			manager2.remove("failFile");
264
		} catch(IOException e) {
258
		} catch (IOException e) {
265
			if (open)
259
			if (open)
266
				fail("unexpected exception", e);
260
				fail("unexpected exception", e);
267
		}
261
		}
Lines 271-277 Link Here
271
			manager2.lookup(permanentFile, false);
265
			manager2.lookup(permanentFile, false);
272
			if (!open)
266
			if (!open)
273
				fail("lookup did not fail.");
267
				fail("lookup did not fail.");
274
		} catch(IOException e) {
268
		} catch (IOException e) {
275
			if (open)
269
			if (open)
276
				fail("unexpected exception", e);
270
				fail("unexpected exception", e);
277
		}
271
		}
Lines 283-295 Link Here
283
		} catch (IOException e) {
277
		} catch (IOException e) {
284
			fail("unexpected exception", e);
278
			fail("unexpected exception", e);
285
			tmpFile = null;
279
			tmpFile = null;
286
		}	
280
		}
287
		if (tmpFile != null) {
281
		if (tmpFile != null) {
288
			try {
282
			try {
289
				manager2.update(new String[] {permanentFile}, new String[] {tmpFile.getName()});
283
				manager2.update(new String[] {permanentFile}, new String[] {tmpFile.getName()});
290
				if (!open)
284
				if (!open)
291
					fail("update did not fail.");
285
					fail("update did not fail.");
292
			} catch(IOException e) {
286
			} catch (IOException e) {
293
				if (open)
287
				if (open)
294
					fail("unexpected exception", e);
288
					fail("unexpected exception", e);
295
			}
289
			}
Lines 299-313 Link Here
299
			manager2.remove(scratchFile);
293
			manager2.remove(scratchFile);
300
			if (!open)
294
			if (!open)
301
				fail("remove did not fail");
295
				fail("remove did not fail");
302
			else // add the file back if expected to complete
296
			else
297
				// add the file back if expected to complete
303
				manager2.add(scratchFile);
298
				manager2.add(scratchFile);
304
			} catch(IOException e) {
299
		} catch (IOException e) {
305
			if (open)
300
			if (open)
306
				fail("unexpected exception", e);
301
				fail("unexpected exception", e);
307
		}
302
		}
308
	}
303
	}
309
	
304
310
	
311
	/**
305
	/**
312
	 * This tests FM remove() then add().  On a remove(), the file can not be deleted as it may
306
	 * This tests FM remove() then add().  On a remove(), the file can not be deleted as it may
313
	 * be in use by another FM. Then add() the file back and see if the version number written to
307
	 * be in use by another FM. Then add() the file back and see if the version number written to
Lines 319-327 Link Here
319
	 */
313
	 */
320
	public void testRemoveThenAdd() {
314
	public void testRemoveThenAdd() {
321
		String fileName = "testRemoveThenAdd.txt";
315
		String fileName = "testRemoveThenAdd.txt";
322
		File file1 = new File(base, fileName+".1");
316
		File file1 = new File(base, fileName + ".1");
323
		File file2 = new File(base, fileName+".2");
317
		File file2 = new File(base, fileName + ".2");
324
		File file3 = new File(base, fileName+".3");
318
		File file3 = new File(base, fileName + ".3");
325
		manager1 = new StorageManager(base, null);
319
		manager1 = new StorageManager(base, null);
326
		// create a permanent file
320
		// create a permanent file
327
		try {
321
		try {
Lines 340-346 Link Here
340
			// sanity check
334
			// sanity check
341
			if (file1.exists() || !file2.exists() || file3.exists())
335
			if (file1.exists() || !file2.exists() || file3.exists())
342
				fail("Failed creating a file revision");
336
				fail("Failed creating a file revision");
343
			
337
344
			manager2 = new StorageManager(base, null);
338
			manager2 = new StorageManager(base, null);
345
			manager2.open(true);
339
			manager2.open(true);
346
			manager2.remove(fileName);
340
			manager2.remove(fileName);
Lines 359-365 Link Here
359
			assertNotNull(testFile);
353
			assertNotNull(testFile);
360
			assertTrue(testFile.getName().endsWith(".3"));
354
			assertTrue(testFile.getName().endsWith(".3"));
361
			assertTrue(file3.exists());
355
			assertTrue(file3.exists());
362
			
356
363
			// open a new manager, ensure that the database was updated
357
			// open a new manager, ensure that the database was updated
364
			// by checking version is also #3
358
			// by checking version is also #3
365
			manager1 = new StorageManager(base, null);
359
			manager1 = new StorageManager(base, null);
Lines 367-382 Link Here
367
			testFile = manager1.lookup(fileName, false);
361
			testFile = manager1.lookup(fileName, false);
368
			assertNotNull(testFile);
362
			assertNotNull(testFile);
369
			assertTrue(testFile.getName().endsWith(".3"));
363
			assertTrue(testFile.getName().endsWith(".3"));
370
			
364
371
			manager1.close();
365
			manager1.close();
372
			manager1 = null;
366
			manager1 = null;
373
			manager2.close();
367
			manager2.close();
374
			manager2 = null;
368
			manager2 = null;
375
		} catch(IOException e) {
369
		} catch (IOException e) {
376
			fail("unexpected exception", e);
370
			fail("unexpected exception", e);
377
		}
371
		}
378
	}
372
	}
379
	
373
380
	/**
374
	/**
381
	 * Test multiple FM do not cleanup any old files as they may be in use.
375
	 * Test multiple FM do not cleanup any old files as they may be in use.
382
	 */
376
	 */
Lines 388-395 Link Here
388
			// this is a Windows-only test
382
			// this is a Windows-only test
389
			return;
383
			return;
390
		String fileName = "testMultipleFileManagers.txt";
384
		String fileName = "testMultipleFileManagers.txt";
391
		File file1 = new File(base, fileName+".1");
385
		File file1 = new File(base, fileName + ".1");
392
		File file2 = new File(base, fileName+".2");
386
		File file2 = new File(base, fileName + ".2");
393
		manager1 = new StorageManager(base, null);
387
		manager1 = new StorageManager(base, null);
394
		try {
388
		try {
395
			manager1.open(true);
389
			manager1.open(true);
Lines 398-410 Link Here
398
			file = manager1.createTempFile(fileName);
392
			file = manager1.createTempFile(fileName);
399
			writeToFile(file, "test contents #1");
393
			writeToFile(file, "test contents #1");
400
			manager1.update(new String[] {fileName}, new String[] {file.getName()});
394
			manager1.update(new String[] {fileName}, new String[] {file.getName()});
401
			
395
402
			// ensure file is version #1
396
			// ensure file is version #1
403
			file = manager1.lookup(fileName, false);
397
			file = manager1.lookup(fileName, false);
404
			assertNotNull(file);
398
			assertNotNull(file);
405
			assertTrue(file.getName().endsWith(".1"));
399
			assertTrue(file.getName().endsWith(".1"));
406
			assertTrue(file1.exists());
400
			assertTrue(file1.exists());
407
			
401
408
			//new fileMangager using version #1
402
			//new fileMangager using version #1
409
			manager2 = new StorageManager(base, null);
403
			manager2 = new StorageManager(base, null);
410
			manager2.open(true);
404
			manager2.open(true);
Lines 413-419 Link Here
413
			assertNotNull(file);
407
			assertNotNull(file);
414
			assertTrue(file.getName().endsWith(".1"));
408
			assertTrue(file.getName().endsWith(".1"));
415
			assertTrue(file1.exists() && !file2.exists());
409
			assertTrue(file1.exists() && !file2.exists());
416
			
410
417
			// back to manager #1, update file again, close
411
			// back to manager #1, update file again, close
418
			file = manager1.createTempFile(fileName);
412
			file = manager1.createTempFile(fileName);
419
			writeToFile(file, "test contents #2");
413
			writeToFile(file, "test contents #2");
Lines 426-432 Link Here
426
			// both files better still exists
420
			// both files better still exists
427
			assertTrue(file1.exists());
421
			assertTrue(file1.exists());
428
			assertTrue(file2.exists());
422
			assertTrue(file2.exists());
429
			
423
430
			// manager #2
424
			// manager #2
431
			// sanity check
425
			// sanity check
432
			file = manager2.lookup(fileName, false);
426
			file = manager2.lookup(fileName, false);
Lines 437-456 Link Here
437
			manager2 = null;
431
			manager2 = null;
438
			assertTrue(!file1.exists());
432
			assertTrue(!file1.exists());
439
			assertTrue(file2.exists());
433
			assertTrue(file2.exists());
440
			
434
441
			// new manager1, does it get version 1?
435
			// new manager1, does it get version 1?
442
			manager1 = new StorageManager(base, null);
436
			manager1 = new StorageManager(base, null);
443
			manager1.open(true);
437
			manager1.open(true);
444
			file = manager1.lookup(fileName, false);
438
			file = manager1.lookup(fileName, false);
445
			assertNotNull(file);
439
			assertNotNull(file);
446
			assertTrue(file.getName().endsWith(".2"));
440
			assertTrue(file.getName().endsWith(".2"));
447
			manager1.close();		
441
			manager1.close();
448
			manager1 = null;
442
			manager1 = null;
449
		} catch(IOException e) {
443
		} catch (IOException e) {
450
			fail("unexpected exception", e);
444
			fail("unexpected exception", e);
451
		}
445
		}
452
	}
446
	}
453
	
447
454
	/**
448
	/**
455
	 * This test will verify that a FM will fail if a lock is held
449
	 * This test will verify that a FM will fail if a lock is held
456
	 */
450
	 */
Lines 460-470 Link Here
460
			// this is a Windows-only test
454
			// this is a Windows-only test
461
			return;
455
			return;
462
		String fileName = "testJavaIOLocking";
456
		String fileName = "testJavaIOLocking";
463
		File lockFile = new File(new File(base,".manager"),".fileTableLock");
457
		File lockFile = new File(new File(base, ".manager"), ".fileTableLock");
464
		lockFile.getParentFile().mkdirs();
458
		lockFile.getParentFile().mkdirs();
465
		try {
459
		try {
466
			new FileOutputStream(lockFile).close();
460
			new FileOutputStream(lockFile).close();
467
		} catch(IOException e) {
461
		} catch (IOException e) {
468
			fail("unexpected exception", e);
462
			fail("unexpected exception", e);
469
		}
463
		}
470
		assertTrue(lockFile.exists());
464
		assertTrue(lockFile.exists());
Lines 476-505 Link Here
476
			try {
470
			try {
477
				manager1.open(true); // wait for lock
471
				manager1.open(true); // wait for lock
478
				fail("open with lock succedded");
472
				fail("open with lock succedded");
479
			} catch(IOException e) {
473
			} catch (IOException e) {
480
				//good
474
				//good
481
			}
475
			}
482
			
476
483
			manager1.open(false); // don't wait, should work
477
			manager1.open(false); // don't wait, should work
484
			try {
478
			try {
485
				manager1.add(fileName);
479
				manager1.add(fileName);
486
				fail("add succedded");
480
				fail("add succedded");
487
			} catch(IOException e) {
481
			} catch (IOException e) {
488
				//good
482
				//good
489
			}
483
			}
490
			//sanity check, file should not be managed
484
			//sanity check, file should not be managed
491
			assertNull(manager1.lookup(fileName, false));
485
			assertNull(manager1.lookup(fileName, false));
492
			manager1.close();
486
			manager1.close();
493
			manager1 = null;
487
			manager1 = null;
494
		} catch(IOException e) {
488
		} catch (IOException e) {
495
			fail("unexpected exception", e);
489
			fail("unexpected exception", e);
496
		} finally {
490
		} finally {
497
			try {
491
			try {
498
				if (fos != null)
492
				if (fos != null)
499
					fos.close();
493
					fos.close();
500
			} catch(IOException e) {
494
			} catch (IOException e) {
501
				fail("unexpected exception", e);
495
				fail("unexpected exception", e);
502
			}
496
			}
503
		}
497
		}
504
	}
498
	}
499
500
	public void testCleanup() {
501
		String fileName = "testCleanup.txt";
502
		File file1 = new File(base, fileName + ".1");
503
		File file2 = new File(base, fileName + ".2");
504
		File file3 = new File(base, fileName + ".3");
505
		System.setProperty("osgi.embedded.cleanupOnOpen", "true");
506
		// create a permanent file
507
		try {
508
			manager1 = new StorageManager(base, null);
509
			manager1.open(true);
510
			manager1.add(fileName);
511
			// create version (1)
512
			File tmpFile = manager1.createTempFile(fileName);
513
			writeToFile(tmpFile, "File exists #1");
514
			manager1.update(new String[] {fileName}, new String[] {tmpFile.getName()});
515
			// sanity check
516
			assertTrue(file1.toString(), file1.exists());
517
			assertFalse(file2.toString(), file2.exists());
518
			assertFalse(file3.toString(), file3.exists());
519
520
			// do it again, now version (2)
521
			tmpFile = manager1.createTempFile(fileName);
522
			writeToFile(tmpFile, "File exists #2");
523
			manager1.update(new String[] {fileName}, new String[] {tmpFile.getName()});
524
			// sanity check
525
			assertFalse(file1.toString(), file1.exists());
526
			assertTrue(file2.toString(), file2.exists());
527
			assertFalse(file3.toString(), file3.exists());
528
529
			// do it again, now version (3)
530
			tmpFile = manager1.createTempFile(fileName);
531
			writeToFile(tmpFile, "File exists #3");
532
			manager1.update(new String[] {fileName}, new String[] {tmpFile.getName()});
533
			// sanity check
534
			assertFalse(file1.toString(), file1.exists());
535
			assertFalse(file2.toString(), file2.exists());
536
			assertTrue(file3.toString(), file3.exists());
537
538
			manager1.close(); // force a cleanup
539
			manager1 = null;
540
			// sanity check
541
			assertFalse(file1.toString(), file1.exists());
542
			assertFalse(file2.toString(), file2.exists());
543
			assertTrue(file3.toString(), file3.exists());
544
		} catch (IOException e) {
545
			fail("unexpected exception", e);
546
		} finally {
547
			System.setProperty("osgi.embedded.cleanupOnOpen", "false");
548
		}
549
	}
505
}
550
}
(-)src/org/eclipse/osgi/tests/services/datalocation/StreamManagerTests.java (-67 / +64 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2006 IBM Corporation and others.
2
 * Copyright (c) 2004, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 11-20 Link Here
11
package org.eclipse.osgi.tests.services.datalocation;
11
package org.eclipse.osgi.tests.services.datalocation;
12
12
13
import java.io.*;
13
import java.io.*;
14
15
import junit.framework.Test;
14
import junit.framework.Test;
16
import junit.framework.TestSuite;
15
import junit.framework.TestSuite;
17
18
import org.eclipse.core.runtime.Platform;
16
import org.eclipse.core.runtime.Platform;
19
import org.eclipse.osgi.storagemanager.ManagedOutputStream;
17
import org.eclipse.osgi.storagemanager.ManagedOutputStream;
20
import org.eclipse.osgi.storagemanager.StorageManager;
18
import org.eclipse.osgi.storagemanager.StorageManager;
Lines 57-75 Link Here
57
		else
55
		else
58
			System.setProperty("osgi.useReliableFiles", reliableFile);
56
			System.setProperty("osgi.useReliableFiles", reliableFile);
59
	}
57
	}
60
	
58
61
	private void rm(File file) {
59
	private void rm(File file) {
62
		if (file.isDirectory()) {
60
		if (file.isDirectory()) {
63
			File[] list = file.listFiles();
61
			File[] list = file.listFiles();
64
			if (list != null) {
62
			if (list != null) {
65
				for (int idx=0; idx<list.length; idx++) {
63
				for (int idx = 0; idx < list.length; idx++) {
66
					rm(list[idx]);
64
					rm(list[idx]);
67
				}
65
				}
68
			}
66
			}
69
		}
67
		}
70
		file.delete();
68
		file.delete();
71
	}
69
	}
72
	
70
73
	private String getInputStreamContents(InputStream is) throws IOException {
71
	private String getInputStreamContents(InputStream is) throws IOException {
74
		StringBuffer sb = new StringBuffer();
72
		StringBuffer sb = new StringBuffer();
75
		byte[] data = new byte[64];
73
		byte[] data = new byte[64];
Lines 77-90 Link Here
77
		try {
75
		try {
78
			while ((len = is.read(data)) != -1) {
76
			while ((len = is.read(data)) != -1) {
79
				sb.append(new String(data, 0, len));
77
				sb.append(new String(data, 0, len));
80
			} 
78
			}
81
		} finally {
79
		} finally {
82
			is.close();
80
			is.close();
83
		}
81
		}
84
		return sb.toString();
82
		return sb.toString();
85
	}
83
	}
86
	
84
87
	
88
	void writeToFile(File file, String str) throws IOException {
85
	void writeToFile(File file, String str) throws IOException {
89
		FileOutputStream fos = new FileOutputStream(file);
86
		FileOutputStream fos = new FileOutputStream(file);
90
		try {
87
		try {
Lines 96-102 Link Here
96
		}
93
		}
97
	}
94
	}
98
95
99
	
100
	/**
96
	/**
101
	 * This tests that FM will keep a backup version of a reliableFile and that
97
	 * This tests that FM will keep a backup version of a reliableFile and that
102
	 * corrupting the reliableFile will recover the previous contents.
98
	 * corrupting the reliableFile will recover the previous contents.
Lines 104-112 Link Here
104
	 */
100
	 */
105
	public void testReliableFile() {
101
	public void testReliableFile() {
106
		String fileName = "testReliableFile.txt";
102
		String fileName = "testReliableFile.txt";
107
		File file1 = new File(base, fileName+".1");
103
		File file1 = new File(base, fileName + ".1");
108
		File file2 = new File(base, fileName+".2");
104
		File file2 = new File(base, fileName + ".2");
109
		File file3 = new File(base, fileName+".3");
105
		File file3 = new File(base, fileName + ".3");
110
		String contents1 = "test reliable file cOntents #1";
106
		String contents1 = "test reliable file cOntents #1";
111
		String contents2 = "test reliable file cOntents #2";
107
		String contents2 = "test reliable file cOntents #2";
112
		try {
108
		try {
Lines 117-123 Link Here
117
			assertNotNull(fmos);
113
			assertNotNull(fmos);
118
			fmos.write(contents1.getBytes());
114
			fmos.write(contents1.getBytes());
119
			fmos.close();
115
			fmos.close();
120
			
116
121
			// Write verion2 of the file
117
			// Write verion2 of the file
122
			fmos = manager1.getOutputStream(fileName);
118
			fmos = manager1.getOutputStream(fileName);
123
			assertNotNull(fmos);
119
			assertNotNull(fmos);
Lines 128-134 Link Here
128
			assertTrue(!file3.exists());
124
			assertTrue(!file3.exists());
129
			manager1.close();
125
			manager1.close();
130
			manager1 = null;
126
			manager1 = null;
131
			
127
132
			//now, open new manager, verify file contents are #2
128
			//now, open new manager, verify file contents are #2
133
			System.setProperty("osgi.useReliableFiles", "true"); // force reliable files
129
			System.setProperty("osgi.useReliableFiles", "true"); // force reliable files
134
			manager2 = new StorageManager(base, null);
130
			manager2 = new StorageManager(base, null);
Lines 138-148 Link Here
138
			assertEquals(contents2, getInputStreamContents(is));
134
			assertEquals(contents2, getInputStreamContents(is));
139
			manager2.close();
135
			manager2.close();
140
			manager2 = null;
136
			manager2 = null;
141
			
137
142
			// need to sleep, FAT32 doesn't have too fine granularity in timestamps
138
			// need to sleep, FAT32 doesn't have too fine granularity in timestamps
143
			try {
139
			try {
144
				Thread.sleep(5000);
140
				Thread.sleep(5000);
145
			} catch(InterruptedException e) {/*ignore*/}
141
			} catch (InterruptedException e) {/*ignore*/
142
			}
146
			//now, corrupt version 2 of the file
143
			//now, corrupt version 2 of the file
147
			RandomAccessFile raf = new RandomAccessFile(file2, "rw");
144
			RandomAccessFile raf = new RandomAccessFile(file2, "rw");
148
			raf.seek(20);
145
			raf.seek(20);
Lines 152-163 Link Here
152
			System.setProperty("osgi.useReliableFiles", "true"); // force reliable files
149
			System.setProperty("osgi.useReliableFiles", "true"); // force reliable files
153
			manager1 = new StorageManager(base, null);
150
			manager1 = new StorageManager(base, null);
154
			manager1.open(true);
151
			manager1.open(true);
155
			
152
156
			//request any valid stream available
153
			//request any valid stream available
157
			is = manager1.getInputStream(fileName);
154
			is = manager1.getInputStream(fileName);
158
			assertNotNull(is);
155
			assertNotNull(is);
159
			assertEquals(contents1, getInputStreamContents(is));
156
			assertEquals(contents1, getInputStreamContents(is));
160
			
157
161
			//now request only the primary file
158
			//now request only the primary file
162
			try {
159
			try {
163
				InputStream[] isSet = manager1.getInputStreamSet(new String[] {fileName});
160
				InputStream[] isSet = manager1.getInputStreamSet(new String[] {fileName});
Lines 166-196 Link Here
166
						isSet[i].close();
163
						isSet[i].close();
167
				}
164
				}
168
				fail("getInputStreamSet was successful");
165
				fail("getInputStreamSet was successful");
169
			} catch(IOException e) {
166
			} catch (IOException e) {
170
				//good
167
				//good
171
			}
168
			}
172
			
169
173
			//now, corrupt version 1 of the file
170
			//now, corrupt version 1 of the file
174
			raf = new RandomAccessFile(file1, "rw");
171
			raf = new RandomAccessFile(file1, "rw");
175
			raf.seek(20);
172
			raf.seek(20);
176
			raf.write('0'); // change 'O' to '0'
173
			raf.write('0'); // change 'O' to '0'
177
			raf.close();
174
			raf.close();
178
			
175
179
			// get input stream should fail
176
			// get input stream should fail
180
			try {
177
			try {
181
				is = manager1.getInputStream(fileName);
178
				is = manager1.getInputStream(fileName);
182
				fail("get input stream succedded");
179
				fail("get input stream succedded");
183
			} catch(IOException e) {
180
			} catch (IOException e) {
184
				//good
181
				//good
185
			}
182
			}
186
			manager1.close();
183
			manager1.close();
187
			manager1 = null;
184
			manager1 = null;
188
		} catch(IOException e) {
185
		} catch (IOException e) {
189
			fail("unexepected exception", e);
186
			fail("unexepected exception", e);
187
		} finally {
188
			System.setProperty("osgi.useReliableFiles", "false"); // force reliable files off
190
		}
189
		}
191
	}
190
	}
192
	
191
193
	
194
	/**
192
	/**
195
	 * This tests if migration from a prior (non-ReliableFile) .fileTable
193
	 * This tests if migration from a prior (non-ReliableFile) .fileTable
196
	 * to the current .fileTable is correct.
194
	 * to the current .fileTable is correct.
Lines 200-207 Link Here
200
		File testDir = new File(base, "testMigrationManager");
198
		File testDir = new File(base, "testMigrationManager");
201
		File managerDir = new File(testDir, ".manager");
199
		File managerDir = new File(testDir, ".manager");
202
		String fileName = "testMigration.txt";
200
		String fileName = "testMigration.txt";
203
		File file2 = new File(testDir, fileName+".2");
201
		File file2 = new File(testDir, fileName + ".2");
204
		File file5 = new File(testDir, fileName+".5");
202
		File file5 = new File(testDir, fileName + ".5");
205
		File fileTable = new File(managerDir, ".fileTable");
203
		File fileTable = new File(managerDir, ".fileTable");
206
		File fileTable1 = new File(managerDir, ".fileTable.1");
204
		File fileTable1 = new File(managerDir, ".fileTable.1");
207
		File fileTable2 = new File(managerDir, ".fileTable.2");
205
		File fileTable2 = new File(managerDir, ".fileTable.2");
Lines 212-225 Link Here
212
		try {
210
		try {
213
			// create a .fileTable and a normal file
211
			// create a .fileTable and a normal file
214
			managerDir.mkdirs();
212
			managerDir.mkdirs();
215
			writeToFile(fileTable, "#safe table\n"+fileName+"=2\n");
213
			writeToFile(fileTable, "#safe table\n" + fileName + "=2\n");
216
			writeToFile(file2, contents1);
214
			writeToFile(file2, contents1);
217
			manager1 = new StorageManager(testDir, null);
215
			manager1 = new StorageManager(testDir, null);
218
			manager1.open(true);
216
			manager1.open(true);
219
			File test = manager1.lookup(fileName, false);
217
			File test = manager1.lookup(fileName, false);
220
			assertNotNull(test);
218
			assertNotNull(test);
221
			assertTrue(test.exists());
219
			assertTrue(test.exists());
222
			
220
223
			// update a new file
221
			// update a new file
224
			File testFile = manager1.createTempFile(fileName);
222
			File testFile = manager1.createTempFile(fileName);
225
			writeToFile(testFile, contents2);
223
			writeToFile(testFile, contents2);
Lines 233-240 Link Here
233
			writeToFile(testFile, contents1);
231
			writeToFile(testFile, contents1);
234
			manager1.update(new String[] {fileName}, new String[] {testFile.getName()});
232
			manager1.update(new String[] {fileName}, new String[] {testFile.getName()});
235
			manager1.close();
233
			manager1.close();
236
			manager1= null;
234
			manager1 = null;
237
			
235
238
			String[] files = managerDir.list();
236
			String[] files = managerDir.list();
239
			assertEquals(4, files.length);
237
			assertEquals(4, files.length);
240
			//original file never gets deleted
238
			//original file never gets deleted
Lines 247-253 Link Here
247
			files = testDir.list();
245
			files = testDir.list();
248
			assertEquals(2, files.length);
246
			assertEquals(2, files.length);
249
			assertTrue(file5.exists());
247
			assertTrue(file5.exists());
250
			
248
251
			manager2 = new StorageManager(testDir, null);
249
			manager2 = new StorageManager(testDir, null);
252
			manager2.open(true);
250
			manager2.open(true);
253
			testFile = manager2.lookup(fileName, false);
251
			testFile = manager2.lookup(fileName, false);
Lines 255-263 Link Here
255
			assertTrue(testFile.exists());
253
			assertTrue(testFile.exists());
256
			assertTrue(testFile.getName().endsWith(".5"));
254
			assertTrue(testFile.getName().endsWith(".5"));
257
			manager2.close();
255
			manager2.close();
258
			manager2=null;
256
			manager2 = null;
259
			
257
260
		} catch(IOException e) {
258
		} catch (IOException e) {
261
			fail("unexepected exception", e);
259
			fail("unexepected exception", e);
262
		}
260
		}
263
	}
261
	}
Lines 270-285 Link Here
270
		testAbort(true);
268
		testAbort(true);
271
		testAbort(false);
269
		testAbort(false);
272
	}
270
	}
273
	
271
274
	private void testAbort(boolean reliable) {
272
	private void testAbort(boolean reliable) {
275
		String fileName;
273
		String fileName;
276
		if (reliable)
274
		if (reliable)
277
			fileName = "abortFileReliable.txt";
275
			fileName = "abortFileReliable.txt";
278
		else 
276
		else
279
			fileName = "abortFileStd.txt";
277
			fileName = "abortFileStd.txt";
280
		File file1 = new File(base, fileName+".1");
278
		File file1 = new File(base, fileName + ".1");
281
		File file2 = new File(base, fileName+".2");
279
		File file2 = new File(base, fileName + ".2");
282
		File file3 = new File(base, fileName+".3");
280
		File file3 = new File(base, fileName + ".3");
283
		String contents1 = "test reliable file contents #1";
281
		String contents1 = "test reliable file contents #1";
284
		String contents2 = "test reliable file contents #2";
282
		String contents2 = "test reliable file contents #2";
285
		try {
283
		try {
Lines 290-302 Link Here
290
			ManagedOutputStream smos = manager1.getOutputStream(fileName);
288
			ManagedOutputStream smos = manager1.getOutputStream(fileName);
291
			smos.write(contents1.getBytes());
289
			smos.write(contents1.getBytes());
292
			smos.close();
290
			smos.close();
293
			
291
294
			//start creating version 2
292
			//start creating version 2
295
			smos = manager1.getOutputStream(fileName);
293
			smos = manager1.getOutputStream(fileName);
296
			smos.write(contents2.getBytes());
294
			smos.write(contents2.getBytes());
297
			smos.abort();
295
			smos.abort();
298
			smos.close(); // shouldn't cause exception, check!
296
			smos.close(); // shouldn't cause exception, check!
299
			
297
300
			// now see if we're still on version #1
298
			// now see if we're still on version #1
301
			assertEquals(1, manager1.getId(fileName));
299
			assertEquals(1, manager1.getId(fileName));
302
			// check contents also
300
			// check contents also
Lines 304-311 Link Here
304
			assertNotNull(is);
302
			assertNotNull(is);
305
			assertEquals(contents1, getInputStreamContents(is));
303
			assertEquals(contents1, getInputStreamContents(is));
306
			manager1.close();
304
			manager1.close();
307
			manager1=null;
305
			manager1 = null;
308
			
306
309
			// open a new manager & check the same thing to ensure the database is correct
307
			// open a new manager & check the same thing to ensure the database is correct
310
			System.setProperty("osgi.useReliableFiles", "true"); // force reliable files
308
			System.setProperty("osgi.useReliableFiles", "true"); // force reliable files
311
			manager2 = new StorageManager(base, null);
309
			manager2 = new StorageManager(base, null);
Lines 318-333 Link Here
318
			assertNotNull(is);
316
			assertNotNull(is);
319
			assertEquals(contents1, getInputStreamContents(is));
317
			assertEquals(contents1, getInputStreamContents(is));
320
			manager2.close();
318
			manager2.close();
321
			manager2=null;
319
			manager2 = null;
322
			assertTrue(file1.exists());
320
			assertTrue(file1.exists());
323
			assertFalse(file2.exists());
321
			assertFalse(file2.exists());
324
			assertFalse(file3.exists());
322
			assertFalse(file3.exists());
325
		} catch(IOException e) {
323
		} catch (IOException e) {
326
			fail("unexepected exception", e);
324
			fail("unexepected exception", e);
327
		}
325
		}
328
	}
326
	}
329
327
330
	
331
	/**
328
	/**
332
	 * This tests if getting an output stream-set work properly.
329
	 * This tests if getting an output stream-set work properly.
333
	 *
330
	 *
Lines 336-342 Link Here
336
		testGetOutputStreamSet(true);
333
		testGetOutputStreamSet(true);
337
		testGetOutputStreamSet(false);
334
		testGetOutputStreamSet(false);
338
	}
335
	}
339
	
336
340
	private void testGetOutputStreamSet(boolean reliable) {
337
	private void testGetOutputStreamSet(boolean reliable) {
341
		File mgrDir;
338
		File mgrDir;
342
		if (reliable)
339
		if (reliable)
Lines 345-354 Link Here
345
			mgrDir = new File(base, "getSetStd");
342
			mgrDir = new File(base, "getSetStd");
346
		String fileName1 = "testSet1.txt";
343
		String fileName1 = "testSet1.txt";
347
		String fileName2 = "testSet2.txt";
344
		String fileName2 = "testSet2.txt";
348
		File file1_1 = new File(mgrDir, fileName1+".1");
345
		File file1_1 = new File(mgrDir, fileName1 + ".1");
349
		File file1_2 = new File(mgrDir, fileName1+".2");
346
		File file1_2 = new File(mgrDir, fileName1 + ".2");
350
		File file2_1 = new File(mgrDir, fileName2+".1");
347
		File file2_1 = new File(mgrDir, fileName2 + ".1");
351
		File file2_2 = new File(mgrDir, fileName2+".2");
348
		File file2_2 = new File(mgrDir, fileName2 + ".2");
352
		String contents1 = "test reliable file contents #1";
349
		String contents1 = "test reliable file contents #1";
353
		String contents2 = "test reliable file contents #2";
350
		String contents2 = "test reliable file contents #2";
354
		try {
351
		try {
Lines 358-364 Link Here
358
			ManagedOutputStream[] outs = manager1.getOutputStreamSet(new String[] {fileName1, fileName2});
355
			ManagedOutputStream[] outs = manager1.getOutputStreamSet(new String[] {fileName1, fileName2});
359
			assertNotNull(outs);
356
			assertNotNull(outs);
360
			assertEquals(2, outs.length);
357
			assertEquals(2, outs.length);
361
			
358
362
			outs[0].write(contents1.getBytes());
359
			outs[0].write(contents1.getBytes());
363
			outs[1].write(contents2.getBytes());
360
			outs[1].write(contents2.getBytes());
364
			outs[1].close();
361
			outs[1].close();
Lines 367-376 Link Here
367
			outs[0].close();
364
			outs[0].close();
368
			assertTrue(file1_1.exists());
365
			assertTrue(file1_1.exists());
369
			assertTrue(file2_1.exists());
366
			assertTrue(file2_1.exists());
370
			
367
371
			outs = manager1.getOutputStreamSet(new String[] {fileName1, fileName2});
368
			outs = manager1.getOutputStreamSet(new String[] {fileName1, fileName2});
372
			assertNotNull(outs);
369
			assertNotNull(outs);
373
			
370
374
			outs[0].write("new data #1".getBytes());
371
			outs[0].write("new data #1".getBytes());
375
			outs[1].write("new data #2".getBytes());
372
			outs[1].write("new data #2".getBytes());
376
			outs[0].close();
373
			outs[0].close();
Lines 381-387 Link Here
381
			assertTrue(file2_2.exists());
378
			assertTrue(file2_2.exists());
382
			manager1.close();
379
			manager1.close();
383
			manager1 = null;
380
			manager1 = null;
384
			
381
385
			if (reliable) {
382
			if (reliable) {
386
				// verify FM thinks they are reliable
383
				// verify FM thinks they are reliable
387
				assertTrue(file1_1.exists());
384
				assertTrue(file1_1.exists());
Lines 389-402 Link Here
389
			} else {
386
			} else {
390
				// verify FM thinks they are not reliable
387
				// verify FM thinks they are not reliable
391
				assertFalse(file1_1.exists());
388
				assertFalse(file1_1.exists());
392
				assertFalse(file2_1.exists());				
389
				assertFalse(file2_1.exists());
393
			}
390
			}
394
		
391
395
		} catch(IOException e) {
392
		} catch (IOException e) {
396
			fail("unexepected exception", e);
393
			fail("unexepected exception", e);
397
		}
394
		}
398
	}
395
	}
399
	
396
400
	/**
397
	/**
401
	 * This tests if aborting a managed stream-set works as expected
398
	 * This tests if aborting a managed stream-set works as expected
402
	 *
399
	 *
Lines 405-411 Link Here
405
		testAbortSet(true);
402
		testAbortSet(true);
406
		testAbortSet(false);
403
		testAbortSet(false);
407
	}
404
	}
408
	
405
409
	private void testAbortSet(boolean reliable) {
406
	private void testAbortSet(boolean reliable) {
410
		File mgrDir;
407
		File mgrDir;
411
		if (reliable)
408
		if (reliable)
Lines 427-433 Link Here
427
			manager1.open(true);
424
			manager1.open(true);
428
			ManagedOutputStream[] outs = manager1.getOutputStreamSet(new String[] {fileName1, fileName2, fileName3, fileName4});
425
			ManagedOutputStream[] outs = manager1.getOutputStreamSet(new String[] {fileName1, fileName2, fileName3, fileName4});
429
			assertNotNull(outs);
426
			assertNotNull(outs);
430
			
427
431
			outs[0].write(contents1.getBytes());
428
			outs[0].write(contents1.getBytes());
432
			outs[1].write(contents2.getBytes());
429
			outs[1].write(contents2.getBytes());
433
			outs[2].write(contents2.getBytes());
430
			outs[2].write(contents2.getBytes());
Lines 449-455 Link Here
449
			assertNull(manager1.lookup(fileName4, false));
446
			assertNull(manager1.lookup(fileName4, false));
450
			manager1.close();
447
			manager1.close();
451
			manager1 = null;
448
			manager1 = null;
452
			
449
453
			// open a new manager & check the same thing to ensure the database is correct
450
			// open a new manager & check the same thing to ensure the database is correct
454
			System.setProperty("osgi.useReliableFiles", reliable ? "true" : "false"); // force reliable files
451
			System.setProperty("osgi.useReliableFiles", reliable ? "true" : "false"); // force reliable files
455
			manager2 = new StorageManager(mgrDir, null);
452
			manager2 = new StorageManager(mgrDir, null);
Lines 463-472 Link Here
463
			assertEquals(1, list.length);
460
			assertEquals(1, list.length);
464
			manager2.close();
461
			manager2.close();
465
			manager2 = null;
462
			manager2 = null;
466
		
463
467
		} catch(IOException e) {
464
		} catch (IOException e) {
468
			fail("unexepected exception", e);
465
			fail("unexepected exception", e);
469
		}
466
		}
470
	}
467
	}
471
	
468
472
}
469
}

Return to bug 259981