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

Collapse All | Expand All

(-)src/org/eclipse/core/tests/internal/filesystem/wrapper/WrapperFileStore.java (-7 / +21 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 *  Copyright (c) 2006, 2008 IBM Corporation and others.
2
 *  Copyright (c) 2006, 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 7-12 Link Here
7
 * 
7
 * 
8
 *  Contributors:
8
 *  Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     James Blackburn (Broadcom Corp.)
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.core.tests.internal.filesystem.wrapper;
12
package org.eclipse.core.tests.internal.filesystem.wrapper;
12
13
Lines 28-33 Link Here
28
		this.baseStore = baseStore;
29
		this.baseStore = baseStore;
29
	}
30
	}
30
31
32
	public static IFileStore newInstance(Class<? extends WrapperFileStore> clazz, IFileStore baseStore) {
33
		try {
34
			return clazz.getConstructor(IFileStore.class).newInstance(baseStore);
35
		} catch (Exception e) {
36
			// Test infrastructure failure...
37
			throw new Error(e);
38
		}
39
	}
40
41
	protected IFileStore createNewWrappedStore(IFileStore store) {
42
		return newInstance(getClass(), store);
43
	}
44
31
	public IFileInfo[] childInfos(int options, IProgressMonitor monitor) throws CoreException {
45
	public IFileInfo[] childInfos(int options, IProgressMonitor monitor) throws CoreException {
32
		return baseStore.childInfos(options, monitor);
46
		return baseStore.childInfos(options, monitor);
33
	}
47
	}
Lines 40-46 Link Here
40
		IFileStore[] childStores = baseStore.childStores(options, monitor);
54
		IFileStore[] childStores = baseStore.childStores(options, monitor);
41
		for (int i = 0; i < childStores.length; i++)
55
		for (int i = 0; i < childStores.length; i++)
42
			// replace ordinary file store with wrapper version
56
			// replace ordinary file store with wrapper version
43
			childStores[i] = new WrapperFileStore(childStores[i]);
57
			childStores[i] = createNewWrappedStore(childStores[i]);
44
		return childStores;
58
		return childStores;
45
	}
59
	}
46
60
Lines 73-87 Link Here
73
	}
87
	}
74
88
75
	public IFileStore getChild(IPath path) {
89
	public IFileStore getChild(IPath path) {
76
		return new WrapperFileStore(baseStore.getChild(path));
90
		return createNewWrappedStore(baseStore.getChild(path));
77
	}
91
	}
78
	
92
79
	public IFileStore getFileStore(IPath path) {
93
	public IFileStore getFileStore(IPath path) {
80
		return new WrapperFileStore(baseStore.getFileStore(path));
94
		return createNewWrappedStore(baseStore.getFileStore(path));
81
	}
95
	}
82
96
83
	public IFileStore getChild(String name) {
97
	public IFileStore getChild(String name) {
84
		return new WrapperFileStore(baseStore.getChild(name));
98
		return createNewWrappedStore(baseStore.getChild(name));
85
	}
99
	}
