View | Details | Raw Unified | Return to bug 69640 | Differences between
and this patch

Collapse All | Expand All

(-)plugin.xml (-5 / +28 lines)
Lines 236-252 Link Here
236
</extension>
236
</extension>
237
<extension
237
<extension
238
      point="org.eclipse.core.runtime.contentTypes">
238
      point="org.eclipse.core.runtime.contentTypes">
239
   <content-type      
240
         name="My Content Type"
241
         id="myContent"
242
         file-extensions="resources-mc"
243
         default-charset="PROVIDER_CHARSET"/>            
239
   <content-type
244
   <content-type
240
         name="A more specific XML"
245
         name="A more specific XML"
241
         id="anotherXML"
246
         id="anotherXML"
242
         base-type="org.eclipse.core.runtime.xml"          
247
         base-type="org.eclipse.core.runtime.xml"          
243
         describer="org.eclipse.core.runtime.content.XMLRootElementContentDescriber:org.eclipse.core.tests.resources.anotherXML"/>
248
         describer="org.eclipse.core.runtime.content.XMLRootElementContentDescriber:org.eclipse.core.tests.resources.anotherXML"/>
244
   <content-type      
249
   <!-- content types for nature-content type association tests -->
245
         name="My Content Type"
250
   <content-type
246
         id="myContent"
251
         name="A content type for testing nature association"
247
         file-extensions="resources-mc"
252
         id="nature_associated_1"
248
         default-charset="PROVIDER_CHARSET"/>         
253
         file-extensions="nature-associated"
254
         base-type="org.eclipse.core.runtime.text"/>
255
   <content-type
256
         name="A derived content type for testing nature association"
257
         id="nature_associated_2"
258
         base-type="org.eclipse.core.tests.resources.nature_associated_1"/>
259
</extension>
260
<extension
261
      id="contentTypeRelated1"
262
      name="Content-type related nature 1"
263
      point="org.eclipse.core.resources.natures">
264
      <content-type id="org.eclipse.core.tests.resources.nature_associated_1"/>
265
</extension>
266
<extension
267
      id="contentTypeRelated2"
268
      name="Content-type related nature 2"
269
      point="org.eclipse.core.resources.natures">
270
      <content-type id="org.eclipse.core.tests.resources.nature_associated_2"/>       
249
</extension>
271
</extension>
272
250
<extension
273
<extension
251
      point="org.eclipse.core.resources.refreshProviders">
274
      point="org.eclipse.core.resources.refreshProviders">
252
   <refreshProvider
275
   <refreshProvider
(-)src/org/eclipse/core/tests/resources/AllTests.java (+1 lines)
Lines 25-30 Link Here
25
	public static Test suite() {
25
	public static Test suite() {
26
		TestSuite suite = new TestSuite(AllTests.class.getName());
26
		TestSuite suite = new TestSuite(AllTests.class.getName());
27
		suite.addTest(CharsetTest.suite());
27
		suite.addTest(CharsetTest.suite());
28
		suite.addTest(ContentDescriptionManagerTest.suite());
28
		suite.addTest(IFileTest.suite());
29
		suite.addTest(IFileTest.suite());
29
		suite.addTest(IFolderTest.suite());
30
		suite.addTest(IFolderTest.suite());
30
		suite.addTest(IPathVariableTest.suite());
31
		suite.addTest(IPathVariableTest.suite());
(-)src/org/eclipse/core/tests/resources/CharsetTest.java (-94 / +3 lines)
Lines 14-21 Link Here
14
import junit.framework.Test;
14
import junit.framework.Test;
15
import junit.framework.TestSuite;
15
import junit.framework.TestSuite;
16
import org.eclipse.core.internal.preferences.EclipsePreferences;
16
import org.eclipse.core.internal.preferences.EclipsePreferences;
17
import org.eclipse.core.internal.resources.ContentDescriptionManager;
18
import org.eclipse.core.internal.resources.Workspace;
19
import org.eclipse.core.resources.*;
17
import org.eclipse.core.resources.*;
20
import org.eclipse.core.runtime.*;
18
import org.eclipse.core.runtime.*;
21
import org.eclipse.core.runtime.content.*;
19
import org.eclipse.core.runtime.content.*;
Lines 66-75 Link Here
66
		}
64
		}
67
	}
65
	}
68
66
69
	private static final String SAMPLE_SPECIFIC_XML = "<?xml version=\"1.0\"?><org.eclipse.core.tests.resources.anotherXML/>";
67
	static final String SAMPLE_SPECIFIC_XML = "<?xml version=\"1.0\"?><org.eclipse.core.tests.resources.anotherXML/>";
70
	private static final String SAMPLE_XML_DEFAULT_ENCODING = "<?xml version=\"1.0\"?><org.eclipse.core.resources.tests.root/>";
