|
Added
Link Here
|
| 1 |
package org.eclipse.gmf.internal.xpand.inactive; |
| 2 |
|
| 3 |
import java.io.BufferedReader; |
| 4 |
import java.io.ByteArrayInputStream; |
| 5 |
import java.io.IOException; |
| 6 |
import java.io.InputStream; |
| 7 |
import java.io.Reader; |
| 8 |
import java.io.StringReader; |
| 9 |
|
| 10 |
import junit.framework.TestCase; |
| 11 |
|
| 12 |
import org.eclipse.core.runtime.content.IContentDescriber; |
| 13 |
import org.eclipse.core.runtime.content.ITextContentDescriber; |
| 14 |
import org.eclipse.gmf.internal.xpand.inactive.ContentDescriber; |
| 15 |
import org.xml.sax.InputSource; |
| 16 |
|
| 17 |
/** |
| 18 |
* |
| 19 |
* @author Torsten Krohn |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
@SuppressWarnings("restriction") |
| 24 |
public class ContentDescriberTest extends TestCase { |
| 25 |
|
| 26 |
public static final String ENCODING_UTF8 = "UTF-8"; |
| 27 |
public static final String ENCODING_ISO = "ISO-8859-1"; |
| 28 |
public static final String ENCODING_MACROMAN = "MacRoman"; |
| 29 |
|
| 30 |
public Reader readerGmfXptUtf8; |
| 31 |
public Reader readerGmfXptIso88591; |
| 32 |
public Reader readerXtextXptUtf8; |
| 33 |
public Reader readerXtextXptIso88591; |
| 34 |
public Reader readerOther; |
| 35 |
|
| 36 |
public InputStream inputStreamGmfXptUtf8; |
| 37 |
public InputStream inputStreamGmfXptIso; |
| 38 |
public InputStream inputStreamXtextXptUtf8; |
| 39 |
public InputStream inputStreamXtextXptIso; |
| 40 |
public InputStream inputStreamOther; |
| 41 |
|
| 42 |
@Override |
| 43 |
protected void setUp() throws Exception { |
| 44 |
super.setUp(); |
| 45 |
|
| 46 |
String xtextxpt = "/*\n"; |
| 47 |
xtextxpt += " * some comments with the word import included\n"; |
| 48 |
xtextxpt += " */\n"; |
| 49 |
xtextxpt += "\n"; |
| 50 |
xtextxpt += "\u00ABIMPORT 'http://www.eclipse.org/gmf/2009/GenModel'\00BB\n"; |
| 51 |
xtextxpt += "\u00ABEXTENSION xpt::diagram::parts::Common\u00BB\n"; |
| 52 |
|
| 53 |
byte[] utf8bytes = xtextxpt.getBytes(ENCODING_UTF8); |
| 54 |
byte[] isobytes = xtextxpt.getBytes(ENCODING_ISO); |
| 55 |
inputStreamXtextXptUtf8 = new ByteArrayInputStream(utf8bytes); |
| 56 |
inputStreamXtextXptIso = new ByteArrayInputStream(isobytes); |
| 57 |
|
| 58 |
InputSource inputSourceXtextXptUtf8 = new InputSource(new StringReader( |
| 59 |
xtextxpt)); |
| 60 |
inputSourceXtextXptUtf8.setEncoding(ENCODING_UTF8); |
| 61 |
readerXtextXptUtf8 = inputSourceXtextXptUtf8.getCharacterStream(); |
| 62 |
|
| 63 |
InputSource inputSourceXtextXptIso = new InputSource(new StringReader( |
| 64 |
xtextxpt)); |
| 65 |
inputSourceXtextXptIso.setEncoding(ENCODING_ISO); |
| 66 |
readerXtextXptIso88591 = inputSourceXtextXptIso.getCharacterStream(); |
| 67 |
|
| 68 |
String gmfxpt = "/*\n"; |
| 69 |
gmfxpt += " * some comments with the word import included\n"; |
| 70 |
gmfxpt += " */\n"; |
| 71 |
gmfxpt += "package org.eclipse.gmf.internal.xpand.inactive;\n"; |
| 72 |
gmfxpt += "\n"; |
| 73 |
gmfxpt += "import java.io.IOException;\n"; |
| 74 |
gmfxpt += "public class MagicClass implements IMagic {\n"; |
| 75 |
|
| 76 |
utf8bytes = gmfxpt.getBytes(ENCODING_UTF8); |
| 77 |
isobytes = gmfxpt.getBytes(ENCODING_ISO); |
| 78 |
inputStreamGmfXptUtf8 = new ByteArrayInputStream(utf8bytes); |
| 79 |
inputStreamGmfXptIso = new ByteArrayInputStream(isobytes); |
| 80 |
|
| 81 |
InputSource inputSourceGmfXptUtf8 = new InputSource(new StringReader( |
| 82 |
gmfxpt)); |
| 83 |
inputSourceGmfXptUtf8.setEncoding(ENCODING_UTF8); |
| 84 |
readerGmfXptUtf8 = inputSourceGmfXptUtf8.getCharacterStream(); |
| 85 |
InputSource inputStreamGmfXptIso = new InputSource(new StringReader( |
| 86 |
gmfxpt)); |
| 87 |
inputStreamGmfXptIso.setEncoding(ENCODING_ISO); |
| 88 |
readerGmfXptIso88591 = inputStreamGmfXptIso.getCharacterStream(); |
| 89 |
|
| 90 |
String other = "/*"; |
| 91 |
other += " * some comments with the word import included\n"; |
| 92 |
other += " */\n"; |
| 93 |
other += "package org.eclipse.gmf.internal.xpand.inactive;\n"; |
| 94 |
|
| 95 |
byte[] otherBytes = other.getBytes(ENCODING_MACROMAN); |
| 96 |
inputStreamOther = new ByteArrayInputStream(otherBytes); |
| 97 |
|
| 98 |
InputSource otherInputSource = new InputSource(new StringReader(other)); |
| 99 |
readerOther = otherInputSource.getCharacterStream(); |
| 100 |
} |
| 101 |
|
| 102 |
public final void testDescribeReader() throws IOException { |
| 103 |
BufferedReader contents = new BufferedReader(readerXtextXptUtf8); |
| 104 |
ITextContentDescriber describer = new ContentDescriber(); |
| 105 |
assertEquals(IContentDescriber.VALID, describer |
| 106 |
.describe(contents, null)); |
| 107 |
|
| 108 |
contents = new BufferedReader(readerXtextXptIso88591); |
| 109 |
assertEquals(IContentDescriber.VALID, describer |
| 110 |
.describe(contents, null)); |
| 111 |
|
| 112 |
contents = new BufferedReader(readerGmfXptUtf8); |
| 113 |
assertEquals(IContentDescriber.INVALID, describer.describe(contents, |
| 114 |
null)); |
| 115 |
|
| 116 |
contents = new BufferedReader(readerGmfXptIso88591); |
| 117 |
assertEquals(IContentDescriber.INVALID, describer.describe(contents, |
| 118 |
null)); |
| 119 |
|
| 120 |
contents = new BufferedReader(readerOther); |
| 121 |
assertEquals(IContentDescriber.INVALID, describer.describe(contents, |
| 122 |
null)); |
| 123 |
} |
| 124 |
|
| 125 |
public final void testDescribeInputStream() throws IOException { |
| 126 |
ITextContentDescriber describer = new ContentDescriber(); |
| 127 |
assertEquals(IContentDescriber.VALID, describer.describe( |
| 128 |
inputStreamXtextXptUtf8, null)); |
| 129 |
|
| 130 |
assertEquals(IContentDescriber.VALID, describer.describe( |
| 131 |
inputStreamXtextXptIso, null)); |
| 132 |
|
| 133 |
assertEquals(IContentDescriber.INVALID, describer.describe( |
| 134 |
inputStreamGmfXptUtf8, null)); |
| 135 |
|
| 136 |
assertEquals(IContentDescriber.INVALID, describer.describe( |
| 137 |
inputStreamGmfXptIso, null)); |
| 138 |
|
| 139 |
assertEquals(IContentDescriber.INVALID, describer.describe( |
| 140 |
inputStreamOther, null)); |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
protected void tearDown() throws Exception { |
| 145 |
super.tearDown(); |
| 146 |
readerGmfXptUtf8 = null; |
| 147 |
readerGmfXptIso88591 = null; |
| 148 |
readerXtextXptUtf8 = null; |
| 149 |
readerXtextXptIso88591 = null; |
| 150 |
} |
| 151 |
} |