[jboss-cvs] JBossAS SVN: r66126 - in projects/microcontainer/trunk/metatype/src: tests/org/jboss/test/metatype and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Oct 15 09:15:24 EDT 2007
Author: alesj
Date: 2007-10-15 09:15:24 -0400 (Mon, 15 Oct 2007)
New Revision: 66126
Added:
projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java
Modified:
projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java
projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/AbstractMetaTypeTest.java
projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/AbstractMetaValueFactoryTest.java
projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/ValuesFactoryTestSuite.java
Log:
Unwrap array fix + tests.
Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java 2007-10-15 11:47:48 UTC (rev 66125)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java 2007-10-15 13:15:24 UTC (rev 66126)
@@ -453,30 +453,19 @@
else if (metaType.isEnum())
return convertValue(((EnumValue)metaValue).getValue(), type);
else if (metaType.isGeneric())
- {
- // todo
return convertValue(((GenericValue)metaValue).getValue(), type);
- }
else if (metaType.isArray())
{
ArrayValue arrayValue = (ArrayValue)metaValue;
- Object array;
- try
- {
- array = type.newArrayInstance(arrayValue.getLength());
- }
- catch (Throwable t)
- {
- throw new UndeclaredThrowableException(t);
- }
+ Object array = newArrayInstance(type, arrayValue.getLength());
for (int i = 0; i < Array.getLength(array); i++)
{
Object element = arrayValue.getValue(i);
if (element instanceof MetaValue)
- {
- TypeInfo elementType = configuration.getTypeInfo(element.getClass());
- element = unwrap((MetaValue)element, elementType);
- }
+ element = unwrapMetaValue((MetaValue)element, type, array);
+ else if (element != null && element.getClass().isArray())
+ element = unwrapArray(array, element);
+
Array.set(array, i, element);
}
return array;
@@ -486,6 +475,91 @@
}
/**
+ * Unwrap MetaValue.
+ *
+ * @param element the meta value
+ * @param type parent type
+ * @param array parent array
+ * @return unwrapped value
+ */
+ protected Object unwrapMetaValue(MetaValue element, TypeInfo type, Object array)
+ {
+ TypeInfo elementType;
+ if (type instanceof ClassInfo)
+ elementType = ((ClassInfo)type).getComponentType();
+ else
+ elementType = getClassInfo(element.getMetaType(), array);
+ return unwrap(element, elementType);
+ }
+
+ /**
+ * Unwrap array.
+ * @param array parent array
+ * @param element current array element
+ * @return unwrapped array element
+ */
+ protected Object unwrapArray(Object array, Object element)
+ {
+ TypeInfo elementType = configuration.getTypeInfo(array.getClass().getComponentType());
+ int subSize = Array.getLength(element);
+ Object newElement = newArrayInstance(elementType, subSize);
+ for(int i = 0; i < subSize; i++)
+ {
+ Object subElement = Array.get(element, i);
+ if (subElement instanceof MetaValue)
+ subElement = unwrapMetaValue((MetaValue)subElement, elementType, array);
+ if (subElement != null && subElement.getClass().isArray())
+ subElement = unwrapArray(newElement, subElement);
+
+ Array.set(newElement, i, subElement);
+ }
+ return newElement;
+ }
+
+ /**
+ * Get new array instance.
+ *
+ * @param typeInfo the type info
+ * @param size the size
+ * @return new array instance
+ */
+ protected Object newArrayInstance(TypeInfo typeInfo, int size)
+ {
+ if (typeInfo == null)
+ throw new IllegalArgumentException("Null type info.");
+
+ try
+ {
+ return typeInfo.newArrayInstance(size);
+ }
+ catch (Throwable t)
+ {
+ throw new UndeclaredThrowableException(t);
+ }
+ }
+
+ /**
+ * Get the class info from meta type.
+ *
+ * @param metaType the meta type
+ * @param array the array to fill
+ * @return class info
+ */
+ protected ClassInfo getClassInfo(MetaType metaType, Object array)
+ {
+ try
+ {
+ // get the classloader from the array we plan to fill
+ ClassLoader cl = array.getClass().getClassLoader();
+ return configuration.getClassInfo(metaType.getClassName(), cl);
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new UndeclaredThrowableException(e);
+ }
+ }
+
+ /**
* Create a meta value from the object
*
* @param value the value
Modified: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/AbstractMetaTypeTest.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/AbstractMetaTypeTest.java 2007-10-15 11:47:48 UTC (rev 66125)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/AbstractMetaTypeTest.java 2007-10-15 13:15:24 UTC (rev 66126)
@@ -24,8 +24,6 @@
import java.util.HashMap;
import java.util.Map;
-import junit.framework.TestSuite;
-
import org.jboss.metatype.api.types.ArrayMetaType;
import org.jboss.metatype.api.types.CompositeMetaType;
import org.jboss.metatype.api.types.ImmutableCompositeMetaType;
@@ -46,18 +44,6 @@
public abstract class AbstractMetaTypeTest extends BaseTestCase
{
/**
- * Create a new testsuite for the class
- *
- * TODO move to BaseTestCase
- * @param clazz the class
- * @return the suite
- */
- public static TestSuite suite(Class<?> clazz)
- {
- return new TestSuite(clazz);
- }
-
- /**
* Create a new AbstractMetaTypeTest.
*
* @param name the test name
Modified: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/AbstractMetaValueFactoryTest.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/AbstractMetaValueFactoryTest.java 2007-10-15 11:47:48 UTC (rev 66125)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/AbstractMetaValueFactoryTest.java 2007-10-15 13:15:24 UTC (rev 66126)
@@ -100,4 +100,16 @@
{
return metaValueFactory.create(value, type);
}
+
+ /**
+ * Unwrap meta value.
+ *
+ * @param value the meta value
+ * @param type expecyed type
+ * @return unwrapped
+ */
+ protected Object unwrapMetaValue(MetaValue value, Type type)
+ {
+ return metaValueFactory.unwrap(value, type);
+ }
}
Added: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java (rev 0)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java 2007-10-15 13:15:24 UTC (rev 66126)
@@ -0,0 +1,180 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* 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, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.metatype.values.factory.test;
+
+import java.util.Date;
+import java.util.Arrays;
+
+import junit.framework.Test;
+import org.jboss.metatype.api.values.MetaValue;
+import org.jboss.test.metatype.values.factory.support.TestEnum;
+import org.jboss.test.metatype.values.factory.support.TestGeneric;
+
+/**
+ * UnwrapValueUnitTestCase.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class UnwrapValueUnitTestCase extends AbstractMetaValueFactoryTest
+{
+ /**
+ * Create a testsuite for this test
+ *
+ * @return the testsuite
+ */
+ public static Test suite()
+ {
+ return suite(UnwrapValueUnitTestCase.class);
+ }
+
+ /**
+ * Create a new UnwrapValueUnitTestCase
+ *
+ * @param name the test name
+ */
+ public UnwrapValueUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testSimpleUnwrap() throws Exception
+ {
+ checkSingle(123);
+ checkSingle(new Date());
+ }
+
+ public void testEnumUnwrap() throws Exception
+ {
+ checkSingle(TestEnum.ONE);
+ }
+
+ public void testGenericUnwrap() throws Exception
+ {
+ checkSingle(new TestGeneric("123"));
+ }
+
+ public void testArrayUnwrap() throws Exception
+ {
+ short[] shorts = new short[128];
+ Double[] doubles = new Double[128];
+ TestEnum[] enums = new TestEnum[128];
+ TestGeneric[] generics = new TestGeneric[128];
+ Integer[][] integers = new Integer[128][128];
+ Integer[][][] triple = new Integer[10][10][10];
+ for(int i = 0; i < 128; i++)
+ {
+ shorts[i] = (short)i;
+ doubles[i] = i / Math.PI;
+ enums[i] = TestEnum.values()[i % 3];
+ generics[i] = new TestGeneric("#" + i);
+ for(int j = 0; j < 128; j++)
+ {
+ integers[i][j] = i * j;
+ for(int k = 0; k < 128; k++)
+ triple[i % 10][j % 10][k % 10] = (i * j * k) % 1000;
+ }
+ }
+
+ checkArray(shorts, new Asserter()
+ {
+ public boolean assertArray(final Object original, final Object unwrapped)
+ {
+ return Arrays.equals((short[])original, (short[])unwrapped);
+ }
+ });
+
+ checkArray(doubles, new Asserter()
+ {
+ public boolean assertArray(final Object original, final Object unwrapped)
+ {
+ return Arrays.equals((Double[])original, (Double[])unwrapped);
+ }
+ });
+
+ checkArray(enums, new Asserter()
+ {
+ public boolean assertArray(final Object original, final Object unwrapped)
+ {
+ return Arrays.equals((Object[])original, (Object[])unwrapped);
+ }
+ });
+
+ checkArray(generics, new Asserter()
+ {
+ public boolean assertArray(final Object original, final Object unwrapped)
+ {
+ return Arrays.equals((Object[])original, (Object[])unwrapped);
+ }
+ });
+
+ checkArray(integers, new Asserter()
+ {
+ public boolean assertArray(final Object original, final Object unwrapped)
+ {
+ Integer[][] first = (Integer[][])original;
+ Integer[][] second = (Integer[][])unwrapped;
+ for(int i = 0; i < first.length; i++)
+ for(int j = 0; j < first[0].length; j++)
+ if (first[i][j].equals(second[i][j]) == false)
+ return false;
+ return true;
+ }
+ });
+
+ checkArray(triple, new Asserter()
+ {
+ public boolean assertArray(final Object original, final Object unwrapped)
+ {
+ Integer[][][] first = (Integer[][][])original;
+ Integer[][][] second = (Integer[][][])unwrapped;
+ for(int i = 0; i < first.length; i++)
+ for(int j = 0; j < first[0].length; j++)
+ for(int k = 0; k < first[0][0].length; k++)
+ if (first[i][j][k].equals(second[i][j][k]) == false)
+ return false;
+ return true;
+ }
+ });
+ }
+
+ protected void checkSingle(Object object)
+ {
+ MetaValue metaValue = createMetaValue(object);
+ assertNotNull(metaValue);
+ Object unwrapped = unwrapMetaValue(metaValue, object.getClass());
+ assertEquals(object, unwrapped);
+ }
+
+ protected void checkArray(Object object, Asserter asserter)
+ {
+ MetaValue metaValue = createMetaValue(object);
+ assertNotNull(metaValue);
+ Class<? extends Object> clazz = object.getClass();
+ Object unwrapped = unwrapMetaValue(metaValue, clazz);
+ assertTrue("Different arrays.", asserter.assertArray(object, unwrapped));
+ }
+
+ private interface Asserter
+ {
+ boolean assertArray(final Object original, final Object unwrapped);
+ }
+}
Modified: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/ValuesFactoryTestSuite.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/ValuesFactoryTestSuite.java 2007-10-15 11:47:48 UTC (rev 66125)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/ValuesFactoryTestSuite.java 2007-10-15 13:15:24 UTC (rev 66126)
@@ -60,7 +60,8 @@
suite.addTest(MapValueFactoryUnitTestCase.suite());
suite.addTest(CompositeValueFactoryUnitTestCase.suite());
suite.addTest(AnnotationValueFactoryUnitTestCase.suite());
-
+ suite.addTest(UnwrapValueUnitTestCase.suite());
+
return suite;
}
}
More information about the jboss-cvs-commits
mailing list