Debian Codegraph @

tests/org.eclipse.emf.test.xml/src/org/eclipse/emf/test/xml/ProcessingInstructionTest.java

raw
1 /**
2 * Copyright (c) 2006 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 * Peter Nehrer IBM - Initial API and implementation
10 */
11 package org.eclipse.emf.test.xml;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertSame;
16 import static org.junit.Assert.assertTrue;
17
18 import java.io.StringReader;
19 import java.util.HashMap;
20
21 import org.eclipse.emf.common.util.URI;
22 import org.eclipse.emf.ecore.EPackage;
23 import org.eclipse.emf.ecore.resource.Resource;
24 import org.eclipse.emf.ecore.resource.ResourceSet;
25 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
26 import org.eclipse.emf.ecore.util.EcoreUtil;
27 import org.eclipse.emf.ecore.util.FeatureMapUtil;
28 import org.eclipse.emf.ecore.xmi.XMLOptions;
29 import org.eclipse.emf.ecore.xmi.XMLResource;
30 import org.eclipse.emf.ecore.xmi.impl.XMLOptionsImpl;
31 import org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl;
32 import org.eclipse.emf.ecore.xmi.util.XMLProcessor;
33 import org.eclipse.emf.ecore.xml.type.AnyType;
34 import org.eclipse.emf.ecore.xml.type.ProcessingInstruction;
35 import org.eclipse.emf.ecore.xml.type.XMLTypeDocumentRoot;
36 import org.eclipse.emf.ecore.xml.type.XMLTypeFactory;
37 import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.xml.sax.InputSource;
41
42
43 public class ProcessingInstructionTest
44 {
45
46 static final String LF = System.getProperty("line.separator");
47
48 static final String XML = "<?pi1 prologue?>" + LF +
49 "<root>" + LF +
50 "\t<?pi2 nested?>" + LF +
51 "</root>" + LF +
52 "<?pi3 epilogue?>";
53
54 private XMLProcessor processor;
55
56 private HashMap<String, Object> options;
57
58 private InputSource input;
59
60 @Before
61 public void setUp() throws Exception
62 {
63 processor = new XMLProcessor();
64 options = new HashMap<String, Object>();
65 XMLOptions xmlOptions = new XMLOptionsImpl();
66 xmlOptions.setProcessAnyXML(true);
67 options.put(XMLResource.OPTION_XML_OPTIONS, xmlOptions);
68 options.put(XMLResource.OPTION_DECLARE_XML, Boolean.FALSE);
69 options.put(XMLResource.OPTION_USE_LEXICAL_HANDLER, Boolean.TRUE);
70 input = new InputSource(new StringReader(XML));
71 }
72
73 @Test
74 public void testLoadPI() throws Exception
75 {
76 Resource res = processor.load(input, options);
77 assertFalse(res.getContents().isEmpty());
78 assertTrue(res.getContents().get(0) instanceof XMLTypeDocumentRoot);
79 XMLTypeDocumentRoot doc = (XMLTypeDocumentRoot) res.getContents().get(0);
80 assertEquals(3, doc.getMixed().size());
81
82 assertSame(XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__PROCESSING_INSTRUCTION, doc.getMixed().getEStructuralFeature(0));
83 assertTrue(doc.getMixed().getValue(0) instanceof ProcessingInstruction);
84 ProcessingInstruction pi = (ProcessingInstruction) doc.getMixed().getValue(0);
85 assertEquals("pi1", pi.getTarget());
86 assertEquals("prologue", pi.getData());
87
88 assertSame(XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__PROCESSING_INSTRUCTION, doc.getMixed().getEStructuralFeature(2));
89 assertTrue(doc.getMixed().getValue(2) instanceof ProcessingInstruction);
90 pi = (ProcessingInstruction) doc.getMixed().getValue(2);
91 assertEquals("pi3", pi.getTarget());
92 assertEquals("epilogue", pi.getData());
93
94 assertTrue(doc.getMixed().getValue(1) instanceof AnyType);
95 AnyType root = (AnyType) doc.getMixed().getValue(1);
96 assertEquals(3, root.getMixed().size());
97 assertSame(XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__PROCESSING_INSTRUCTION, root.getMixed().getEStructuralFeature(1));
98 assertTrue(root.getMixed().getValue(1) instanceof ProcessingInstruction);
99 pi = (ProcessingInstruction) root.getMixed().getValue(1);
100 assertEquals("pi2", pi.getTarget());
101 assertEquals("nested", pi.getData());
102 }
103
104 @Test
105 public void testSavePI() throws Exception
106 {
107 EPackage pkg = processor.getExtendedMetaData().demandPackage(null);
108 XMLTypeDocumentRoot doc = (XMLTypeDocumentRoot) EcoreUtil.create(processor.getExtendedMetaData().getDocumentRoot(pkg));
109 FeatureMapUtil.addProcessingInstruction(doc.getMixed(), "pi1", "prologue");
110 AnyType root = XMLTypeFactory.eINSTANCE.createAnyType();
111 doc.getMixed().add(processor.getExtendedMetaData().demandFeature(null, "root", true), root);
112 FeatureMapUtil.addText(root.getMixed(), "\n\t");
113 FeatureMapUtil.addProcessingInstruction(root.getMixed(), "pi2", "nested");
114 FeatureMapUtil.addText(root.getMixed(), "\n");
115 FeatureMapUtil.addProcessingInstruction(doc.getMixed(), "pi3", "epilogue");
116
117 ResourceSet resourceSet = new ResourceSetImpl();
118 resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMLResourceFactoryImpl());
119 Resource res = resourceSet.createResource(URI.createURI("test.xml"));
120 res.getContents().add(doc);
121 String xml = processor.saveToString(res, options);
122
123 assertEquals(XML, xml);
124 }
125 }