[jboss-cvs] JBossAS SVN: r64531 - in projects/microcontainer/trunk/metatype/src: main/org/jboss/metatype/api/values and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Aug 10 02:20:26 EDT 2007


Author: scott.stark at jboss.org
Date: 2007-08-10 02:20:25 -0400 (Fri, 10 Aug 2007)
New Revision: 64531

Added:
   projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/test/ArrayValueSupportUnitTestCase.java
Modified:
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/AbstractMetaType.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/ArrayMetaType.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/ArrayValue.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/ArrayValueSupport.java
   projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/types/test/ArrayMetaTypeUnitTestCase.java
   projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/test/ValuesTestSuite.java
Log:
JBMICROCONT-199, better support for native arrays

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/AbstractMetaType.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/AbstractMetaType.java	2007-08-09 21:37:31 UTC (rev 64530)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/AbstractMetaType.java	2007-08-10 06:20:25 UTC (rev 64531)
@@ -195,7 +195,14 @@
          }
       }
       if (ok == false)
-         throw new IllegalArgumentException("Not a MetaType allowed class name: " + className);
+      {
+         // Check for a primative array type
+         int index = className.lastIndexOf('[');
+         if (index == className.length()-2)
+            ok = ArrayMetaType.isPrimitiveEncoding(className.substring(index+1));
+         if (ok == false)
+            throw new IllegalArgumentException("Not a MetaType allowed class name: " + className);
+      }
 
       // Looks ok
       this.className = className;

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/ArrayMetaType.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/ArrayMetaType.java	2007-08-09 21:37:31 UTC (rev 64530)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/types/ArrayMetaType.java	2007-08-10 06:20:25 UTC (rev 64531)
@@ -21,13 +21,15 @@
 */
 package org.jboss.metatype.api.types;
 
+import java.io.Serializable;
+
 /**
  * ArrayMetaType.
  *
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
  * @version $Revision: 1.1 $
  */
