| 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 java.io.ByteArrayInputStream; |
| 15 |
import java.io.ByteArrayOutputStream; |
| 16 |
import java.util.Collection; |
| 17 |
import java.util.HashMap; |
| 18 |
import java.util.Map; |
| 19 |
|
| 20 |
import javax.xml.parsers.DocumentBuilder; |
| 21 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 22 |
|
| 23 |
import org.eclipse.emf.common.util.URI; |
| 24 |
import org.eclipse.emf.ecore.EPackage; |
| 25 |
import org.eclipse.emf.ecore.resource.Resource; |
| 26 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
| 27 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 28 |
import org.eclipse.emf.ecore.xmi.XMLResource; |
| 29 |
import org.eclipse.emf.ecore.xmi.impl.ElementHandlerImpl; |
| 30 |
import org.eclipse.emf.ecore.xmi.impl.GenericXMLResourceFactoryImpl; |
| 31 |
import org.eclipse.emf.test.common.TestUtil; |
| 32 |
import org.eclipse.emf.test.xml.AllSuites; |
| 33 |
import org.junit.After; |
| 34 |
import org.junit.Before; |
| 35 |
import org.junit.Test; |
| 36 |
|
| 37 |
|
| 38 |
public class ElementHandlerTest |
| 39 |
{ |
| 40 |
final static String BASE_XML_URI = TestUtil.getPluginDirectory(AllSuites.PLUGIN_ID) + "/data/xml/"; |
| 41 |
|
| 42 |
protected DocumentBuilder builder; |
| 43 |
|
| 44 |
@Before |
| 45 |
public void setUp() throws Exception |
| 46 |
{ |
| 47 |
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xml", new GenericXMLResourceFactoryImpl()); |
| 48 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 49 |
factory.setNamespaceAware(true); |
| 50 |
builder = factory.newDocumentBuilder(); |
| 51 |
} |
| 52 |
|
| 53 |
@After |
| 54 |
public void tearDown() throws Exception |
| 55 |
{ |
| 56 |
builder = null; |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void testSubsitutionGroup() throws Exception |
| 61 |
{ |
| 62 |
String inputXML = BASE_XML_URI + "elementHandler.xml"; |
| 63 |
String expectedXML = BASE_XML_URI + "elementHandlerOutput.xml"; |
| 64 |
URI uri = URI.createFileURI(inputXML); |
| 65 |
ResourceSet resourceSet = new ResourceSetImpl(); |
| 66 |
Resource resource = resourceSet.createResource(uri); |
| 67 |
Map<Object, Object> options = new HashMap<Object, Object>(); |
| 68 |
options.put(XMLResource.OPTION_ELEMENT_HANDLER, new ElementHandlerImpl(false)); |
| 69 |
options.put(XMLResource.OPTION_SUPPRESS_DOCUMENT_ROOT, Boolean.TRUE); |
| 70 |
resource.load(options); |
| 71 |
|
| 72 |
ByteArrayOutputStream outputstream = new ByteArrayOutputStream(2064); |
| 73 |
resource.save(outputstream, options); |
| 74 |
|
| 75 |
CompareXML.compareFiles(builder, expectedXML, new ByteArrayInputStream(outputstream.toByteArray())); |
| 76 |
} |
| 77 |
|
| 78 |
@Test |
| 79 |
public void testComplexDeducedRoot() throws Exception |
| 80 |
{ |
| 81 |
String inputXML = BASE_XML_URI + "elementHandlerComplexRoot.xml"; |
| 82 |
String expectedXML = BASE_XML_URI + "elementHandlerComplexRootOutput.xml"; |
| 83 |
URI uri = URI.createFileURI(inputXML); |
| 84 |
ResourceSet resourceSet = new ResourceSetImpl(); |
| 85 |
Resource resource = resourceSet.createResource(uri); |
| 86 |
Map<Object, Object> options = new HashMap<Object, Object>(); |
| 87 |
options.put(XMLResource.OPTION_ELEMENT_HANDLER, new ElementHandlerImpl(false)); |
| 88 |
options.put(XMLResource.OPTION_SUPPRESS_DOCUMENT_ROOT, Boolean.TRUE); |
| 89 |
resource.load(options); |
| 90 |
|
| 91 |
ByteArrayOutputStream outputstream = new ByteArrayOutputStream(2064); |
| 92 |
resource.save(outputstream, options); |
| 93 |
|
| 94 |
CompareXML.compareFiles(builder, expectedXML, new ByteArrayInputStream(outputstream.toByteArray())); |
| 95 |
} |
| 96 |
|
| 97 |
@Test |
| 98 |
public void testSimpleDeducedRoot() throws Exception |
| 99 |
{ |
| 100 |
String inputXML = BASE_XML_URI + "elementHandlerSimpleRoot.xml"; |
| 101 |
String expectedXML = BASE_XML_URI + "elementHandlerSimpleRootOutput.xml"; |
| 102 |
URI uri = URI.createFileURI(inputXML); |
| 103 |
ResourceSet resourceSet = new ResourceSetImpl(); |
| 104 |
Resource resource = resourceSet.createResource(uri); |
| 105 |
Map<Object, Object> options = new HashMap<Object, Object>(); |
| 106 |
options.put(XMLResource.OPTION_ELEMENT_HANDLER, new ElementHandlerImpl(false)); |
| 107 |
options.put(XMLResource.OPTION_SUPPRESS_DOCUMENT_ROOT, Boolean.TRUE); |
| 108 |
resource.load(options); |
| 109 |
|
| 110 |
ByteArrayOutputStream outputstream = new ByteArrayOutputStream(2064); |
| 111 |
@SuppressWarnings("unchecked") |
| 112 |
ElementHandlerImpl elementHandler = new ElementHandlerImpl(false, (Collection<EPackage>)(Collection<?>)resourceSet.getPackageRegistry().values()); |
| 113 |
options.put(XMLResource.OPTION_ELEMENT_HANDLER, elementHandler); |
| 114 |
resource.save(outputstream, options); |
| 115 |
|
| 116 |
CompareXML.compareFiles(builder, expectedXML, new ByteArrayInputStream(outputstream.toByteArray())); |
| 117 |
|
| 118 |
Resource resource2 = resourceSet.createResource(uri); |
| 119 |
resource2.load(new ByteArrayInputStream(outputstream.toByteArray()), options); |
| 120 |
outputstream = new ByteArrayOutputStream(2064); |
| 121 |
resource2.save(outputstream, options); |
| 122 |
CompareXML.compareFiles(builder, expectedXML, new ByteArrayInputStream(outputstream.toByteArray())); |
| 123 |
} |
| 124 |
} |