68
	private static final String SAMPLE_XML_DEFAULT_ENCODING = "<?xml version=\"1.0\"?><org.eclipse.core.resources.tests.root/>";
71
	private static final String SAMPLE_XML_ISO_8859_1_ENCODING = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><org.eclipse.core.resources.tests.root/>";
69
	static final String SAMPLE_XML_ISO_8859_1_ENCODING = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><org.eclipse.core.resources.tests.root/>";
72
	private static final String SAMPLE_XML_US_ASCII_ENCODING = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?><org.eclipse.core.resources.tests.root/>";
70
	static final String SAMPLE_XML_US_ASCII_ENCODING = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?><org.eclipse.core.resources.tests.root/>";
73
71
74
	private String savedWorkspaceCharset;
72
	private String savedWorkspaceCharset;
75
73
Lines 251-332 Link Here
251
		}
249
		}
252
	}
250
	}
253
251
254
	public void testBug79151() {
255
		IWorkspace workspace = getWorkspace();
256
		IProject project = workspace.getRoot().getProject("MyProject");
257
		IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
258
		IContentType xml = contentTypeManager.getContentType("org.eclipse.core.runtime.xml");
259
		String newExtension = "xml_bug_79151";
260
		IFile file1 = project.getFile("file.xml");
261
		IFile file2 = project.getFile("file." + newExtension);
262
		ensureExistsInWorkspace(file1, getContents(SAMPLE_XML_ISO_8859_1_ENCODING));
263
		ensureExistsInWorkspace(file2, getContents(SAMPLE_XML_US_ASCII_ENCODING));
264
		// ensure we start in a known state
265
		((Workspace) workspace).getContentDescriptionManager().invalidateCache(true);
266
		// wait for cache flush to finish
267
		waitForCacheFlush();
268
		// cache is new at this point
269
		assertEquals("0.9", ContentDescriptionManager.EMPTY_CACHE, ((Workspace) workspace).getContentDescriptionManager().getCacheState());
270
271
		IContentDescription description1a = null, description1b = null, description1c = null, description1d = null;
272
		IContentDescription description2 = null;
273
		try {
274
			description1a = file1.getContentDescription();
275
			description2 = file2.getContentDescription();
276
		} catch (CoreException e) {
277
			fail("1.0", e);
278
		}
279
		assertNotNull("1.1", description1a);
280
		assertEquals("1.2", xml, description1a.getContentType());
281
		assertNull("1.3", description2);
282
		try {
283
			description1b = file1.getContentDescription();
284
			// ensure it comes from the cache (should be the very same object)
285
			assertNotNull(" 2.0", description1b);
286
			assertSame("2.1", description1a, description1b);
287
		} catch (CoreException e) {
288
			fail("2.2", e);
289
		}
290
		try {
291
			// change the content type
292
			xml.addFileSpec(newExtension, IContentType.FILE_EXTENSION_SPEC);
293
		} catch (CoreException e) {
294
			fail("3.0", e);
295
		}
296
		try {
297
			try {
298
				description1c = file1.getContentDescription();
299
				description2 = file2.getContentDescription();
300
			} catch (CoreException e) {
301
				fail("4.0", e);
302
			}
303
			// ensure it does *not* come from the cache (should be a different object)
304
			assertNotNull("4.1", description1c);
305
			assertNotSame("4.2", description1a, description1c);
306
			assertEquals("4.3", xml, description1c.getContentType());
307
			assertNotNull("4.4", description2);
308
			assertEquals("4.5", xml, description2.getContentType());
309
		} finally {
310
			try {
311
				// dissociate the xml2 extension from the XML content type
312
				xml.removeFileSpec(newExtension, IContentType.FILE_EXTENSION_SPEC);
313
			} catch (CoreException e) {
314
				fail("4.99", e);
315
			}
316
		}
317
		try {
318
			description1d = file1.getContentDescription();
319
			description2 = file2.getContentDescription();
320
		} catch (CoreException e) {
321
			fail("5.0", e);
322
		}
323
		// ensure it does *not* come from the cache (should be a different object)
324
		assertNotNull("5.1", description1d);
325
		assertNotSame("5.2", description1c, description1d);
326
		assertEquals("5.3", xml, description1d.getContentType());
327
		assertNull("5.4", description2);
328
	}
329
330
	public void testChangesDifferentProject() throws CoreException {
252
	public void testChangesDifferentProject() throws CoreException {
331
		IWorkspace workspace = getWorkspace();
253
		IWorkspace workspace = getWorkspace();
332
		IProject project1 = workspace.getRoot().getProject("Project1");
254
		IProject project1 = workspace.getRoot().getProject("Project1");
Lines 1046-1062 Link Here
1046
			}
968
			}
1047
		}
969
		}
1048
	}
970
	}
1049
1050
	/**
1051
	 * Blocks the calling thread until the cache flush job completes.
1052
	 */
