| 1 |
/** |
| 2 |
* Copyright (c) 2002-2004 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.xmi; |
| 13 |
|
| 14 |
|
| 15 |
import static org.junit.Assert.assertEquals; |
| 16 |
import static org.junit.Assert.assertFalse; |
| 17 |
|
| 18 |
import java.io.IOException; |
| 19 |
import java.io.StringWriter; |
| 20 |
import java.net.MalformedURLException; |
| 21 |
|
| 22 |
import org.eclipse.emf.common.util.URI; |
| 23 |
import org.eclipse.emf.ecore.EPackage; |
| 24 |
import org.eclipse.emf.ecore.impl.EPackageRegistryImpl; |
| 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.BasicExtendedMetaData; |
| 30 |
import org.eclipse.emf.ecore.util.ExtendedMetaData; |
| 31 |
import org.eclipse.emf.ecore.xmi.XMLResource; |
| 32 |
import org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl; |
| 33 |
import org.eclipse.emf.test.common.TestUtil; |
| 34 |
import org.eclipse.emf.test.models.library.Book; |
| 35 |
import org.eclipse.emf.test.models.library.Library; |
| 36 |
import org.eclipse.emf.test.models.library.LibraryFactory; |
| 37 |
import org.eclipse.emf.test.models.library.LibraryPackage; |
| 38 |
import org.eclipse.emf.test.models.library.Writer; |
| 39 |
import org.eclipse.emf.test.xml.AllSuites; |
| 40 |
import org.junit.After; |
| 41 |
import org.junit.Before; |
| 42 |
import org.junit.Test; |
| 43 |
|
| 44 |
|
| 45 |
public class LaxFeatureNamespaceMatchingTest |
| 46 |
{ |
| 47 |
final static String BASE_XML_URI = TestUtil.getPluginDirectory(AllSuites.PLUGIN_ID) + "/data/xml/"; |
| 48 |
final static String LF = System.getProperty("line.separator"); |
| 49 |
|
| 50 |
ResourceSet resourceSet; |
| 51 |
|
| 52 |
@Before |
| 53 |
public void setUp() throws Exception |
| 54 |
{ |
| 55 |
resourceSet = new ResourceSetImpl(); |
| 56 |
LibraryPackage.eINSTANCE.eClass(); |
| 57 |
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put |
| 58 |
("*", |
| 59 |
new XMLResourceFactoryImpl() |
| 60 |
{ |
| 61 |
@Override |
| 62 |
public Resource createResource(URI uri) |
| 63 |
{ |
| 64 |
XMLResource result = (XMLResource)super.createResource(uri); |
| 65 |
|
| 66 |
ExtendedMetaData extendedMetaData = |
| 67 |
new BasicExtendedMetaData(new EPackageRegistryImpl(EPackage.Registry.INSTANCE)) |
| 68 |
{ |
| 69 |
@Override |
| 70 |
protected boolean isFeatureNamespaceMatchingLax() |
| 71 |
{ |
| 72 |
return true; |
| 73 |
} |
| 74 |
}; |
| 75 |
extendedMetaData.putPackage(null, LibraryPackage.eINSTANCE); |
| 76 |
result.getDefaultLoadOptions().put(XMLResource.OPTION_EXTENDED_META_DATA, extendedMetaData); |
| 77 |
result.getDefaultSaveOptions().put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE); |
| 78 |
|
| 79 |
result.getDefaultLoadOptions().put(XMLResource.OPTION_USE_ENCODED_ATTRIBUTE_STYLE, Boolean.TRUE); |
| 80 |
result.getDefaultSaveOptions().put(XMLResource.OPTION_USE_ENCODED_ATTRIBUTE_STYLE, Boolean.TRUE); |
| 81 |
|
| 82 |
result.getDefaultLoadOptions().put(XMLResource.OPTION_USE_LEXICAL_HANDLER, Boolean.TRUE); |
| 83 |
|
| 84 |
return result; |
| 85 |
} |
| 86 |
}); |
| 87 |
} |
| 88 |
|
| 89 |
@After |
| 90 |
public void tearDown() throws Exception |
| 91 |
{ |
| 92 |
resourceSet = null; |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void test() throws MalformedURLException, IOException |
| 97 |
{ |
| 98 |
Resource firstLibraryResource = resourceSet.createResource(URI.createFileURI(BASE_XML_URI + "first.library")); |
| 99 |
Library firstLibrary = LibraryFactory.eINSTANCE.createLibrary(); |
| 100 |
firstLibrary.setName("first"); |
| 101 |
Book firstBook = LibraryFactory.eINSTANCE.createBook(); |
| 102 |
firstBook.setTitle("First Book"); |
| 103 |
firstLibrary.getBooks().add(firstBook); |
| 104 |
firstLibraryResource.getContents().add(firstLibrary); |
| 105 |
StringWriter firstWriter = new StringWriter(); |
| 106 |
firstLibraryResource.save(new URIConverter.WriteableOutputStream(firstWriter, "UTF-8"), null); |
| 107 |
|
| 108 |
Resource secondLibraryResource = resourceSet.createResource(URI.createFileURI(BASE_XML_URI + "second.library")); |
| 109 |
Library secondLibrary = LibraryFactory.eINSTANCE.createLibrary(); |
| 110 |
secondLibrary.setName("second"); |
| 111 |
Book secondBook = LibraryFactory.eINSTANCE.createBook(); |
| 112 |
secondBook.setTitle("Second Book"); |
| 113 |
secondLibrary.getBooks().add(secondBook); |
| 114 |
secondLibraryResource.getContents().add(secondLibrary); |
| 115 |
StringWriter secondWriter = new StringWriter(); |
| 116 |
secondLibraryResource.save(new URIConverter.WriteableOutputStream(secondWriter, "UTF-8"), null); |
| 117 |
|
| 118 |
XMLResource mainLibraryResource = (XMLResource)resourceSet.createResource(URI.createFileURI(BASE_XML_URI + "main.library")); |
| 119 |
Library mainLibrary = LibraryFactory.eINSTANCE.createLibrary(); |
| 120 |
mainLibrary.setName("main"); |
| 121 |
Writer firstAuthor = LibraryFactory.eINSTANCE.createWriter(); |
| 122 |
firstAuthor.setName("First Author"); |
| 123 |
firstAuthor.getBooks().add(firstBook); |
| 124 |
mainLibrary.getWriters().add(firstAuthor); |
| 125 |
Writer secondAuthor = LibraryFactory.eINSTANCE.createWriter(); |
| 126 |
secondAuthor.setName("Second Author"); |
| 127 |
secondAuthor.getBooks().add(secondBook); |
| 128 |
mainLibrary.getWriters().add(secondAuthor); |
| 129 |
mainLibraryResource.getContents().add(mainLibrary); |
| 130 |
|
| 131 |
StringWriter mainWriter = new StringWriter(); |
| 132 |
mainLibraryResource.save(new URIConverter.WriteableOutputStream(mainWriter, "UTF-8"), null); |
| 133 |
String expectedResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LF + |
| 134 |
"<org.eclipse.emf.test.models.library:Library xmlns:org.eclipse.emf.test.models.library=\"http:///org.eclipse.emf.test.models/Library\" name=\"main\">" + LF + |
| 135 |
|
| 136 |
" <writers name=\"First Author\" books=\"first.library#//@books.0\"/>" + LF + |
| 137 |
" <writers name=\"Second Author\" books=\"second.library#//@books.0\"/>" + LF + |
| 138 |
"</org.eclipse.emf.test.models.library:Library>" + LF; |
| 139 |
String result = mainWriter.toString(); |
| 140 |
assertEquals(expectedResult, result); |
| 141 |
|
| 142 |
Resource reloadedMainLibraryResource = resourceSet.createResource(URI.createFileURI(BASE_XML_URI + "/main2.library")); |
| 143 |
result = result.replaceAll(":?org\\.eclipse\\.emf\\.test\\.models\\.library:?", ""); |
| 144 |
assertFalse(expectedResult.equals(result)); |
| 145 |
// System.err.println("###" + result); |
| 146 |
reloadedMainLibraryResource.load(new URIConverter.ReadableInputStream(result), null); |
| 147 |
mainWriter = new StringWriter(); |
| 148 |
mainLibraryResource.save(new URIConverter.WriteableOutputStream(mainWriter, "UTF-8"), null); |
| 149 |
result = mainWriter.toString(); |
| 150 |
assertEquals(expectedResult, result); |
| 151 |
|
| 152 |
reloadedMainLibraryResource = resourceSet.createResource(URI.createFileURI(BASE_XML_URI + "/main3.library")); |
| 153 |
result = result.replaceAll("<writers ", "<org.eclipse.emf.test.models.library:writers "); |
| 154 |
assertFalse(expectedResult.equals(result)); |
| 155 |
// System.err.println("###" + result); |
| 156 |
reloadedMainLibraryResource.load(new URIConverter.ReadableInputStream(result), null); |
| 157 |
mainWriter = new StringWriter(); |
| 158 |
mainLibraryResource.save(new URIConverter.WriteableOutputStream(mainWriter, "UTF-8"), null); |
| 159 |
assertEquals(expectedResult, mainWriter.toString()); |
| 160 |
|
| 161 |
reloadedMainLibraryResource = resourceSet.createResource(URI.createFileURI(BASE_XML_URI + "/main4.library")); |
| 162 |
result = result.replaceAll(":?org.eclipse.emf.test.models.library:?", "").replaceAll("xmlns=[^ ]* ", ""); |
| 163 |
// System.err.println("###" + result); |
| 164 |
assertFalse(expectedResult.equals(result)); |
| 165 |
reloadedMainLibraryResource.load(new URIConverter.ReadableInputStream(result), null); |
| 166 |
mainWriter = new StringWriter(); |
| 167 |
mainLibraryResource.save(new URIConverter.WriteableOutputStream(mainWriter, "UTF-8"), null); |
| 168 |
result = mainWriter.toString(); |
| 169 |
assertEquals(expectedResult, result); |
| 170 |
} |
| 171 |
} |