Skip to content

Commit

Permalink
Fixed bug 62373 - file specs and case-sensitivity
Browse files Browse the repository at this point in the history
  • Loading branch information
rchaves committed May 27, 2004
1 parent 0c36353 commit 8cee0a0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,18 +351,16 @@ 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) {
if (fileSpecs == null)
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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"));
Expand All @@ -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")));
}

/**
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 8cee0a0

Please sign in to comment.