1053
	protected void waitForCacheFlush() {
1054
		try {
1055
			Platform.getJobManager().join(ContentDescriptionManager.FAMILY_DESCRIPTION_CACHE_FLUSH, null);
1056
		} catch (OperationCanceledException e) {
1057
			//ignore
1058
		} catch (InterruptedException e) {
1059
			//ignore
1060
		}
1061
	}
1062
}
971
}
(-)src/org/eclipse/core/tests/resources/ContentDescriptionManagerTest.java (+222 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005 IBM 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
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.tests.resources;
12
13
import java.io.ByteArrayInputStream;
14
import java.io.InputStream;
15
import junit.framework.Test;
16
import junit.framework.TestSuite;
17
import org.eclipse.core.internal.resources.ContentDescriptionManager;
18
import org.eclipse.core.internal.resources.Workspace;
19
import org.eclipse.core.resources.*;
20
import org.eclipse.core.runtime.*;
21
import org.eclipse.core.runtime.content.*;
22
23
public class ContentDescriptionManagerTest extends ResourceTest {
24
25
	private static final String CONTENT_TYPE_RELATED_NATURE2 = "org.eclipse.core.tests.resources.contentTypeRelated2";
26
	private static final String CONTENT_TYPE_RELATED_NATURE1 = "org.eclipse.core.tests.resources.contentTypeRelated1";
27
28
	public static Test suite() {
29
		return new TestSuite(ContentDescriptionManagerTest.class);
30
	}
31
32
	public ContentDescriptionManagerTest(String name) {
33
		super(name);
34
	}
35
36
	protected InputStream projectDescriptionWithNatures(String project, String[] natures) {
37
		StringBuffer contents = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?><projectDescription><name>" + project + "</name><natures>");
38
		for (int i = 0; i < natures.length; i++)
39
			contents.append("<nature>" + natures[i] + "</nature>");
40
		contents.append("</natures></projectDescription>");
41
		return new ByteArrayInputStream(contents.toString().getBytes());
42
	}
43
44
	public void testNatureContentTypeAssociation() {
45
		IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
46
		IContentType baseType = contentTypeManager.getContentType("org.eclipse.core.tests.resources.nature_associated_1");
47
		IContentType derivedType = contentTypeManager.getContentType("org.eclipse.core.tests.resources.nature_associated_2");
48
		assertNotNull("0.1", baseType);
49
		assertNotNull("0.2", derivedType);
50
		IProject project = getWorkspace().getRoot().getProject("proj1");
51
		IFile file = project.getFile("file.nature-associated");
52
		IFile descFile = project.getFile(IProjectDescription.DESCRIPTION_FILE_NAME);
53
		ensureExistsInWorkspace(file, "it really does not matter");
54
		IContentDescription description = null;
55
56
		// originally, project description has no natures		
57
		try {
58
			descFile.setContents(projectDescriptionWithNatures(project.getName(), new String[0]), IResource.FORCE, getMonitor());
59
		} catch (CoreException e) {
60
			fail("1.0", e);
61
		}
62
		waitForCacheFlush();
63
		try {
64
			description = file.getContentDescription();
65
		} catch (CoreException e) {
66
			fail("1.1", e);
67
		}
68
		assertNotNull("1.2", description);
69
		assertSame("1.3", derivedType, description.getContentType());
70
71
		// change project description to include one of the natures		
72
		try {
73
			descFile.setContents(projectDescriptionWithNatures(project.getName(), new String[] {CONTENT_TYPE_RELATED_NATURE1}), IResource.FORCE, getMonitor());
74
		} catch (CoreException e) {
75
			fail("2.0", e);
76
		}
77
		waitForCacheFlush();
78
		try {
79
			description = file.getContentDescription();
80
		} catch (CoreException e) {
81
			fail("2.1", e);
82
		}
83
		assertNotNull("2.2", description);
84
		assertSame("2.3", baseType, description.getContentType());
85
86
		// change project description to include the other nature		
87
		try {
88
			descFile.setContents(projectDescriptionWithNatures(project.getName(), new String[] {CONTENT_TYPE_RELATED_NATURE2}), IResource.FORCE, getMonitor());
89
		} catch (CoreException e) {
90
			fail("3.0", e);
91
		}
92
		waitForCacheFlush();
93
		try {
94
			description = file.getContentDescription();
95
		} catch (CoreException e) {
96
			fail("3.1", e);
97
		}
98
		assertNotNull("3.2", description);
99
		assertSame("3.3", derivedType, description.getContentType());
100
101
		// change project description to include both of the natures
102
		try {
103
			descFile.setContents(projectDescriptionWithNatures(project.getName(), new String[] {CONTENT_TYPE_RELATED_NATURE1, CONTENT_TYPE_RELATED_NATURE2}), IResource.FORCE, getMonitor());
104
		} catch (CoreException e) {
105
			fail("4.0", e);
106
		}
107
		waitForCacheFlush();
108
		try {
109
			description = file.getContentDescription();
110
		} catch (CoreException e) {
111
			fail("4.1", e);
112
		}
113
		assertNotNull("4.2", description);
114
		assertSame("4.3", derivedType, description.getContentType());
115
116
		// back to no natures
117
		try {
118
			descFile.setContents(projectDescriptionWithNatures(project.getName(), new String[0]), IResource.FORCE, getMonitor());
119
		} catch (CoreException e) {
120
			fail("5.0", e);
121
		}
122
		waitForCacheFlush();
123
		try {
124
			description = file.getContentDescription();
125
		} catch (CoreException e) {
126
			fail("5.1", e);
127
		}
128
		assertNotNull("5.2", description);
129
		assertSame("5.3", derivedType, description.getContentType());
130
131
	}
132
133
	public void testBug79151() {
134
		IWorkspace workspace = getWorkspace();
135
		IProject project = workspace.getRoot().getProject("MyProject");
136
		IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
137
		IContentType xml = contentTypeManager.getContentType("org.eclipse.core.runtime.xml");
138
		String newExtension = "xml_bug_79151";
139
		IFile file1 = project.getFile("file.xml");
140
		IFile file2 = project.getFile("file." + newExtension);
141
		ensureExistsInWorkspace(file1, getContents(CharsetTest.SAMPLE_XML_ISO_8859_1_ENCODING));
142
		ensureExistsInWorkspace(file2, getContents(CharsetTest.SAMPLE_XML_US_ASCII_ENCODING));
143
		// ensure we start in a known state
144
		((Workspace) workspace).getContentDescriptionManager().invalidateCache(true, null);
145
		// wait for cache flush to finish
146
		waitForCacheFlush();
147
		// cache is new at this point
148
		assertEquals("0.9", ContentDescriptionManager.EMPTY_CACHE, ((Workspace) workspace).getContentDescriptionManager().getCacheState());
149
150
		IContentDescription description1a = null, description1b = null, description1c = null, description1d = null;
151
		IContentDescription description2 = null;
152
		try {
153
			description1a = file1.getContentDescription();
154
			description2 = file2.getContentDescription();
155
		} catch (CoreException e) {
156
			fail("1.0", e);
157
		}
158
		assertNotNull("1.1", description1a);
159
		assertEquals("1.2", xml, description1a.getContentType());
160
		assertNull("1.3", description2);
161
		try {
162
			description1b = file1.getContentDescription();
163
			// ensure it comes from the cache (should be the very same object)
164
			assertNotNull(" 2.0", description1b);
165
			assertSame("2.1", description1a, description1b);
166
		} catch (CoreException e) {
167
			fail("2.2", e);
168
		}
169
		try {
170
			// change the content type
171
			xml.addFileSpec(newExtension, IContentType.FILE_EXTENSION_SPEC);
172
		} catch (CoreException e) {
173
			fail("3.0", e);
174
		}
175
		try {
176
			try {
177
				description1c = file1.getContentDescription();
178
				description2 = file2.getContentDescription();
179
			} catch (CoreException e) {
180
				fail("4.0", e);
181
			}
182
			// ensure it does *not* come from the cache (should be a different object)
183
			assertNotNull("4.1", description1c);
184
			assertNotSame("4.2", description1a, description1c);
185
			assertEquals("4.3", xml, description1c.getContentType());
186
			assertNotNull("4.4", description2);
187
			assertEquals("4.5", xml, description2.getContentType());
188
		} finally {
189
			try {
190
				// dissociate the xml2 extension from the XML content type
191
				xml.removeFileSpec(newExtension, IContentType.FILE_EXTENSION_SPEC);
192
			} catch (CoreException e) {
193
				fail("4.99", e);
194
			}
195
		}
196
		try {
197
			description1d = file1.getContentDescription();
198
			description2 = file2.getContentDescription();
199
		} catch (CoreException e) {
200
			fail("5.0", e);
201
		}
202
		// ensure it does *not* come from the cache (should be a different object)
203
		assertNotNull("5.1", description1d);
204
		assertNotSame("5.2", description1c, description1d);
205
		assertEquals("5.3", xml, description1d.getContentType());
206
		assertNull("5.4", description2);
207
	}
208
209
	/**
210
	 * Blocks the calling thread until the cache flush job completes.
211
	 */
212
	protected void waitForCacheFlush() {
213
		try {
214
			Platform.getJobManager().join(ContentDescriptionManager.FAMILY_DESCRIPTION_CACHE_FLUSH, null);
215
		} catch (OperationCanceledException e) {
216
			//ignore
217
		} catch (InterruptedException e) {
218
			//ignore
219
		}
220
	}
221
222
}

Return to bug 69640