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 |
} |