| 1 |
/** |
| 2 |
* Copyright (c) 2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM - Initial API and implementation |
| 10 |
*/ |
| 11 |
|
| 12 |
package org.eclipse.emf.test.xml.encoding; |
| 13 |
|
| 14 |
|
| 15 |
import static org.junit.Assert.assertEquals; |
| 16 |
|
| 17 |
import java.io.ByteArrayInputStream; |
| 18 |
import java.util.HashMap; |
| 19 |
import java.util.Map; |
| 20 |
|
| 21 |
import org.eclipse.emf.common.util.URI; |
| 22 |
import org.eclipse.emf.ecore.xmi.XMLOptions; |
| 23 |
import org.eclipse.emf.ecore.xmi.XMLResource; |
| 24 |
import org.eclipse.emf.ecore.xmi.impl.XMLOptionsImpl; |
| 25 |
import org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl; |
| 26 |
import org.junit.Test; |
| 27 |
|
| 28 |
|
| 29 |
public class XMLHeaderTest |
| 30 |
{ |
| 31 |
@Test |
| 32 |
public void testDeprecatedMethods() throws Exception |
| 33 |
{ |
| 34 |
test("1.0", "ASCII", true); |
| 35 |
test("1.1", "UTF-8", true); |
| 36 |
test("1.1", "UTF-16BE", true); |
| 37 |
test("1.1", "UTF-16LE", true); |
| 38 |
} |
| 39 |
|
| 40 |
@Test |
| 41 |
public void testNonDeprecatedMethods() throws Exception |
| 42 |
{ |
| 43 |
test("1.0", "ASCII", false); |
| 44 |
test("1.1", "UTF-8", false); |
| 45 |
test("1.1", "UTF-16BE", false); |
| 46 |
test("1.1", "UTF-16LE", false); |
| 47 |
} |
| 48 |
|
| 49 |
protected void test(String xmlVersion, String encoding, boolean useNonDeprecatedMethods) throws Exception |
| 50 |
{ |
| 51 |
XMLResource resource = (XMLResource) new XMLResourceFactoryImpl().createResource(URI.createURI("encoding.xml")); |
| 52 |
resource.setEncoding("junk"); |
| 53 |
|
| 54 |
XMLOptions option = new XMLOptionsImpl(); |
| 55 |
option.setProcessAnyXML(true); |
| 56 |
Map<Object, Object> options = new HashMap<Object, Object>(); |
| 57 |
options.put(XMLResource.OPTION_XML_OPTIONS, option); |
| 58 |
options.put(XMLResource.OPTION_USE_DEPRECATED_METHODS, useNonDeprecatedMethods ? Boolean.FALSE : Boolean.TRUE); |
| 59 |
|
| 60 |
String source = "<?xml version=\""+ xmlVersion + "\" encoding=\"" + encoding + "\"?><any/>"; |
| 61 |
ByteArrayInputStream in = new ByteArrayInputStream(source.getBytes(encoding)); |
| 62 |
resource.load(in, options); |
| 63 |
assertEquals(xmlVersion, resource.getXMLVersion()); |
| 64 |
assertEquals(encoding, resource.getEncoding()); |
| 65 |
} |
| 66 |
} |