Bug 559888 - Move test resources to JUnit 4 Convert content tests. Change-Id: Ic6e91bba89275925a3f37bf14f94fb2d832e2036 Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
diff --git a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/ContentDescriptionTest.java b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/ContentDescriptionTest.java index ff314fd..4a3dd13 100644 --- a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/ContentDescriptionTest.java +++ b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/ContentDescriptionTest.java
@@ -13,10 +13,18 @@ *******************************************************************************/ package org.eclipse.core.tests.resources.content; +import static org.eclipse.core.tests.resources.AutomatedTests.PI_RESOURCES_TESTS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import org.eclipse.core.internal.content.*; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.QualifiedName; import org.eclipse.core.runtime.content.IContentDescription; +import org.junit.Test; public class ContentDescriptionTest extends ContentTypeTest { private static final String CT_VOID = PI_RESOURCES_TESTS + '.' + "void"; @@ -25,14 +33,11 @@ private static final QualifiedName FOO_PROPERTY = new QualifiedName(PI_RESOURCES_TESTS, "foo"); private static final QualifiedName FRED_PROPERTY = new QualifiedName(PI_RESOURCES_TESTS, "fred"); - public ContentDescriptionTest(String name) { - super(name); - } - private ContentType getContentType() { return ((ContentTypeHandler) Platform.getContentTypeManager().getContentType(CT_VOID)).getTarget(); } + @Test public void testAllProperties() { ContentDescription description = new ContentDescription(IContentDescription.ALL, getContentType()); assertTrue("1.0", description.isRequested(FOO_PROPERTY)); @@ -55,6 +60,7 @@ } } + @Test public void testOneProperty() { ContentDescription description = new ContentDescription(new QualifiedName[] {FOO_PROPERTY}, getContentType()); assertTrue("1.0", description.isRequested(FOO_PROPERTY)); @@ -76,6 +82,7 @@ } } + @Test public void testZeroProperties() { ContentDescription description = new ContentDescription(new QualifiedName[0], getContentType()); assertFalse("1.0", description.isRequested(FOO_PROPERTY)); @@ -91,6 +98,7 @@ } } + @Test public void testMultipleProperties() { ContentDescription description = new ContentDescription(new QualifiedName[] {FOO_PROPERTY, BAR_PROPERTY, ZOO_PROPERTY}, getContentType()); assertTrue("1.0", description.isRequested(FOO_PROPERTY));
diff --git a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/ContentTypeTest.java b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/ContentTypeTest.java index 342cb41..2941537 100644 --- a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/ContentTypeTest.java +++ b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/ContentTypeTest.java
@@ -13,23 +13,58 @@ *******************************************************************************/ package org.eclipse.core.tests.resources.content; +import java.io.ByteArrayInputStream; +import java.io.InputStream; import org.eclipse.core.runtime.Platform; -import org.eclipse.core.tests.harness.CoreTest; import org.eclipse.core.tests.resources.AutomatedTests; import org.osgi.framework.BundleContext; /** * Common superclass for all content type tests. */ -public abstract class ContentTypeTest extends CoreTest { - public static final String PI_RESOURCES_TESTS = AutomatedTests.PI_RESOURCES_TESTS; +public abstract class ContentTypeTest { public static final String TEST_FILES_ROOT = "Plugin_Testing/"; - public ContentTypeTest(String name) { - super(name); + public BundleContext getContext() { + return Platform.getBundle(AutomatedTests.PI_RESOURCES_TESTS).getBundleContext(); } - public BundleContext getContext() { - return Platform.getBundle(PI_RESOURCES_TESTS).getBundleContext(); + /** + * Return an input stream with some random text to use as contents for a file + * resource. + */ + public InputStream getRandomContents() { + return new ByteArrayInputStream(getRandomString().getBytes()); } + + /** + * Return String with some random text to use as contents for a file resource. + */ + public String getRandomString() { + switch ((int) Math.round(Math.random() * 10)) { + case 0: + return "este e' o meu conteudo (portuguese)"; + case 1: + return "ho ho ho"; + case 2: + return "I'll be back"; + case 3: + return "don't worry, be happy"; + case 4: + return "there is no imagination for more sentences"; + case 5: + return "Alexandre Bilodeau, Canada's first home gold. 14/02/2010"; + case 6: + return "foo"; + case 7: + return "bar"; + case 8: + return "foobar"; + case 9: + return "case 9"; + default: + return "these are my contents"; + } + } + }
diff --git a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/IContentTypeManagerTest.java b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/IContentTypeManagerTest.java index cd558c6..bdd70db 100644 --- a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/IContentTypeManagerTest.java +++ b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/IContentTypeManagerTest.java
@@ -14,12 +14,21 @@ *******************************************************************************/ package org.eclipse.core.tests.resources.content; +import static org.eclipse.core.tests.resources.AutomatedTests.PI_RESOURCES_TESTS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + import java.io.*; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.HashSet; import java.util.Set; -import junit.framework.AssertionFailedError; import org.eclipse.core.internal.content.*; import org.eclipse.core.runtime.*; import org.eclipse.core.runtime.content.*; @@ -27,8 +36,9 @@ import org.eclipse.core.runtime.content.XMLContentDescriber; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.core.runtime.preferences.*; -import org.eclipse.core.tests.harness.BundleTestingHelper; -import org.eclipse.core.tests.harness.TestRegistryChangeListener; +import org.eclipse.core.tests.harness.*; +import org.junit.After; +import org.junit.Test; import org.osgi.service.prefs.BackingStoreException; import org.osgi.service.prefs.Preferences; @@ -59,9 +69,9 @@ private final static String MINIMAL_XML = "<?xml version=\"1.0\"?><org.eclipse.core.resources.tests.root/>"; private final static String SAMPLE_BIN1_OFFSET = "12345"; - private final static byte[] SAMPLE_BIN1_SIGNATURE = {0x10, (byte) 0xAB, (byte) 0xCD, (byte) 0xFF}; + private final static byte[] SAMPLE_BIN1_SIGNATURE = { 0x10, (byte) 0xAB, (byte) 0xCD, (byte) 0xFF }; private final static String SAMPLE_BIN2_OFFSET = ""; - private final static byte[] SAMPLE_BIN2_SIGNATURE = {0x10, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF}; + private final static byte[] SAMPLE_BIN2_SIGNATURE = { 0x10, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF }; private final static String XML_DTD_EXTERNAL_ENTITY = "<?xml version=\"1.0\"?><!DOCTYPE project SYSTEM \"org.eclipse.core.resources.tests.some.dtd\" [<!ENTITY someentity SYSTEM \"someentity.xml\">]><org.eclipse.core.resources.tests.root/>"; private final static String XML_DTD_US_ASCII = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?><!DOCTYPE sometype SYSTEM \"org.eclipse.core.resources.tests.some.dtd\"><org.eclipse.core.resources.tests.root/>"; private final static String XML_ISO_8859_1 = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><org.eclipse.core.resources.tests.root/>"; @@ -87,10 +97,6 @@ private final static String XML_ROOT_ELEMENT_NS_WILDCARD2 = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?><!DOCTYPE Joker SYSTEM \"org.eclipse.core.resources.tests.some.dtd3\"><Joker/>"; private final static String XML_ROOT_ELEMENT_EMPTY_NS = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?><!DOCTYPE Joker SYSTEM \"org.eclipse.core.resources.tests.some.dtd3\"><rootElement>"; - public IContentTypeManagerTest(String name) { - super(name); - } - /** * Helps to ensure we don't get fooled by case sensitivity in file names/specs. */ @@ -112,8 +118,10 @@ return false; } - private IContentDescription getDescriptionFor(IContentTypeMatcher finder, String contents, Charset encoding, String fileName, QualifiedName[] options, boolean text) throws IOException { - return text ? finder.getDescriptionFor(getReader(contents), fileName, options) : finder.getDescriptionFor(getInputStream(contents, encoding), fileName, options); + private IContentDescription getDescriptionFor(IContentTypeMatcher finder, String contents, Charset encoding, + String fileName, QualifiedName[] options, boolean text) throws IOException { + return text ? finder.getDescriptionFor(getReader(contents), fileName, options) + : finder.getDescriptionFor(getInputStream(contents, encoding), fileName, options); } public InputStream getInputStream(byte[][] contents) { @@ -150,17 +158,18 @@ return candidate.isKindOf(text); } - @Override - protected void tearDown() throws Exception { - super.tearDown(); - // some tests here will trigger a charset delta job (any causing ContentTypeChangeEvents to be broadcast) + @After + public void tearDown() throws Exception { + // some tests here will trigger a charset delta job (any causing + // ContentTypeChangeEvents to be broadcast) // ensure none is left running after we finish - Job.getJobManager().join(FAMILY_CHARSET_DELTA, getMonitor()); + Job.getJobManager().join(FAMILY_CHARSET_DELTA, new FussyProgressMonitor()); } /** * This test shows how we deal with aliases. */ + @Test public void testAlias() throws IOException { final IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); IContentType alias = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".alias"); @@ -179,8 +188,9 @@ assertEquals("1.5", alias, selected[0]); assertEquals("1.6", derived, selected[1]); - //test late addition of content type - TestRegistryChangeListener listener = new TestRegistryChangeListener(Platform.PI_RUNTIME, ContentTypeBuilder.PT_CONTENTTYPES, null, null); + // test late addition of content type + TestRegistryChangeListener listener = new TestRegistryChangeListener(Platform.PI_RUNTIME, + ContentTypeBuilder.PT_CONTENTTYPES, null, null); BundleTestingHelper.runWithBundles("2", () -> { IContentType alias1 = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".alias"); assertNull("2.1.1", alias1); @@ -193,17 +203,19 @@ assertEquals("2.2.1", 2, selected1.length); assertEquals("2.2.2", target1, selected1[0]); assertEquals("2.2.3", derived1, selected1[1]); + IContentType[] selected2; try { - selected1 = contentTypeManager.findContentTypesFor(getRandomContents(), "foo.missing-target"); + selected2 = contentTypeManager.findContentTypesFor(getRandomContents(), "foo.missing-target"); } catch (IOException e) { - fail("2.2.4", e); + throw new AssertionError(e); } - assertEquals("2.2.5", 2, selected1.length); - assertEquals("2.2.6", target1, selected1[0]); - assertEquals("2.2.7", derived1, selected1[1]); - }, getContext(), new String[] {ContentTypeTest.TEST_FILES_ROOT + "content/bundle02"}, listener); + assertEquals("2.2.5", 2, selected2.length); + assertEquals("2.2.6", target1, selected2[0]); + assertEquals("2.2.7", derived1, selected2[1]); + }, getContext(), new String[] { ContentTypeTest.TEST_FILES_ROOT + "content/bundle02" }, listener); } + @Test public void testAssociationInheritance() throws CoreException { IContentTypeManager manager = Platform.getContentTypeManager(); IContentTypeMatcher finder = manager.getMatcher(new LocalSelectionPolicy(), null); @@ -270,6 +282,7 @@ assertEquals("10.1", assoc2, selected[0]); } + @Test public void testAssociations() throws CoreException { IContentType text = Platform.getContentTypeManager().getContentType(Platform.PI_RUNTIME + ".text"); @@ -282,20 +295,23 @@ assertTrue("0.3", text.isAssociatedWith(changeCase("text.txt_pluginadded"))); // check provider defined settings - String[] providerDefinedExtensions = text.getFileSpecs(IContentType.FILE_EXTENSION_SPEC | IContentType.IGNORE_USER_DEFINED); + String[] providerDefinedExtensions = text + .getFileSpecs(IContentType.FILE_EXTENSION_SPEC | IContentType.IGNORE_USER_DEFINED); assertTrue("1.0", contains(providerDefinedExtensions, "txt")); assertFalse("1.1", contains(providerDefinedExtensions, "txt_useradded")); assertTrue("1.2", contains(providerDefinedExtensions, "txt_pluginadded")); // check user defined settings - String[] textUserDefinedExtensions = text.getFileSpecs(IContentType.FILE_EXTENSION_SPEC | IContentType.IGNORE_PRE_DEFINED); + String[] textUserDefinedExtensions = text + .getFileSpecs(IContentType.FILE_EXTENSION_SPEC | IContentType.IGNORE_PRE_DEFINED); assertFalse("2.0", contains(textUserDefinedExtensions, "txt")); assertTrue("2.1", contains(textUserDefinedExtensions, "txt_useradded")); assertFalse("2.2", contains(textUserDefinedExtensions, "txt_pluginadded")); // 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.0", contains( + text.getFileSpecs(IContentType.FILE_EXTENSION_SPEC | IContentType.IGNORE_USER_DEFINED), "txt")); assertTrue("3.1", text.isAssociatedWith(changeCase("text.txt"))); assertTrue("3.2", text.isAssociatedWith(changeCase("text.txt_useradded"))); assertTrue("3.3", text.isAssociatedWith(changeCase("text.txt_pluginadded"))); @@ -309,18 +325,21 @@ assertTrue("4.3", text.isAssociatedWith(changeCase("text.txt_pluginadded"))); } + @Test public void testBinaryTypes() throws IOException { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); IContentType sampleBinary1 = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".sample-binary1"); IContentType sampleBinary2 = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".sample-binary2"); InputStream contents; - contents = getInputStream(new byte[][] {SAMPLE_BIN1_OFFSET.getBytes(), SAMPLE_BIN1_SIGNATURE, " extra contents".getBytes()}); + contents = getInputStream( + new byte[][] { SAMPLE_BIN1_OFFSET.getBytes(), SAMPLE_BIN1_SIGNATURE, " extra contents".getBytes() }); IContentDescription description = contentTypeManager.getDescriptionFor(contents, null, IContentDescription.ALL); assertNotNull("6.0", description); assertEquals("6.1", sampleBinary1, description.getContentType()); - contents = getInputStream(new byte[][] {SAMPLE_BIN2_OFFSET.getBytes(), SAMPLE_BIN2_SIGNATURE, " extra contents".getBytes()}); + contents = getInputStream( + new byte[][] { SAMPLE_BIN2_OFFSET.getBytes(), SAMPLE_BIN2_SIGNATURE, " extra contents".getBytes() }); description = contentTypeManager.getDescriptionFor(contents, null, IContentDescription.ALL); assertNotNull("7.0", description); assertEquals("7.1", sampleBinary2, description.getContentType()); @@ -332,83 +351,108 @@ assertEquals("8.1", 1, selected.length); assertEquals("8.2", sampleBinary2.getId(), selected[0].getId()); // (we used to blow up here) - description = contentTypeManager.getDescriptionFor(getReader(getRandomString()), "test.samplebin2", IContentDescription.ALL); + description = contentTypeManager.getDescriptionFor(getReader(getRandomString()), "test.samplebin2", + IContentDescription.ALL); assertNull("8.3", description); } + @Test public void testByteOrderMark() throws IOException { IContentType text = Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT); - QualifiedName[] options = new QualifiedName[] {IContentDescription.BYTE_ORDER_MARK}; + QualifiedName[] options = new QualifiedName[] { IContentDescription.BYTE_ORDER_MARK }; IContentDescription description; // tests with UTF-8 BOM String UTF8_BOM = new String(IContentDescription.BOM_UTF_8, StandardCharsets.ISO_8859_1); - description = text.getDescriptionFor(new ByteArrayInputStream((UTF8_BOM + MINIMAL_XML).getBytes(StandardCharsets.ISO_8859_1)), options); + description = text.getDescriptionFor( + new ByteArrayInputStream((UTF8_BOM + MINIMAL_XML).getBytes(StandardCharsets.ISO_8859_1)), options); assertNotNull("1.0", description.getProperty(IContentDescription.BYTE_ORDER_MARK)); - assertEquals("1.1", IContentDescription.BOM_UTF_8, description.getProperty(IContentDescription.BYTE_ORDER_MARK)); + assertEquals("1.1", IContentDescription.BOM_UTF_8, + description.getProperty(IContentDescription.BYTE_ORDER_MARK)); // tests with UTF-16 Little Endian BOM String UTF16_LE_BOM = new String(IContentDescription.BOM_UTF_16LE, StandardCharsets.ISO_8859_1); - description = text.getDescriptionFor(new ByteArrayInputStream((UTF16_LE_BOM + MINIMAL_XML).getBytes(StandardCharsets.ISO_8859_1)), options); + description = text.getDescriptionFor( + new ByteArrayInputStream((UTF16_LE_BOM + MINIMAL_XML).getBytes(StandardCharsets.ISO_8859_1)), options); assertNotNull("2.0", description.getProperty(IContentDescription.BYTE_ORDER_MARK)); - assertEquals("2.1", IContentDescription.BOM_UTF_16LE, description.getProperty(IContentDescription.BYTE_ORDER_MARK)); + assertEquals("2.1", IContentDescription.BOM_UTF_16LE, + description.getProperty(IContentDescription.BYTE_ORDER_MARK)); // tests with UTF-16 Big Endian BOM String UTF16_BE_BOM = new String(IContentDescription.BOM_UTF_16BE, StandardCharsets.ISO_8859_1); - description = text.getDescriptionFor(new ByteArrayInputStream((UTF16_BE_BOM + MINIMAL_XML).getBytes(StandardCharsets.ISO_8859_1)), options); + description = text.getDescriptionFor( + new ByteArrayInputStream((UTF16_BE_BOM + MINIMAL_XML).getBytes(StandardCharsets.ISO_8859_1)), options); assertNotNull("3.0", description.getProperty(IContentDescription.BYTE_ORDER_MARK)); - assertEquals("3.1", IContentDescription.BOM_UTF_16BE, description.getProperty(IContentDescription.BYTE_ORDER_MARK)); + assertEquals("3.1", IContentDescription.BOM_UTF_16BE, + description.getProperty(IContentDescription.BYTE_ORDER_MARK)); // test with no BOM - description = text.getDescriptionFor(new ByteArrayInputStream(MINIMAL_XML.getBytes(StandardCharsets.ISO_8859_1)), options); + description = text.getDescriptionFor( + new ByteArrayInputStream(MINIMAL_XML.getBytes(StandardCharsets.ISO_8859_1)), options); assertNull("4.0", description.getProperty(IContentDescription.BYTE_ORDER_MARK)); // tests for partial BOM // first byte of UTF-16 Big Endian + minimal xml - String UTF16_BE_BOM_1byte = new String(new byte[] {(byte) 0xFE}, "ISO-8859-1"); - description = text.getDescriptionFor(new ByteArrayInputStream((UTF16_BE_BOM_1byte + MINIMAL_XML).getBytes(StandardCharsets.ISO_8859_1)), options); + String UTF16_BE_BOM_1byte = new String(new byte[] { (byte) 0xFE }, "ISO-8859-1"); + description = text.getDescriptionFor( + new ByteArrayInputStream((UTF16_BE_BOM_1byte + MINIMAL_XML).getBytes(StandardCharsets.ISO_8859_1)), + options); assertNull("5.0", description.getProperty(IContentDescription.BYTE_ORDER_MARK)); - // first byte of UTF-16 Big Endian only (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=199252) - description = text.getDescriptionFor(new ByteArrayInputStream((UTF16_BE_BOM_1byte).getBytes(StandardCharsets.ISO_8859_1)), options); + // first byte of UTF-16 Big Endian only (see + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=199252) + description = text.getDescriptionFor( + new ByteArrayInputStream((UTF16_BE_BOM_1byte).getBytes(StandardCharsets.ISO_8859_1)), options); assertNull("5.1", description.getProperty(IContentDescription.BYTE_ORDER_MARK)); // first byte of UTF-16 Little Endian + minimal xml - String UTF16_LE_BOM_1byte = new String(new byte[] {(byte) 0xFF}, StandardCharsets.ISO_8859_1); - description = text.getDescriptionFor(new ByteArrayInputStream((UTF16_LE_BOM_1byte + MINIMAL_XML).getBytes(StandardCharsets.ISO_8859_1)), options); + String UTF16_LE_BOM_1byte = new String(new byte[] { (byte) 0xFF }, StandardCharsets.ISO_8859_1); + description = text.getDescriptionFor( + new ByteArrayInputStream((UTF16_LE_BOM_1byte + MINIMAL_XML).getBytes(StandardCharsets.ISO_8859_1)), + options); assertNull("6.0", description.getProperty(IContentDescription.BYTE_ORDER_MARK)); // first byte of UTF-16 Little Endian only - description = text.getDescriptionFor(new ByteArrayInputStream((UTF16_LE_BOM_1byte).getBytes(StandardCharsets.ISO_8859_1)), options); + description = text.getDescriptionFor( + new ByteArrayInputStream((UTF16_LE_BOM_1byte).getBytes(StandardCharsets.ISO_8859_1)), options); assertNull("6.1", description.getProperty(IContentDescription.BYTE_ORDER_MARK)); // first byte of UTF-8 + minimal xml - String UTF8_BOM_1byte = new String(new byte[] {(byte) 0xEF}, StandardCharsets.ISO_8859_1); - description = text.getDescriptionFor(new ByteArrayInputStream((UTF8_BOM_1byte + MINIMAL_XML).getBytes(StandardCharsets.ISO_8859_1)), options); + String UTF8_BOM_1byte = new String(new byte[] { (byte) 0xEF }, StandardCharsets.ISO_8859_1); + description = text.getDescriptionFor( + new ByteArrayInputStream((UTF8_BOM_1byte + MINIMAL_XML).getBytes(StandardCharsets.ISO_8859_1)), + options); assertNull("7.0", description.getProperty(IContentDescription.BYTE_ORDER_MARK)); // first byte of UTF-8 only - description = text.getDescriptionFor(new ByteArrayInputStream((UTF8_BOM_1byte).getBytes(StandardCharsets.ISO_8859_1)), options); + description = text.getDescriptionFor( + new ByteArrayInputStream((UTF8_BOM_1byte).getBytes(StandardCharsets.ISO_8859_1)), options); assertNull("7.1", description.getProperty(IContentDescription.BYTE_ORDER_MARK)); // two first bytes of UTF-8 + minimal xml - String UTF8_BOM_2bytes = new String(new byte[] {(byte) 0xEF, (byte) 0xBB}, StandardCharsets.ISO_8859_1); - description = text.getDescriptionFor(new ByteArrayInputStream((UTF8_BOM_2bytes + MINIMAL_XML).getBytes(StandardCharsets.ISO_8859_1)), options); + String UTF8_BOM_2bytes = new String(new byte[] { (byte) 0xEF, (byte) 0xBB }, StandardCharsets.ISO_8859_1); + description = text.getDescriptionFor( + new ByteArrayInputStream((UTF8_BOM_2bytes + MINIMAL_XML).getBytes(StandardCharsets.ISO_8859_1)), + options); assertNull("8.0", description.getProperty(IContentDescription.BYTE_ORDER_MARK)); // two first bytes of UTF-8 only - description = text.getDescriptionFor(new ByteArrayInputStream((UTF8_BOM_2bytes).getBytes(StandardCharsets.ISO_8859_1)), options); + description = text.getDescriptionFor( + new ByteArrayInputStream((UTF8_BOM_2bytes).getBytes(StandardCharsets.ISO_8859_1)), options); assertNull("8.1", description.getProperty(IContentDescription.BYTE_ORDER_MARK)); } /** * See bug 90218. */ + @Test public void testContentAndNameMatching() throws IOException /* not really */ { IContentTypeManager manager = Platform.getContentTypeManager(); - byte[][] contents0 = {{0x0A, 0x0B, 0x0E, 0x10}}; - byte[][] contents1 = {{0x0A, 0x0B, 0x0C, 0x10}}; - byte[][] contents2 = {{0x0A, 0x0B, 0x0D, 0x10}}; - byte[][] invalidContents = {{0, 0, 0, 0}}; + byte[][] contents0 = { { 0x0A, 0x0B, 0x0E, 0x10 } }; + byte[][] contents1 = { { 0x0A, 0x0B, 0x0C, 0x10 } }; + byte[][] contents2 = { { 0x0A, 0x0B, 0x0D, 0x10 } }; + byte[][] invalidContents = { { 0, 0, 0, 0 } }; // base matches *.mybinary files starting with 0x0a 0x0b IContentType base = manager.getContentType(PI_RESOURCES_TESTS + ".binary_base"); - // derived1 matches *.mybinary and specifically foo.mybinary files starting with 0x0a 0x0b 0xc + // derived1 matches *.mybinary and specifically foo.mybinary files starting with + // 0x0a 0x0b 0xc IContentType derived1 = manager.getContentType(PI_RESOURCES_TESTS + ".binary_derived1"); - // derived2 matches *.mybinary (inherits filespec from base) files starting with 0x0a 0x0b 0xd + // derived2 matches *.mybinary (inherits filespec from base) files starting with + // 0x0a 0x0b 0xd IContentType derived2 = manager.getContentType(PI_RESOURCES_TESTS + ".binary_derived2"); IContentType[] selected; @@ -419,19 +463,22 @@ assertEquals("1.1", base, selected[0]); selected = manager.findContentTypesFor(getInputStream(contents0), "foo.mybinary"); - // we know also that the second one will be derived1, because it has a full name matching + // we know also that the second one will be derived1, because it has a full name + // matching assertEquals("2.0", 3, selected.length); assertEquals("2.1", base, selected[0]); assertEquals("2.2", derived1, selected[1]); selected = manager.findContentTypesFor(getInputStream(contents1), "foo.mybinary"); - // derived1 will be first because both base and derived1 have a strong content matching, so more specific wins + // derived1 will be first because both base and derived1 have a strong content + // matching, so more specific wins assertEquals("3.0", 3, selected.length); assertEquals("3.1", derived1, selected[0]); assertEquals("3.2", base, selected[1]); selected = manager.findContentTypesFor(getInputStream(contents2), "foo.mybinary"); - // same as 3.* - derived1 is last because content matching is weak, althoug name matching is strong + // same as 3.* - derived1 is last because content matching is weak, althoug name + // matching is strong assertEquals("4.0", 3, selected.length); assertEquals("4.1", derived2, selected[0]); assertEquals("4.2", base, selected[1]); @@ -450,7 +497,7 @@ /* * Tests both text and byte stream-based getDescriptionFor methods. - */ + */@Test public void testContentDescription() throws IOException, CoreException { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); IContentTypeMatcher finder = contentTypeManager.getMatcher(new LocalSelectionPolicy(), null); @@ -466,52 +513,61 @@ String sufix = text ? "-text" : "-binary"; IContentDescription description; - description = getDescriptionFor(finder, MINIMAL_XML, StandardCharsets.UTF_8, "foo.xml", IContentDescription.ALL, text); + description = getDescriptionFor(finder, MINIMAL_XML, StandardCharsets.UTF_8, "foo.xml", + IContentDescription.ALL, text); assertNotNull("1.0" + sufix, description); assertEquals("1.1" + sufix, xmlType, description.getContentType()); assertSame("1.2", xmlType.getDefaultDescription(), description); - description = getDescriptionFor(finder, MINIMAL_XML, StandardCharsets.UTF_8, "foo.xml", new QualifiedName[] {IContentDescription.CHARSET}, text); + description = getDescriptionFor(finder, MINIMAL_XML, StandardCharsets.UTF_8, "foo.xml", + new QualifiedName[] { IContentDescription.CHARSET }, text); assertNotNull("2.0" + sufix, description); assertEquals("2.1" + sufix, xmlType, description.getContentType()); // the default charset should have been filled by the content type manager assertEquals("2.2" + sufix, "UTF-8", description.getProperty(IContentDescription.CHARSET)); assertSame("2.3", xmlType.getDefaultDescription(), description); - description = getDescriptionFor(finder, XML_ISO_8859_1, StandardCharsets.ISO_8859_1, "foo.xml", new QualifiedName[] {IContentDescription.CHARSET}, text); + description = getDescriptionFor(finder, XML_ISO_8859_1, StandardCharsets.ISO_8859_1, "foo.xml", + new QualifiedName[] { IContentDescription.CHARSET }, text); assertNotNull("2.3a" + sufix, description); assertEquals("2.3b" + sufix, xmlType, description.getContentType()); assertEquals("2.3c" + sufix, "ISO-8859-1", description.getProperty(IContentDescription.CHARSET)); assertNotSame("2.3d", xmlType.getDefaultDescription(), description); // ensure we handle single quotes properly (bug 65443) - description = getDescriptionFor(finder, XML_ISO_8859_1_SINGLE_QUOTES, StandardCharsets.ISO_8859_1, "foo.xml", new QualifiedName[] {IContentDescription.CHARSET}, text); + description = getDescriptionFor(finder, XML_ISO_8859_1_SINGLE_QUOTES, StandardCharsets.ISO_8859_1, + "foo.xml", new QualifiedName[] { IContentDescription.CHARSET }, text); assertNotNull("2.3e" + sufix, description); assertEquals("2.3f" + sufix, xmlType, description.getContentType()); assertEquals("2.3g" + sufix, "ISO-8859-1", description.getProperty(IContentDescription.CHARSET)); assertNotSame("2.3h", xmlType.getDefaultDescription(), description); - description = getDescriptionFor(finder, XML_UTF_16, StandardCharsets.UTF_16, "foo.xml", new QualifiedName[] {IContentDescription.CHARSET, IContentDescription.BYTE_ORDER_MARK}, text); + description = getDescriptionFor(finder, XML_UTF_16, StandardCharsets.UTF_16, "foo.xml", + new QualifiedName[] { IContentDescription.CHARSET, IContentDescription.BYTE_ORDER_MARK }, text); assertNotNull("2.4a" + sufix, description); assertEquals("2.4b" + sufix, xmlType, description.getContentType()); assertEquals("2.4c" + sufix, "UTF-16", description.getProperty(IContentDescription.CHARSET)); - assertTrue("2.4d" + sufix, text || IContentDescription.BOM_UTF_16BE == description.getProperty(IContentDescription.BYTE_ORDER_MARK)); + assertTrue("2.4d" + sufix, text || IContentDescription.BOM_UTF_16BE == description + .getProperty(IContentDescription.BYTE_ORDER_MARK)); assertNotSame("2.4e", xmlType.getDefaultDescription(), description); - description = getDescriptionFor(finder, XML_UTF_16BE, StandardCharsets.UTF_8, "foo.xml", new QualifiedName[] {IContentDescription.CHARSET}, text); + description = getDescriptionFor(finder, XML_UTF_16BE, StandardCharsets.UTF_8, "foo.xml", + new QualifiedName[] { IContentDescription.CHARSET }, text); assertNotNull("2.5a" + sufix, description); assertEquals("2.5b" + sufix, xmlType, description.getContentType()); assertEquals("2.5c" + sufix, "UTF-16BE", description.getProperty(IContentDescription.CHARSET)); assertNotSame("2.5d", xmlType.getDefaultDescription(), description); - description = getDescriptionFor(finder, XML_UTF_16LE, StandardCharsets.UTF_8, "foo.xml", new QualifiedName[] {IContentDescription.CHARSET}, text); + description = getDescriptionFor(finder, XML_UTF_16LE, StandardCharsets.UTF_8, "foo.xml", + new QualifiedName[] { IContentDescription.CHARSET }, text); assertNotNull("2.6a" + sufix, description); assertEquals("2.6b" + sufix, xmlType, description.getContentType()); // the default charset should have been filled by the content type manager assertEquals("2.6c" + sufix, "UTF-16LE", description.getProperty(IContentDescription.CHARSET)); assertNotSame("2.6d", xmlType.getDefaultDescription(), description); - description = getDescriptionFor(finder, MINIMAL_XML, StandardCharsets.UTF_8, "foo.xml", IContentDescription.ALL, text); + description = getDescriptionFor(finder, MINIMAL_XML, StandardCharsets.UTF_8, "foo.xml", + IContentDescription.ALL, text); assertNotNull("4.0" + sufix, description); assertEquals("4.1" + sufix, xmlType, description.getContentType()); assertEquals("4.2" + sufix, "UTF-8", description.getProperty(IContentDescription.CHARSET)); @@ -541,7 +597,9 @@ assertSame("5.13", mytext.getDefaultDescription(), description); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=176354 - description = getDescriptionFor(finder, "<?xml version=\'1.0\' encoding=\'UTF-8\'?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tns=\"http://www.example.org/\" xmlns:ns0=\"http://another.example.org/\"><soapenv:Header /><soapenv:Body><ns0:x /></soapenv:Body></soapenv:Envelope>", StandardCharsets.UTF_8, "foo.xml", new QualifiedName[] {IContentDescription.CHARSET}, text); + description = getDescriptionFor(finder, + "<?xml version=\'1.0\' encoding=\'UTF-8\'?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tns=\"http://www.example.org/\" xmlns:ns0=\"http://another.example.org/\"><soapenv:Header /><soapenv:Body><ns0:x /></soapenv:Body></soapenv:Envelope>", + StandardCharsets.UTF_8, "foo.xml", new QualifiedName[] { IContentDescription.CHARSET }, text); assertNotNull("5.14" + sufix, description); assertEquals("5.15" + sufix, xmlType, description.getContentType()); assertEquals("5.16" + sufix, "UTF-8", description.getProperty(IContentDescription.CHARSET)); @@ -554,50 +612,70 @@ } - /** - * @see IContentTypeMatcher#findContentTypeFor - */ + @Test public void testContentDetection() throws IOException { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); IContentTypeMatcher finder; IContentType inappropriate = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".sample-binary1"); IContentType appropriate = contentTypeManager.getContentType(Platform.PI_RUNTIME + ".xml"); - IContentType appropriateSpecific1 = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".xml-based-different-extension"); - IContentType appropriateSpecific1LowPriority = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".xml-based-different-extension-low-priority"); - IContentType appropriateSpecific2 = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".xml-based-specific-name"); + IContentType appropriateSpecific1 = contentTypeManager + .getContentType(PI_RESOURCES_TESTS + ".xml-based-different-extension"); + IContentType appropriateSpecific1LowPriority = contentTypeManager + .getContentType(PI_RESOURCES_TESTS + ".xml-based-different-extension-low-priority"); + IContentType appropriateSpecific2 = contentTypeManager + .getContentType(PI_RESOURCES_TESTS + ".xml-based-specific-name"); // if only inappropriate is provided, none will be selected - finder = contentTypeManager.getMatcher(new SubsetSelectionPolicy(new IContentType[] {inappropriate}), null); + finder = contentTypeManager.getMatcher(new SubsetSelectionPolicy(new IContentType[] { inappropriate }), null); assertNull("1.0", finder.findContentTypeFor(getInputStream(MINIMAL_XML), null)); // if inappropriate and appropriate are provided, appropriate will be selected - finder = contentTypeManager.getMatcher(new SubsetSelectionPolicy(new IContentType[] {inappropriate, appropriate}), null); + finder = contentTypeManager + .getMatcher(new SubsetSelectionPolicy(new IContentType[] { inappropriate, appropriate }), null); assertEquals("2.0", appropriate, finder.findContentTypeFor(getInputStream(MINIMAL_XML), null)); - // if inappropriate, appropriate and a more specific appropriate type are provided, the specific type will be selected - finder = contentTypeManager.getMatcher(new SubsetSelectionPolicy(new IContentType[] {inappropriate, appropriate, appropriateSpecific1}), null); + // if inappropriate, appropriate and a more specific appropriate type are + // provided, the specific type will be selected + finder = contentTypeManager.getMatcher( + new SubsetSelectionPolicy(new IContentType[] { inappropriate, appropriate, appropriateSpecific1 }), + null); assertEquals("3.0", appropriateSpecific1, finder.findContentTypeFor(getInputStream(MINIMAL_XML), null)); - finder = contentTypeManager.getMatcher(new SubsetSelectionPolicy(new IContentType[] {inappropriate, appropriate, appropriateSpecific2}), null); + finder = contentTypeManager.getMatcher( + new SubsetSelectionPolicy(new IContentType[] { inappropriate, appropriate, appropriateSpecific2 }), + null); assertEquals("3.1", appropriateSpecific2, finder.findContentTypeFor(getInputStream(MINIMAL_XML), null)); - // if all are provided, the more specific types will appear before the more generic types - finder = contentTypeManager.getMatcher(new SubsetSelectionPolicy(new IContentType[] {inappropriate, appropriate, appropriateSpecific1, appropriateSpecific2}), null); + // if all are provided, the more specific types will appear before the more + // generic types + finder = contentTypeManager.getMatcher( + new SubsetSelectionPolicy( + new IContentType[] { inappropriate, appropriate, appropriateSpecific1, appropriateSpecific2 }), + null); IContentType[] selected = finder.findContentTypesFor(getInputStream(MINIMAL_XML), null); assertEquals("4.0", 3, selected.length); assertTrue("4.1", appropriateSpecific1.equals(selected[0]) || appropriateSpecific1.equals(selected[1])); assertTrue("4.2", appropriateSpecific2.equals(selected[0]) || appropriateSpecific2.equals(selected[1])); assertTrue("4.3", appropriate.equals(selected[2])); - // if appropriate and a more specific appropriate type (but with low priority) are provided, the specific type will be selected - finder = contentTypeManager.getMatcher(new SubsetSelectionPolicy(new IContentType[] {appropriate, appropriateSpecific1LowPriority}), null); - assertEquals("5.0", appropriateSpecific1LowPriority, finder.findContentTypeFor(getInputStream(MINIMAL_XML), null)); + // if appropriate and a more specific appropriate type (but with low priority) + // are provided, the specific type will be selected + finder = contentTypeManager.getMatcher( + new SubsetSelectionPolicy(new IContentType[] { appropriate, appropriateSpecific1LowPriority }), null); + assertEquals("5.0", appropriateSpecific1LowPriority, + finder.findContentTypeFor(getInputStream(MINIMAL_XML), null)); - // if appropriate and two specific appropriate types (but one with lower priority) are provided, the specific type with higher priority will be selected - finder = contentTypeManager.getMatcher(new SubsetSelectionPolicy(new IContentType[] {appropriate, appropriateSpecific1, appropriateSpecific1LowPriority}), null); + // if appropriate and two specific appropriate types (but one with lower + // priority) are provided, the specific type with higher priority will be + // selected + finder = contentTypeManager.getMatcher( + new SubsetSelectionPolicy( + new IContentType[] { appropriate, appropriateSpecific1, appropriateSpecific1LowPriority }), + null); assertEquals("5.1", appropriateSpecific1, finder.findContentTypeFor(getInputStream(MINIMAL_XML), null)); } + @Test public void testDefaultProperties() throws IOException /* never actually thrown */ { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); @@ -647,11 +725,12 @@ } /** - * The fooBar content type is associated with the "foo.bar" file name and - * the "bar" file extension (what is bogus, anyway). This test ensures it - * does not appear twice in the list of content types associated with the - * "foo.bar" file name. + * The fooBar content type is associated with the "foo.bar" file name and the + * "bar" file extension (what is bogus, anyway). This test ensures it does not + * appear twice in the list of content types associated with the "foo.bar" file + * name. */ + @Test public void testDoubleAssociation() { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); @@ -668,12 +747,14 @@ } /** - * Obtains a reference to a known content type, then installs a bundle that contributes a content type, - * and makes sure a new obtained reference to the same content type is not identical (shows - * that the content type catalog has been discarded and rebuilt). Then uninstalls that bundle - * and checks again the same thing (because the content type catalog should be rebuilt whenever + * Obtains a reference to a known content type, then installs a bundle that + * contributes a content type, and makes sure a new obtained reference to the + * same content type is not identical (shows that the content type catalog has + * been discarded and rebuilt). Then uninstalls that bundle and checks again the + * same thing (because the content type catalog should be rebuilt whenever * content types are dynamicaly added/removed). */ + @Test public void testDynamicChanges() { final IContentType[] text = new IContentType[4]; final IContentTypeManager manager = Platform.getContentTypeManager(); @@ -685,8 +766,9 @@ text[1] = ((ContentTypeHandler) text[1]).getTarget(); assertEquals("2.0", text[0], text[1]); assertEquals("2.1", text[0], text[1]); - // make arbitrary dynamic changes to the contentTypes extension point - TestRegistryChangeListener listener = new TestRegistryChangeListener(Platform.PI_RUNTIME, ContentTypeBuilder.PT_CONTENTTYPES, null, null); + // make arbitrary dynamic changes to the contentTypes extension point + TestRegistryChangeListener listener = new TestRegistryChangeListener(Platform.PI_RUNTIME, + ContentTypeBuilder.PT_CONTENTTYPES, null, null); BundleTestingHelper.runWithBundles("3", () -> { IContentType missing = manager.getContentType("org.eclipse.bundle01.missing"); assertNotNull("3.1", missing); @@ -696,7 +778,7 @@ text[2] = ((ContentTypeHandler) text[2]).getTarget(); assertEquals("3.3", text[0], text[2]); assertNotSame("3.4", text[0], text[2]); - }, getContext(), new String[] {ContentTypeTest.TEST_FILES_ROOT + "content/bundle01"}, listener); + }, getContext(), new String[] { ContentTypeTest.TEST_FILES_ROOT + "content/bundle01" }, listener); assertNull("4.0", manager.getContentType("org.eclipse.bundle01.missing")); // ensure the content type instances are all different text[3] = manager.getContentType(IContentTypeManager.CT_TEXT); @@ -709,8 +791,10 @@ } /** - * Similar to testDynamicChanges, but using the org.eclipse.core.contenttype.contentTypes extension point. + * Similar to testDynamicChanges, but using the + * org.eclipse.core.contenttype.contentTypes extension point. */ + @Test public void testDynamicChangesNewExtension() { final IContentType[] text = new IContentType[4]; final IContentTypeManager manager = Platform.getContentTypeManager(); @@ -722,8 +806,9 @@ text[1] = ((ContentTypeHandler) text[1]).getTarget(); assertEquals("2.0", text[0], text[1]); assertSame("2.1", text[0], text[1]); - // make arbitrary dynamic changes to the contentTypes extension point - TestRegistryChangeListener listener = new TestRegistryChangeListener(IContentConstants.CONTENT_NAME, ContentTypeBuilder.PT_CONTENTTYPES, null, null); + // make arbitrary dynamic changes to the contentTypes extension point + TestRegistryChangeListener listener = new TestRegistryChangeListener(IContentConstants.CONTENT_NAME, + ContentTypeBuilder.PT_CONTENTTYPES, null, null); BundleTestingHelper.runWithBundles("3", () -> { IContentType contentType = manager.getContentType("org.eclipse.bug485227.bug485227_contentType"); assertNotNull("3.1 Contributed content type not found", contentType); @@ -733,10 +818,13 @@ text[2] = ((ContentTypeHandler) text[2]).getTarget(); assertEquals("3.3", text[0], text[2]); assertNotSame("3.4", text[0], text[2]); - assertEquals("3.5 default extension not associated", contentType, manager.findContentTypeFor("file.bug485227")); - assertEquals("3.6 additional extension not associated", contentType, manager.findContentTypeFor("file.bug485227_2")); - }, getContext(), new String[] {ContentTypeTest.TEST_FILES_ROOT + "content/bug485227"}, listener); - assertNull("4.0 Content type not cleared after bundle uninstall", manager.getContentType("org.eclipse.bug485227.bug485227_contentType")); + assertEquals("3.5 default extension not associated", contentType, + manager.findContentTypeFor("file.bug485227")); + assertEquals("3.6 additional extension not associated", contentType, + manager.findContentTypeFor("file.bug485227_2")); + }, getContext(), new String[] { ContentTypeTest.TEST_FILES_ROOT + "content/bug485227" }, listener); + assertNull("4.0 Content type not cleared after bundle uninstall", + manager.getContentType("org.eclipse.bug485227.bug485227_contentType")); // ensure the content type instances are all different text[3] = manager.getContentType(IContentTypeManager.CT_TEXT); assertNotNull("5.0", text[3]); @@ -747,7 +835,8 @@ assertNotSame("5.4", text[2], text[3]); } - public void testEvents() { + @Test + public void testEvents() throws CoreException { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); IContentType myType = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".myContent"); assertNotNull("0.9", myType); @@ -758,66 +847,39 @@ contentTypeManager.addContentTypeChangeListener(tracer); // add a file spec and check event - try { - myType.addFileSpec("another.file.name", IContentType.FILE_NAME_SPEC); - } catch (CoreException e) { - fail("1.0", e); - } + myType.addFileSpec("another.file.name", IContentType.FILE_NAME_SPEC); assertTrue("1.1", tracer.isOnlyChange(myType)); // remove a non-existing file spec - should not cause an event to be fired tracer.reset(); - try { - myType.removeFileSpec("another.file.name", IContentType.FILE_EXTENSION_SPEC); - } catch (CoreException e) { - fail("2.0", e); - } + myType.removeFileSpec("another.file.name", IContentType.FILE_EXTENSION_SPEC); assertTrue("2.1", !tracer.isOnlyChange(myType)); // add a file spec again and check no event is generated tracer.reset(); - try { - myType.addFileSpec("another.file.name", IContentType.FILE_NAME_SPEC); - } catch (CoreException e) { - fail("3.0", e); - } + myType.addFileSpec("another.file.name", IContentType.FILE_NAME_SPEC); assertTrue("3.1", !tracer.isOnlyChange(myType)); // remove a file spec and check event tracer.reset(); - try { - myType.removeFileSpec("another.file.name", IContentType.FILE_NAME_SPEC); - } catch (CoreException e) { - fail("4.0", e); - } + myType.removeFileSpec("another.file.name", IContentType.FILE_NAME_SPEC); assertTrue("4.1", tracer.isOnlyChange(myType)); // change the default charset and check event tracer.reset(); - try { - myType.setDefaultCharset("FOO"); - } catch (CoreException e) { - fail("5.0", e); - } + myType.setDefaultCharset("FOO"); assertTrue("5.1", tracer.isOnlyChange(myType)); // set the default charset to the same - no event should be generated tracer.reset(); - try { - myType.setDefaultCharset("FOO"); - } catch (CoreException e) { - fail("6.0", e); - } + myType.setDefaultCharset("FOO"); assertTrue("6.1", !tracer.isOnlyChange(myType)); - try { - myType.setDefaultCharset("ABC"); - } catch (CoreException e1) { - fail("7.0", e1); - } + myType.setDefaultCharset("ABC"); } - public void testFileSpecConflicts() { + @Test + public void testFileSpecConflicts() throws IOException { IContentTypeManager manager = Platform.getContentTypeManager(); // when not submitting contents, for related types, most general type prevails IContentType conflict1a = manager.getContentType(PI_RESOURCES_TESTS + ".base_conflict1"); @@ -833,15 +895,11 @@ assertNotNull("2.0", conflict2base); assertNotNull("2.1", conflict2sub); // when submitting contents, for related types, descendant comes first - IContentType[] selectedConflict2; - try { - selectedConflict2 = manager.findContentTypesFor(getRandomContents(), "test.conflict2"); - assertEquals("2.2", 2, selectedConflict2.length); - assertEquals("2.3", selectedConflict2[0], conflict2base); - assertEquals("2.4", selectedConflict2[1], conflict2sub); - } catch (IOException e) { - fail("2.5", e); - } + + IContentType[] selectedConflict2 = manager.findContentTypesFor(getRandomContents(), "test.conflict2"); + assertEquals("2.2", 2, selectedConflict2.length); + assertEquals("2.3", selectedConflict2[0], conflict2base); + assertEquals("2.4", selectedConflict2[1], conflict2sub); IContentType conflict3base = manager.getContentType(PI_RESOURCES_TESTS + ".base_conflict3"); IContentType conflict3sub = manager.getContentType(PI_RESOURCES_TESTS + ".sub_conflict3"); @@ -851,19 +909,16 @@ assertNotNull("3.0.3", conflict3unrelated); // Two unrelated types (sub_conflict3 and unrelated conflict3) are in conflict. - // Order will be based on depth (more general first since they don't have describers) + // Order will be based on depth (more general first since they don't have + // describers) - IContentType[] selectedConflict3; - try { - selectedConflict3 = manager.findContentTypesFor(getRandomContents(), "test.conflict3"); - assertEquals("4.0", 2, selectedConflict3.length); - assertEquals("4.1", selectedConflict3[0], conflict3unrelated); - assertEquals("4.2", selectedConflict3[1], conflict3sub); - } catch (IOException e) { - fail("4.3", e); - } + IContentType[] selectedConflict3 = manager.findContentTypesFor(getRandomContents(), "test.conflict3"); + assertEquals("4.0", 2, selectedConflict3.length); + assertEquals("4.1", selectedConflict3[0], conflict3unrelated); + assertEquals("4.2", selectedConflict3[1], conflict3sub); } + @Test public void testFindContentType() throws IOException { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); IContentTypeMatcher finder = contentTypeManager.getMatcher(new LocalSelectionPolicy(), null); @@ -885,14 +940,17 @@ assertTrue("3.0", contains(multiple, xmlContentType)); } + @Test public void testFindContentTypPredefinedRegexp() throws IOException, CoreException { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); IContentTypeMatcher finder = contentTypeManager.getMatcher(new LocalSelectionPolicy(), null); - IContentType targetContentType = contentTypeManager.getContentType("org.eclipse.core.tests.resources.predefinedContentTypeWithRegexp"); + IContentType targetContentType = contentTypeManager + .getContentType("org.eclipse.core.tests.resources.predefinedContentTypeWithRegexp"); assertNotNull("Target content-type not found", targetContentType); - IContentType single = finder.findContentTypeFor(getInputStream("Just a test"), "somepredefinedContentTypeWithRegexpFile"); + IContentType single = finder.findContentTypeFor(getInputStream("Just a test"), + "somepredefinedContentTypeWithRegexpFile"); assertEquals(targetContentType, single); single = finder.findContentTypeFor(getInputStream("Just a test"), "somepredefinedContentTypeWithPatternFile"); assertEquals(targetContentType, single); @@ -900,6 +958,7 @@ assertEquals(targetContentType, single); } + @Test public void testFindContentTypeUserRegexp() throws IOException, CoreException { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); IContentTypeMatcher finder = contentTypeManager.getMatcher(new LocalSelectionPolicy(), null); @@ -914,6 +973,7 @@ assertEquals("Text content should now match *Text* files", textContentType, single); } + @Test public void testImportFileAssociation() throws CoreException { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); assertNull(contentTypeManager.findContentTypeFor("*.bug122217")); @@ -924,13 +984,15 @@ assertNotNull(contentTypeManager.findContentTypeFor("*.bug122217")); } + @Test public void testInvalidMarkup() { final IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); IContentTypeMatcher finder = contentTypeManager.getMatcher(new LocalSelectionPolicy(), null); assertEquals("1.0", 0, finder.findContentTypesFor("invalid.missing.identifier").length); assertEquals("2.0", 0, finder.findContentTypesFor("invalid.missing.name").length); assertNull("3.0", contentTypeManager.getContentType(PI_RESOURCES_TESTS + '.' + "invalid-missing-name")); - TestRegistryChangeListener listener = new TestRegistryChangeListener(Platform.PI_RUNTIME, ContentTypeBuilder.PT_CONTENTTYPES, null, null); + TestRegistryChangeListener listener = new TestRegistryChangeListener(Platform.PI_RUNTIME, + ContentTypeBuilder.PT_CONTENTTYPES, null, null); BundleTestingHelper.runWithBundles("1", () -> { // ensure the invalid content types are not available assertEquals("1.2", 0, contentTypeManager.findContentTypesFor("invalid.missing.identifier").length); @@ -941,29 +1003,29 @@ assertNotNull("1.5", invalidDescriber); // name based matching should work fine assertEquals("1.6", invalidDescriber, contentTypeManager.findContentTypeFor("invalid.describer")); + // the describer class is invalid, content matchong should fail + IContentType nullContentType; try { - // the describer class is invalid, content matchong should fail - assertNull("1.7", contentTypeManager.findContentTypeFor(getRandomContents(), "invalid.describer")); + nullContentType = contentTypeManager.findContentTypeFor(getRandomContents(), "invalid.describer"); } catch (IOException e) { - fail("1.8", e); + throw new AssertionError(e); } - }, getContext(), new String[] {ContentTypeTest.TEST_FILES_ROOT + "content/bundle03"}, listener); + assertNull("1.7", nullContentType); + }, getContext(), new String[] { ContentTypeTest.TEST_FILES_ROOT + "content/bundle03" }, listener); } /** * Bugs 67841 and 62443 */ - public void testIOException() { + @Test + public void testIOException() throws IOException { IContentTypeManager manager = Platform.getContentTypeManager(); IContentType xml = manager.getContentType(Platform.PI_RUNTIME + ".xml"); IContentType rootElement = manager.getContentType(PI_RESOURCES_TESTS + ".root-element"); - IContentType[] selected = null; - try { - selected = manager.findContentTypesFor(getInputStream(XML_US_ASCII_INVALID, StandardCharsets.ISO_8859_1), "test.xml"); - } catch (IOException ioe) { - // a SAXException is usually caught (and silently ignored) in XMLRootElementDescriber in these cases - fail("1.0", ioe); - } + // a SAXException is usually caught (and silently ignored) in + // XMLRootElementDescriber in these cases + IContentType[] selected = manager + .findContentTypesFor(getInputStream(XML_US_ASCII_INVALID, StandardCharsets.ISO_8859_1), "test.xml"); assertTrue("1.1", contains(selected, xml)); assertTrue("1.2", contains(selected, rootElement)); @@ -979,41 +1041,35 @@ return "This exception was thrown for testing purposes"; } } - try { - selected = manager.findContentTypesFor(new InputStream() { + assertThrows(FakeIOException.class, () -> manager.findContentTypesFor(new InputStream() { - @Override - public int available() { - // trick the client into reading the file - return Integer.MAX_VALUE; - } + @Override + public int available() { + // trick the client into reading the file + return Integer.MAX_VALUE; + } - @Override - public int read() throws IOException { - throw new FakeIOException(); - } + @Override + public int read() throws IOException { + throw new FakeIOException(); + } - @Override - public int read(byte[] b, int off, int len) throws IOException { - throw new FakeIOException(); - } - }, "test.xml"); - // an exception will happen when reading the stream... should be thrown to the caller - fail("2.0"); - } catch (FakeIOException fioe) { - // sucess - } catch (IOException ioe) { - // this should never happen, but just in case... - fail("2.1"); - } + @Override + public int read(byte[] b, int off, int len) throws IOException { + throw new FakeIOException(); + } + }, "test.xml")); } + @Test public void testIsKindOf() { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); IContentType textContentType = contentTypeManager.getContentType(Platform.PI_RUNTIME + '.' + "text"); IContentType xmlContentType = contentTypeManager.getContentType(Platform.PI_RUNTIME + ".xml"); - IContentType xmlBasedDifferentExtensionContentType = contentTypeManager.getContentType(PI_RESOURCES_TESTS + '.' + "xml-based-different-extension"); - IContentType xmlBasedSpecificNameContentType = contentTypeManager.getContentType(PI_RESOURCES_TESTS + '.' + "xml-based-specific-name"); + IContentType xmlBasedDifferentExtensionContentType = contentTypeManager + .getContentType(PI_RESOURCES_TESTS + '.' + "xml-based-different-extension"); + IContentType xmlBasedSpecificNameContentType = contentTypeManager + .getContentType(PI_RESOURCES_TESTS + '.' + "xml-based-specific-name"); IContentType binaryContentType = contentTypeManager.getContentType(PI_RESOURCES_TESTS + '.' + "sample-binary1"); assertTrue("1.0", textContentType.isKindOf(textContentType)); assertTrue("2.0", xmlContentType.isKindOf(textContentType)); @@ -1025,6 +1081,7 @@ assertFalse("5.0", binaryContentType.isKindOf(textContentType)); } + @Test public void testListParsing() { String[] list; list = Util.parseItems(null); @@ -1080,6 +1137,7 @@ assertEquals("10.4", "", list[3]); } + @Test public void testMyContentDescriber() throws IOException { IContentTypeManager manager = Platform.getContentTypeManager(); IContentType myContent = manager.getContentType(PI_RESOURCES_TESTS + '.' + "myContent"); @@ -1088,23 +1146,28 @@ assertEquals("0.7", myContent, manager.findContentTypeFor("myContent.mc2")); assertEquals("0.8", myContent, manager.findContentTypeFor("foo.myContent1")); assertEquals("0.9", myContent, manager.findContentTypeFor("bar.myContent2")); - IContentDescription description = manager.getDescriptionFor(getInputStream(MyContentDescriber.SIGNATURE, StandardCharsets.US_ASCII), "myContent.mc1", IContentDescription.ALL); + IContentDescription description = manager.getDescriptionFor( + getInputStream(MyContentDescriber.SIGNATURE, StandardCharsets.US_ASCII), "myContent.mc1", + IContentDescription.ALL); assertNotNull("1.0", description); assertEquals("1.1", myContent, description.getContentType()); assertNotSame("1.2", myContent.getDefaultDescription(), description); for (int i = 0; i < MyContentDescriber.MY_OPTIONS.length; i++) { - assertEquals("2." + i, MyContentDescriber.MY_OPTION_VALUES[i], description.getProperty(MyContentDescriber.MY_OPTIONS[i])); + assertEquals("2." + i, MyContentDescriber.MY_OPTION_VALUES[i], + description.getProperty(MyContentDescriber.MY_OPTIONS[i])); } } + @Test public void testNoExtensionAssociation() { - //TODO use a IContentTypeMatcher instead + // TODO use a IContentTypeMatcher instead final IContentTypeManager manager = Platform.getContentTypeManager(); IContentType[] selected = manager.findContentTypesFor("file_with_no_extension"); assertEquals("0.1", 0, selected.length); - TestRegistryChangeListener listener = new TestRegistryChangeListener(Platform.PI_RUNTIME, ContentTypeBuilder.PT_CONTENTTYPES, null, null); + TestRegistryChangeListener listener = new TestRegistryChangeListener(Platform.PI_RUNTIME, + ContentTypeBuilder.PT_CONTENTTYPES, null, null); BundleTestingHelper.runWithBundles("1", () -> { final String namespace = "org.eclipse.bundle04"; @@ -1133,8 +1196,8 @@ try { nonEmpty.addFileSpec("", IContentType.FILE_EXTENSION_SPEC); - } catch (CoreException e1) { - fail("1.3.0", e1); + } catch (CoreException e) { + throw new AssertionError(e); } try { selected1 = manager.findContentTypesFor("file_with_no_extension"); @@ -1143,16 +1206,17 @@ } finally { try { nonEmpty.removeFileSpec("", IContentType.FILE_EXTENSION_SPEC); - } catch (CoreException e2) { - fail("1.3.3", e2); + } catch (CoreException e) { + throw new AssertionError(e); } } selected1 = manager.findContentTypesFor("file_with_no_extension"); assertEquals("1.4.0", 4, selected1.length); assertFalse("1.4.1", contains(selected1, nonEmpty)); - }, getContext(), new String[] {ContentTypeTest.TEST_FILES_ROOT + "content/bundle04"}, listener); + }, getContext(), new String[] { ContentTypeTest.TEST_FILES_ROOT + "content/bundle04" }, listener); } + @Test public void testOrderWithEmptyFiles() throws IOException { IContentTypeManager manager = Platform.getContentTypeManager(); IContentTypeMatcher finder = manager.getMatcher(new LocalSelectionPolicy(), null); @@ -1168,9 +1232,10 @@ } /** - * This test shows how we deal with orphan file associations (associations - * whose content types are missing). + * This test shows how we deal with orphan file associations (associations whose + * content types are missing). */ + @Test public void testOrphanContentType() { final IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); IContentType orphan = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".orphan"); @@ -1181,8 +1246,9 @@ assertEquals("1.2", 0, contentTypeManager.findContentTypesFor("orphan.orphan").length); assertEquals("1.3", 0, contentTypeManager.findContentTypesFor("foo.orphan2").length); - //test late addition of content type - orphan2 should become visible - TestRegistryChangeListener listener = new TestRegistryChangeListener(Platform.PI_RUNTIME, ContentTypeBuilder.PT_CONTENTTYPES, null, null); + // test late addition of content type - orphan2 should become visible + TestRegistryChangeListener listener = new TestRegistryChangeListener(Platform.PI_RUNTIME, + ContentTypeBuilder.PT_CONTENTTYPES, null, null); BundleTestingHelper.runWithBundles("2", () -> { IContentType orphan1 = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".orphan"); @@ -1197,16 +1263,18 @@ // check whether an orphan association was added to the dynamically added bundle assertEquals("2.7", 1, contentTypeManager.findContentTypesFor("foo.orphan2").length); assertEquals("2.8", missing1, contentTypeManager.findContentTypesFor("foo.orphan2")[0]); - }, getContext(), new String[] {ContentTypeTest.TEST_FILES_ROOT + "content/bundle01"}, listener); + }, getContext(), new String[] { ContentTypeTest.TEST_FILES_ROOT + "content/bundle01" }, listener); } /** * Regression test for bug 68894 */ + @Test public void testPreferences() throws CoreException, BackingStoreException { IContentTypeManager manager = Platform.getContentTypeManager(); IContentType text = manager.getContentType(IContentTypeManager.CT_TEXT); - Preferences textPrefs = InstanceScope.INSTANCE.getNode(ContentTypeManager.CONTENT_TYPE_PREF_NODE).node(text.getId()); + Preferences textPrefs = InstanceScope.INSTANCE.getNode(ContentTypeManager.CONTENT_TYPE_PREF_NODE) + .node(text.getId()); assertNotNull("0.1", text); // ensure the "default charset" preference is being properly used @@ -1223,7 +1291,7 @@ assertFalse("2.01", text.isAssociatedWith("xyz.bar")); assertFalse("2.03", text.isAssociatedWith("foo.ext")); assertFalse("2.04", text.isAssociatedWith("bar.ext")); - //Null entries first to avoid interference from other tests + // Null entries first to avoid interference from other tests textPrefs.remove(ContentType.PREF_FILE_NAMES); textPrefs.remove(ContentType.PREF_FILE_EXTENSIONS); // play with file name associations first... @@ -1264,6 +1332,7 @@ } } + @Test public void testRegistry() { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); IContentTypeMatcher finder = contentTypeManager.getMatcher(new LocalSelectionPolicy(), null); @@ -1281,12 +1350,14 @@ assertNotNull("2.3", xmlDescriber); assertTrue("2.4", xmlDescriber instanceof XMLContentDescriber); - IContentType xmlBasedDifferentExtensionContentType = contentTypeManager.getContentType(PI_RESOURCES_TESTS + '.' + "xml-based-different-extension"); + IContentType xmlBasedDifferentExtensionContentType = contentTypeManager + .getContentType(PI_RESOURCES_TESTS + '.' + "xml-based-different-extension"); assertNotNull("3.0", xmlBasedDifferentExtensionContentType); assertTrue("3.1", isText(contentTypeManager, xmlBasedDifferentExtensionContentType)); assertEquals("3.2", xmlContentType, xmlBasedDifferentExtensionContentType.getBaseType()); - IContentType xmlBasedSpecificNameContentType = contentTypeManager.getContentType(PI_RESOURCES_TESTS + '.' + "xml-based-specific-name"); + IContentType xmlBasedSpecificNameContentType = contentTypeManager + .getContentType(PI_RESOURCES_TESTS + '.' + "xml-based-specific-name"); assertNotNull("4.0", xmlBasedSpecificNameContentType); assertTrue("4.1", isText(contentTypeManager, xmlBasedSpecificNameContentType)); assertEquals("4.2", xmlContentType, xmlBasedSpecificNameContentType.getBaseType()); @@ -1318,81 +1389,108 @@ assertTrue("9.4", contains(fooBarTypes, subFooBar)); } + @Test public void testRootElementAndDTDDescriber() throws IOException { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); IContentType rootElement = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".root-element"); IContentType dtdElement = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".dtd"); IContentType nsRootElement = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".ns-root-element"); IContentType nsWildcard = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".ns-wildcard"); - IContentType emptyNsRootElement = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".empty-ns-root-element"); + IContentType emptyNsRootElement = contentTypeManager + .getContentType(PI_RESOURCES_TESTS + ".empty-ns-root-element"); IContentType xmlType = contentTypeManager.getContentType(Platform.PI_RUNTIME + ".xml"); - IContentType[] contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_ISO_8859_1, StandardCharsets.ISO_8859_1), "fake.xml"); + IContentType[] contentTypes = contentTypeManager.findContentTypesFor( + getInputStream(XML_ROOT_ELEMENT_ISO_8859_1, StandardCharsets.ISO_8859_1), "fake.xml"); assertTrue("1.0", contentTypes.length > 0); assertEquals("1.1", rootElement, contentTypes[0]); // bugs 64053 and 63298 - contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_EXTERNAL_ENTITY, StandardCharsets.UTF_8), "fake.xml"); + contentTypes = contentTypeManager.findContentTypesFor( + getInputStream(XML_ROOT_ELEMENT_EXTERNAL_ENTITY, StandardCharsets.UTF_8), "fake.xml"); assertTrue("2.0", contentTypes.length > 0); assertEquals("2.1", rootElement, contentTypes[0]); // bug 63625 - contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_EXTERNAL_ENTITY2, StandardCharsets.UTF_8), "fake.xml"); + contentTypes = contentTypeManager.findContentTypesFor( + getInputStream(XML_ROOT_ELEMENT_EXTERNAL_ENTITY2, StandardCharsets.UTF_8), "fake.xml"); assertTrue("3.0", contentTypes.length > 0); assertEquals("3.1", rootElement, contentTypes[0]); // bug 135575 - contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_MATCH1, StandardCharsets.UTF_8), "fake.xml"); + contentTypes = contentTypeManager + .findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_MATCH1, StandardCharsets.UTF_8), "fake.xml"); assertTrue("4.0", contentTypes.length > 0); assertEquals("4.1", nsRootElement, contentTypes[0]); - contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_MATCH2, StandardCharsets.UTF_8), "fake.xml"); + contentTypes = contentTypeManager + .findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_MATCH2, StandardCharsets.UTF_8), "fake.xml"); assertTrue("4.2", contentTypes.length > 0); assertEquals("4.3", nsRootElement, contentTypes[0]); - contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_WRONG_ELEM, StandardCharsets.UTF_8), "fake.xml"); + contentTypes = contentTypeManager.findContentTypesFor( + getInputStream(XML_ROOT_ELEMENT_NS_WRONG_ELEM, StandardCharsets.UTF_8), "fake.xml"); assertTrue("4.4", contentTypes.length > 0); assertEquals("4.5", xmlType, contentTypes[0]); - contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_WRONG_NS, StandardCharsets.UTF_8), "fake.xml"); + contentTypes = contentTypeManager + .findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_WRONG_NS, StandardCharsets.UTF_8), "fake.xml"); assertTrue("4.6", contentTypes.length > 0); assertEquals("4.7", xmlType, contentTypes[0]); - contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_MIXUP, StandardCharsets.UTF_8), "fake.xml"); + contentTypes = contentTypeManager + .findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_MIXUP, StandardCharsets.UTF_8), "fake.xml"); assertTrue("4.8", contentTypes.length > 0); assertEquals("4.9", xmlType, contentTypes[0]); - contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_WILDCARD, StandardCharsets.UTF_8), "fake.xml"); + contentTypes = contentTypeManager + .findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_WILDCARD, StandardCharsets.UTF_8), "fake.xml"); assertTrue("4.10", contentTypes.length > 0); assertEquals("4.11", nsWildcard, contentTypes[0]); - contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_WILDCARD2, StandardCharsets.UTF_8), "fake.xml"); + contentTypes = contentTypeManager + .findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_WILDCARD2, StandardCharsets.UTF_8), "fake.xml"); assertTrue("4.12", contentTypes.length > 0); assertEquals("4.13", nsWildcard, contentTypes[0]); - contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_EMPTY_NS, StandardCharsets.UTF_8), "fake.xml"); + contentTypes = contentTypeManager + .findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_EMPTY_NS, StandardCharsets.UTF_8), "fake.xml"); assertTrue("4.14", contentTypes.length > 0); assertEquals("4.15", emptyNsRootElement, contentTypes[0]); - contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_DTD_US_ASCII, StandardCharsets.US_ASCII), "fake.xml"); + contentTypes = contentTypeManager + .findContentTypesFor(getInputStream(XML_DTD_US_ASCII, StandardCharsets.US_ASCII), "fake.xml"); assertTrue("5.0", contentTypes.length > 0); assertEquals("5.1", dtdElement, contentTypes[0]); - contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_DTD_EXTERNAL_ENTITY, StandardCharsets.UTF_8), "fake.xml"); + contentTypes = contentTypeManager + .findContentTypesFor(getInputStream(XML_DTD_EXTERNAL_ENTITY, StandardCharsets.UTF_8), "fake.xml"); assertTrue("5.4", contentTypes.length > 0); assertEquals("5.5", dtdElement, contentTypes[0]); // bug 67975 - IContentDescription description = contentTypeManager.getDescriptionFor(getInputStream(new byte[][] {IContentDescription.BOM_UTF_16BE, XML_ROOT_ELEMENT_NO_DECL.getBytes("UTF-16BE")}), "fake.xml", IContentDescription.ALL); + IContentDescription description = contentTypeManager.getDescriptionFor(getInputStream( + new byte[][] { IContentDescription.BOM_UTF_16BE, XML_ROOT_ELEMENT_NO_DECL.getBytes("UTF-16BE") }), + "fake.xml", IContentDescription.ALL); assertNotNull("6.0", description); assertEquals("6.1", rootElement, description.getContentType()); - assertEquals("6.2", IContentDescription.BOM_UTF_16BE, description.getProperty(IContentDescription.BYTE_ORDER_MARK)); + assertEquals("6.2", IContentDescription.BOM_UTF_16BE, + description.getProperty(IContentDescription.BYTE_ORDER_MARK)); - description = contentTypeManager.getDescriptionFor(getInputStream(new byte[][] {IContentDescription.BOM_UTF_16LE, XML_ROOT_ELEMENT_NO_DECL.getBytes("UTF-16LE")}), "fake.xml", IContentDescription.ALL); + description = contentTypeManager.getDescriptionFor(getInputStream( + new byte[][] { IContentDescription.BOM_UTF_16LE, XML_ROOT_ELEMENT_NO_DECL.getBytes("UTF-16LE") }), + "fake.xml", IContentDescription.ALL); assertNotNull("7.0", description); assertEquals("7.1", rootElement, description.getContentType()); - assertEquals("7.2", IContentDescription.BOM_UTF_16LE, description.getProperty(IContentDescription.BYTE_ORDER_MARK)); + assertEquals("7.2", IContentDescription.BOM_UTF_16LE, + description.getProperty(IContentDescription.BYTE_ORDER_MARK)); - // due to bug 67048, the test below fails with Crimson parser (does not handle UTF-8 BOMs) - // description = contentTypeManager.getDescriptionFor(getInputStream(new byte[][] {IContentDescription.BOM_UTF_8,XML_ROOT_ELEMENT_NO_DECL.getBytes("UTF-8")}), "fake.xml", IContentDescription.ALL); - // assertTrue("7.0", description != null); - // assertEquals("7.1", rootElement, description.getContentType()); - // assertEquals("7.2", IContentDescription.BOM_UTF_8, description.getProperty(IContentDescription.BYTE_ORDER_MARK)); + // due to bug 67048, the test below fails with Crimson parser (does not handle + // UTF-8 BOMs) + // description = contentTypeManager.getDescriptionFor(getInputStream(new + // byte[][] + // {IContentDescription.BOM_UTF_8,XML_ROOT_ELEMENT_NO_DECL.getBytes("UTF-8")}), + // "fake.xml", IContentDescription.ALL); + // assertTrue("7.0", description != null); + // assertEquals("7.1", rootElement, description.getContentType()); + // assertEquals("7.2", IContentDescription.BOM_UTF_8, + // description.getProperty(IContentDescription.BYTE_ORDER_MARK)); // bug 84354 - contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NO_DECL, StandardCharsets.UTF_8), "test.txt"); + contentTypes = contentTypeManager + .findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NO_DECL, StandardCharsets.UTF_8), "test.txt"); assertTrue("8.0", contentTypes.length > 0); assertEquals("8.1", contentTypeManager.getContentType(IContentTypeManager.CT_TEXT), contentTypes[0]); } @@ -1400,6 +1498,7 @@ /** * Bug 66976 */ + @Test public void testSignatureBeyondBufferLimit() throws IOException { int bufferLimit = ContentTypeManager.BLOCK_SIZE * 4; // create a long XML comment as prefix @@ -1410,7 +1509,8 @@ comment.append("-->"); IContentTypeManager manager = Platform.getContentTypeManager(); IContentType rootElement = manager.getContentType(PI_RESOURCES_TESTS + ".root-element"); - IContentType selected = manager.findContentTypeFor(getInputStream(comment + XML_ROOT_ELEMENT_NO_DECL, StandardCharsets.US_ASCII), "fake.xml"); + IContentType selected = manager.findContentTypeFor( + getInputStream(comment + XML_ROOT_ELEMENT_NO_DECL, StandardCharsets.US_ASCII), "fake.xml"); assertNotNull("1.0", selected); assertEquals("1.1", rootElement, selected); } @@ -1418,44 +1518,38 @@ /** * See also: bug 72796. */ - public void testUserDefinedAssociations() { + @Test + public void testUserDefinedAssociations() throws CoreException { IContentTypeManager manager = Platform.getContentTypeManager(); IContentType text = manager.getContentType((Platform.PI_RUNTIME + ".text")); assertNull("0.1", manager.findContentTypeFor("test.mytext")); // associate a user-defined file spec - try { - text.addFileSpec("mytext", IContentType.FILE_EXTENSION_SPEC); - } catch (CoreException e) { - fail("1.0", e); - } + text.addFileSpec("mytext", IContentType.FILE_EXTENSION_SPEC); boolean assertionFailed = false; try { IContentType result = manager.findContentTypeFor("test.mytext"); assertNotNull("1.1", result); assertEquals("1.2", text, result); - } catch (AssertionFailedError afe) { + } catch (AssertionError afe) { assertionFailed = true; throw afe; } finally { - try { - text.removeFileSpec("mytext", IContentType.FILE_EXTENSION_SPEC); - } catch (CoreException e) { - if (!assertionFailed) { - fail(" 2.0", e); - } - } + text.removeFileSpec("mytext", IContentType.FILE_EXTENSION_SPEC); + assertFalse(assertionFailed); } IContentType result = manager.findContentTypeFor("test.mytext"); assertNull("3.0", result); } + @Test public void testDescriberInvalidation() throws IOException { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); IContentType type_bug182337_A = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".Bug182337_A"); IContentType type_bug182337_B = contentTypeManager.getContentType(PI_RESOURCES_TESTS + ".Bug182337_B"); - IContentType[] contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_MATCH2, StandardCharsets.UTF_8), "Bug182337.Bug182337"); + IContentType[] contentTypes = contentTypeManager.findContentTypesFor( + getInputStream(XML_ROOT_ELEMENT_NS_MATCH2, StandardCharsets.UTF_8), "Bug182337.Bug182337"); assertEquals("1.0", 2, contentTypes.length); assertEquals("1.1", type_bug182337_A, contentTypes[0]); assertEquals("1.1", type_bug182337_B, contentTypes[1]); @@ -1471,7 +1565,8 @@ assertEquals("1.2", 0, contentTypes.length); // Describer should be invalidated by now - contentTypes = contentTypeManager.findContentTypesFor(getInputStream(XML_ROOT_ELEMENT_NS_MATCH2, StandardCharsets.UTF_8), "Bug182337.Bug182337"); + contentTypes = contentTypeManager.findContentTypesFor( + getInputStream(XML_ROOT_ELEMENT_NS_MATCH2, StandardCharsets.UTF_8), "Bug182337.Bug182337"); assertEquals("1.3", 0, contentTypes.length); }
diff --git a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/LocalSelectionPolicy.java b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/LocalSelectionPolicy.java index fb669c7..b986d0d 100644 --- a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/LocalSelectionPolicy.java +++ b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/LocalSelectionPolicy.java
@@ -18,6 +18,7 @@ import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.content.IContentType; import org.eclipse.core.runtime.content.IContentTypeManager.ISelectionPolicy; +import org.eclipse.core.tests.resources.AutomatedTests; /** * Selection policy that filters out any content types that do not @@ -29,7 +30,7 @@ List<IContentType> result = new ArrayList<>(candidates.length); for (IContentType candidate : candidates) { String namespace = getNamespace(candidate.getId()); - if (namespace.equals(ContentTypeTest.PI_RESOURCES_TESTS) || namespace.equals(Platform.PI_RUNTIME)) { + if (namespace.equals(AutomatedTests.PI_RESOURCES_TESTS) || namespace.equals(Platform.PI_RUNTIME)) { result.add(candidate); } }
diff --git a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/MyContentDescriber.java b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/MyContentDescriber.java index d77ed1a..9fe6f50 100644 --- a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/MyContentDescriber.java +++ b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/MyContentDescriber.java
@@ -13,6 +13,8 @@ *******************************************************************************/ package org.eclipse.core.tests.resources.content; +import static org.eclipse.core.tests.resources.AutomatedTests.PI_RESOURCES_TESTS; + import java.io.IOException; import java.io.InputStream; import org.eclipse.core.runtime.QualifiedName; @@ -20,10 +22,13 @@ import org.eclipse.core.runtime.content.IContentDescription; public class MyContentDescriber implements IContentDescriber { - public static final Object[] MY_OPTION_VALUES = {"FOO", null, "BAR"}; + public static final Object[] MY_OPTION_VALUES = { "FOO", null, "BAR" }; public static final String SIGNATURE = "MY_CONTENTS"; - public static final QualifiedName[] MY_OPTIONS = {new QualifiedName(ContentTypeTest.PI_RESOURCES_TESTS, "my_option_1"), new QualifiedName(ContentTypeTest.PI_RESOURCES_TESTS, "my_option_2"), new QualifiedName(ContentTypeTest.PI_RESOURCES_TESTS, "my_option_3")}; + public static final QualifiedName[] MY_OPTIONS = { new QualifiedName(PI_RESOURCES_TESTS, "my_option_1"), + new QualifiedName(PI_RESOURCES_TESTS, + "my_option_2"), + new QualifiedName(PI_RESOURCES_TESTS, "my_option_3") }; @Override public int describe(InputStream contents, IContentDescription description) throws IOException {
diff --git a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/SpecificContextTest.java b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/SpecificContextTest.java index f7f474b..2e2dd9c 100644 --- a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/SpecificContextTest.java +++ b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/SpecificContextTest.java
@@ -13,17 +13,28 @@ *******************************************************************************/ package org.eclipse.core.tests.resources.content; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import org.eclipse.core.internal.content.ContentTypeManager; import org.eclipse.core.internal.preferences.EclipsePreferences; import org.eclipse.core.runtime.*; import org.eclipse.core.runtime.content.*; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.IScopeContext; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; /** * Tests content type matcher with a non-default context for user preferences. */ public class SpecificContextTest extends ContentTypeTest { + + @Rule + public TestName name = new TestName(); + /** * A minimal scope implementation. */ @@ -51,65 +62,46 @@ } } - public SpecificContextTest(String name) { - super(name); - } - - public void testContentTypeLookup() { + @Test + public void testContentTypeLookup() throws CoreException { IContentTypeManager global = Platform.getContentTypeManager(); final SingleNodeScope scope = new SingleNodeScope(); IContentTypeMatcher local = global.getMatcher(new LocalSelectionPolicy(), scope); IContentType textContentType = global.getContentType(Platform.PI_RUNTIME + '.' + "text"); - try { - // added "<test case name>.global" to the text content type as a global file spec - textContentType.addFileSpec(getName() + ".global", IContentType.FILE_NAME_SPEC); - } catch (CoreException e) { - fail("0.1", e); - } - try { - // added "<test case name>.local" to the text content type as a local (scope-specific) file spec - textContentType.getSettings(scope).addFileSpec(getName() + ".local", IContentType.FILE_NAME_SPEC); - } catch (CoreException e) { - fail("0.2", e); - } - // make ensure associations are properly recognized when doing content type lookup - assertEquals("1.0", textContentType, global.findContentTypeFor(getName() + ".global")); - assertEquals("1.1", null, local.findContentTypeFor(getName() + ".global")); - assertEquals("2.0", textContentType, local.findContentTypeFor(getName() + ".local")); - assertEquals("2.1", null, global.findContentTypeFor(getName() + ".local")); + // added "<test case name>.global" to the text content type as a global file + // spec + textContentType.addFileSpec(name.getMethodName() + ".global", IContentType.FILE_NAME_SPEC); + // added "<test case name>.local" to the text content type as a local + // (scope-specific) file spec + textContentType.getSettings(scope).addFileSpec(name.getMethodName() + ".local", IContentType.FILE_NAME_SPEC); + // make ensure associations are properly recognized when doing content type + // lookup + assertEquals("1.0", textContentType, global.findContentTypeFor(name.getMethodName() + ".global")); + assertEquals("1.1", null, local.findContentTypeFor(name.getMethodName() + ".global")); + assertEquals("2.0", textContentType, local.findContentTypeFor(name.getMethodName() + ".local")); + assertEquals("2.1", null, global.findContentTypeFor(name.getMethodName() + ".local")); try { - textContentType.removeFileSpec(getName() + ".global", IContentType.FILE_NAME_SPEC); + textContentType.removeFileSpec(name.getMethodName() + ".global", IContentType.FILE_NAME_SPEC); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } } - public void testIsAssociatedWith() { + @Test + public void testIsAssociatedWith() throws CoreException { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); final SingleNodeScope scope = new SingleNodeScope(); IContentType textContentType = contentTypeManager.getContentType(Platform.PI_RUNTIME + '.' + "text"); IContentTypeSettings localSettings = null; - try { - localSettings = textContentType.getSettings(scope); - } catch (CoreException e) { - fail("0.1", e); - } + localSettings = textContentType.getSettings(scope); // haven't added association yet assertFalse("1.0", textContentType.isAssociatedWith("hello.foo", scope)); assertFalse("1.1", textContentType.isAssociatedWith("hello.foo")); - try { - // associate at the scope level - localSettings.addFileSpec("foo", IContentType.FILE_EXTENSION_SPEC); - } catch (CoreException e) { - fail("1.5", e); - } - try { - localSettings = textContentType.getSettings(scope); - } catch (CoreException e) { - fail("2.1", e); - } + // associate at the scope level + localSettings.addFileSpec("foo", IContentType.FILE_EXTENSION_SPEC); + localSettings = textContentType.getSettings(scope); // scope-specific settings should contain the filespec we just added String[] fileSpecs = localSettings.getFileSpecs(IContentType.FILE_EXTENSION_SPEC); assertEquals("2.2", 1, fileSpecs.length);
diff --git a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/TestBug94498.java b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/TestBug94498.java index ff9ef70..9911dbb 100644 --- a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/TestBug94498.java +++ b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/TestBug94498.java
@@ -14,39 +14,31 @@ package org.eclipse.core.tests.resources.content; import junit.framework.Test; +import junit.framework.TestCase; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.content.IContentType; import org.eclipse.core.runtime.content.IContentTypeManager; +import org.eclipse.core.tests.resources.AutomatedTests; import org.eclipse.core.tests.session.WorkspaceSessionTestSuite; -public class TestBug94498 extends ContentTypeTest { +public class TestBug94498 extends TestCase { private static final String FILE_NAME = "foo.bar.zoo"; public static Test suite() { - return new WorkspaceSessionTestSuite(PI_RESOURCES_TESTS, TestBug94498.class); + return new WorkspaceSessionTestSuite(AutomatedTests.PI_RESOURCES_TESTS, TestBug94498.class); } - public TestBug94498(String name) { - super(name); - } - - @org.junit.Test - public void test1() { + public void test1() throws CoreException { IContentType text = Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT); assertNotNull("1.0", text); - try { - text.addFileSpec(FILE_NAME, IContentType.FILE_NAME_SPEC); - } catch (CoreException e) { - fail("1.9", e); - } + text.addFileSpec(FILE_NAME, IContentType.FILE_NAME_SPEC); String[] fileSpecs = text.getFileSpecs(IContentType.FILE_NAME_SPEC | IContentType.IGNORE_PRE_DEFINED); assertEquals("2.0", 1, fileSpecs.length); assertEquals("2.1", FILE_NAME, fileSpecs[0]); } - @org.junit.Test public void test2() { IContentType text = Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT); assertNotNull("1.0", text);
diff --git a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/XMLContentDescriberTest.java b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/XMLContentDescriberTest.java index 79f17af..645ada5 100644 --- a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/XMLContentDescriberTest.java +++ b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/content/XMLContentDescriberTest.java
@@ -13,11 +13,16 @@ *******************************************************************************/ package org.eclipse.core.tests.resources.content; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + import java.io.*; import java.util.Arrays; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.QualifiedName; import org.eclipse.core.runtime.content.IContentDescription; +import org.junit.Test; /** * Ensures the XMLContentDescriber is able to handle encodings properly. @@ -31,10 +36,7 @@ private final static String ENCODING_EMPTY = ""; private final static String WHITESPACE_CHARACTERS = " \n\r"; - public XMLContentDescriberTest(String name) { - super(name); - } - + @Test public void testEncodedContents1() throws Exception { checkEncodedContents(ENCODING_UTF16, ENCODING_UTF16, ENCODING_UTF16); checkEncodedContents(ENCODING_UTF8, ENCODING_UTF8, ENCODING_UTF8); @@ -46,12 +48,14 @@ checkEncodedContents(ENCODING_NOTSUPPORTED, ENCODING_NOTSUPPORTED, ENCODING_UTF16); } + @Test public void testEncodedContents2() throws Exception { checkEncodedContents2(ENCODING_UTF16, ENCODING_UTF16); checkEncodedContents2(ENCODING_UTF8, ENCODING_UTF8); checkEncodedContents2(ENCODING_NOTSUPPORTED, ENCODING_NOTSUPPORTED); } + @Test public void testEncodedContents3() throws Exception { boolean[][] flags = { {true, true, false}, {true, false, false}, {false, true, false}, {false, false, false}, {true, true, true}, {true, false, true}, {false, true, true}, {false, false, true}}; @@ -79,6 +83,7 @@ } } + @Test public void testBug258208() throws Exception { IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(getInputStream(ENCODING_EMPTY, ENCODING_UTF8, false, true, false), "fake.xml", new QualifiedName[] {IContentDescription.CHARSET}); assertNull("1.0", description);