| 1 |
/** |
| 2 |
* Copyright (c) 2011 Ed Merks 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 |
* Ed Merks - Initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.emf.test.xml.xmi; |
| 12 |
|
| 13 |
import static org.junit.Assert.assertEquals; |
| 14 |
import static org.junit.Assert.fail; |
| 15 |
|
| 16 |
import java.io.IOException; |
| 17 |
import java.io.StringWriter; |
| 18 |
import java.util.HashMap; |
| 19 |
import java.util.Map; |
| 20 |
|
| 21 |
import org.eclipse.emf.common.util.URI; |
| 22 |
import org.eclipse.emf.ecore.EPackage; |
| 23 |
import org.eclipse.emf.ecore.EcorePackage; |
| 24 |
import org.eclipse.emf.ecore.resource.Resource; |
| 25 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
| 26 |
import org.eclipse.emf.ecore.resource.URIConverter; |
| 27 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 28 |
import org.eclipse.emf.ecore.util.EcoreUtil; |
| 29 |
import org.eclipse.emf.ecore.xmi.XMLResource; |
| 30 |
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl; |
| 31 |
import org.eclipse.emf.ecore.xmi.impl.GenericXMLResourceFactoryImpl; |
| 32 |
import org.junit.After; |
| 33 |
import org.junit.Before; |
| 34 |
import org.junit.Test; |
| 35 |
|
| 36 |
|
| 37 |
public class LineWidthTest |
| 38 |
{ |
| 39 |
protected ResourceSet resourceSet; |
| 40 |
|
| 41 |
@Before |
| 42 |
public void setUp() throws Exception |
| 43 |
{ |
| 44 |
resourceSet = new ResourceSetImpl(); |
| 45 |
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", new GenericXMLResourceFactoryImpl()); |
| 46 |
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new EcoreResourceFactoryImpl()); |
| 47 |
} |
| 48 |
|
| 49 |
@After |
| 50 |
public void tearDown() throws Exception |
| 51 |
{ |
| 52 |
resourceSet = null; |
| 53 |
} |
| 54 |
|
| 55 |
public void process(String resourceName, int lineWidth, int attributeCount) |
| 56 |
{ |
| 57 |
EPackage ecorePackage = EcoreUtil.copy(EcorePackage.eINSTANCE); |
| 58 |
Resource ecoreResource = resourceSet.createResource(URI.createURI(resourceName)); |
| 59 |
ecoreResource.getContents().add(ecorePackage); |
| 60 |
|
| 61 |
Map<Object, Object> options = new HashMap<Object, Object>(); |
| 62 |
options.put(XMLResource.OPTION_LINE_WIDTH, lineWidth); |
| 63 |
|
| 64 |
StringWriter writer1 = new StringWriter(); |
| 65 |
StringWriter writer2 = new StringWriter(); |
| 66 |
try |
| 67 |
{ |
| 68 |
ecoreResource.save(new URIConverter.WriteableOutputStream(writer1, "UTF8"), options); |
| 69 |
options.put(XMLResource.OPTION_USE_FILE_BUFFER, true); |
| 70 |
ecoreResource.save(new URIConverter.WriteableOutputStream(writer2, "UTF8"), options); |
| 71 |
} |
| 72 |
catch (IOException exception) |
| 73 |
{ |
| 74 |
fail("Failure saving to XML"); |
| 75 |
} |
| 76 |
|
| 77 |
String string1 = writer1.toString(); |
| 78 |
String string2 = writer2.toString(); |
| 79 |
assertEquals(string1, string2); |
| 80 |
String[] lines = string1.split("\\n"); |
| 81 |
String firstElement = lines[1]; |
| 82 |
int count = 0; |
| 83 |
for (int i = 0, length = firstElement.length(); i < length; ++i) |
| 84 |
{ |
| 85 |
if (firstElement.charAt(i) == '=') |
| 86 |
{ |
| 87 |
++count; |
| 88 |
} |
| 89 |
} |
| 90 |
assertEquals("First line doesn't contain " + attributeCount + "attribute(s): " + firstElement, attributeCount, count); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void testXMLShortLine() |
| 95 |
{ |
| 96 |
process("Ecore.xml", 10, 0); |
| 97 |
} |
| 98 |
|
| 99 |
@Test |
| 100 |
public void testXMLMediumLine() |
| 101 |
{ |
| 102 |
process("Ecore.xml", 30, 1); |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void testXMLLongLine() |
| 107 |
{ |
| 108 |
process("Ecore.xml", 70, 2); |
| 109 |
} |
| 110 |
|
| 111 |
@Test |
| 112 |
public void testXMLVeryLongLine() |
| 113 |
{ |
| 114 |
process("Ecore.xml", 1000, 5); |
| 115 |
} |
| 116 |
|
| 117 |
@Test |
| 118 |
public void testXMIShortLine() |
| 119 |
{ |
| 120 |
process("Ecore.xmi", 10, 0); |
| 121 |
} |
| 122 |
|
| 123 |
@Test |
| 124 |
public void testXMIMediumLine() |
| 125 |
{ |
| 126 |
process("Ecore.xmi", 30, 1); |
| 127 |
} |
| 128 |
|
| 129 |
@Test |
| 130 |
public void testXMILongLine() |
| 131 |
{ |
| 132 |
process("Ecore.xmi", 40, 2); |
| 133 |
} |
| 134 |
|
| 135 |
@Test |
| 136 |
public void testXMIVeryLongLine() |
| 137 |
{ |
| 138 |
process("Ecore.xmi", 1000, 7); |
| 139 |
} |
| 140 |
} |