| 1 |
/** |
| 2 |
* Copyright (c) 2006 Jesper Steen M�ller |
| 3 |
* |
| 4 |
* All rights reserved. This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* http://www.eclipse.org/legal/epl-v20.html |
| 8 |
* |
| 9 |
* Contributors: |
| 10 |
* IBM - Initial API and implementation |
| 11 |
*/ |
| 12 |
|
| 13 |
package org.eclipse.emf.test.xml.encoding; |
| 14 |
|
| 15 |
|
| 16 |
import static org.junit.Assert.assertEquals; |
| 17 |
import static org.junit.Assert.assertTrue; |
| 18 |
|
| 19 |
import java.io.File; |
| 20 |
import java.io.IOException; |
| 21 |
import java.util.Arrays; |
| 22 |
import java.util.Collection; |
| 23 |
import java.util.HashMap; |
| 24 |
|
| 25 |
import org.eclipse.emf.common.util.URI; |
| 26 |
import org.eclipse.emf.ecore.EAnnotation; |
| 27 |
import org.eclipse.emf.ecore.EcoreFactory; |
| 28 |
import org.eclipse.emf.ecore.xmi.XMIResource; |
| 29 |
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; |
| 30 |
import org.junit.After; |
| 31 |
import org.junit.Before; |
| 32 |
import org.junit.Test; |
| 33 |
import org.junit.runner.RunWith; |
| 34 |
import org.junit.runners.Parameterized; |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* @author jsm |
| 39 |
*/ |
| 40 |
@RunWith(Parameterized.class) |
| 41 |
public class UnicodeEncodingTest |
| 42 |
{ |
| 43 |
// From the Unicode spec |
| 44 |
public static final int MIN_SUPPLEMENTARY_CODE_POINT = 0x010000; |
| 45 |
|
| 46 |
public static final int MIN_CODE_POINT = 0x000000; |
| 47 |
|
| 48 |
public static final int MAX_CODE_POINT = 0x10ffff; |
| 49 |
|
| 50 |
public static final char MIN_HIGH_SURROGATE = '\uD800'; |
| 51 |
|
| 52 |
public static final char MAX_HIGH_SURROGATE = '\uDBFF'; |
| 53 |
|
| 54 |
public static final char MIN_LOW_SURROGATE = '\uDC00'; |
| 55 |
|
| 56 |
public static final char MAX_LOW_SURROGATE = '\uDFFF'; |
| 57 |
|
| 58 |
public static char[] toSurrogatePair(int codePoint) |
| 59 |
{ |
| 60 |
if (codePoint < 0 || codePoint > MAX_CODE_POINT) |
| 61 |
{ |
| 62 |
throw new IllegalArgumentException(); |
| 63 |
} |
| 64 |
if (codePoint < MIN_SUPPLEMENTARY_CODE_POINT) |
| 65 |
{ |
| 66 |
return new char []{ (char)codePoint }; |
| 67 |
} |
| 68 |
char[] result = new char [2]; |
| 69 |
|
| 70 |
int offset = codePoint - MIN_SUPPLEMENTARY_CODE_POINT; |
| 71 |
result[0] = (char)((offset >>> 10) + MIN_HIGH_SURROGATE); |
| 72 |
result[1] = (char)((offset & 0x3ff) + MIN_LOW_SURROGATE); |
| 73 |
|
| 74 |
return result; |
| 75 |
} |
| 76 |
|
| 77 |
protected File tempFile; |
| 78 |
protected final String encodingName; |
| 79 |
protected final String xmlVersion; |
| 80 |
|
| 81 |
public UnicodeEncodingTest(String encoding, String xmlVersion) |
| 82 |
{ |
| 83 |
encodingName = encoding; |
| 84 |
this.xmlVersion = xmlVersion; |
| 85 |
} |
| 86 |
|
| 87 |
@Parameterized.Parameters(name="Encoding {0} for XML {1}") |
| 88 |
public static Collection<Object[]> parameters() |
| 89 |
{ |
| 90 |
return |
| 91 |
Arrays.asList |
| 92 |
(new Object[][] |
| 93 |
{ |
| 94 |
{ "UTF-8", "1.0" }, |
| 95 |
{ "UTF-16BE", "1.0" }, |
| 96 |
{ "UTF-16LE", "1.0" }, |
| 97 |
{ "ASCII", "1.0" }, |
| 98 |
{ "ISO-8859-1", "1.0" }, |
| 99 |
// { "ISO-8859-5", "1.0" }, |
| 100 |
{ "ASCII", "1.1" }, |
| 101 |
}); |
| 102 |
} |
| 103 |
|
| 104 |
@Before |
| 105 |
public void setUp() throws Exception |
| 106 |
{ |
| 107 |
tempFile = File.createTempFile("EMF-encoding-test-" + encodingName, ".tmp.xml"); |
| 108 |
} |
| 109 |
|
| 110 |
@After |
| 111 |
public void tearDown() throws Exception |
| 112 |
{ |
| 113 |
tempFile.delete(); |
| 114 |
} |
| 115 |
|
| 116 |
public void doEMFSaveAndLoad(String testString) throws IOException |
| 117 |
{ |
| 118 |
URI fileURI = URI.createFileURI(tempFile.toString()); |
| 119 |
String sourceValue = testString + " represented as XML in " + encodingName; |
| 120 |
EAnnotation eObject = EcoreFactory.eINSTANCE.createEAnnotation(); |
| 121 |
|
| 122 |
eObject.setSource(sourceValue); // Including international characters |
| 123 |
|
| 124 |
XMIResource resource = new XMIResourceImpl(); |
| 125 |
resource.getContents().add(eObject); |
| 126 |
resource.setEncoding(encodingName); |
| 127 |
resource.setXMLVersion(xmlVersion); |
| 128 |
resource.setURI(fileURI); |
| 129 |
resource.save(new HashMap<String, Object>()); |
| 130 |
|
| 131 |
XMIResource loadedResource = new XMIResourceImpl(); |
| 132 |
loadedResource.setURI(fileURI); |
| 133 |
loadedResource.load(new HashMap<String, Object>()); |
| 134 |
assertTrue("No errors should occur while loading", loadedResource.getErrors().isEmpty()); |
| 135 |
|
| 136 |
EAnnotation loadedAnnotation = (EAnnotation)loadedResource.getContents().get(0); |
| 137 |
assertEquals("String read back by EMF was different from the one being saved", sourceValue, loadedAnnotation.getSource()); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
@Test |
| 142 |
public void testStraightASCII() throws Exception |
| 143 |
{ |
| 144 |
// Test in the ASCII range |
| 145 |
doEMFSaveAndLoad("Just straight ASCII small < and (&) big >"); |
| 146 |
} |
| 147 |
|
| 148 |
@Test |
| 149 |
public void testCharactersIn8bit() throws Exception |
| 150 |
{ |
| 151 |
// Test in the 8 bit range of Unicode |
| 152 |
doEMFSaveAndLoad("A name in Western Europe: Jesper Steen M\u00F8ller"); |
| 153 |
} |
| 154 |
|
| 155 |
// Test outside the 8 bit range of Unicode |
| 156 |
@Test |
| 157 |
public void testArabicLetter() throws Exception |
| 158 |
{ |
| 159 |
// Test in the 16 bit range of Unicode |
| 160 |
doEMFSaveAndLoad("This is an arabic glyph: \uFECE. "); |
| 161 |
} |
| 162 |
|
| 163 |
// Test beyond the 16 bit range of Unicode |
| 164 |
@Test |
| 165 |
public void testSupplementaryContent() throws Exception |
| 166 |
{ |
| 167 |
/** |
| 168 |
* Test in the supplementary code point area (this will get tricky) |
| 169 |
* See <http://www.unicode.org/charts/PDF/U10330.pdf> |
| 170 |
*/ |
| 171 |
char kusma[] = toSurrogatePair(0x1033A); // GOTHIC LETTER KUSMA |
| 172 |
doEMFSaveAndLoad("This is a gothic letter: " + new String(kusma) + ". "); |
| 173 |
|
| 174 |
// XML saving and loading has errors, too! |
| 175 |
} |
| 176 |
|
| 177 |
@Test |
| 178 |
public void testControlCharacters() throws Exception |
| 179 |
{ |
| 180 |
if ("1.1".equals(xmlVersion)) |
| 181 |
{ |
| 182 |
StringBuffer text = new StringBuffer(); |
| 183 |
for (char i = 1; i <= 0x1F; ++i) |
| 184 |
{ |
| 185 |
text.append(i); |
| 186 |
} |
| 187 |
doEMFSaveAndLoad("These are control characters: " + text + ". "); |
| 188 |
} |
| 189 |
} |
| 190 |
} |