-public class ArrayMetaType extends AbstractMetaType
+public class ArrayMetaType<T extends Serializable> extends AbstractMetaType
 {
    /** The serialVersionUID */
    private static final long serialVersionUID = -2062790692152055156L;
@@ -37,6 +39,8 @@
 
    /** The element type for the array */
    private MetaType elementType;
+   /** Is elementType a primative array */
+   private boolean primitiveArray;
 
    /** Cached hash code */
    private transient int cachedHashCode = Integer.MIN_VALUE;
@@ -44,14 +48,117 @@
    /** Cached string representation */
    private transient String cachedToString = null;
 
+   private static final int PRIMITIVE_WRAPPER_NAME_INDEX = 0;
+   private static final int PRIMITIVE_TYPE_NAME_INDEX = 1;
+   private static final int PRIMITIVE_TYPE_ENCODING_INDEX  = 2;
+   private static final int PRIMITIVE_OPEN_TYPE_INDEX  = 3;
+
+   private static final Object[][] PRIMITIVE_ARRAY_TYPES = {
+       { Boolean.class.getName(),   boolean.class.getName(), "Z", SimpleMetaType.BOOLEAN },
+       { Character.class.getName(), char.class.getName(),    "C", SimpleMetaType.CHARACTER },
+       { Byte.class.getName(),      byte.class.getName(),    "B", SimpleMetaType.BYTE },
+       { Short.class.getName(),     short.class.getName(),   "S", SimpleMetaType.SHORT },
+       { Integer.class.getName(),   int.class.getName(),     "I", SimpleMetaType.INTEGER },
+       { Long.class.getName(),      long.class.getName(),    "J", SimpleMetaType.LONG },
+       { Float.class.getName(),     float.class.getName(),   "F", SimpleMetaType.FLOAT },
+       { Double.class.getName(),    double.class.getName(),  "D", SimpleMetaType.DOUBLE }
+   };
+
+   static boolean isPrimitiveEncoding(final String primitiveKey)
+   {
+      for (Object[] typeDescr : PRIMITIVE_ARRAY_TYPES)
+      {
+          if (typeDescr[PRIMITIVE_TYPE_ENCODING_INDEX].equals(primitiveKey))
+          {
+              return true;
+          }
+      }
+      return false;
+   }
+   static SimpleMetaType<?> getPrimitiveMetaType(String primitiveTypeName)
+   {
+      for (Object[] typeDescr : PRIMITIVE_ARRAY_TYPES)
+      {
+         if (primitiveTypeName.equals(typeDescr[PRIMITIVE_TYPE_NAME_INDEX]))
+              return (SimpleMetaType<?>) typeDescr[PRIMITIVE_OPEN_TYPE_INDEX];
+      }
+      return null;
+   }
    /**
+    * Get the char encoding string for the type name.
+    * @param typeName - the primitive wrapper type name
+    * @return char encoding string.
+    */
+   static String getPrimativeEncoding(String typeName)
+   {
+      for (Object[] typeDescr : PRIMITIVE_ARRAY_TYPES)
+      {
+         if (typeName.equals(typeDescr[PRIMITIVE_WRAPPER_NAME_INDEX]))
+              return (String) typeDescr[PRIMITIVE_TYPE_ENCODING_INDEX];
+      }
+      return null;
+   }
+   /**
+    * Get the char encoding string for the type name.
+    * @param typeName - the primitive wrapper type name
+    * @return primitive type name string.
+    */
+   static String getPrimativeName(String typeName)
+   {
+      for (Object[] typeDescr : PRIMITIVE_ARRAY_TYPES)
+      {
+         if (typeName.equals(typeDescr[PRIMITIVE_WRAPPER_NAME_INDEX]))
+              return (String) typeDescr[PRIMITIVE_TYPE_NAME_INDEX];
+      }
+      return null;
+   }
+
+   public static <E extends Serializable> ArrayMetaType<E[]> getArrayType(MetaType<E> elementType)
+   {
+      ArrayMetaType<E[]> arrayType = new ArrayMetaType<E[]>(1, elementType);
+      return arrayType;
+   }
+   public static <T extends Serializable> ArrayMetaType<T> getPrimitiveArrayType(Class<T> arrayClass)
+   {
+      if (!arrayClass.isArray())
+      {
+         throw new IllegalArgumentException("arrayClass must be an array");
+      }
+
+      int n = 1;
+      Class<?> componentType = arrayClass.getComponentType();
+      while (componentType.isArray())
+      {
+         n++;
+         componentType = componentType.getComponentType();
+      }
+      String componentTypeName = componentType.getName();
+
+      if (!componentType.isPrimitive())
+      {
+          throw new IllegalArgumentException(
+              "component type of the array must be a primitive type");
+      }
+
+      SimpleMetaType<?> simpleType = getPrimitiveMetaType(componentTypeName);
+
+      // Build primitive array
+      //
+       ArrayMetaType at = new ArrayMetaType(simpleType, true);
+       if (n > 1)
+           at = new ArrayMetaType<T>(n - 1, at);
+       return at;
+   }
+
+   /**
     * Generate the class name
     * 
     * @param dimension the dimension
     * @param elementType the element type
+    * @param isPrimative is this a primitive type
     * @return the class name
     */
-   private static String genName(int dimension, MetaType elementType)
+   private static String genName(int dimension, MetaType elementType, boolean isPrimative)
    {
       if (dimension < 1)
          throw new IllegalArgumentException("negative dimension");
@@ -62,9 +169,16 @@
       StringBuilder buffer = new StringBuilder();
       for (int i=0; i < dimension; i++)
          buffer.append('[');
-      buffer.append('L');
-      buffer.append(elementType.getClassName());
-      buffer.append(';');
+      if (isPrimative)
+      {
+         buffer.append(getPrimativeEncoding(elementType.getClassName()));
+      }
+      else
+      {
+         buffer.append('L');
+         buffer.append(elementType.getClassName());
+         buffer.append(';');
+      }
       return buffer.toString();
    }
 
@@ -73,9 +187,10 @@
     * 
     * @param dimension the dimension
     * @param elementType the element type
+    * @param isPrimative is this a primitive type
     * @return the type name
     */
-   private static String genType(int dimension, MetaType elementType)
+   private static String genType(int dimension, MetaType elementType, boolean isPrimative)
    {
       if (dimension < 1)
          throw new IllegalArgumentException("negative dimension");
@@ -86,9 +201,16 @@
       StringBuilder buffer = new StringBuilder();
       for (int i=0; i < dimension; i++)
          buffer.append('[');
-      buffer.append('L');
-      buffer.append(elementType.getTypeName());
-      buffer.append(';');
+      if (isPrimative)
+      {
+         buffer.append(getPrimativeEncoding(elementType.getClassName()));
+      }
+      else
+      {
+         buffer.append('L');
+         buffer.append(elementType.getClassName());
+         buffer.append(';');
+      }
       return buffer.toString();
    }
 
@@ -97,14 +219,18 @@
     * 
     * @param dimension the dimension
     * @param elementType the element type
+    * @param isPrimative is this a primitive type
     * @return the description
     */
-   private static String genDesc(int dimension, MetaType elementType)
+   private static String genDesc(int dimension, MetaType elementType, boolean isPrimative)
    {
       StringBuilder buffer = new StringBuilder();
       buffer.append(new Integer(dimension));
       buffer.append("-dimension array of ");
-      buffer.append(elementType.getTypeName());
+      if (isPrimative)
+         buffer.append(getPrimativeName(elementType.getTypeName()));
+      else
+         buffer.append(elementType.getTypeName());
       return buffer.toString();
    }
 
@@ -117,10 +243,31 @@
     */
    public ArrayMetaType(int dimension, MetaType elementType)
    {
-      super(genName(dimension, elementType), genType(dimension, elementType), genDesc(dimension, elementType));
+      super(genName(dimension, elementType, false),
+            genType(dimension, elementType, false),
+            genDesc(dimension, elementType, false));
       this.dimension = dimension;
       this.elementType = elementType;
+      this.primitiveArray = false;
    }
+   /**
+    * Construct an ArrayMetaType.
+    * @param elementType
+    * @param primitiveArray
+    */
+   public ArrayMetaType(SimpleMetaType<?> elementType, boolean primitiveArray)
+   {
+      this(1, elementType, primitiveArray);
+   }
+   public ArrayMetaType(int dimension, SimpleMetaType<?> elementType, boolean primitiveArray)
+   {
+      super(genName(dimension, elementType, primitiveArray),
+            genType(dimension, elementType, primitiveArray),
+            genDesc(dimension, elementType, primitiveArray));
+      this.dimension = dimension;
+      this.elementType = elementType;
+      this.primitiveArray = primitiveArray;
+   }
 
    /**
     * Get the dimension of the array
@@ -142,6 +289,11 @@
       return elementType;
    }
 
+   public boolean isPrimitiveArray()
+   {
+      return primitiveArray;
+   } 
+
    @Override
    @SuppressWarnings("unchecked")
    public boolean isValue(Object obj)

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/ArrayValue.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/ArrayValue.java	2007-08-09 21:37:31 UTC (rev 64530)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/ArrayValue.java	2007-08-10 06:20:25 UTC (rev 64531)
@@ -21,22 +21,39 @@
 */
 package org.jboss.metatype.api.values;
 
+import java.io.Serializable;
+
 import org.jboss.metatype.api.types.ArrayMetaType;
 
 /**
  * ArrayValue.
  * 
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author Scott.Stark at jboss.org
  * @version $Revision: 1.1 $
  */
-public interface ArrayValue extends MetaValue
+public interface ArrayValue<T extends Serializable>
+   extends MetaValue, Iterable<T>
 {
-   ArrayMetaType getMetaType();
+   ArrayMetaType<T> getMetaType();
    
    /**
-    * Get the underlying value
+    * Get the underlying array value. This will not be an
+    * Object[] in general.
+    * @see #getValue(int)
     * 
     * @return the underlying value
     */
-   public Object[] getValue();
+   public Object getValue();
+   /**
+    * Get the length of the array.
+    * @return length of the array.
+    */
+   public int getLength();
+   /**
+    * Get the array element at index.
+    * @param index - index into the array.
+    * @return element at index.
+    */
+   public Object getValue(int index); 
 }

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/ArrayValueSupport.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/ArrayValueSupport.java	2007-08-09 21:37:31 UTC (rev 64530)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/ArrayValueSupport.java	2007-08-10 06:20:25 UTC (rev 64531)
@@ -21,7 +21,10 @@
 */
 package org.jboss.metatype.api.values;
 
+import java.io.Serializable;
+import java.lang.reflect.Array;
 import java.util.Arrays;
+import java.util.Iterator;
 
 import org.jboss.metatype.api.types.ArrayMetaType;
 
@@ -32,16 +35,17 @@
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
  * @version $Revision: 1.1 $
  */
-public class ArrayValueSupport extends AbstractMetaValue implements ArrayValue
+public class ArrayValueSupport<T extends Serializable> extends AbstractMetaValue
+   implements ArrayValue<T>
 {
    /** The serialVersionUID */
    private static final long serialVersionUID = 1131827130033538066L;
 
    /** The array meta type */
-   private ArrayMetaType metaType;
+   private ArrayMetaType<T> metaType;
    
    /** The value */
-   private Object[] value;
+   private Object value;
    
    /**
     * Create a new ArrayValueSupport.
@@ -49,7 +53,7 @@
     * @param metaType the array meta type
     * @throws IllegalArgumentException for a null array MetaType
     */
-   public ArrayValueSupport(ArrayMetaType metaType)
+   public ArrayValueSupport(ArrayMetaType<T> metaType)
    {
       if (metaType == null)
          throw new IllegalArgumentException("Null array meta type");
@@ -63,13 +67,13 @@
     * @param value the value
     * @throws IllegalArgumentException for a null array MetaType
     */
-   public ArrayValueSupport(ArrayMetaType metaType, Object[] value)
+   public ArrayValueSupport(ArrayMetaType<T> metaType, Object value)
    {
       this(metaType);
       this.value = value;
    }
 
-   public ArrayMetaType getMetaType()
+   public ArrayMetaType<T> getMetaType()
    {
       return metaType;
    }
@@ -79,17 +83,38 @@
     * 
     * @return the value.
     */
-   public Object[] getValue()
+   public Object getValue()
    {
       return value;
    }
 
    /**
+    * Get the length of the array.
+    * @return length of the array.
+    */
+   public int getLength()
+   {
+      int length = Array.getLength(value);
+      return length;
+   }
+
+   public Object getValue(int index)
+   {
+      Object element = Array.get(value, index);
+      return element;
+   }
+
+   public Iterator<T> iterator()
+   {
+      return new ArrayValueIterator(value);
+   }
+
+   /**
     * Set the value.
     * 
     * @param value the value.
     */
-   public void setValue(Object[] value)
+   public void setValue(Object value)
    {
       this.value = value;
    } 
@@ -99,22 +124,45 @@
    {
       if (obj == this)
          return true;
-      
+
       if (obj == null || obj instanceof ArrayValue == false)
          return false;
 
-      ArrayValue other = (ArrayValue) obj;
+      ArrayValue<T> other = (ArrayValue<T>) obj;
       if (metaType.equals(other.getMetaType()) == false)
          return false;
 
-      Object[] otherValue = other.getValue();
+      Object otherValue = other.getValue();
       if (value == null && otherValue == null)
          return true;
       if (value == null && otherValue != null)
          return false;
-      return Arrays.deepEquals(value, otherValue);
+
+      // Deep equals check
+      boolean equals = false;
+      if (value instanceof Object[] && otherValue instanceof Object[])
+         equals = Arrays.deepEquals((Object[]) value, (Object[]) otherValue);
+      else if (value instanceof byte[] && otherValue instanceof byte[])
+         equals = Arrays.equals((byte[]) value, (byte[]) otherValue);
+      else if (value instanceof short[] && otherValue instanceof short[])
+         equals = Arrays.equals((short[]) value, (short[]) otherValue);
+      else if (value instanceof int[] && otherValue instanceof int[])
+         equals = Arrays.equals((int[]) value, (int[]) otherValue);
+      else if (value instanceof long[] && otherValue instanceof long[])
+         equals = Arrays.equals((long[]) value, (long[]) otherValue);
+      else if (value instanceof char[] && otherValue instanceof char[])
+         equals = Arrays.equals((char[]) value, (char[]) otherValue);
+      else if (value instanceof float[] && otherValue instanceof float[])
+         equals = Arrays.equals((float[]) value, (float[]) otherValue);
+      else if (value instanceof double[] && otherValue instanceof double[])
+         equals = Arrays.equals((double[]) value, (double[]) otherValue);
+      else if (value instanceof boolean[] && otherValue instanceof boolean[])
+         equals = Arrays.equals((boolean[]) value, (boolean[]) otherValue);
+      else
+         equals = value.equals(otherValue);
+      return equals;
    }
-   
+
    @Override
    public int hashCode()
    {
@@ -126,19 +174,83 @@
    @Override
    public String toString()
    {
-      return metaType + ":" + Arrays.deepToString(value);
+      return metaType + ":" + deepToString();
    }
 
    @Override
    public MetaValue clone()
    {
       ArrayValueSupport result = (ArrayValueSupport) super.clone();
-
-      if (value != null && value.length > 0)
+      int length = getLength();
+      if (value != null && length > 0)
       {
-         result.value = new Object[value.length];
-         System.arraycopy(value, 0, value, 0, value.length);
+         // TODO: This is wrong as value is not an Object[] in general
+         result.value = new Object[length];
+         System.arraycopy(value, 0, result.value, 0, length);
       }
       return result;
    }
+
+   /**
+    * 
+    * @return
+    */
+   protected String deepToString()
+   {
+      String deepToString;
+      if (value == null)
+         deepToString = "null";
+      else if (value instanceof byte[])
+         deepToString = Arrays.toString((byte[]) value);
+      else if (value instanceof short[])
+         deepToString = Arrays.toString((short[]) value);
+      else if (value instanceof int[])
+         deepToString = Arrays.toString((int[]) value);
+      else if (value instanceof long[])
+         deepToString = Arrays.toString((long[]) value);
+      else if (value instanceof char[])
+         deepToString = Arrays.toString((char[]) value);
+      else if (value instanceof float[])
+         deepToString = Arrays.toString((float[]) value);
+      else if (value instanceof double[])
+         deepToString = Arrays.toString((double[]) value);
+      else if (value instanceof boolean[])
+         deepToString = Arrays.toString((boolean[]) value);
+      else if (value instanceof Object[])
+         deepToString = Arrays.deepToString((Object[]) value);
+      else
+         deepToString = value.toString();
+      return deepToString;
+   }
+
+   private static class ArrayValueIterator<T> implements Iterator<T>
+   {
+      private int index;
+      private int length;
+      private Object array;
+
+      ArrayValueIterator(Object array)
+      {
+         this.array = array;
+         this.index = 0;
+         this.length = Array.getLength(array);
+      }
+      public boolean hasNext()
+      {
+         return index < length;
+      }
+
+      public T next()
+      {
+         Object next = Array.get(array, index ++);
+         T t = (T) next;
+         return t;
+      }
+
+      public void remove()
+      {
+         throw new UnsupportedOperationException(); 
+      }
+      
+   }
 }

Modified: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/types/test/ArrayMetaTypeUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/types/test/ArrayMetaTypeUnitTestCase.java	2007-08-09 21:37:31 UTC (rev 64530)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/types/test/ArrayMetaTypeUnitTestCase.java	2007-08-10 06:20:25 UTC (rev 64531)
@@ -78,6 +78,16 @@
       assertTrue("Type should be an array", arrayType.isArray());
    }
 
+   public void testCharArrayType()
+   {
+      ArrayMetaType arrayType = ArrayMetaType.getPrimitiveArrayType(char[].class);
+      assertEquals("[C", arrayType.getClassName());
+      assertEquals("1-dimension array of char", arrayType.getDescription());
+      assertEquals("[C", arrayType.getTypeName());
+      assertTrue("Type should be an array", arrayType.isArray());
+      assertEquals(SimpleMetaType.CHARACTER, arrayType.getElementType());
+   }
+
    /**
     * Test the the dimension for an array meta type
     * 

Added: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/test/ArrayValueSupportUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/test/ArrayValueSupportUnitTestCase.java	                        (rev 0)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/test/ArrayValueSupportUnitTestCase.java	2007-08-10 06:20:25 UTC (rev 64531)
@@ -0,0 +1,155 @@
+/*
+* 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.test;
+
+import java.lang.reflect.Array;
+
+import junit.framework.Test;
+
+import org.jboss.metatype.api.types.ArrayMetaType;
+import org.jboss.metatype.api.types.SimpleMetaType;
+import org.jboss.metatype.api.values.ArrayValueSupport;
+import org.jboss.test.metatype.AbstractMetaTypeTest;
+
+/**
+ * Tests of the ArrayValueSupport class.
+ * 
+ * @author Scott.Stark at jboss.org
+ * @version $Revision$
+ */
+public class ArrayValueSupportUnitTestCase extends AbstractMetaTypeTest
+{
+   /**
+    * Create a testsuite for this test
+    * 
+    * @return the testsuite
+    */
+   public static Test suite()
+   {
+      return suite(ArrayValueSupportUnitTestCase.class);
+   }
+   
+   /**
+    * Create a new ArrayValueSupportUnitTestCase.
+    * 
+    * @param name the test name
+    */
+   public ArrayValueSupportUnitTestCase(String name)
+   {
+      super(name);
+   }
+
+   static <T> T[] convert(Object array, T[] t)
+   {
+      int length = Array.getLength(array);
+      Class c = t.getClass().getComponentType();
+      T[] result = (T[]) Array.newInstance(c, length);
+      for(int n = 0; n < length; n ++)
+      {
+         result[n] = (T) Array.get(array, n);
+      }
+      return result;
+   }
+
+   public void testCharArray() throws Exception
+   {
+      ArrayMetaType<Character> type = new ArrayMetaType<Character>(1, SimpleMetaType.CHARACTER);
+      char[] value = {'h', 'e', 'l', 'l', 'o'};
+      ArrayValueSupport<Character> avs = new ArrayValueSupport<Character>(type, value);
+      // Use getValue(int) accessor
+      for(int n = 0; n < avs.getLength(); n ++)
+      {
+         Object element = avs.getValue(n);
+         assertEquals(value[n], element);
+      }
+      // Use typesafe foreach Iterable
+      int i = 0;
+      for(Character c : avs)
+      {
+         assertEquals("["+i+"]", (Character) value[i++], c);         
+      }
+      // Validate the primative array
+      char[] raw = (char[]) avs.getValue();
+      for(int n = 0; n < value.length; n ++)
+      {
+         assertEquals(value[n], raw[n]);
+      }
+   }
+   public void testCharacterArray() throws Exception
+   {
+      ArrayMetaType type = new ArrayMetaType(1, SimpleMetaType.CHARACTER);
+      Character[] value = {'h', 'e', 'l', 'l', 'o'};
+      ArrayValueSupport avs = new ArrayValueSupport(type, value);
+      for(int n = 0; n < avs.getLength(); n ++)
+      {
+         Object element = avs.getValue(n);
+         assertEquals(value[n], element);
+      }
+      // Validate the raw array
+      Character[] raw = (Character[]) avs.getValue();
+      for(int n = 0; n < value.length; n ++)
+      {
+         assertEquals(value[n], raw[n]);
+      }
+   }
+
+   public void test2DCharArray() throws Exception
+   {
+      ArrayMetaType<Character[]> type = new ArrayMetaType<Character[]>(2, SimpleMetaType.CHARACTER, true);
+      char[][] value = {{'h', 'e'}, {'l', 'l', 'o'}};
+      ArrayValueSupport<Character[]> avs = new ArrayValueSupport<Character[]>(type, value);
+      assertEquals(value.length, avs.getLength());
+      for(int m = 0; m < value.length; m ++)
+      {
+         Object arraym = avs.getValue(m);
+         for(int n = 0; n < value[m].length; n ++)
+         {
+            // Have to use the java.lang.reflect.Array to access nested elements
+            Object valuenm = Array.get(arraym, n);
+            assertEquals("["+m+"]["+n+"]", value[m][n], valuenm);
+         }
+      }
+
+      /* FIXME: Use typesafe foreach Iterable: current broken with CCE on [C
+      int i = 0, j = 0;
+      for(Character[] carray : avs)
+      {
+         for(Character c : carray)
+         {
+            Character cij = value[i ++][j ++];
+            assertEquals("["+i+"], ["+j+"]", cij , c);
+         }
+      }
+      */
+
+      // Validate the primitive 2d array
+      char[][] raw = (char[][]) avs.getValue();
+      for(int m = 0; m < value.length; m ++)
+      {
+         for(int n = 0; n < value[m].length; n ++)
+         {
+            assertEquals("["+m+"]["+n+"]", value[m][n], raw[m][n]);
+         }
+      }
+   }
+
+}


Property changes on: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/test/ArrayValueSupportUnitTestCase.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + native

Modified: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/test/ValuesTestSuite.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/test/ValuesTestSuite.java	2007-08-09 21:37:31 UTC (rev 64530)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/test/ValuesTestSuite.java	2007-08-10 06:20:25 UTC (rev 64531)
@@ -52,6 +52,7 @@
    {
       TestSuite suite = new TestSuite("Values Tests");
 
+      suite.addTest(ArrayValueSupportUnitTestCase.suite());
       suite.addTest(SimpleValueSupportUnitTestCase.suite());
       suite.addTest(CompositeValueSupportUnitTestCase.suite());
       suite.addTest(TableValueSupportUnitTestCase.suite());




More information about the jboss-cvs-commits mailing list