diff --git a/pom.xml b/pom.xml index 24189f5..5f6c35b 100644 --- a/pom.xml +++ b/pom.xml @@ -65,37 +65,44 @@ + org.apache.maven.plugins maven-resources-plugin - 2.6 + 3.0.2 + org.apache.maven.plugins maven-surefire-plugin - 2.12.4 + 2.19.1 + org.apache.maven.plugins maven-compiler-plugin - 2.5.1 + 3.6.1 + org.apache.maven.plugins maven-source-plugin - 2.2.1 + 3.0.1 + org.apache.maven.plugins maven-javadoc-plugin - 2.9 + 2.10.4 + org.apache.maven.plugins maven-gpg-plugin - 1.4 + 1.6 + org.apache.maven.plugins maven-deploy-plugin - 2.7 + 2.8.2 org.nuiton helper-maven-plugin - 2.0 + 2.3.2 diff --git a/src/com/esotericsoftware/yamlbeans/Beans.java b/src/com/esotericsoftware/yamlbeans/Beans.java index d7d1b06..0d195e7 100644 --- a/src/com/esotericsoftware/yamlbeans/Beans.java +++ b/src/com/esotericsoftware/yamlbeans/Beans.java @@ -24,6 +24,7 @@ import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.lang.reflect.WildcardType; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -289,7 +290,18 @@ private Class getElementTypeFromGenerics (Type type) { if (isCollection(rawType) || isMap(rawType)) { Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); if (actualTypeArguments.length > 0) { - return (Class)actualTypeArguments[actualTypeArguments.length - 1]; + final Type cType = actualTypeArguments[actualTypeArguments.length - 1]; + if (cType instanceof Class) { + return (Class) cType; + } else if (cType instanceof WildcardType) { + WildcardType t = (WildcardType) cType; + final Type bound = t.getUpperBounds()[0]; + return bound instanceof Class ? (Class) bound : null; + } else if (cType instanceof ParameterizedType) { + ParameterizedType t = (ParameterizedType) cType; + final Type rt = t.getRawType(); + return rt instanceof Class ? (Class) rt : null; + } } } } diff --git a/test/com/esotericsoftware/yamlbeans/BeansTest.java b/test/com/esotericsoftware/yamlbeans/BeansTest.java new file mode 100644 index 0000000..e90d009 --- /dev/null +++ b/test/com/esotericsoftware/yamlbeans/BeansTest.java @@ -0,0 +1,729 @@ +package com.esotericsoftware.yamlbeans; + +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.InvocationTargetException; +import java.util.*; + +import static org.junit.Assert.*; + +public class BeansTest { + + private static final double DELTA = 0.0001; + + private YamlConfig yamlConfig; + + @Before + public void setup() throws Exception { + yamlConfig = new YamlConfig(); + } + + @Test + public void isScalar() throws Exception { + // Scalar Type + assertTrue(Beans.isScalar(String.class)); + assertTrue(Beans.isScalar(Integer.class)); + assertTrue(Beans.isScalar(Boolean.class)); + assertTrue(Beans.isScalar(Float.class)); + assertTrue(Beans.isScalar(Long.class)); + assertTrue(Beans.isScalar(Double.class)); + assertTrue(Beans.isScalar(Short.class)); + assertTrue(Beans.isScalar(Byte.class)); + assertTrue(Beans.isScalar(Character.class)); + // Other Type + assertFalse(Beans.isScalar(Void.class)); + assertFalse(Beans.isScalar(Number.class)); + assertFalse(Beans.isScalar(Beans.class)); + assertFalse(Beans.isScalar(YamlReader.class)); + assertFalse(Beans.isScalar(YamlWriter.class)); + } + + @Test + public void getDeferredConstruction() throws Exception { + DeferredConstruction construction = Beans.getDeferredConstruction(MockClass.class, yamlConfig); + assertEquals(null, construction); + } + + @Test + public void getDeferredConstructionWithPrivateConstructor() throws Exception { + DeferredConstruction construction = Beans.getDeferredConstruction(MockClassWithPrivateConstructor.class, yamlConfig); + assertEquals(null, construction); + } + + @Test + public void createObject() throws Exception { + MockClass mockClass = (MockClass) Beans.createObject(MockClass.class, false); + MockClassWithPrivateConstructor mockClassWithPrivateConstructor = (MockClassWithPrivateConstructor) Beans.createObject(MockClassWithPrivateConstructor.class, true); + List listCase = (List) Beans.createObject(List.class, false); + Set setCase = (Set) Beans.createObject(Set.class, false); + Map mapCase = (Map) Beans.createObject(Map.class, false); + + assertNotNull(mockClass); + assertNotNull(mockClassWithPrivateConstructor); + assertNotNull(listCase); + assertNotNull(setCase); + assertNotNull(mapCase); + + if (!(listCase instanceof ArrayList)) { + fail(); + } + if (!(setCase instanceof HashSet)) { + fail(); + } + if (!(mapCase instanceof HashMap)) { + fail(); + } + + try { + MockClassWithoutNoArgConstructor mockClassWithoutNoArgConstructor = (MockClassWithoutNoArgConstructor) Beans.createObject(MockClassWithoutNoArgConstructor.class, false); + fail(); + } catch (InvocationTargetException e) { + + } + try { + mockClassWithPrivateConstructor = (MockClassWithPrivateConstructor) Beans.createObject(MockClassWithPrivateConstructor.class, false); + fail(); + } catch (InvocationTargetException e) { + + } + } + + @Test + public void getPropertiesWithNullType() throws Exception { + try { + Beans.getProperties(null, false, false, yamlConfig); + fail(); + } catch (IllegalArgumentException e) { + // nothing + } + } + + @Test + public void getPropertiesWithBeanProperties() throws Exception { + final Set properties = Beans.getProperties(MockClass.class, true, false, yamlConfig); + assertEquals(9, properties.size()); + } + + @Test + public void getPropertiesWithPrivateFields() throws Exception { + final Set properties = Beans.getProperties(MockClass.class, false, true, yamlConfig); + assertEquals(9, properties.size()); + } + + @Test + public void getPropertyWithNullType() throws Exception { + try { + Beans.getProperty(null, null, false, false, yamlConfig); + fail(); + } catch (IllegalArgumentException e) { + // nothing + } + } + + @Test + public void getPropertyWithNullName() throws Exception { + try { + Beans.getProperty(MockClass.class, null, false, false, yamlConfig); + fail(); + } catch (IllegalArgumentException e) { + // nothing + } + } + + @Test + public void getPropertyWithEmptyName() throws Exception { + try { + Beans.getProperty(MockClass.class, "", false, false, yamlConfig); + fail(); + } catch (IllegalArgumentException e) { + // nothing + } + + } + + @Test + public void getPropertyBooleanTypeWithBeanProperties() throws Exception { + final Beans.Property booleanTypeProperty = Beans.getProperty(MockClass.class, "booleanType", true, false, yamlConfig); + + assertTrue(booleanTypeProperty instanceof Beans.MethodProperty); + assertEquals(MockClass.class, booleanTypeProperty.getDeclaringClass()); + assertEquals("booleanType", booleanTypeProperty.getName()); + assertEquals(boolean.class, booleanTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals(false, mockClass.booleanType); + + booleanTypeProperty.set(mockClass, true); + assertEquals(true, booleanTypeProperty.get(mockClass)); + + booleanTypeProperty.set(mockClass, false); + assertEquals(false, booleanTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyCharTypeWithBeanProperties() throws Exception { + final Beans.Property charTypeProperty = Beans.getProperty(MockClass.class, "charType", true, false, yamlConfig); + + assertTrue(charTypeProperty instanceof Beans.MethodProperty); + assertEquals(MockClass.class, charTypeProperty.getDeclaringClass()); + assertEquals("charType", charTypeProperty.getName()); + assertEquals(char.class, charTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals(0, mockClass.charType); + + charTypeProperty.set(mockClass, 'a'); + assertEquals('a', charTypeProperty.get(mockClass)); + + charTypeProperty.set(mockClass, Character.MAX_VALUE); + assertEquals(Character.MAX_VALUE, charTypeProperty.get(mockClass)); + + charTypeProperty.set(mockClass, Character.MIN_VALUE); + assertEquals(Character.MIN_VALUE, charTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyByteTypeWithBeanProperties() throws Exception { + final Beans.Property byteTypeProperty = Beans.getProperty(MockClass.class, "byteType", true, false, yamlConfig); + + assertTrue(byteTypeProperty instanceof Beans.MethodProperty); + assertEquals(MockClass.class, byteTypeProperty.getDeclaringClass()); + assertEquals("byteType", byteTypeProperty.getName()); + assertEquals(byte.class, byteTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals(0, mockClass.byteType); + + byteTypeProperty.set(mockClass, (byte) 1); + assertEquals((byte) 1, byteTypeProperty.get(mockClass)); + + byteTypeProperty.set(mockClass, Byte.MAX_VALUE); + assertEquals(Byte.MAX_VALUE, byteTypeProperty.get(mockClass)); + + byteTypeProperty.set(mockClass, Byte.MIN_VALUE); + assertEquals(Byte.MIN_VALUE, byteTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyShortTypeWithBeanProperties() throws Exception { + final Beans.Property shortTypeProperty = Beans.getProperty(MockClass.class, "shortType", true, false, yamlConfig); + + assertTrue(shortTypeProperty instanceof Beans.MethodProperty); + assertEquals(MockClass.class, shortTypeProperty.getDeclaringClass()); + assertEquals("shortType", shortTypeProperty.getName()); + assertEquals(short.class, shortTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals(0, mockClass.shortType); + + shortTypeProperty.set(mockClass, (short) 1); + assertEquals((short) 1, shortTypeProperty.get(mockClass)); + + shortTypeProperty.set(mockClass, Short.MAX_VALUE); + assertEquals(Short.MAX_VALUE, shortTypeProperty.get(mockClass)); + + shortTypeProperty.set(mockClass, Short.MIN_VALUE); + assertEquals(Short.MIN_VALUE, shortTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyIntTypeWithBeanProperties() throws Exception { + final Beans.Property intTypeProperty = Beans.getProperty(MockClass.class, "intType", true, false, yamlConfig); + + assertTrue(intTypeProperty instanceof Beans.MethodProperty); + assertEquals(MockClass.class, intTypeProperty.getDeclaringClass()); + assertEquals("intType", intTypeProperty.getName()); + assertEquals(int.class, intTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals(0, mockClass.intType); + + intTypeProperty.set(mockClass, 1); + assertEquals(1, intTypeProperty.get(mockClass)); + + intTypeProperty.set(mockClass, Integer.MAX_VALUE); + assertEquals(Integer.MAX_VALUE, intTypeProperty.get(mockClass)); + + intTypeProperty.set(mockClass, Integer.MIN_VALUE); + assertEquals(Integer.MIN_VALUE, intTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyLongTypeWithBeanProperties() throws Exception { + final Beans.Property longTypeProperty = Beans.getProperty(MockClass.class, "longType", true, false, yamlConfig); + + assertTrue(longTypeProperty instanceof Beans.MethodProperty); + assertEquals(MockClass.class, longTypeProperty.getDeclaringClass()); + assertEquals("longType", longTypeProperty.getName()); + assertEquals(long.class, longTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals(0, mockClass.longType); + + longTypeProperty.set(mockClass, 1l); + assertEquals(1l, longTypeProperty.get(mockClass)); + + longTypeProperty.set(mockClass, Long.MAX_VALUE); + assertEquals(Long.MAX_VALUE, longTypeProperty.get(mockClass)); + + longTypeProperty.set(mockClass, Long.MIN_VALUE); + assertEquals(Long.MIN_VALUE, longTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyFloatTypeWithBeanProperties() throws Exception { + final Beans.Property floatTypeProperty = Beans.getProperty(MockClass.class, "floatType", true, false, yamlConfig); + + assertTrue(floatTypeProperty instanceof Beans.MethodProperty); + assertEquals(MockClass.class, floatTypeProperty.getDeclaringClass()); + assertEquals("floatType", floatTypeProperty.getName()); + assertEquals(float.class, floatTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals((float) 0, mockClass.floatType, DELTA); + + floatTypeProperty.set(mockClass, (float) 1.0); + assertEquals((float) 1.0, floatTypeProperty.get(mockClass)); + + floatTypeProperty.set(mockClass, Float.MAX_VALUE); + assertEquals(Float.MAX_VALUE, floatTypeProperty.get(mockClass)); + + floatTypeProperty.set(mockClass, Float.MIN_VALUE); + assertEquals(Float.MIN_VALUE, floatTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyDoubleTypeWithBeanProperties() throws Exception { + final Beans.Property doubleTypeProperty = Beans.getProperty(MockClass.class, "doubleType", true, false, yamlConfig); + + assertTrue(doubleTypeProperty instanceof Beans.MethodProperty); + assertEquals(MockClass.class, doubleTypeProperty.getDeclaringClass()); + assertEquals("doubleType", doubleTypeProperty.getName()); + assertEquals(double.class, doubleTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals((double) 0, mockClass.doubleType, DELTA); + + doubleTypeProperty.set(mockClass, (double) 1.0); + assertEquals((double) 1.0, doubleTypeProperty.get(mockClass)); + + doubleTypeProperty.set(mockClass, Double.MAX_VALUE); + assertEquals(Double.MAX_VALUE, doubleTypeProperty.get(mockClass)); + + doubleTypeProperty.set(mockClass, Double.MIN_VALUE); + assertEquals(Double.MIN_VALUE, doubleTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyStringTypeWithBeanProperties() throws Exception { + final Beans.Property stringTypeProperty = Beans.getProperty(MockClass.class, "stringType", true, false, yamlConfig); + + assertTrue(stringTypeProperty instanceof Beans.MethodProperty); + assertEquals(MockClass.class, stringTypeProperty.getDeclaringClass()); + assertEquals("stringType", stringTypeProperty.getName()); + assertEquals(String.class, stringTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals(null, mockClass.stringType); + + stringTypeProperty.set(mockClass, "4cho"); + assertEquals("4cho", stringTypeProperty.get(mockClass)); + + stringTypeProperty.set(mockClass, "ghkim"); + assertEquals("ghkim", stringTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyNullWithBeanProperties() throws Exception { + final Beans.Property nullProperty = Beans.getProperty(MockClass.class, "nullType", true, false, yamlConfig); + + assertNull(nullProperty); + } + + @Test + public void getPropertyBooleanTypeWithPrivateFields() throws Exception { + final Beans.Property booleanTypeProperty = Beans.getProperty(MockClass.class, "booleanType", false, true, null); + + assertTrue(booleanTypeProperty instanceof Beans.FieldProperty); + assertEquals(MockClass.class, booleanTypeProperty.getDeclaringClass()); + assertEquals("booleanType", booleanTypeProperty.getName()); + assertEquals(boolean.class, booleanTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals(false, mockClass.booleanType); + + booleanTypeProperty.set(mockClass, true); + assertEquals(true, booleanTypeProperty.get(mockClass)); + + booleanTypeProperty.set(mockClass, false); + assertEquals(false, booleanTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyCharTypeWithPrivateFields() throws Exception { + final Beans.Property charTypeProperty = Beans.getProperty(MockClass.class, "charType", false, true, yamlConfig); + + assertTrue(charTypeProperty instanceof Beans.FieldProperty); + assertEquals(MockClass.class, charTypeProperty.getDeclaringClass()); + assertEquals("charType", charTypeProperty.getName()); + assertEquals(char.class, charTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals(0, mockClass.charType); + + charTypeProperty.set(mockClass, 'a'); + assertEquals('a', charTypeProperty.get(mockClass)); + + charTypeProperty.set(mockClass, Character.MAX_VALUE); + assertEquals(Character.MAX_VALUE, charTypeProperty.get(mockClass)); + + charTypeProperty.set(mockClass, Character.MIN_VALUE); + assertEquals(Character.MIN_VALUE, charTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyByteTypeWithPrivateFields() throws Exception { + final Beans.Property byteTypeProperty = Beans.getProperty(MockClass.class, "byteType", false, true, yamlConfig); + + assertTrue(byteTypeProperty instanceof Beans.FieldProperty); + assertEquals(MockClass.class, byteTypeProperty.getDeclaringClass()); + assertEquals("byteType", byteTypeProperty.getName()); + assertEquals(byte.class, byteTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals(0, mockClass.byteType); + + byteTypeProperty.set(mockClass, (byte) 1); + assertEquals((byte) 1, byteTypeProperty.get(mockClass)); + + byteTypeProperty.set(mockClass, Byte.MAX_VALUE); + assertEquals(Byte.MAX_VALUE, byteTypeProperty.get(mockClass)); + + byteTypeProperty.set(mockClass, Byte.MIN_VALUE); + assertEquals(Byte.MIN_VALUE, byteTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyShortTypeWithPrivateFields() throws Exception { + final Beans.Property shortTypeProperty = Beans.getProperty(MockClass.class, "shortType", false, true, yamlConfig); + + assertTrue(shortTypeProperty instanceof Beans.FieldProperty); + assertEquals(MockClass.class, shortTypeProperty.getDeclaringClass()); + assertEquals("shortType", shortTypeProperty.getName()); + assertEquals(short.class, shortTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals(0, mockClass.shortType); + + shortTypeProperty.set(mockClass, (short) 1); + assertEquals((short) 1, shortTypeProperty.get(mockClass)); + + shortTypeProperty.set(mockClass, Short.MAX_VALUE); + assertEquals(Short.MAX_VALUE, shortTypeProperty.get(mockClass)); + + shortTypeProperty.set(mockClass, Short.MIN_VALUE); + assertEquals(Short.MIN_VALUE, shortTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyIntTypeWithPrivateFields() throws Exception { + final Beans.Property intTypeProperty = Beans.getProperty(MockClass.class, "intType", false, true, yamlConfig); + + assertTrue(intTypeProperty instanceof Beans.FieldProperty); + assertEquals(MockClass.class, intTypeProperty.getDeclaringClass()); + assertEquals("intType", intTypeProperty.getName()); + assertEquals(int.class, intTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals(0, mockClass.intType); + + intTypeProperty.set(mockClass, 1); + assertEquals(1, intTypeProperty.get(mockClass)); + + intTypeProperty.set(mockClass, Integer.MAX_VALUE); + assertEquals(Integer.MAX_VALUE, intTypeProperty.get(mockClass)); + + intTypeProperty.set(mockClass, Integer.MIN_VALUE); + assertEquals(Integer.MIN_VALUE, intTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyLongTypeWithPrivateFields() throws Exception { + final Beans.Property longTypeProperty = Beans.getProperty(MockClass.class, "longType", false, true, yamlConfig); + + assertTrue(longTypeProperty instanceof Beans.FieldProperty); + assertEquals(MockClass.class, longTypeProperty.getDeclaringClass()); + assertEquals("longType", longTypeProperty.getName()); + assertEquals(long.class, longTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals(0, mockClass.longType); + + longTypeProperty.set(mockClass, 1l); + assertEquals(1l, longTypeProperty.get(mockClass)); + + longTypeProperty.set(mockClass, Long.MAX_VALUE); + assertEquals(Long.MAX_VALUE, longTypeProperty.get(mockClass)); + + longTypeProperty.set(mockClass, Long.MIN_VALUE); + assertEquals(Long.MIN_VALUE, longTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyFloatTypeWithPrivateFields() throws Exception { + final Beans.Property floatTypeProperty = Beans.getProperty(MockClass.class, "floatType", false, true, yamlConfig); + + assertTrue(floatTypeProperty instanceof Beans.FieldProperty); + assertEquals(MockClass.class, floatTypeProperty.getDeclaringClass()); + assertEquals("floatType", floatTypeProperty.getName()); + assertEquals(float.class, floatTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals((float) 0, mockClass.floatType, DELTA); + + floatTypeProperty.set(mockClass, (float) 1.0); + assertEquals((float) 1.0, floatTypeProperty.get(mockClass)); + + floatTypeProperty.set(mockClass, Float.MAX_VALUE); + assertEquals(Float.MAX_VALUE, floatTypeProperty.get(mockClass)); + + floatTypeProperty.set(mockClass, Float.MIN_VALUE); + assertEquals(Float.MIN_VALUE, floatTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyDoubleTypeWithPrivateFields() throws Exception { + final Beans.Property doubleTypeProperty = Beans.getProperty(MockClass.class, "doubleType", false, true, yamlConfig); + + assertTrue(doubleTypeProperty instanceof Beans.FieldProperty); + assertEquals(MockClass.class, doubleTypeProperty.getDeclaringClass()); + assertEquals("doubleType", doubleTypeProperty.getName()); + assertEquals(double.class, doubleTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals((double) 0, mockClass.doubleType, DELTA); + + doubleTypeProperty.set(mockClass, (double) 1.0); + assertEquals((double) 1.0, doubleTypeProperty.get(mockClass)); + + doubleTypeProperty.set(mockClass, Double.MAX_VALUE); + assertEquals(Double.MAX_VALUE, doubleTypeProperty.get(mockClass)); + + doubleTypeProperty.set(mockClass, Double.MIN_VALUE); + assertEquals(Double.MIN_VALUE, doubleTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyStringTypeWithPrivateFields() throws Exception { + final Beans.Property stringTypeProperty = Beans.getProperty(MockClass.class, "stringType", false, true, yamlConfig); + + assertTrue(stringTypeProperty instanceof Beans.FieldProperty); + assertEquals(MockClass.class, stringTypeProperty.getDeclaringClass()); + assertEquals("stringType", stringTypeProperty.getName()); + assertEquals(String.class, stringTypeProperty.getType()); + + MockClass mockClass = new MockClass(); + assertEquals(null, mockClass.stringType); + + stringTypeProperty.set(mockClass, "4cho"); + assertEquals("4cho", stringTypeProperty.get(mockClass)); + + stringTypeProperty.set(mockClass, "ghkim"); + assertEquals("ghkim", stringTypeProperty.get(mockClass)); + } + + @Test + public void getPropertyNullWithPrivateFields() throws Exception { + final Beans.Property nullProperty = Beans.getProperty(MockClass.class, "nullType", false, true, yamlConfig); + + assertNull(nullProperty); + } + + private static class MockClass { + + private boolean booleanType; + private char charType; + private byte byteType; + private short shortType; + private int intType; + private long longType; + private float floatType; + private double doubleType; + private String stringType; + + public MockClass() { + + } + + public boolean isBooleanType() { + return booleanType; + } + + public void setBooleanType(boolean booleanType) { + this.booleanType = booleanType; + } + + public char getCharType() { + return charType; + } + + public void setCharType(char charType) { + this.charType = charType; + } + + public byte getByteType() { + return byteType; + } + + public void setByteType(byte byteType) { + this.byteType = byteType; + } + + public short getShortType() { + return shortType; + } + + public void setShortType(short shortType) { + this.shortType = shortType; + } + + public int getIntType() { + return intType; + } + + public void setIntType(int intType) { + this.intType = intType; + } + + public long getLongType() { + return longType; + } + + public void setLongType(long longType) { + this.longType = longType; + } + + public float getFloatType() { + return floatType; + } + + public void setFloatType(float floatType) { + this.floatType = floatType; + } + + public double getDoubleType() { + return doubleType; + } + + public void setDoubleType(double doubleType) { + this.doubleType = doubleType; + } + + public String getStringType() { + return stringType; + } + + public void setStringType(String stringType) { + this.stringType = stringType; + } + } + + private static class MockClassWithPrivateConstructor { + + private boolean booleanType; + private char charType; + private byte byteType; + private short shortType; + private int intType; + private long longType; + private float floatType; + private double doubleType; + private String stringType; + + private MockClassWithPrivateConstructor() { + + } + + public boolean isBooleanType() { + return booleanType; + } + + public void setBooleanType(boolean booleanType) { + this.booleanType = booleanType; + } + + public char getCharType() { + return charType; + } + + public void setCharType(char charType) { + this.charType = charType; + } + + public byte getByteType() { + return byteType; + } + + public void setByteType(byte byteType) { + this.byteType = byteType; + } + + public short getShortType() { + return shortType; + } + + public void setShortType(short shortType) { + this.shortType = shortType; + } + + public int getIntType() { + return intType; + } + + public void setIntType(int intType) { + this.intType = intType; + } + + public long getLongType() { + return longType; + } + + public void setLongType(long longType) { + this.longType = longType; + } + + public float getFloatType() { + return floatType; + } + + public void setFloatType(float floatType) { + this.floatType = floatType; + } + + public double getDoubleType() { + return doubleType; + } + + public void setDoubleType(double doubleType) { + this.doubleType = doubleType; + } + + public String getStringType() { + return stringType; + } + + public void setStringType(String stringType) { + this.stringType = stringType; + } + } + + private static class MockClassWithoutNoArgConstructor { + + private Object anyType; + + public MockClassWithoutNoArgConstructor(Object anyType) { + this.anyType = anyType; + } + } +} \ No newline at end of file diff --git a/test/com/esotericsoftware/yamlbeans/GenericTest.java b/test/com/esotericsoftware/yamlbeans/GenericTest.java index 0b5d341..15a2a99 100644 --- a/test/com/esotericsoftware/yamlbeans/GenericTest.java +++ b/test/com/esotericsoftware/yamlbeans/GenericTest.java @@ -127,6 +127,8 @@ static class Test { public Map structMap; public List integerList; public List structList; + public Map> multiMap; + public Map wildMap; @Override public boolean equals(Object o) { @@ -151,6 +153,12 @@ public boolean equals(Object o) { if (structMap != null ? !structMap.equals(test.structMap) : test.structMap != null) { return false; } + if (multiMap != null ? !multiMap.equals(test.multiMap) : test.multiMap != null) { + return false; + } + if (wildMap != null ? !wildMap.equals(test.wildMap) : test.wildMap != null) { + return false; + } return true; } @@ -161,6 +169,8 @@ public int hashCode() { result = 31 * result + (structMap != null ? structMap.hashCode() : 0); result = 31 * result + (integerList != null ? integerList.hashCode() : 0); result = 31 * result + (structList != null ? structList.hashCode() : 0); + result = 31 * result + (multiMap != null ? multiMap.hashCode() : 0); + result = 31 * result + (wildMap != null ? wildMap.hashCode() : 0); return result; } } diff --git a/test/com/esotericsoftware/yamlbeans/tokenizer/TokenizerTest.java b/test/com/esotericsoftware/yamlbeans/tokenizer/TokenizerTest.java new file mode 100644 index 0000000..6a328bf --- /dev/null +++ b/test/com/esotericsoftware/yamlbeans/tokenizer/TokenizerTest.java @@ -0,0 +1,198 @@ +package com.esotericsoftware.yamlbeans.tokenizer; + +import com.esotericsoftware.yamlbeans.tokenizer.Tokenizer.TokenizerException; +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.Iterator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class TokenizerTest { + + private Token STREAM_START = new Token(TokenType.STREAM_START); + private Token STREAM_END = new Token(TokenType.STREAM_END); + private Token VALUE = new Token(TokenType.VALUE); + private Token KEY = new Token(TokenType.KEY); + private Token BLOCK_MAPPING_START = new Token(TokenType.BLOCK_MAPPING_START); + private Token BLOCK_END = new Token(TokenType.BLOCK_END); + + /** + * Purpose: Getting the next token correctly + * Input: getNextToken() gets the next token and moves to the next token + * Expected: + * return Token.STREAM_START + * return Token.BLOCK_MAPPING_START + * return Token.KEY + * ... + * return Token.STREAM.END + * return null + */ + @Test + public void testGetNextToken() throws FileNotFoundException { + Tokenizer tokenizer = new Tokenizer(new FileReader("test/test1.yml")); + assertEquals(tokenizer.getNextToken() + "", STREAM_START + ""); + assertEquals(tokenizer.getNextToken() + "", BLOCK_MAPPING_START + ""); + assertEquals(tokenizer.getNextToken() + "", KEY + ""); + assertEquals(tokenizer.getNextToken() + "", ""); + assertEquals(tokenizer.getNextToken() + "", VALUE + ""); + assertEquals(tokenizer.getNextToken() + "", ""); + assertEquals(tokenizer.getNextToken() + "", BLOCK_END + ""); + assertEquals(tokenizer.getNextToken() + "", STREAM_END + ""); + assertNull(tokenizer.getNextToken()); + } + + /** + * Purpose: Ensuring that the constructor Tokenizer(Reader) is working properly + * Input: Tokenizer(Reader) FileReader("test/test1.yml") -> Tokenizer(FileReader("test/test1.yml")) + * Expected: + * peekNextToken() = Token.STREAM_START + * Tokenizer(BufferedReader(FileReader("test/test1.yml"))) = Tokenizer(FileReader("test/test1.yml")) + * Tokenizer(null) throws IllegalArgumentException.class + */ + @Test(expected = IllegalArgumentException.class) + public void testTokenizerReader() throws FileNotFoundException { + Tokenizer tokenizer = new Tokenizer(new FileReader("test/test1.yml")); + assertEquals(tokenizer.peekNextToken() + "", STREAM_START + ""); + Tokenizer tokenizer_nbuffered = new Tokenizer((new BufferedReader(new FileReader("test/test1.yml")))); + + Iterator tokenizer_iter = tokenizer.iterator(); + Iterator tokenizer_nbuffered_iter = tokenizer_nbuffered.iterator(); + + while (tokenizer_iter.hasNext() || tokenizer_nbuffered_iter.hasNext()) { + assertEquals(tokenizer_iter.next() + "", tokenizer_nbuffered_iter.next() + ""); + } + + Tokenizer tokenizer_null = new Tokenizer((FileReader) null); + } + + /** + * Purpose: Ensuring that the constructor Tokenizer(String) is working properly + * Input: Tokenizer(String) FileReader("12: 13") = Tokenizer(FileReader("test/test1.yml")), The content of "test/test1.yml" is "12: 13" + * Expected: + * Tokenizer(FileReader("12: 13")) = Tokenizer(FileReader("test/test1.yml")), The content of "test/test1.yml" is "12: 13" + */ + @Test + public void testTokenizerString() throws FileNotFoundException { + Iterator tokenizer_iter = new Tokenizer(new FileReader("test/test1.yml")).iterator(); + Iterator tokenizer_string_iter = new Tokenizer("12: 13").iterator(); + + while (tokenizer_iter.hasNext() || tokenizer_string_iter.hasNext()) { + assertEquals(tokenizer_iter.next() + "", tokenizer_string_iter.next() + ""); + } + } + + /** + * Purpose: peeking the next token correctly + * Input: peekNextToken() peeks the next token and does not move to the next token + * Expected: + * return Token.STREAM_START + * (moves by another function) + * return Token.BLOCK_MAPPING_START + * (moves by another function) + * return Token.KEY + * (moves by another function) + * ... + * return Token.STREAM.END + * (moves by another function) + * return null + */ + @Test + public void testPeekNextToken() throws FileNotFoundException { + Tokenizer tokenizer = new Tokenizer(new FileReader("test/test1.yml")); + assertEquals(tokenizer.peekNextToken() + "", STREAM_START + ""); + tokenizer.getNextToken(); + assertEquals(tokenizer.peekNextToken() + "", BLOCK_MAPPING_START + ""); + tokenizer.getNextToken(); + assertEquals(tokenizer.peekNextToken() + "", KEY + ""); + tokenizer.getNextToken(); + assertEquals(tokenizer.peekNextToken() + "", ""); + tokenizer.getNextToken(); + assertEquals(tokenizer.peekNextToken() + "", VALUE + ""); + tokenizer.getNextToken(); + assertEquals(tokenizer.peekNextToken() + "", ""); + tokenizer.getNextToken(); + assertEquals(tokenizer.peekNextToken() + "", BLOCK_END + ""); + tokenizer.getNextToken(); + assertEquals(tokenizer.peekNextToken() + "", STREAM_END + ""); + tokenizer.getNextToken(); + assertNull(tokenizer.peekNextToken()); + } + + /** + * Purpose: peeking the type of the next token correctly + * Input: peekNextToken() peeks the type of the next token and does not move to the next token + * Expected: + * return TokenType.STREAM_START + * (moves by another function) + * return TokenType.BLOCK_MAPPING_START + * (moves by another function) + * return TokenType.KEY + * (moves by another function) + * ... + * return TokenType.STREAM.END + * (moves by another function) + * return null + */ + @Test + public void testPeekNextTokenType() throws FileNotFoundException { + Tokenizer tokenizer = new Tokenizer(new FileReader("test/test1.yml")); + assertEquals(tokenizer.peekNextTokenType(), TokenType.STREAM_START); + tokenizer.getNextToken(); + assertEquals(tokenizer.peekNextTokenType(), TokenType.BLOCK_MAPPING_START); + tokenizer.getNextToken(); + assertEquals(tokenizer.peekNextTokenType(), TokenType.KEY); + tokenizer.getNextToken(); + assertEquals(tokenizer.peekNextTokenType(), TokenType.SCALAR); + tokenizer.getNextToken(); + assertEquals(tokenizer.peekNextTokenType(), TokenType.VALUE); + tokenizer.getNextToken(); + assertEquals(tokenizer.peekNextTokenType(), TokenType.SCALAR); + tokenizer.getNextToken(); + assertEquals(tokenizer.peekNextTokenType(), TokenType.BLOCK_END); + tokenizer.getNextToken(); + assertEquals(tokenizer.peekNextTokenType(), TokenType.STREAM_END); + tokenizer.getNextToken(); + assertNull(tokenizer.peekNextTokenType()); + } + + + /** + * Purpose: Ensuring that the iterator's functions are working properly + * Input: iterator().hasNext() returns the same value as (Tokenizer.peekNextToken != null) + * iterator().next() returns the same value as Tokenizer.getNextToken() + * Expected: + * Tokenizer.iterator().hasNext() = (Tokenizer.peekNextToken != null) + * Tokenizer.iterator().next() = Tokenizer.getNextToken() + * Tokenizer.iterator().remove() throws UnsupportedOperationException.class + */ + @Test(expected = UnsupportedOperationException.class) + public void testIterator() throws FileNotFoundException { + Tokenizer tokenizer = new Tokenizer(new FileReader("test/test1.yml")); + Iterator iter = new Tokenizer(new FileReader("test/test1.yml")).iterator(); + assertEquals(iter.hasNext(), tokenizer.peekNextToken() != null); + assertEquals(iter.next(), tokenizer.getNextToken()); + iter.remove(); + } + + /** + * Purpose: Getting the next token in a closed reader + * Input: close() Tokenizer.reader -> Tokenizer.reader.close() + * Expected: + * Tokenizer.getNextToken() = Token.STREAM_START + * Tokenizer.getNextToken() throws TokenizerException.class + */ + @Test(expected = TokenizerException.class) + public void testClose() throws IOException { + Tokenizer tokenizer = new Tokenizer(new FileReader("test/test1.yml")); + tokenizer.close(); + + assertEquals(tokenizer.getNextToken() + "", STREAM_START + ""); + tokenizer.getNextToken(); + } + +} \ No newline at end of file diff --git a/test/test1.yml b/test/test1.yml new file mode 100644 index 0000000..8068c9c --- /dev/null +++ b/test/test1.yml @@ -0,0 +1 @@ +12: 13 \ No newline at end of file