/*
* XML 2 Java Binding (X2JB) - the excellent Java tool.
* Copyright 2009, by Richard Opalka.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not see the FSF site:
* http://www.fsf.org/ and search for the LGPL License document there.
*/
package test.org.x2jb.bind;
import java.math.BigDecimal;
import java.math.BigInteger;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.w3c.dom.Element;
import org.x2jb.bind.XML2Java;
import test.sample.invalid.AttributeHandlerMappedToElement;
import test.sample.invalid.AttributeRequiredButNotPresent;
import test.sample.invalid.AttributeRequiredButNotPresent2;
import test.sample.invalid.BindingMismatch;
import test.sample.invalid.ElementHandlerMappedToAttribute;
import test.sample.invalid.ElementRequiredButNotPresent;
import test.sample.invalid.ElementRequiredButNotPresent2;
import test.sample.invalid.ElementRequiredButNotPresent3;
import test.sample.invalid.ElementRequiredButNotPresent4;
import test.sample.invalid.Getter2;
import test.sample.invalid.GetterThrowsException;
import test.sample.invalid.InnerIfaceMappedToAttribute;
import test.sample.invalid.InnerIfaceNamespaceMismatch;
import test.sample.invalid.InvalidElementName;
import test.sample.invalid.MultipleElementsButSimpleReturnType;
import test.sample.invalid.MultipleElementsButSimpleReturnType2;
import test.sample.invalid.NoGetter1;
import test.sample.invalid.NoGetter2;
import test.sample.invalid.NotExistingAttributeHandler;
import test.sample.invalid.NotExistingElementHandler;
import test.sample.invalid.NullParameterForRequiredBean;
import test.sample.invalid.NullParameterForRequiredBeanArray;
import test.sample.invalid.Setter2;
import test.sample.invalid.SetterGetterTypeMismatch;
import test.sample.invalid.SetterThrowsException;
import test.sample.invalid.WrongGetterSignature1;
import test.sample.invalid.WrongGetterSignature2;
import test.sample.invalid.WrongGetterSignature3;
import test.sample.invalid.WrongGetterSignature4;
import test.sample.invalid.WrongGetterSignature5;
import test.sample.invalid.WrongGetterSignature6;
import test.sample.invalid.WrongMultiDimensionalArray1;
import test.sample.invalid.WrongMultiDimensionalArray2;
import test.sample.invalid.WrongSetterSignature1;
import test.sample.invalid.WrongSetterSignature2;
import test.sample.invalid.WrongSetterSignature3;
import test.sample.invalid.WrongSetterSignature4;
import test.sample.valid.AllSupportedSimpleTypes;
import test.sample.valid.AllSupportedSimpleTypesWrongAttributeValue;
import test.sample.valid.AllSupportedSimpleTypesWrongElementValue;
import test.sample.valid.Config1;
import test.sample.valid.Config2;
import test.sample.valid.Config3;
import test.sample.valid.CustomHandler;
import test.sample.valid.CustomHandler2;
import test.sample.valid.CustomHandler4;
import test.sample.valid.DefaultValues;
import test.sample.valid.ElementOrAttributeNotPresent;
import test.sample.valid.MultiDimensionalArrays;
import test.sample.valid.MutableConfig;
import test.sample.valid.ServerConfig;
/**
* XML2Java Binding Java Tool Test
*
* @author <a href="mailto:richard_opalka@yahoo.com">Richard Opalka</a>
*/
public final class XML2JavaTest extends TestCase
{
private DocumentBuilder builder;
public XML2JavaTest( final String s )
{
super( s );
}
protected void setUp() throws Exception
{
super.setUp();
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware( true );
dbf.setExpandEntityReferences( true );
dbf.setIgnoringComments( true );
this.builder = dbf.newDocumentBuilder();
}
protected void tearDown() throws Exception
{
this.builder = null;
super.tearDown();
}
public void testNullReturnValues() throws Exception
{
final Element element = this.getRootElement( "/correct/test1.xml" );
final ElementOrAttributeNotPresent cfg = XML2Java.bind( element, ElementOrAttributeNotPresent.class );
Assert.assertNull( "Expected null", cfg.getNoSuchAttribute() );
Assert.assertNull( "Expected null", cfg.getNoSuchElement() );
Assert.assertTrue( "Expected zero length array", cfg.getNoSuchElementAsArray().length == 0 );
}
public void testMutableConfigsPlusDefaultBindings() throws Exception
{
final Element element = this.getRootElement( "/correct/config.xml" );
final MutableConfig config = XML2Java.bind( element, MutableConfig.class );
// assert values load from config file - this verifies default binding is constructed properly
Assert.assertFalse( "Unexpected primitive boolean value", config.getBooleanPrimitive() );
Assert.assertTrue( "Unexpected primitive byte value", config.getBytePrimitive() == ( ( byte ) 1 ) );
Assert.assertTrue( "Unexpected primitive char value", config.getCharPrimitive() == ( ( char ) 'c' ) );
Assert.assertTrue( "Unexpected primitive short value", config.getShortPrimitive() == ( ( short ) 2 ) );
Assert.assertTrue( "Unexpected primitive int value", config.getIntPrimitive() == 3 );
Assert.assertTrue( "Unexpected primitive long value", config.getLongPrimitive() == 4 );
Assert.assertTrue( "Unexpected primitive float value", config.getFloatPrimitive() == ( ( float ) 5. ) );
Assert.assertTrue( "Unexpected primitive double value", config.getDoublePrimitive() == 6. );
Assert.assertTrue( "Unexpected Boolean value", config.getBoolean().equals( Boolean.FALSE ) );
Assert.assertTrue( "Unexpected Byte value", config.getByte().equals( Byte.valueOf( ( byte ) 7 ) ) );
Assert.assertTrue( "Unexpected Character value", config.getCharacter().equals( Character.valueOf( 'C' ) ) );
Assert.assertTrue( "Unexpected Short value", config.getShort().equals( Short.valueOf( ( short ) 8 ) ) );
Assert.assertTrue( "Unexpected Integer value", config.getInteger().equals( Integer.valueOf( 9 ) ) );
Assert.assertTrue( "Unexpected Long value", config.getLong().equals( Long.valueOf( 10 ) ) );
Assert.assertTrue( "Unexpected Float value", config.getFloat().equals( Float.valueOf( ( float ) 11. ) ) );
Assert.assertTrue( "Unexpected Double value", config.getDouble().equals( Double.valueOf( 12. ) ) );
Assert.assertTrue( "Unexpected String value", config.getString().equals( "hello" ) );
Assert.assertTrue( "Unexpected QName value",
config.getQName().equals( QName.valueOf( "{http://mycompany}/department" ) ) );
Assert.assertTrue( "Unexpected BigInteger value", config.getBigInteger().equals( BigInteger.valueOf( 13 ) ) );
Assert.assertTrue( "Unexpected BigDecimal value", config.getBigDecimal().equals( BigDecimal.valueOf( 14 ) ) );
XML2JavaTest.ensureArray( config.getBooleanArray(), Boolean.FALSE, 4 );
// modifying values
config.setBooleanPrimitive( true );
Assert.assertTrue( "Unexpected primitive boolean value", config.getBooleanPrimitive() );
config.setBytePrimitive( ( byte ) 2 );
Assert.assertTrue( "Unexpected primitive byte value", config.getBytePrimitive() == ( ( byte ) 2 ) );
config.setCharPrimitive( 'n' );
Assert.assertTrue( "Unexpected primitive char value", config.getCharPrimitive() == ( ( char ) 'n' ) );
config.setShortPrimitive( ( short ) 4 );
Assert.assertTrue( "Unexpected primitive short value", config.getShortPrimitive() == ( ( short ) 4 ) );
config.setIntPrimitive( 6 );
Assert.assertTrue( "Unexpected primitive int value", config.getIntPrimitive() == 6 );
config.setLongPrimitive( 8 );
Assert.assertTrue( "Unexpected primitive long value", config.getLongPrimitive() == 8 );
config.setFloatPrimitive( ( float ) 10. );
Assert.assertTrue( "Unexpected primitive float value", config.getFloatPrimitive() == ( ( float ) 10. ) );
config.setDoublePrimitive( 12. );
Assert.assertTrue( "Unexpected primitive double value", config.getDoublePrimitive() == 12. );
config.setBoolean( Boolean.TRUE );
Assert.assertTrue( "Unexpected Boolean value", config.getBoolean().equals( Boolean.TRUE ) );
config.setByte( Byte.valueOf( ( byte ) 14 ) );
Assert.assertTrue( "Unexpected Byte value", config.getByte().equals( Byte.valueOf( ( byte ) 14 ) ) );
config.setCharacter( Character.valueOf( 'N' ) );
Assert.assertTrue( "Unexpected Character value", config.getCharacter().equals( Character.valueOf( 'N' ) ) );
config.setShort( Short.valueOf( ( short ) 16 ) );
Assert.assertTrue( "Unexpected Short value", config.getShort().equals( Short.valueOf( ( short ) 16 ) ) );
config.setInteger( Integer.valueOf( 18 ) );
Assert.assertTrue( "Unexpected Integer value", config.getInteger().equals( Integer.valueOf( 18 ) ) );
config.setLong( Long.valueOf( 20 ) );
Assert.assertTrue( "Unexpected Long value", config.getLong().equals( Long.valueOf( 20 ) ) );
config.setFloat( Float.valueOf( ( float ) 22. ) );
Assert.assertTrue( "Unexpected Float value", config.getFloat().equals( Float.valueOf( ( float ) 22. ) ) );
config.setDouble( Double.valueOf( 24. ) );
Assert.assertTrue( "Unexpected Double value", config.getDouble().equals( Double.valueOf( 24. ) ) );
config.setString( "modified hello" );
Assert.assertTrue( "Unexpected String value", config.getString().equals( "modified hello" ) );
config.setQName( QName.valueOf( "{http://mycompany}/modified_department" ) );
Assert.assertTrue( "Unexpected QName value",
config.getQName().equals( QName.valueOf( "{http://mycompany}/modified_department" ) ) );
config.setBigInteger( BigInteger.valueOf( 26 ) );
Assert.assertTrue( "Unexpected BigInteger value", config.getBigInteger().equals( BigInteger.valueOf( 26 ) ) );
config.setBigDecimal( BigDecimal.valueOf( 28. ) );
Assert.assertTrue( "Unexpected BigDecimal value", config.getBigDecimal().equals( BigDecimal.valueOf( 28. ) ) );
final Boolean[] clonedArray = config.getBooleanArray();
for ( int i = 0; i < clonedArray.length; i++ )
{
clonedArray[ i ] = Boolean.TRUE;
}
XML2JavaTest.ensureArray( config.getBooleanArray(), Boolean.FALSE, 4 );
config.setBooleanArray( clonedArray );
XML2JavaTest.ensureArray( config.getBooleanArray(), Boolean.TRUE, 4 );
}
private static void ensureArray( final Boolean[] array, final Boolean expectedValue, final int expectedLength )
{
Assert.assertNotNull( "Unextected array value", array );
Assert.assertTrue( "Unxpected array length", array.length == expectedLength );
for ( int i = 0; i < array.length; i++ )
{
Assert.assertTrue( array[ i ].equals( expectedValue ) );
}
}
public void testObjectMethods() throws Exception
{
final Element element = this.getRootElement( "/correct/test1.xml" );
final ElementOrAttributeNotPresent cfg1 = XML2Java.bind( element, ElementOrAttributeNotPresent.class );
final ElementOrAttributeNotPresent cfg2 = XML2Java.bind( element, ElementOrAttributeNotPresent.class );
Assert.assertNotNull( cfg1.toString() );
Assert.assertNotNull( cfg2.toString() );
Assert.assertFalse( "Two different proxies can't have the same hashcode", cfg1.hashCode() == cfg2.hashCode() );
Assert.assertFalse( "Two different proxies can't be the same", cfg1.equals( cfg2 ) );
}
public void testAllSupportedSimpleTypes() throws Exception
{
final Element element = this.getRootElement( "/correct/test5.xml" );
final AllSupportedSimpleTypes cfg = XML2Java.bind( element, AllSupportedSimpleTypes.class );
Assert.assertEquals( "a00", cfg.getAttributeAsString(), "0" );
Assert.assertEquals( "a01", cfg.getAttributeAsQName(), QName.valueOf( "{http://namespace}/attribute" ) );
Assert.assertEquals( "a02", cfg.getAttributeAsBoolean(), Boolean.FALSE );
Assert.assertEquals( "a03", cfg.getAttributeAsByte(), new Byte( "0" ) );
Assert.assertEquals( "a04", cfg.getAttributeAsCharacter(), new Character( '0' ) );
Assert.assertEquals( "a05", cfg.getAttributeAsShort(), new Short( "0" ) );
Assert.assertEquals( "a06", cfg.getAttributeAsInteger(), new Integer( "0" ) );
Assert.assertEquals( "a07", cfg.getAttributeAsLong(), new Long( "0" ) );
Assert.assertEquals( "a08", cfg.getAttributeAsFloat(), new Float( "0" ) );
Assert.assertEquals( "a09", cfg.getAttributeAsDouble(), new Double( "0" ) );
Assert.assertEquals( "a10", cfg.getAttributeAsBigDecimal(), BigDecimal.ZERO );
Assert.assertEquals( "a11", cfg.getAttributeAsBigInteger(), BigInteger.ZERO );
Assert.assertFalse( "a12", cfg.getAttributeAsPrimitiveBoolean() );
Assert.assertTrue( "a13", cfg.getAttributeAsPrimitiveByte() == ( ( byte ) 0 ) );
Assert.assertTrue( "a14", cfg.getAttributeAsPrimitiveCharacter() == ( ( char ) '0' ) );
Assert.assertTrue( "a15", cfg.getAttributeAsPrimitiveShort() == ( ( short ) 0 ) );
Assert.assertTrue( "a16", cfg.getAttributeAsPrimitiveInteger() == ( ( int ) 0 ) );
Assert.assertTrue( "a17", cfg.getAttributeAsPrimitiveLong() == ( ( long ) 0 ) );
Assert.assertTrue( "a18", cfg.getAttributeAsPrimitiveFloat() == ( ( float ) 0 ) );
Assert.assertTrue( "a19", cfg.getAttributeAsPrimitiveDouble() == ( ( double ) 0 ) );
Assert.assertEquals( "e00", cfg.getElementAsString(), "1" );
Assert.assertEquals( "a01", cfg.getElementAsQName(), QName.valueOf( "{http://namespace}/element" ) );
Assert.assertEquals( "e02", cfg.getElementAsBigDecimal(), BigDecimal.ONE );
Assert.assertEquals( "e03", cfg.getElementAsBigInteger(), BigInteger.ONE );
Assert.assertEquals( "e04", cfg.getElementAsBoolean(), Boolean.TRUE );
Assert.assertEquals( "e05", cfg.getElementAsByte(), new Byte( "1" ) );
Assert.assertEquals( "e06", cfg.getElementAsCharacter(), new Character( '1' ) );
Assert.assertEquals( "e07", cfg.getElementAsShort(), new Short( "1" ) );
Assert.assertEquals( "e08", cfg.getElementAsInteger(), new Integer( "1" ) );
Assert.assertEquals( "e09", cfg.getElementAsLong(), new Long( "1" ) );
Assert.assertEquals( "e10", cfg.getElementAsFloat(), new Float( "1" ) );
Assert.assertEquals( "e11", cfg.getElementAsDouble(), new Double( "1" ) );
Assert.assertTrue( "e12", cfg.getElementAsPrimitiveBoolean() );
Assert.assertTrue( "e13", cfg.getElementAsPrimitiveByte() == ( ( byte ) 1 ) );
Assert.assertTrue( "e14", cfg.getElementAsPrimitiveCharacter() == ( ( char ) '1' ) );
Assert.assertTrue( "e15", cfg.getElementAsPrimitiveShort() == ( ( short ) 1 ) );
Assert.assertTrue( "e16", cfg.getElementAsPrimitiveInteger() == ( ( int ) 1 ) );
Assert.assertTrue( "e17", cfg.getElementAsPrimitiveLong() == ( ( long ) 1 ) );
Assert.assertTrue( "e18", cfg.getElementAsPrimitiveFloat() == ( ( float ) 1 ) );
Assert.assertTrue( "e19", cfg.getElementAsPrimitiveDouble() == ( ( double ) 1 ) );
}
public void testNotInterfaceAttack() throws Exception
{
this.runNegativeTest(
"/correct/test5.xml",
String.class,
new String[]
{
"Passed class reference",
"is not interface",
}
);
}
public void testAllSupportedSimpleTypesWrongValue() throws Exception
{
this.runNegativeTest(
"/correct/test5.xml",
AllSupportedSimpleTypesWrongAttributeValue.class,
new String[]
{
"Incorrect value",
"abc",
}
);
this.runNegativeTest(
"/correct/test5.xml",
AllSupportedSimpleTypesWrongElementValue.class,
new String[]
{
"Incorrect value",
"def",
}
);
}
public void testDefaultValues() throws Exception
{
final Element element = this.getRootElement( "/correct/test5.xml" );
final DefaultValues cfg = XML2Java.bind( element, DefaultValues.class );
Assert.assertTrue( "b00", cfg.getDefaultStringArray().length == 0 );
Assert.assertTrue( "b10", cfg.getDefaultBigDecimalArray().length == 0 );
Assert.assertTrue( "b20", cfg.getDefaultBigIntegerArray().length == 0 );
Assert.assertTrue( "b30", cfg.getDefaultByteArray().length == 0 );
Assert.assertTrue( "b40", cfg.getDefaultCharacterArray().length == 0 );
Assert.assertTrue( "b50", cfg.getDefaultDoubleArray().length == 0 );
Assert.assertTrue( "b60", cfg.getDefaultFloatArray().length == 0 );
Assert.assertTrue( "b70", cfg.getDefaultIntegerArray().length == 0 );
Assert.assertTrue( "b80", cfg.getDefaultLongArray().length == 0 );
Assert.assertTrue( "b90", cfg.getDefaultShortArray().length == 0 );
Assert.assertTrue( "b31", cfg.getDefaultPrimitiveByteArray().length == 0 );
Assert.assertTrue( "b41", cfg.getDefaultPrimitiveCharacterArray().length == 0 );
Assert.assertTrue( "b51", cfg.getDefaultPrimitiveDoubleArray().length == 0 );
Assert.assertTrue( "b61", cfg.getDefaultPrimitiveFloatArray().length == 0 );
Assert.assertTrue( "b71", cfg.getDefaultPrimitiveIntegerArray().length == 0 );
Assert.assertTrue( "b81", cfg.getDefaultPrimitiveLongArray().length == 0 );
Assert.assertTrue( "b91", cfg.getDefaultPrimitiveShortArray().length == 0 );
Assert.assertTrue( "b01", cfg.getDefaultUnusedIfaceArray().length == 0 );
Assert.assertTrue( "a00", cfg.getAttributeDefaultAsString() == null );
Assert.assertTrue( "a10", cfg.getAttributeDefaultAsBigDecimal() == null );
Assert.assertTrue( "a20", cfg.getAttributeDefaultAsBigInteger() == null );
Assert.assertTrue( "a30", cfg.getAttributeDefaultAsByte() == null );
Assert.assertTrue( "a40", cfg.getAttributeDefaultAsCharacter() == null );
Assert.assertTrue( "a50", cfg.getAttributeDefaultAsDouble() == null );
Assert.assertTrue( "a60", cfg.getAttributeDefaultAsFloat() == null );
Assert.assertTrue( "a70", cfg.getAttributeDefaultAsInteger() == null );
Assert.assertTrue( "a80", cfg.getAttributeDefaultAsLong() == null );
Assert.assertTrue( "a90", cfg.getAttributeDefaultAsShort() == null );
Assert.assertTrue( "a31", cfg.getAttributeDefaultAsPrimitiveByte() == ( ( byte ) 0 ) );
Assert.assertTrue( "a41", cfg.getAttributeDefaultAsPrimitiveCharacter() == ( ( char ) 0 ) );
Assert.assertTrue( "a51", cfg.getAttributeDefaultAsPrimitiveDouble() == ( ( double ) 0.0 ) );
Assert.assertTrue( "a61", cfg.getAttributeDefaultAsPrimitiveFloat() == ( ( float ) 0.0 ) );
Assert.assertTrue( "a71", cfg.getAttributeDefaultAsPrimitiveInteger() == ( ( int ) 0 ) );
Assert.assertTrue( "a81", cfg.getAttributeDefaultAsPrimitiveLong() == ( ( long ) 0 ) );
Assert.assertTrue( "a91", cfg.getAttributeDefaultAsPrimitiveShort() == ( ( short ) 0 ) );
Assert.assertTrue( "e00", cfg.getElementDefaultAsString() == null );
Assert.assertTrue( "e10", cfg.getElementDefaultAsBigDecimal() == null );
Assert.assertTrue( "e20", cfg.getElementDefaultAsBigInteger() == null );
Assert.assertTrue( "e30", cfg.getElementDefaultAsByte() == null );
Assert.assertTrue( "e40", cfg.getElementDefaultAsCharacter() == null );
Assert.assertTrue( "e50", cfg.getElementDefaultAsDouble() == null );
Assert.assertTrue( "e60", cfg.getElementDefaultAsFloat() == null );
Assert.assertTrue( "e70", cfg.getElementDefaultAsInteger() == null );
Assert.assertTrue( "e80", cfg.getElementDefaultAsLong() == null );
Assert.assertTrue( "e90", cfg.getElementDefaultAsShort() == null );
Assert.assertTrue( "e31", cfg.getElementDefaultAsPrimitiveByte() == ( ( byte ) 0 ) );
Assert.assertTrue( "e41", cfg.getElementDefaultAsPrimitiveCharacter() == ( ( char ) 0 ) );
Assert.assertTrue( "e51", cfg.getElementDefaultAsPrimitiveDouble() == ( ( double ) 0.0 ) );
Assert.assertTrue( "e61", cfg.getElementDefaultAsPrimitiveFloat() == ( ( float ) 0.0 ) );
Assert.assertTrue( "e71", cfg.getElementDefaultAsPrimitiveInteger() == ( ( int ) 0 ) );
Assert.assertTrue( "e81", cfg.getElementDefaultAsPrimitiveLong() == ( ( long ) 0 ) );
Assert.assertTrue( "e91", cfg.getElementDefaultAsPrimitiveShort() == ( ( short ) 0 ) );
}
public void testDefaultValuesEnduranceTest() throws Exception
{
final Element element = this.getRootElement( "/correct/test5.xml" );
for ( int i = 0; i < 10000; i++ )
{
XML2Java.bind( element, DefaultValues.class );
}
}
public void test1() throws Exception
{
final Element element = this.getRootElement( "/correct/test1.xml" );
final Config1 cfg = XML2Java.bind( element, Config1.class );
Assert.assertTrue( "F1", cfg.getAttributeXyz().equals( "asdf" ) );
Assert.assertTrue( "F2", cfg.getElementTest().equals( "TEST" ) );
Assert.assertTrue( "F41", cfg.getElementTestArray()[ 0 ].equals( new Character( 'A' ) ) );
Assert.assertTrue( "F42", cfg.getElementTestArray()[ 1 ].equals( new Character( 'S' ) ) );
Assert.assertTrue( "F43", cfg.getElementTestArray()[ 2 ].equals( new Character( 'D' ) ) );
Assert.assertTrue( "F44", cfg.getElementTestArray()[ 3 ].equals( new Character( 'F' ) ) );
final Config1.Inner1 inner = cfg.getElementInner();
Assert.assertTrue( "F4", inner.getAttributeAsdf().equals( "xyz" ) );
Assert.assertTrue( "F5", inner.getElementTest2().equals( "TEST_INNER" ) );
Assert.assertTrue( "F61", inner.getElementTestArray2()[ 0 ].equals( new Character( 'J' ) ) );
Assert.assertTrue( "F62", inner.getElementTestArray2()[ 1 ].equals( new Character( 'K' ) ) );
Assert.assertTrue( "F63", inner.getElementTestArray2()[ 2 ].equals( new Character( 'L' ) ) );
Assert.assertTrue( "F64", inner.getElementTestArray2()[ 3 ].equals( new Character( 'O' ) ) );
final Config1.Inner1.Inner11[] outer = cfg.getElementInner().getElementOuter();
Assert.assertTrue( "F71", outer[ 0 ].getAttributeJklo().equals( "r1" ) );
Assert.assertTrue( "F72", outer[ 1 ].getAttributeJklo().equals( "r2" ) );
Assert.assertTrue( "F73", outer[ 2 ].getAttributeJklo().equals( "r3" ) );
}
public void test2() throws Exception
{
final Element element = this.getRootElement( "/correct/test2.xml" );
final Config2 cfg = XML2Java.bind( element, Config2.class );
Assert.assertTrue( "F1", cfg.getAttributeXyz().equals( "asdf" ) );
Assert.assertTrue( "F2", cfg.getElementTest().equals( "TEST" ) );
Assert.assertTrue( "F41", cfg.getElementTestArray()[ 0 ].equals( new Character( 'A' ) ) );
Assert.assertTrue( "F42", cfg.getElementTestArray()[ 1 ].equals( new Character( 'S' ) ) );
Assert.assertTrue( "F43", cfg.getElementTestArray()[ 2 ].equals( new Character( 'D' ) ) );
Assert.assertTrue( "F44", cfg.getElementTestArray()[ 3 ].equals( new Character( 'F' ) ) );
final Config2.Inner1 inner = cfg.getElementInner();
Assert.assertTrue( "F4", inner.getAttributeAsdf().equals( "xyz" ) );
Assert.assertTrue( "F5", inner.getElementTest2().equals( "TEST_INNER" ) );
Assert.assertTrue( "F61", inner.getElementTestArray2()[ 0 ].equals( new Character( 'J' ) ) );
Assert.assertTrue( "F62", inner.getElementTestArray2()[ 1 ].equals( new Character( 'K' ) ) );
Assert.assertTrue( "F63", inner.getElementTestArray2()[ 2 ].equals( new Character( 'L' ) ) );
Assert.assertTrue( "F64", inner.getElementTestArray2()[ 3 ].equals( new Character( 'O' ) ) );
final Config2.Inner1.Inner11[] outer = cfg.getElementInner().getElementOuter();
Assert.assertTrue( "F71", outer[ 0 ].getAttributeJklo().equals( "r1" ) );
Assert.assertTrue( "F72", outer[ 1 ].getAttributeJklo().equals( "r2" ) );
Assert.assertTrue( "F73", outer[ 2 ].getAttributeJklo().equals( "r3" ) );
}
public void test3() throws Exception
{
final Element element = this.getRootElement( "/correct/test3.xml" );
final Config2 cfg = XML2Java.bind( element, Config2.class );
Assert.assertTrue( "F1", cfg.getAttributeXyz().equals( "asdf" ) );
Assert.assertTrue( "F2", cfg.getElementTest().equals( "TEST" ) );
Assert.assertTrue( "F41", cfg.getElementTestArray()[ 0 ].equals( new Character( 'A' ) ) );
Assert.assertTrue( "F42", cfg.getElementTestArray()[ 1 ].equals( new Character( 'S' ) ) );
Assert.assertTrue( "F43", cfg.getElementTestArray()[ 2 ].equals( new Character( 'D' ) ) );
Assert.assertTrue( "F44", cfg.getElementTestArray()[ 3 ].equals( new Character( 'F' ) ) );
final Config2.Inner1 inner = cfg.getElementInner();
Assert.assertTrue( "F4", inner.getAttributeAsdf().equals( "xyz" ) );
Assert.assertTrue( "F5", inner.getElementTest2().equals( "TEST_INNER" ) );
Assert.assertTrue( "F61", inner.getElementTestArray2()[ 0 ].equals( new Character( 'J' ) ) );
Assert.assertTrue( "F62", inner.getElementTestArray2()[ 1 ].equals( new Character( 'K' ) ) );
Assert.assertTrue( "F63", inner.getElementTestArray2()[ 2 ].equals( new Character( 'L' ) ) );
Assert.assertTrue( "F64", inner.getElementTestArray2()[ 3 ].equals( new Character( 'O' ) ) );
final Config2.Inner1.Inner11[] outer = cfg.getElementInner().getElementOuter();
Assert.assertTrue( "F71", outer[ 0 ].getAttributeJklo().equals( "r1" ) );
Assert.assertTrue( "F72", outer[ 1 ].getAttributeJklo().equals( "r2" ) );
Assert.assertTrue( "F73", outer[ 2 ].getAttributeJklo().equals( "r3" ) );
}
public void test4() throws Exception
{
final Element element = this.getRootElement( "/correct/test4.xml" );
final Config3 cfg = XML2Java.bind( element, Config3.class );
Assert.assertTrue( "F1", cfg.getAttributeXyz().equals( "asdf" ) );
Assert.assertTrue( "F2", cfg.getElementTest().equals( "TEST" ) );
Assert.assertTrue( "F41", cfg.getElementTestArray()[ 0 ].equals( new Character( 'A' ) ) );
Assert.assertTrue( "F42", cfg.getElementTestArray()[ 1 ].equals( new Character( 'S' ) ) );
Assert.assertTrue( "F43", cfg.getElementTestArray()[ 2 ].equals( new Character( 'D' ) ) );
Assert.assertTrue( "F44", cfg.getElementTestArray()[ 3 ].equals( new Character( 'F' ) ) );
final Config3.Inner1 inner = cfg.getElementInner();
Assert.assertTrue( "F4", inner.getAttributeAsdf().equals( "xyz" ) );
Assert.assertTrue( "F5", inner.getElementTest2().equals( "TEST_INNER" ) );
Assert.assertTrue( "F61", inner.getElementTestArray2()[ 0 ].equals( new Character( 'J' ) ) );
Assert.assertTrue( "F62", inner.getElementTestArray2()[ 1 ].equals( new Character( 'K' ) ) );
Assert.assertTrue( "F63", inner.getElementTestArray2()[ 2 ].equals( new Character( 'L' ) ) );
Assert.assertTrue( "F64", inner.getElementTestArray2()[ 3 ].equals( new Character( 'O' ) ) );
final Config3.Inner1.Inner11[] outer = cfg.getElementInner().getElementOuter();
Assert.assertTrue( "F71", outer[ 0 ].getAttributeJklo().equals( "r1" ) );
Assert.assertTrue( "F72", outer[ 1 ].getAttributeJklo().equals( "r2" ) );
Assert.assertTrue( "F73", outer[ 2 ].getAttributeJklo().equals( "r3" ) );
}
public void testCustomHandler() throws Exception
{
final Element element = this.getRootElement( "/correct/test1.xml" );
final CustomHandler cfg = XML2Java.bind( element, CustomHandler.class );
final String[] retVal = cfg.getInner().getTestArray1();
Assert.assertTrue( "CH1", "j".equals( retVal[ 0 ] ) );
Assert.assertTrue( "CH2", "k".equals( retVal[ 1 ] ) );
Assert.assertTrue( "CH3", "l".equals( retVal[ 2 ] ) );
Assert.assertTrue( "CH4", "o".equals( retVal[ 3 ] ) );
final CustomHandler2 cfg2 = XML2Java.bind( element, CustomHandler2.class );
final String[] retVal2 = cfg2.getInner().getTestArray1();
Assert.assertTrue( "CH1", "j".equals( retVal2[ 0 ] ) );
Assert.assertTrue( "CH2", "k".equals( retVal2[ 1 ] ) );
Assert.assertTrue( "CH3", "l".equals( retVal2[ 2 ] ) );
Assert.assertTrue( "CH4", "o".equals( retVal2[ 3 ] ) );
final CustomHandler4 cfg4 = XML2Java.bind( element, CustomHandler4.class );
final String[] retVal4 = cfg4.getInner().getValues();
Assert.assertTrue( "CH1", "j".equals( retVal4[ 0 ] ) );
Assert.assertTrue( "CH2", "k".equals( retVal4[ 1 ] ) );
Assert.assertTrue( "CH3", "l".equals( retVal4[ 2 ] ) );
Assert.assertTrue( "CH4", "o".equals( retVal4[ 3 ] ) );
}
public void testMultidimensionalArray() throws Exception
{
final Element element = this.getRootElement( "/correct/test6.xml" );
final MultiDimensionalArrays cfg = XML2Java.bind( element, MultiDimensionalArrays.class );
final int[][] array1 = cfg.getArray1();
Assert.assertTrue( "a11", array1[ 0 ][ 0 ] == 1 );
Assert.assertTrue( "a12", array1[ 0 ][ 1 ] == 2 );
Assert.assertTrue( "a13", array1[ 0 ][ 2 ] == 3 );
Assert.assertTrue( "a14", array1[ 1 ][ 0 ] == 4 );
Assert.assertTrue( "a15", array1[ 1 ][ 1 ] == 5 );
Assert.assertTrue( "a16", array1[ 1 ][ 2 ] == 6 );
Assert.assertTrue( "a17", array1[ 2 ][ 0 ] == 7 );
Assert.assertTrue( "a18", array1[ 2 ][ 1 ] == 8 );
Assert.assertTrue( "a19", array1[ 2 ][ 2 ] == 9 );
final int[] array2 = cfg.getArray2();
Assert.assertTrue( "a21", array2[ 0 ] == 1 );
Assert.assertTrue( "a22", array2[ 1 ] == 2 );
Assert.assertTrue( "a23", array2[ 2 ] == 3 );
Assert.assertTrue( "a24", array2[ 3 ] == 4 );
Assert.assertTrue( "a25", array2[ 4 ] == 5 );
Assert.assertTrue( "a26", array2[ 5 ] == 6 );
Assert.assertTrue( "a27", array2[ 6 ] == 7 );
Assert.assertTrue( "a28", array2[ 7 ] == 8 );
Assert.assertTrue( "a29", array2[ 8 ] == 9 );
}
public void testServerConfig() throws Exception
{
final Element element = this.getRootElement( "/correct/server.xml" );
final ServerConfig cfg = XML2Java.bind( element, ServerConfig.class );
final ServerConfig.Module[] modules = cfg.getModules();
Assert.assertTrue( modules.length == 3 );
Assert.assertEquals( modules[ 0 ].getInstanceName(), "TransportRepository" );
Assert.assertEquals( modules[ 0 ].getImplementationClass(), "com.test.module.TransportRepositoryImpl" );
Assert.assertEquals( modules[ 1 ].getInstanceName(), "InterceptorRepository" );
Assert.assertEquals( modules[ 1 ].getImplementationClass(), "com.test.module.InterceptorRepositoryImpl" );
Assert.assertEquals( modules[ 2 ].getInstanceName(), "PackageRepository" );
Assert.assertEquals( modules[ 2 ].getImplementationClass(), "com.test.module.PackageRepositoryImpl" );
final Element packageRepositoryModuleElement = modules[ 2 ].getCurrentElement();
Assert.assertNotNull( packageRepositoryModuleElement );
final ServerConfig.PackageRepositoryConfig pckgConfig =
XML2Java.bind( packageRepositoryModuleElement, ServerConfig.PackageRepositoryConfig.class );
Assert.assertEquals( pckgConfig.getAppDir(), "app" );
Assert.assertEquals( pckgConfig.getWorkDir(), "work" );
Assert.assertEquals( pckgConfig.getSystemDir(), "system" );
final ServerConfig.PackageRepositoryConfig2 pckgConfig2 =
XML2Java.bind( packageRepositoryModuleElement, ServerConfig.PackageRepositoryConfig2.class );
final Element[] subelements = pckgConfig2.getSubelements();
Assert.assertTrue( subelements.length == 3 );
Assert.assertEquals( subelements[ 0 ].getNodeName(), "appDir" );
Assert.assertEquals( Helper.getTextContent( subelements[ 0 ] ), "app" );
Assert.assertEquals( subelements[ 1 ].getNodeName(), "workDir" );
Assert.assertEquals( Helper.getTextContent( subelements[ 1 ] ), "work" );
Assert.assertEquals( subelements[ 2 ].getNodeName(), "sysCtx" );
Assert.assertEquals( Helper.getTextContent( subelements[ 2 ] ), "system" );
final ServerConfig.PackageRepositoryConfig3 pckgConfig3 =
XML2Java.bind( packageRepositoryModuleElement, ServerConfig.PackageRepositoryConfig3.class );
final Element[] appDirSubelements = pckgConfig3.getAllAppDirSubelements();
Assert.assertTrue( appDirSubelements.length == 1 );
Assert.assertEquals( subelements[ 0 ].getNodeName(), "appDir" );
Assert.assertEquals( Helper.getTextContent( subelements[ 0 ] ), "app" );
final ServerConfig.PackageRepositoryConfig4 pckgConfig4 =
XML2Java.bind( packageRepositoryModuleElement, ServerConfig.PackageRepositoryConfig4.class );
final Element appDirSubelement = pckgConfig4.getAppDirSubelement();
Assert.assertEquals( appDirSubelement.getNodeName(), "appDir" );
Assert.assertEquals( Helper.getTextContent( appDirSubelement ), "app" );
}
public void testWrongMappingOfElementHandlerToAttribute() throws Exception
{
this.runNegativeTest(
"/correct/test1.xml",
ElementHandlerMappedToAttribute.class,
new String[]
{
"Attribute handler",
"for class",
"not found",
}
);
}
public void testWrongMappingOfAttributeHandlerToElement() throws Exception
{
this.runNegativeTest(
"/correct/test1.xml",
AttributeHandlerMappedToElement.class,
new String[]
{
"Element handler",
"for class",
"not found",
}
);
}
public void testNonExistingElementHandler() throws Exception
{
this.runNegativeTest(
"/correct/test1.xml",
NotExistingElementHandler.class,
new String[]
{
"test.sample.valid.VirtualElementHandler",
}
);
}
public void testNonExistingAttributeHandler() throws Exception
{
this.runNegativeTest(
"/correct/test1.xml",
NotExistingAttributeHandler.class,
new String[]
{
"test.sample.valid.VirtualElementHandler",
}
);
}
public void testWrongMultiDimensionalArray1() throws Exception
{
this.runNegativeTest(
"/incorrect/test8.xml",
WrongMultiDimensionalArray1.class,
new String[]
{
"Default element handler for class",
"not found",
}
);
}
public void testWrongMultiDimensionalArray2() throws Exception
{
this.runNegativeTest(
"/incorrect/test8.xml",
WrongMultiDimensionalArray2.class,
new String[]
{
"Default element handler for class",
"not found",
}
);
}
public void testElementRequiredButNotPresent() throws Exception
{
this.runNegativeTest(
"/incorrect/test4.xml",
ElementRequiredButNotPresent.class,
new String[]
{
"Element",
"must contain subelement",
"as specified in method",
}
);
}
public void testElementRequiredButNotPresent2() throws Exception
{
this.runNegativeTest(
"/incorrect/test4.xml",
ElementRequiredButNotPresent2.class,
new String[]
{
"Element",
"must contain subelement",
"as specified in method",
}
);
}
public void testElementRequiredButNotPresent3() throws Exception
{
this.runNegativeTest(
"/incorrect/test4.xml",
ElementRequiredButNotPresent3.class,
new String[]
{
"Element",
"must contain subelement",
"as specified in method",
}
);
}
public void testElementRequiredButNotPresent4() throws Exception
{
this.runNegativeTest(
"/incorrect/test4.xml",
ElementRequiredButNotPresent4.class,
new String[]
{
"Element",
"must contain subelement",
"as specified in method",
}
);
}
public void testAttributeRequiredButNotPresent() throws Exception
{
this.runNegativeTest(
"/incorrect/test6.xml",
AttributeRequiredButNotPresent.class,
new String[]
{
"Method",
"specifies mandatory attribute",
"but this attribute is not present",
}
);
}
public void testAttributeRequiredButNotPresent2() throws Exception
{
this.runNegativeTest(
"/incorrect/test7.xml",
AttributeRequiredButNotPresent2.class,
new String[]
{
"Method",
"specifies mandatory attribute",
"but this attribute is not present",
}
);
}
public void testIncorrectElementName() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
InvalidElementName.class,
new String[]
{
"Method",
"contains incorrect node name value",
}
);
}
public void testMultipleElementsButSimpleReturnType() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
MultipleElementsButSimpleReturnType.class,
new String[]
{
"Method",
"does not return array but multiple elements",
"are present",
}
);
}
public void testMultipleElementsButSimpleReturnType2() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
MultipleElementsButSimpleReturnType2.class,
new String[]
{
"Method",
"does not return array but multiple elements",
"are present",
}
);
}
public void testUnsupportedVoidReturnTypeInGetter() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
WrongGetterSignature1.class,
new String[]
{
"Wrong method",
"signature. Method must follow Java Beans property format",
}
);
this.runNegativeTest(
"/incorrect/test2.xml",
WrongGetterSignature2.class,
new String[]
{
"Wrong method",
"signature. Method must follow Java Beans property format",
}
);
}
public void testGetterMethodWithParam() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
WrongGetterSignature3.class,
new String[]
{
"Wrong method",
"signature. Method must follow Java Beans property format",
}
);
}
public void testGetterWithoutBeanName() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
WrongGetterSignature4.class,
new String[]
{
"Wrong method",
"signature. Method must follow Java Beans property format",
}
);
this.runNegativeTest(
"/incorrect/test2.xml",
WrongGetterSignature5.class,
new String[]
{
"Wrong method",
"signature. Method must follow Java Beans property format",
}
);
}
public void testGetterWithoutReturnType() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
WrongGetterSignature6.class,
new String[]
{
"Wrong method",
"signature. Method must follow Java Beans property format",
}
);
}
public void testSetterWithoutBeanName() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
WrongSetterSignature1.class,
new String[]
{
"Wrong method",
"signature. Method must follow Java Beans property format",
}
);
}
public void testSetterWithoutParam() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
WrongSetterSignature2.class,
new String[]
{
"Wrong method",
"signature. Method must follow Java Beans property format",
}
);
}
public void testSetterWithReturnType() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
WrongSetterSignature3.class,
new String[]
{
"Wrong method",
"signature. Method must follow Java Beans property format",
}
);
this.runNegativeTest(
"/incorrect/test2.xml",
WrongSetterSignature4.class,
new String[]
{
"Wrong method",
"signature. Method must follow Java Beans property format",
}
);
}
public void testSetterGetterBindingMismatch() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
BindingMismatch.class,
new String[]
{
"Binding mismatch: setter",
"defines binding:",
"and getter",
}
);
}
public void testSetterGetterTypeMismatch() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
SetterGetterTypeMismatch.class,
new String[]
{
"Property type mismatch: setter",
"defines type:",
"and getter",
}
);
}
public void testTwoSetters() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
Setter2.class,
new String[]
{
"Two setters defined for property",
"First method:",
"second method:",
}
);
}
public void testTwoGetters() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
Getter2.class,
new String[]
{
"Two getters defined for property",
"First method:",
"second method:",
}
);
}
public void testNoGetter() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
NoGetter1.class,
new String[]
{
"No getter found for property",
"Only setter is defined:",
}
);
}
public void testWrongGetterThatThrowsException() throws Exception
{
this.runNegativeTest(
"/incorrect/test9.xml",
GetterThrowsException.class,
new String[]
{
"Method",
"cannot declare exceptions to be thrown",
}
);
}
public void testWrongSetterThatThrowsException() throws Exception
{
this.runNegativeTest(
"/incorrect/test9.xml",
SetterThrowsException.class,
new String[]
{
"Method",
"cannot declare exceptions to be thrown",
}
);
}
public void testWrongIsGetter() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
NoGetter2.class,
new String[]
{
"Wrong method",
"signature. Method must follow Java Beans property format",
}
);
}
public void testNullParameterForRequiredBean() throws Exception
{
final Element element = this.getRootElement( "/incorrect/test9.xml" );
final NullParameterForRequiredBean cfg = XML2Java.bind( element, NullParameterForRequiredBean.class );
Assert.assertTrue( "Expected false", cfg.isProperty().equals( Boolean.FALSE ) );
try
{
cfg.setProperty( null );
Assert.fail( "This test method had to fail!" );
}
catch ( Exception e )
{
final String[] expectedSubstrings = new String[]
{
"Cannot accept null value for bean",
"marked as required",
};
Helper.assertContains( e, expectedSubstrings );
}
}
public void testNullParameterForRequiredBeanArray() throws Exception
{
final Element element = this.getRootElement( "/incorrect/test9.xml" );
final NullParameterForRequiredBeanArray cfg = XML2Java.bind( element, NullParameterForRequiredBeanArray.class );
this.assertArrayValues( cfg );
try
{
cfg.setProperties( null );
Assert.fail( "This test method had to fail!" );
}
catch ( Exception e )
{
final String[] expectedSubstrings = new String[]
{
"Cannot accept null value for bean",
"marked as required",
};
Helper.assertContains( e, expectedSubstrings );
}
this.assertArrayValues( cfg );
try
{
cfg.setProperties( new Integer[] {} );
Assert.fail( "This test method had to fail!" );
}
catch ( Exception e )
{
final String[] expectedSubstrings = new String[]
{
"Cannot accept zero length array for bean",
"marked as required. Array size have to be at least 1",
};
Helper.assertContains( e, expectedSubstrings );
}
this.assertArrayValues( cfg );
try
{
final Integer[] newValues = new Integer[]
{
1, null,
};
cfg.setProperties( newValues );
Assert.fail( "This test method had to fail!" );
}
catch ( Exception e )
{
final String[] expectedSubstrings = new String[]
{
"Cannot accept null value inside array for bean",
"marked as required",
};
Helper.assertContains( e, expectedSubstrings );
}
this.assertArrayValues( cfg );
}
private void assertArrayValues( final NullParameterForRequiredBeanArray cfg ) throws Exception
{
final Integer[] properties = cfg.getProperties();
Assert.assertTrue( "Expected length 3", properties.length == 3 );
Assert.assertTrue( "Expected value 1", properties[ 0 ] == 0 );
Assert.assertTrue( "Expected value 2", properties[ 1 ] == 1 );
Assert.assertTrue( "Expected value 3", properties[ 2 ] == 2 );
}
public void testInnerIfaceNamespaceMismatch() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
InnerIfaceNamespaceMismatch.class,
new String[]
{
"Element",
"must contain subelement",
"as specified in method",
}
);
}
public void testInnerIfaceMappedToAttribute() throws Exception
{
this.runNegativeTest(
"/incorrect/test2.xml",
InnerIfaceMappedToAttribute.class,
new String[]
{
"Interface",
"must be mapped to element",
}
);
}
private void runNegativeTest
(
final String resourceName,
final Class< ? > clazz,
final String[] expectedSubstrings
)
throws Exception
{
try
{
XML2Java.bind( this.getRootElement( resourceName ), clazz );
Assert.fail( "This test method had to fail!" );
}
catch ( Exception e )
{
Helper.assertContains( e, expectedSubstrings );
}
}
private Element getRootElement( final String resourceName ) throws Exception
{
return this.builder.parse( this.getClass().getResourceAsStream( resourceName ) ).getDocumentElement();
}
}