86
100
87
	public IFileSystem getFileSystem() {
101
	public IFileSystem getFileSystem() {
Lines 94-100 Link Here
94
108
95
	public IFileStore getParent() {
109
	public IFileStore getParent() {
96
		IFileStore baseParent = baseStore.getParent();
110
		IFileStore baseParent = baseStore.getParent();
97
		return baseParent == null ? null : new WrapperFileStore(baseParent);
111
		return baseParent == null ? null : createNewWrappedStore(baseParent);
98
	}
112
	}
99
113
100
	public int hashCode() {
114
	public int hashCode() {
(-)src/org/eclipse/core/tests/internal/filesystem/wrapper/WrapperFileSystem.java (-8 / +32 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
2
 * Copyright (c) 2006, 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 7-12 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     James Blackburn (Broadcom Corp.)
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.core.tests.internal.filesystem.wrapper;
12
package org.eclipse.core.tests.internal.filesystem.wrapper;
12
13
Lines 22-48 Link Here
22
/**
23
/**
23
 * A simple file system implementation that acts as a wrapper around the
24
 * A simple file system implementation that acts as a wrapper around the
24
 * local file system.
25
 * local file system.
26
 * <p>
27
 * Also allows tests to inject a custom FileStore template class (derived from 
28
 * {@link WrapperFileStore}). Tests can use {@link #setCustomFileStore(Class)}
29
 * to override default {@link WrapperFileStore} behaviour.
30
 * </p>
25
 */
31
 */
26
public class WrapperFileSystem extends FileSystem {
32
public class WrapperFileSystem extends FileSystem {
27
33
28
	private static final IFileStore NULL_ROOT = EFS.getNullFileSystem().getStore(Path.ROOT);
34
	protected static final IFileStore NULL_ROOT = EFS.getNullFileSystem().getStore(Path.ROOT);
29
35
30
	private static final String SCHEME_WRAPPED = "wrapped";
36
	private static final String SCHEME_WRAPPED = "wrapped";
31
37
32
	private static WrapperFileSystem instance;
38
	private static volatile WrapperFileSystem instance;
39
40
	/** Custom file-store wrapper */
41
	private static volatile Class<? extends WrapperFileStore> customFS = WrapperFileStore.class;
33
42
34
	public static URI getBasicURI(URI wrappedURI) {
43
	public static URI getBasicURI(URI wrappedURI) {
35
		Assert.isLegal(SCHEME_WRAPPED.equals(wrappedURI.getScheme()));
44
		Assert.isLegal(SCHEME_WRAPPED.equals(wrappedURI.getScheme()));
36
		return URI.create(wrappedURI.getQuery());
45
		return URI.create(wrappedURI.getQuery());
37
	}
46
	}
38
47
39
	public static WrapperFileSystem getInstance() {
48
	public static synchronized WrapperFileSystem getInstance() {
40
		WrapperFileSystem tmpInstance = instance;
49
		if (instance != null)
41
		if (tmpInstance != null)
50
			return instance;
42
			return tmpInstance;
43
		return instance = new WrapperFileSystem();
51
		return instance = new WrapperFileSystem();
44
	}
52
	}
45
53
54
	/**
55
	 * Use fs as the WrapperFileStore to use in this filesystem.
56
	 * Allows tests to easily override existing IFileStore behaviour.
57
	 * By extending {@link WrapperFileStore} conditions difficult to simulate
58
	 * on the LocalFileSystem can be provoked.
59
	 * 
60
	 * @param fs filestore, or null to use default {@link WrapperFileStore}
61
	 *        based implementation.
62
	 */
63
	public static void setCustomFileStore(Class<? extends WrapperFileStore> fs) {
64
		if (fs == null)
65
			customFS = WrapperFileStore.class;
66
		else
67
			customFS = fs;
68
	}
69
46
	public static URI getWrappedURI(URI baseURI) {
70
	public static URI getWrappedURI(URI baseURI) {
47
		try {
71
		try {
48
			return new URI(SCHEME_WRAPPED, null, baseURI.getPath(), baseURI.toString(), null);
72
			return new URI(SCHEME_WRAPPED, null, baseURI.getPath(), baseURI.toString(), null);
Lines 69-74 Link Here
69
			CoreTest.log(ResourceTest.PI_RESOURCES_TESTS, e);
93
			CoreTest.log(ResourceTest.PI_RESOURCES_TESTS, e);
70
			return NULL_ROOT;
94
			return NULL_ROOT;
71
		}
95
		}
72
		return new WrapperFileStore(baseStore);
96
		return WrapperFileStore.newInstance(customFS, baseStore);
73
	}
97
	}
74
}
98
}
(-)src/org/eclipse/core/tests/resources/ResourceTest.java (+1 lines)
Lines 773-778 Link Here
773
			try {
773
			try {
774
				os = new FileOutputStream(osFile);
774
				os = new FileOutputStream(osFile);
775
				os.write(newContent.getBytes("UTF8"));
775
				os.write(newContent.getBytes("UTF8"));
776
				os.close();
776
			} finally {
777
			} finally {
777
				FileUtil.safeClose(os);
778
				FileUtil.safeClose(os);
778
			}
779
			}
(-)src/org/eclipse/core/tests/resources/regression/AllTests.java (+1 lines)
Lines 59-64 Link Here
59
		suite.addTest(Bug_288315.suite());
59
		suite.addTest(Bug_288315.suite());
60
		suite.addTest(Bug_329836.suite());
60
		suite.addTest(Bug_329836.suite());
61
		suite.addTest(Bug_331445.suite());
61
		suite.addTest(Bug_331445.suite());
62
		suite.addTest(Bug_332543.suite());
62
		suite.addTest(IFileTest.suite());
63
		suite.addTest(IFileTest.suite());
63
		suite.addTest(IFolderTest.suite());
64
		suite.addTest(IFolderTest.suite());
64
		suite.addTest(IProjectTest.suite());
65
		suite.addTest(IProjectTest.suite());
(-)src/org/eclipse/core/tests/resources/regression/Bug_332543.java (+108 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2011 Broadcom Corporation and others.
3
 *  All rights reserved. This program and the accompanying materials
4
 *  are made available under the terms of the Eclipse Public License v1.0
5
 *  which accompanies this distribution, and is available at
6
 *  http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 *  Contributors:
9
 *     James Blackburn (Broadcom Corp.) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.tests.resources.regression;
12
13
import java.io.*;
14
import java.net.URI;
15
import junit.framework.Test;
16
import junit.framework.TestSuite;
17
import org.eclipse.core.filesystem.IFileStore;
18
import org.eclipse.core.filesystem.URIUtil;
19
import org.eclipse.core.resources.*;
20
import org.eclipse.core.runtime.*;
21
import org.eclipse.core.tests.internal.filesystem.wrapper.WrapperFileStore;
22
import org.eclipse.core.tests.internal.filesystem.wrapper.WrapperFileSystem;
23
import org.eclipse.core.tests.resources.ResourceTest;
24
25
/**
26
 * This tests that I/O Exception on OuptuStream#close() after IFile#setContents is correctly reported.
27
 */
28
public class Bug_332543 extends ResourceTest {
29
30
	public static Test suite() {
31
		return new TestSuite(Bug_332543.class);
32
	}
33
34
	/**
35
	 * Wrapper FS which throws an IOException when someone
36
	 * closes an output stream...
37
	 */
38
	public static class IOErrOnCloseFileStore extends WrapperFileStore {
39
		public IOErrOnCloseFileStore(IFileStore store) {
40
			super(store);
41
		}
42
43
		public OutputStream openOutputStream(int options, IProgressMonitor monitor) throws CoreException {
44
			OutputStream os = super.openOutputStream(options, monitor);
45
			os = new BufferedOutputStream(os) {
46
				public void close() throws java.io.IOException {
47
					// We close the output stream (so there aren't issues deleting the project during tear-down)
48
					super.close();
49
					// But we also throw IOException as if the operation had failed.
50
					throw new IOException("Whoops I dunno how to close!");
51
				}
52
			};
53
			return os;
54
		}
55
	}
56
57
	@Override
58
	protected void setUp() throws Exception {
59
		super.setUp();
60
	}
61
62
	@Override
63
	protected void tearDown() throws Exception {
64
		WrapperFileSystem.setCustomFileStore(null);
65
		super.tearDown();
66
	}
67
68
	public void testBug() throws Exception {
69
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
70
71
		String proj_name = getUniqueString();
72
		IPath proj_loc = root.getLocation().append(proj_name);
73
		URI proj_uri = WrapperFileSystem.getWrappedURI(URIUtil.toURI(proj_loc));
74
75
		IProjectDescription desc = ResourcesPlugin.getWorkspace().newProjectDescription(proj_name);
76
		desc.setLocationURI(proj_uri);
77
		// Create the project on the wrapped file system
78
		IProject project = root.getProject(desc.getName());
79
		project.create(desc, getMonitor());
80
81
		// Create a file in the project
82
		IFile f = project.getFile("foo.txt");
83
		ensureExistsInFileSystem(f);
84
85
		// Set our evil IOException on close() fs.
86
		WrapperFileSystem.setCustomFileStore(IOErrOnCloseFileStore.class);
87
88
		// Now open the project
89
		project.open(getMonitor());
90
91
		// Try #setContents on an existing file
92
		try {
93
			f.setContents(new ByteArrayInputStream("Random".getBytes()), false, true, getMonitor());
94
			fail("1.0");
95
		} catch (CoreException e) {
96
			// This is expected.
97
		}
98
99
		// Try create on a non-existent file
100
		f = project.getFile("foo1.txt");
101
		try {
102
			f.create(new ByteArrayInputStream("Random".getBytes()), false, getMonitor());
103
			fail("2.0");
104
		} catch (CoreException e) {
105
			// This is expected.
106
		}
107
	}
108
}

Return to bug 332543