Debian Codegraph @

tests/org.eclipse.emf.test.xml/src/org/eclipse/emf/test/xml/xmi/QNameTest.java

raw
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 import static org.junit.Assert.fail;
16
17 import java.io.ByteArrayInputStream;
18 import java.io.ByteArrayOutputStream;
19 import java.util.HashMap;
20
21 import javax.xml.XMLConstants;
22 import javax.xml.namespace.QName;
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25
26 import org.eclipse.emf.common.util.URI;
27 import org.eclipse.emf.ecore.resource.Resource;
28 import org.eclipse.emf.ecore.resource.ResourceSet;
29 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
30 import org.eclipse.emf.ecore.util.ExtendedMetaData;
31 import org.eclipse.emf.ecore.xmi.XMLResource;
32 import org.eclipse.emf.ecore.xmi.impl.GenericXMLResourceFactoryImpl;
33 import org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl;
34 import org.eclipse.emf.ecore.xml.type.XMLTypeFactory;
35 import org.eclipse.emf.ecore.xml.type.util.XMLTypeUtil;
36 import org.eclipse.emf.test.common.TestUtil;
37 import org.eclipse.emf.test.models.qname.DocumentRoot;
38 import org.eclipse.emf.test.models.qname.QNameFactory;
39 import org.eclipse.emf.test.models.qname.QNamePackage;
40 import org.eclipse.emf.test.xml.AllSuites;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Test;
44
45
46 /**
47 * XMI tests: loading qname.xml
48 */
49 public class QNameTest
50 {
51 final static String BASE_XML_URI = TestUtil.getPluginDirectory(AllSuites.PLUGIN_ID) + "/data/xml/";
52
53 final static String BASE_XMI_URI = TestUtil.getPluginDirectory(AllSuites.PLUGIN_ID) + "/data/xmi/";
54
55 protected DocumentBuilder builder;
56
57 protected String inputXML;
58
59 protected String expectedXML;
60
61 protected HashMap<String, Object> options;
62
63 @Before
64 public void setUp() throws Exception
65 {
66 QNamePackage.eINSTANCE.getName();
67 Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xml", new XMLResourceFactoryImpl());
68 inputXML = BASE_XML_URI + "qname.xml";
69 expectedXML = BASE_XMI_URI + "qnameOutput.xml";
70 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
71 factory.setNamespaceAware(true);
72 builder = factory.newDocumentBuilder();
73 options = new HashMap<String, Object>();
74 }
75
76 @After
77 public void tearDown() throws Exception
78 {
79 builder = null;
80 options = null;
81 }
82
83 @Test
84 public void testQname() throws Exception
85 {
86 URI uri = URI.createFileURI(inputXML);
87 ResourceSet resourceSet = new ResourceSetImpl();
88 Resource resource = resourceSet.createResource(uri);
89 options.put(XMLResource.OPTION_EXTENDED_META_DATA, ExtendedMetaData.INSTANCE);
90 resource.load(options);
91
92 ByteArrayOutputStream outputstream = new ByteArrayOutputStream(2064);
93 resource.save(outputstream, options);
94
95 CompareXML.compareFiles(builder, expectedXML, new ByteArrayInputStream(outputstream.toByteArray()));
96 }
97
98 @Test
99 public void testNullPrefixQname() throws Exception
100 {
101 ResourceSet resourceSet = new ResourceSetImpl();
102 resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", new GenericXMLResourceFactoryImpl());
103 Resource resource = resourceSet.createResource(URI.createURI("dummy.xml"));
104 DocumentRoot documentRoot = QNameFactory.eINSTANCE.createDocumentRoot();
105 documentRoot.getXMLNSPrefixMap().put("", "namespace");
106 documentRoot.setAnyE(XMLTypeFactory.eINSTANCE.createQName("namespace", "qname", ""));
107 resource.getContents().add(documentRoot);
108 ByteArrayOutputStream outputstream = new ByteArrayOutputStream(2064);
109 resource.save(outputstream, options);
110 ByteArrayInputStream in = new ByteArrayInputStream(outputstream.toByteArray());
111 Resource resource2 = resourceSet.createResource(URI.createURI("dummy2.xml"));
112 resource2.load(in, options);
113 DocumentRoot documentRoot2 = (DocumentRoot)resource2.getContents().get(0);
114 QName qname = documentRoot2.getAnyE();
115 assertEquals(qname.getPrefix(), "");
116 }
117
118 @Test
119 public void testValidQNames()
120 {
121 QName qname = (QName)XMLTypeUtil.createQName("http://www.example.org/test", "name", "test");
122 assertEquals("http://www.example.org/test", qname.getNamespaceURI());
123 assertEquals("name", qname.getLocalPart());
124 assertEquals("test", qname.getPrefix());
125
126 qname = (QName)XMLTypeUtil.createQName(null, "name", "test");
127 assertEquals(XMLConstants.NULL_NS_URI, qname.getNamespaceURI());
128 assertEquals("name", qname.getLocalPart());
129 assertEquals("test", qname.getPrefix());
130
131 qname = (QName)XMLTypeUtil.createQName("http://www.example.org/test", "name", null);
132 assertEquals("http://www.example.org/test", qname.getNamespaceURI());
133 assertEquals("name", qname.getLocalPart());
134 assertEquals(XMLConstants.DEFAULT_NS_PREFIX, qname.getPrefix());
135
136 qname = (QName)XMLTypeUtil.createQName(null, "name", null);
137 assertEquals(XMLConstants.NULL_NS_URI, qname.getNamespaceURI());
138 assertEquals("name", qname.getLocalPart());
139 assertEquals(XMLConstants.DEFAULT_NS_PREFIX, qname.getPrefix());
140
141 try
142 {
143 qname = (QName)XMLTypeUtil.createQName("http://www.example.org/test", null, "test");
144 fail("null local part is invalid and should not be allowed");
145 }
146 catch (IllegalArgumentException e)
147 {
148 // This is expected: a null local part is not allowed.
149 }
150 }
151 }