Changeset 2608

Show
Ignore:
Timestamp:
10/30/08 08:56:29 (2 months ago)
Author:
leo
Message:

Generics tests

Location:
XerialJ/trunk/xerial-core/src
Files:
2 added
11 modified

Legend:

Unmodified
Added
Removed
  • XerialJ/trunk/xerial-core/src/main/java/org/xerial/core/XerialErrorCode.java

    r2607 r2608  
    3636    INVALID_STATE, 
    3737    SYNTAX_ERROR, 
    38  
     38    NOT_INITIALIZED, 
     39     
    3940    // option parser error 
    4041    DUPLICATE_OPTION, 
    4142    NO_OPTION, 
    4243    NO_USAGE_ANNOTATION, 
     44 
    4345    // type  
    4446    MISSING_TYPE_PARAMETER, 
     47    NOT_A_COLLECTION, 
     48    INACCESSIBLE_METHOD,  
    4549 
    4650    ; 
  • XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/bean/TypeReference.java

    r2606 r2608  
    2727import java.lang.reflect.ParameterizedType; 
    2828import java.lang.reflect.Type; 
    29 import java.util.Collection; 
    3029 
    3130import org.xerial.core.XerialError; 
     
    4241 * @author leo 
    4342 */ 
    44 public class TypeReference<T extends Collection<?>> { 
     43public class TypeReference<T> 
     44{ 
    4545 
    4646    private final Type type; 
     
    5454            this.type = ((ParameterizedType) superClass).getActualTypeArguments()[0]; 
    5555    } 
    56  
    5756     
    5857    /** 
     
    6665 
    6766    /** 
    68      * Gets the row type of the referenced type 
     67     * Gets the element type of the referenced type 
     68     *  
    6969     * @return 
    7070     */ 
  • XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/shell/ArgumentItem.java

    r2607 r2608  
    106106    } 
    107107 
     108    public void initialize(Object optionHolder) throws OptionParserException 
     109    { 
     110        argumentSetter.initialize(optionHolder); 
     111    } 
     112 
    108113} 
  • XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/shell/OptionItem.java

    r2607 r2608  
    2828import java.lang.reflect.Method; 
    2929 
    30 import org.xerial.util.bean.BeanException; 
    31 import org.xerial.util.bean.TypeConverter; 
     30import org.xerial.util.bean.TypeInformation; 
    3231import org.xerial.util.cui.OptionParserException; 
    3332 
     
    10099        return optionDescriptor; 
    101100    } 
     101     
     102    public boolean takesMultipleArguments() 
     103    { 
     104        return TypeInformation.isCollection(optionSetter.getOptionDataType()); 
     105    } 
     106 
    102107 
    103108    public void setOption(Object bean, String value) throws OptionParserException 
    104109    { 
    105         Class< ? > optionType = optionSetter.getOptionDataType(); 
    106         try 
    107         { 
    108             Object convertedValue = TypeConverter.convertType(optionType, value); 
    109             optionSetter.setOption(bean, convertedValue); 
    110         } 
    111         catch (BeanException e) 
    112         { 
    113             throw new OptionParserException(ShellError.WRONG_DATA_TYPE, e); 
    114         } 
     110        optionSetter.setOption(bean, value); 
     111 
     112        //        Class< ? > optionType = optionSetter.getOptionDataType(); 
     113        //        try 
     114        //        { 
     115        //            Object convertedValue = TypeConverter.convertType(optionType, value); 
     116        //            optionSetter.setOption(bean, convertedValue); 
     117        //        } 
     118        //        catch (BeanException e) 
     119        //        { 
     120        //            throw new OptionParserException(ShellError.WRONG_DATA_TYPE, e); 
     121        //        } 
    115122 
    116123    } 
    117124 
     125    public void initialize(Object optionHolder) throws OptionParserException 
     126    { 
     127        optionSetter.initialize(optionHolder); 
     128    } 
     129 
    118130} 
  • XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/shell/OptionParser.java

    r2607 r2608  
    7575    public void parse(String[] args) throws OptionParserException 
    7676    { 
     77        // initialize collections in the OptionHolder 
     78        for (OptionItem each : schema.getOptionItemList()) 
     79        { 
     80            each.initialize(optionHolder); 
     81        } 
     82        for (ArgumentItem each : schema.getArgumentItemList()) 
     83        { 
     84            each.initialize(optionHolder); 
     85        } 
     86 
    7787        HashSet<Option> activatedOption = new HashSet<Option>(); 
    7888        HashSet<Argument> activatedArgument = new HashSet<Argument>(); 
  • XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/shell/OptionSchema.java

    r2607 r2608  
    246246            if (arg.index() == argumentIndex) 
    247247                return each; 
    248             if (arg.index() > argumentIndex && each.takesMultipleArguments()) 
     248            if (arg.index() < argumentIndex && each.takesMultipleArguments()) 
    249249                return each; 
    250250        } 
  • XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/shell/OptionSetter.java

    r2603 r2608  
    3838 
    3939    boolean takesArgument(); 
     40     
     41    void initialize(Object bean) throws OptionParserException; 
    4042} 
  • XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/shell/OptionSetterViaField.java

    r2607 r2608  
    2626 
    2727import java.lang.reflect.Field; 
     28import java.lang.reflect.InvocationTargetException; 
     29import java.lang.reflect.Method; 
    2830 
     31import org.xerial.core.XerialError; 
     32import org.xerial.core.XerialErrorCode; 
     33import org.xerial.util.bean.BeanException; 
     34import org.xerial.util.bean.TypeConverter; 
    2935import org.xerial.util.bean.TypeInformation; 
    3036import org.xerial.util.cui.OptionParserException; 
     
    5056    } 
    5157 
    52     public void setOption(Object bean, Object value) throws OptionParserException 
     58    protected Object getValue(Object bean) 
     59    { 
     60        Object value = null; 
     61        try 
     62        { 
     63            value = field.get(bean); 
     64        } 
     65        catch (IllegalAccessException e) 
     66        { 
     67            field.setAccessible(true); 
     68            try 
     69            { 
     70                value = field.get(bean); 
     71            } 
     72            catch (IllegalAccessException e1) 
     73            { 
     74                throw new IllegalAccessError(e1.getMessage()); 
     75            } 
     76        } 
     77        return value; 
     78    } 
     79     
     80    protected void setValue(Object bean, Object value) 
    5381    { 
    5482        try 
     
    6896            } 
    6997        } 
     98 
     99    } 
     100     
     101    public void setOption(Object bean, Object value) throws OptionParserException 
     102    { 
     103        try 
     104        { 
     105            if (setterTakesMultipleArguments()) 
     106            { 
     107                Object collection = getValue(bean); 
     108                if (collection == null) 
     109                { 
     110                    throw new XerialError(XerialErrorCode.NOT_INITIALIZED); 
     111                } 
     112                 
     113                // use adder 
     114                try 
     115                { 
     116                    Method adder = getOptionDataType().getMethod("add", Object.class); 
     117                    // TODO type convertion of value to the collection element   
     118                    adder.invoke(collection, value); 
     119                } 
     120                catch (SecurityException e) 
     121                { 
     122                    throw new XerialError(XerialErrorCode.INACCESSIBLE_METHOD, "add of " 
     123                            + getOptionDataType().toString()); 
     124                } 
     125                catch (NoSuchMethodException e) 
     126                { 
     127                    throw new XerialError(XerialErrorCode.NOT_A_COLLECTION, getOptionDataType().toString()); 
     128                } 
     129                catch (IllegalAccessException e) 
     130                { 
     131                    throw new XerialError(XerialErrorCode.INACCESSIBLE_METHOD, "add of " 
     132                            + getOptionDataType().toString()); 
     133                } 
     134                catch (InvocationTargetException e) 
     135                { 
     136                    throw new XerialError(XerialErrorCode.INACCESSIBLE_METHOD, e); 
     137                } 
     138            } 
     139            else 
     140            { 
     141                Class< ? > optionType = getOptionDataType(); 
     142                Object convertedValue = TypeConverter.convertType(optionType, value); 
     143                setValue(bean, convertedValue); 
     144            } 
     145        } 
    70146        catch (IllegalArgumentException e) 
     147        { 
     148            throw new OptionParserException(ShellError.WRONG_DATA_TYPE, e); 
     149        } 
     150        catch (BeanException e) 
    71151        { 
    72152            throw new OptionParserException(ShellError.WRONG_DATA_TYPE, e); 
     
    86166    } 
    87167 
     168     
     169    public void initialize(Object bean) throws OptionParserException 
     170    { 
     171        try 
     172        { 
     173            if (setterTakesMultipleArguments()) 
     174            { 
     175                Object collection = getValue(bean); 
     176 
     177                // initialize the array 
     178                if (collection == null) 
     179                { 
     180                    collection = TypeInformation.createInstance(getOptionDataType()); 
     181                    setValue(bean, collection); 
     182                } 
     183            } 
     184        } 
     185        catch (BeanException e) 
     186        { 
     187            throw new OptionParserException(e.getErrorCode()); 
     188        } 
     189    } 
     190 
    88191} 
  • XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/shell/OptionSetterViaMethod.java

    r2607 r2608  
    9292    } 
    9393 
     94    public void initialize(Object bean) throws OptionParserException 
     95    { 
     96    // do nothing 
     97    } 
     98 
    9499} 
  • XerialJ/trunk/xerial-core/src/test/java/org/xerial/util/bean/TypeReferenceTest.java

    r2606 r2608  
    2525package org.xerial.util.bean; 
    2626 
    27 import static org.junit.Assert.*; 
     27import static org.junit.Assert.assertEquals; 
     28import static org.junit.Assert.assertNotNull; 
    2829 
    2930import java.lang.reflect.Type; 
    3031import java.util.List; 
     32import java.util.Map; 
    3133 
    3234import org.junit.Test; 
     
    4143        assertEquals(String.class, c); 
    4244         
     45        Type k = new TypeReference<Map<String, Integer>>() {}.getElementType()[0]; 
     46        Type v = new TypeReference<Map<String, Integer>>() {}.getElementType()[1]; 
     47        assertEquals(String.class, k); 
     48        assertEquals(Integer.class, v); 
    4349    } 
     50     
     51    @Test 
     52    public void elementType() 
     53    { 
     54        Type c = new TypeReference<String>() {}.getType(); 
     55        assertEquals(String.class, c); 
     56    } 
     57     
     58    class GenericReference 
     59    { 
     60        Class< ? > c; 
     61 
     62        public GenericReference(Class< ? > c) 
     63        { 
     64            this.c = c; 
     65        } 
     66         
     67        Object newInstance() 
     68        { 
     69            try 
     70            { 
     71                return TypeInformation.createInstance(c); 
     72            } 
     73            catch (BeanException e) 
     74            { 
     75                return null; 
     76            } 
     77        } 
     78 
     79    } 
     80     
     81    @Test 
     82    public void genericTypeReference() 
     83    { 
     84        GenericReference gref = new GenericReference(String.class); 
     85         
     86        String str = (String) gref.newInstance(); 
     87 
     88        assertNotNull(str); 
     89        assertEquals(String.class, str.getClass()); 
     90 
     91    } 
     92 
    4493} 
  • XerialJ/trunk/xerial-core/src/test/java/org/xerial/util/shell/OptionParserTest.java

    r2607 r2608  
    2626 
    2727import static org.junit.Assert.assertEquals; 
     28import static org.junit.Assert.assertFalse; 
    2829import static org.junit.Assert.assertNotNull; 
    2930import static org.junit.Assert.assertTrue; 
     
    3536import org.junit.Test; 
    3637import org.xerial.util.cui.OptionParserException; 
     38import org.xerial.util.log.LogLevel; 
     39import org.xerial.util.log.Logger; 
    3740 
    3841public class OptionParserTest 
    3942{ 
    40  
     43    private static Logger _logger = Logger.getLogger(OptionParserTest.class); 
     44     
    4145    @Before 
    4246    public void setUp() throws Exception 
     
    6064 
    6165    @Test 
    62     public void option() throws OptionParserException 
     66    public void optionBinding() throws OptionParserException 
    6367    { 
    6468        MyOption myOption = new MyOption(); 
     
    7478        assertEquals("2.txt", myOption.fileList.get(1)); 
    7579 
     80         
    7681    } 
     82     
     83    @Test 
     84    public void initializeCollectionsInOptionHolder() throws OptionParserException 
     85    { 
     86        MyOption myOption = new MyOption(); 
     87        OptionParser parser = new OptionParser(myOption); 
     88 
     89        parser.parse(new String[] { "add" }); 
     90 
     91        assertEquals("add", myOption.subCommand); 
     92        assertFalse(myOption.displayHelp); 
     93        assertNotNull(myOption.fileList); 
     94        assertEquals(0, myOption.fileList.size()); 
     95 
     96    } 
     97     
     98    class LoglevelCommand 
     99    { 
     100        @Option(symbol = "l", longName = "loglevel", varName = "LOG_LEVEL") 
     101        private LogLevel loglevel = LogLevel.INFO; 
     102    } 
     103 
     104    @Test 
     105    public void enumOption() throws OptionParserException 
     106    { 
     107        LoglevelCommand opt = new LoglevelCommand(); 
     108        OptionParser parser = new OptionParser(opt); 
     109        parser.parse(new String[] { "--loglevel=DEBUG" }); 
     110 
     111        assertEquals(LogLevel.DEBUG, opt.loglevel); 
     112    } 
     113     
     114    class MultipleName 
     115    { 
     116        @Option(longName = "name") 
     117        private List<String> name; 
     118    } 
     119 
     120    @Test 
     121    public void mulpleOptionOccurences() throws OptionParserException 
     122    { 
     123        MultipleName mn = new MultipleName(); 
     124        OptionParser parser = new OptionParser(mn); 
     125        parser.parse(new String[] { "--name=leo", "--name=yui" }); 
     126 
     127        assertNotNull(mn.name); 
     128        assertEquals(2, mn.name.size()); 
     129        assertEquals("leo", mn.name.get(0)); 
     130        assertEquals("leo", mn.name.get(1)); 
     131    } 
     132 
    77133}