diff --git a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/content/ContentType.java b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/content/ContentType.java index b2f9cdda0f5..3234e42082f 100644 --- a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/content/ContentType.java +++ b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/content/ContentType.java @@ -351,10 +351,8 @@ boolean hasAnyFileSpec() { } /** - * @param text - * the file spec string - * @param typeMask - * FILE_NAME_SPEC or FILE_EXTENSION_SPEC + * @param text the file spec string + * @param typeMask FILE_NAME_SPEC or FILE_EXTENSION_SPEC * @return true if this file spec has already been added, false otherwise */ private boolean hasFileSpec(String text, int typeMask) { @@ -362,7 +360,7 @@ private boolean hasFileSpec(String text, int typeMask) { return false; for (Iterator i = fileSpecs.iterator(); i.hasNext();) { FileSpec spec = (FileSpec) i.next(); - if ((spec.getBasicType() == typeMask) && text.equals(spec.getText())) + if (spec.equals(text, typeMask)) return true; } return false; diff --git a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/content/ContentTypeManager.java b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/content/ContentTypeManager.java index f61dfc31c88..f5399fd1e16 100644 --- a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/content/ContentTypeManager.java +++ b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/content/ContentTypeManager.java @@ -126,9 +126,10 @@ protected void addContentType(IContentType contentType) { private void addFileSpecContributor(IContentType contentType, int fileSpecType, Map fileSpecsMap) { String[] fileSpecs = contentType.getFileSpecs(fileSpecType); for (int i = 0; i < fileSpecs.length; i++) { - Set existing = (Set) fileSpecsMap.get(fileSpecs[i]); + String mappingKey = FileSpec.getMappingKeyFor(fileSpecs[i]); + Set existing = (Set) fileSpecsMap.get(mappingKey); if (existing == null) - fileSpecsMap.put(fileSpecs[i], existing = new TreeSet(conflictComparator)); + fileSpecsMap.put(mappingKey, existing = new TreeSet(conflictComparator)); existing.add(contentType); } } @@ -198,7 +199,7 @@ public IContentType[] findContentTypesFor(String fileName) { List result = new ArrayList(5); int count = 0; // files associated by name should appear before those associated by extension - SortedSet allByFileName = (SortedSet) fileNames.get(fileName); + SortedSet allByFileName = (SortedSet) fileNames.get(FileSpec.getMappingKeyFor(fileName)); if (allByFileName != null && !allByFileName.isEmpty()) { ContentType main = ((ContentType) allByFileName.first()).getTarget(); result.add(count++, main); @@ -213,7 +214,7 @@ public IContentType[] findContentTypesFor(String fileName) { String fileExtension = getFileExtension(fileName); if (fileExtension != null) { - SortedSet allByFileExtension = (SortedSet) fileExtensions.get(fileExtension); + SortedSet allByFileExtension = (SortedSet) fileExtensions.get(FileSpec.getMappingKeyFor(fileExtension)); if (allByFileExtension != null && !allByFileExtension.isEmpty()) { ContentType main = ((ContentType) allByFileExtension.first()).getTarget(); if (!result.contains(main)) { diff --git a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/content/FileSpec.java b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/content/FileSpec.java index d7c0345e5cc..4cb73aa5188 100644 --- a/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/content/FileSpec.java +++ b/bundles/org.eclipse.core.runtime/src/org/eclipse/core/internal/content/FileSpec.java @@ -42,10 +42,18 @@ public boolean equals(Object other) { if (!(other instanceof FileSpec)) return false; FileSpec otherFileSpec = (FileSpec) other; - return getBasicType() == otherFileSpec.getBasicType() && text.equals(otherFileSpec.text); + return getBasicType() == otherFileSpec.getBasicType() && text.equalsIgnoreCase(otherFileSpec.text); } + + public boolean equals(String text, int basicType) { + return getBasicType() == basicType && this.text.equalsIgnoreCase(text); + } public int hashCode() { return text.hashCode(); } + + public static String getMappingKeyFor(String fileSpecText) { + return fileSpecText.toLowerCase(); + } } \ No newline at end of file diff --git a/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/content/IContentTypeManagerTest.java b/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/content/IContentTypeManagerTest.java index d245bae00b4..7531ea372ee 100644 --- a/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/content/IContentTypeManagerTest.java +++ b/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/content/IContentTypeManagerTest.java @@ -62,19 +62,19 @@ public void testRegistry() { assertNotNull("4.0", xmlBasedSpecificNameContentType); assertTrue("4.1", isText(contentTypeManager, xmlBasedSpecificNameContentType)); assertEquals("4.2", xmlContentType, xmlBasedSpecificNameContentType.getBaseType()); - IContentType[] xmlTypes = contentTypeManager.findContentTypesFor("foo.xml"); + IContentType[] xmlTypes = contentTypeManager.findContentTypesFor(changeCase("foo.xml")); assertTrue("5.1", contains(xmlTypes, xmlContentType)); IContentType binaryContentType = contentTypeManager.getContentType(RuntimeTest.PI_RUNTIME_TESTS + '.' + "sample-binary1"); assertNotNull("6.0", binaryContentType); assertTrue("6.1", !isText(contentTypeManager, binaryContentType)); assertNull("6.2", binaryContentType.getBaseType()); - IContentType[] binaryTypes = contentTypeManager.findContentTypesFor("foo.samplebin1"); + IContentType[] binaryTypes = contentTypeManager.findContentTypesFor(changeCase("foo.samplebin1")); assertEquals("7.0", 1, binaryTypes.length); assertEquals("7.1", binaryContentType, binaryTypes[0]); IContentType myText = contentTypeManager.getContentType(PI_RUNTIME_TESTS + ".mytext"); assertNotNull("8.0", myText); assertEquals("8.1", "BAR", myText.getDefaultCharset()); - IContentType[] fooBarTypes = contentTypeManager.findContentTypesFor("foo.bar"); + IContentType[] fooBarTypes = contentTypeManager.findContentTypesFor(changeCase("foo.bar")); assertEquals("9.0", 2, fooBarTypes.length); IContentType fooBar = contentTypeManager.getContentType(PI_RUNTIME_TESTS + ".fooBar"); assertNotNull("9.1", fooBar); @@ -228,7 +228,7 @@ public void testDoubleAssociation() { IContentType subFooBarType = contentTypeManager.getContentType(RuntimeTest.PI_RUNTIME_TESTS + '.' + "subFooBar"); assertNotNull("1.1", subFooBarType); // ensure we don't get fooBar twice - IContentType[] fooBarAssociated = contentTypeManager.findContentTypesFor("foo.bar"); + IContentType[] fooBarAssociated = contentTypeManager.findContentTypesFor(changeCase("foo.bar")); assertEquals("2.1", 2, fooBarAssociated.length); assertTrue("2.2", contains(fooBarAssociated, fooBarType)); assertTrue("2.3", contains(fooBarAssociated, subFooBarType)); @@ -254,11 +254,11 @@ public void testIsKindOf() { public void testFindContentType() throws UnsupportedEncodingException, IOException { IContentTypeManager contentTypeManager = LocalContentTypeManager.getLocalContentTypeManager(); IContentType textContentType = contentTypeManager.getContentType(Platform.PI_RUNTIME + '.' + "text"); - IContentType single = contentTypeManager.findContentTypeFor(getInputStream("Just a test"), "file.txt"); + IContentType single = contentTypeManager.findContentTypeFor(getInputStream("Just a test"), changeCase("file.txt")); assertNotNull("1.0", single); assertEquals("1.1", textContentType, single); IContentType xmlContentType = contentTypeManager.getContentType(Platform.PI_RUNTIME + ".xml"); - single = contentTypeManager.findContentTypeFor(getInputStream(XML_UTF_8, "UTF-8"), "foo.xml"); + single = contentTypeManager.findContentTypeFor(getInputStream(XML_UTF_8, "UTF-8"), changeCase("foo.xml")); assertNotNull("2.0", single); assertEquals("2.1", xmlContentType, single); IContentType[] multiple = contentTypeManager.findContentTypesFor(getInputStream(XML_UTF_8, "UTF-8"), null); @@ -270,9 +270,9 @@ public void testAssociations() throws CoreException { // associate a user-defined file spec text.addFileSpec("ini", IContentType.FILE_EXTENSION_SPEC); // test associations - assertTrue("0.1", text.isAssociatedWith("text.txt")); - assertTrue("0.2", text.isAssociatedWith("text.ini")); - assertTrue("0.3", text.isAssociatedWith("text.tkst")); + assertTrue("0.1", text.isAssociatedWith(changeCase("text.txt"))); + assertTrue("0.2", text.isAssociatedWith(changeCase("text.ini"))); + assertTrue("0.3", text.isAssociatedWith(changeCase("text.tkst"))); // check provider defined settings String[] providerDefinedExtensions = text.getFileSpecs(IContentType.FILE_EXTENSION_SPEC | IContentType.IGNORE_USER_DEFINED); assertTrue("1.0", contains(providerDefinedExtensions, "txt")); @@ -286,15 +286,15 @@ public void testAssociations() throws CoreException { // removing pre-defined file specs should not do anything text.removeFileSpec("txt", IContentType.FILE_EXTENSION_SPEC); assertTrue("3.0", contains(text.getFileSpecs(IContentType.FILE_EXTENSION_SPEC | IContentType.IGNORE_USER_DEFINED), "txt")); - assertTrue("3.1", text.isAssociatedWith("text.txt")); - assertTrue("3.2", text.isAssociatedWith("text.ini")); - assertTrue("3.3", text.isAssociatedWith("text.tkst")); + assertTrue("3.1", text.isAssociatedWith(changeCase("text.txt"))); + assertTrue("3.2", text.isAssociatedWith(changeCase("text.ini"))); + assertTrue("3.3", text.isAssociatedWith(changeCase("text.tkst"))); // removing user file specs is the normal case and has to work as expected text.removeFileSpec("ini", IContentType.FILE_EXTENSION_SPEC); assertTrue("4.0", !contains(text.getFileSpecs(IContentType.FILE_EXTENSION_SPEC | IContentType.IGNORE_PRE_DEFINED), "ini")); - assertTrue("4.1", text.isAssociatedWith("text.txt")); - assertTrue("4.2", !text.isAssociatedWith("text.ini")); - assertTrue("4.3", text.isAssociatedWith("text.tkst")); + assertTrue("4.1", text.isAssociatedWith(changeCase("text.txt"))); + assertTrue("4.2", !text.isAssociatedWith(changeCase("text.ini"))); + assertTrue("4.3", text.isAssociatedWith(changeCase("text.tkst"))); } /** @@ -434,4 +434,15 @@ private boolean isText(IContentTypeManager manager, IContentType candidate) { public static Test suite() { return new TestSuite(IContentTypeManagerTest.class); } + /** + * Helps to ensure we don't get fooled by case sensitivity in file names/specs. + */ + private String changeCase(String original) { + StringBuffer result = new StringBuffer(original); + for (int i = result.length()-1; i >= 0; i--) { + char originalChar = original.charAt(i); + result.setCharAt(i, i % 2 == 0 ? Character.toLowerCase(originalChar) : Character.toUpperCase(originalChar)); + } + return result.toString(); + } } \ No newline at end of file