| 1 |
/** |
| 2 |
* Copyright (c) 2014 Eclipse contributorsand 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 |
* stefan schalomon, msg-systems.com - Initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.emf.test.xml; |
| 12 |
|
| 13 |
import java.io.IOException; |
| 14 |
import java.io.OutputStream; |
| 15 |
import java.io.StringWriter; |
| 16 |
import java.io.Writer; |
| 17 |
|
| 18 |
import org.eclipse.emf.ecore.resource.URIConverter; |
| 19 |
import org.eclipse.emf.ecore.xmi.impl.XMLString; |
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
public class XMLStringTest |
| 23 |
{ |
| 24 |
/** |
| 25 |
* A test that causes {@link ArrayIndexOutOfBoundsException}. |
| 26 |
*/ |
| 27 |
@Test |
| 28 |
public void testXMLString() throws Exception |
| 29 |
{ |
| 30 |
XMLString doc = new XMLString(); |
| 31 |
// write XML with default encoding - this will resize |
| 32 |
// doc.outputchars to a length greater default BUFFER_SIZE, |
| 33 |
// doc.outputbytes is not used during this call |
| 34 |
writeDoc( doc, false, 5, 10, 1000 ); |
| 35 |
|
| 36 |
// now write a bigger XML with ASCII encoding - doc.outputbytes |
| 37 |
// will be initialized with default BUFFER_SIZE, that is, |
| 38 |
// doc.outputbytes.length < doc.outputchars.length which will |
| 39 |
// eventually trigger the ArrayIndexOutOfBoundsException |
| 40 |
writeDoc( doc, true, 20, 20, 1000 ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* A test with overridden {@link XMLString} that fixes the AIOOBE bug. |
| 45 |
*/ |
| 46 |
@Test |
| 47 |
public void testFixedXMLString() throws Exception |
| 48 |
{ |
| 49 |
XMLString doc = new XMLString() |
| 50 |
{ |
| 51 |
private static final long serialVersionUID = 1L; |
| 52 |
|
| 53 |
@Override |
| 54 |
public void writeAscii( OutputStream os, int flushThreshold ) throws IOException |
| 55 |
{ |
| 56 |
int requiredLength = Math.max( BUFFER_SIZE, outputchars.length ); |
| 57 |
// ensure the byte and character buffers have the same length |
| 58 |
if( outputbytes == null || outputbytes.length < requiredLength ) |
| 59 |
{ |
| 60 |
outputbytes = new byte[requiredLength]; |
| 61 |
} |
| 62 |
|
| 63 |
super.writeAscii( os, flushThreshold ); |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
writeDoc( doc, false, 5, 10, 1000 ); |
| 68 |
// with the fix above, the following call will work |
| 69 |
writeDoc( doc, true, 20, 20, 1000 ); |
| 70 |
} |
| 71 |
|
| 72 |
private void writeDoc( XMLString doc, |
| 73 |
boolean ascii, |
| 74 |
int elementCount, |
| 75 |
int attributeCount, |
| 76 |
int contentSize ) throws Exception |
| 77 |
{ |
| 78 |
doc.reset( null, null, Integer.MAX_VALUE, null ); |
| 79 |
doc.startElement( "doc" ); |
| 80 |
for( int e = 1; e <= elementCount; e++ ) |
| 81 |
{ |
| 82 |
StringBuilder content = new StringBuilder(); |
| 83 |
for( int c = 1; c <= contentSize; c++ ) |
| 84 |
{ |
| 85 |
content.append( Long.toHexString( 0xBAADF00DDE7EC7EDL ) ); |
| 86 |
} |
| 87 |
|
| 88 |
doc.startElement( "elem" + e ); |
| 89 |
for( int a = 1; a <= attributeCount; a++ ) |
| 90 |
{ |
| 91 |
doc.startAttribute( "attr" + a ); |
| 92 |
doc.addAttributeContent( "AttributeValue:" + content.toString() ); |
| 93 |
doc.endAttribute(); |
| 94 |
} |
| 95 |
doc.endContentElement( "ElementContent:" + content.toString() ); |
| 96 |
} |
| 97 |
doc.endElement(); |
| 98 |
|
| 99 |
StringWriter writer = new StringWriter(); |
| 100 |
if( ascii ) |
| 101 |
{ |
| 102 |
doc.writeAscii( new URIConverter.WriteableOutputStream( writer, "ASCII"), Integer.MAX_VALUE ); |
| 103 |
} |
| 104 |
else |
| 105 |
{ |
| 106 |
doc.write( (Writer) writer, Integer.MAX_VALUE ); |
| 107 |
} |
| 108 |
} |
| 109 |
} |