| 1 |
/** |
| 2 |
* Copyright (c) 2002-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 |
package org.eclipse.emf.test.xml.xmi; |
| 12 |
|
| 13 |
|
| 14 |
import static org.junit.Assert.assertEquals; |
| 15 |
|
| 16 |
import java.io.InputStream; |
| 17 |
import java.io.OutputStream; |
| 18 |
import java.io.StringWriter; |
| 19 |
import java.util.HashMap; |
| 20 |
|
| 21 |
import javax.xml.parsers.DocumentBuilder; |
| 22 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 23 |
|
| 24 |
import org.eclipse.emf.common.util.URI; |
| 25 |
import org.eclipse.emf.ecore.resource.Resource; |
| 26 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
| 27 |
import org.eclipse.emf.ecore.resource.URIConverter; |
| 28 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 29 |
import org.eclipse.emf.ecore.util.ExtendedMetaData; |
| 30 |
import org.eclipse.emf.ecore.xmi.XMLResource; |
| 31 |
import org.eclipse.emf.ecore.xmi.impl.XMLParserPoolImpl; |
| 32 |
import org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl; |
| 33 |
import org.eclipse.emf.test.common.TestUtil; |
| 34 |
import org.eclipse.emf.test.models.customer.CustomerPackage; |
| 35 |
import org.eclipse.emf.test.models.movie.db.DBPackage; |
| 36 |
import org.eclipse.emf.test.models.order.OrderPackage; |
| 37 |
import org.eclipse.emf.test.xml.AllSuites; |
| 38 |
import org.junit.After; |
| 39 |
import org.junit.Before; |
| 40 |
import org.junit.Test; |
| 41 |
import org.w3c.dom.Document; |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Test for XMI package: loading data/order.xml |
| 46 |
*/ |
| 47 |
public class OrderTest |
| 48 |
{ |
| 49 |
final static String BASE_XML_URI = TestUtil.getPluginDirectory(AllSuites.PLUGIN_ID) + "/data/xml/"; |
| 50 |
|
| 51 |
protected String inputXML; |
| 52 |
|
| 53 |
protected HashMap<String, Object> options; |
| 54 |
|
| 55 |
@Before |
| 56 |
public void setUp() throws Exception |
| 57 |
{ |
| 58 |
OrderPackage.eINSTANCE.getName(); |
| 59 |
CustomerPackage.eINSTANCE.getName(); |
| 60 |
DBPackage.eINSTANCE.getName(); |
| 61 |
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xml", new XMLResourceFactoryImpl()); |
| 62 |
inputXML = BASE_XML_URI + "order.xml"; |
| 63 |
options = new HashMap<String, Object>(); |
| 64 |
} |
| 65 |
|
| 66 |
@After |
| 67 |
public void tearDown() throws Exception |
| 68 |
{ |
| 69 |
options = null; |
| 70 |
} |
| 71 |
|
| 72 |
@Test |
| 73 |
public void testQname() throws Exception |
| 74 |
{ |
| 75 |
URI uri = URI.createFileURI(inputXML); |
| 76 |
ResourceSet resourceSet = new ResourceSetImpl(); |
| 77 |
XMLResource resource = (XMLResource)resourceSet.createResource(uri); |
| 78 |
options.put(XMLResource.OPTION_EXTENDED_META_DATA, ExtendedMetaData.INSTANCE); |
| 79 |
options.put(XMLResource.OPTION_USE_DEPRECATED_METHODS, Boolean.FALSE); |
| 80 |
options.put(XMLResource.OPTION_USE_PARSER_POOL, new XMLParserPoolImpl()); |
| 81 |
resource.load(options); |
| 82 |
StringWriter stringWriter = new StringWriter(); |
| 83 |
OutputStream out = new URIConverter.WriteableOutputStream(stringWriter, null); |
| 84 |
resource.save(out, null); |
| 85 |
String result1 = stringWriter.toString(); |
| 86 |
stringWriter.getBuffer().setLength(0); |
| 87 |
resource.unload(); |
| 88 |
|
| 89 |
InputStream input = resourceSet.getURIConverter().createInputStream(uri); |
| 90 |
try |
| 91 |
{ |
| 92 |
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 93 |
documentBuilderFactory.setNamespaceAware(true); |
| 94 |
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
| 95 |
Document document = documentBuilder.parse(input); |
| 96 |
resource = (XMLResource)resourceSet.createResource(uri); |
| 97 |
options.put(XMLResource.OPTION_USE_DEPRECATED_METHODS, Boolean.FALSE); |
| 98 |
options.put(XMLResource.OPTION_EXTENDED_META_DATA, ExtendedMetaData.INSTANCE); |
| 99 |
resource.load(document, options); |
| 100 |
resource.save(out, null); |
| 101 |
resource.unload(); |
| 102 |
String result2 = stringWriter.toString(); |
| 103 |
stringWriter.getBuffer().setLength(0); |
| 104 |
resource.unload(); |
| 105 |
options.put(XMLResource.OPTION_DOM_USE_NAMESPACES_IN_SCOPE, Boolean.TRUE); |
| 106 |
resource.load(document.getDocumentElement(), options); |
| 107 |
resource.save(out, null); |
| 108 |
String result3 = stringWriter.toString(); |
| 109 |
stringWriter.getBuffer().setLength(0); |
| 110 |
resource.unload(); |
| 111 |
|
| 112 |
assertEquals(result1, result2); |
| 113 |
assertEquals(result1, result3); |
| 114 |
} |
| 115 |
finally |
| 116 |
{ |
| 117 |
input.close(); |
| 118 |
} |
| 119 |
} |
| 120 |
} |