001 /* 002 * $Id: ArrayElementsTest.java 1230454 2012-01-12 09:35:53Z mcucchiara $ 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 package org.apache.commons.ognl.test; 021 022 import org.apache.commons.ognl.test.objects.Root; 023 import org.junit.Before; 024 import org.junit.runner.RunWith; 025 import org.junit.runners.Parameterized; 026 import org.junit.runners.Parameterized.Parameters; 027 028 import java.util.ArrayList; 029 import java.util.Arrays; 030 import java.util.Collection; 031 032 @RunWith(value = Parameterized.class) 033 public class ArrayElementsTest 034 extends OgnlTestCase 035 { 036 037 private static String[] STRING_ARRAY = new String[] { "hello", "world" }; 038 039 private static int[] INT_ARRAY = new int[] { 10, 20 }; 040 041 private static Root ROOT = new Root(); 042 043 private static Object[][] TESTS = { 044 // Array elements test 045 { STRING_ARRAY, "length", 2 }, 046 { STRING_ARRAY, "#root[1]", "world" }, 047 { INT_ARRAY, "#root[1]", 20 }, 048 { INT_ARRAY, "#root[1]", 20, "50", 50 }, 049 { INT_ARRAY, "#root[1]", 50, new String[] { "50", "100" }, 50 }, 050 { ROOT, "intValue", 0, new String[] { "50", "100" }, 50 }, 051 { ROOT, "array", ROOT.getArray(), new String[] { "50", "100" }, new int[] { 50, 100 } }, 052 { null, "\"{Hello}\".toCharArray()[6]", '}' }, 053 { null, "\"Tapestry\".toCharArray()[2]", 'p' }, 054 { null, "{'1','2','3'}", 055 Arrays.asList( '1', '2', '3' ) }, 056 { null, "{ true, !false }", Arrays.asList( Boolean.TRUE, Boolean.TRUE ) } }; 057 058 /* 059 * =================================================================== Private static methods 060 * =================================================================== 061 */ 062 /* 063 * =================================================================== Public static methods 064 * =================================================================== 065 */ 066 @Parameters 067 public static Collection<Object[]> data() 068 { 069 Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length); 070 for ( Object[] TEST : TESTS ) 071 { 072 Object[] tmp = new Object[6]; 073 tmp[0] = TEST[1]; 074 tmp[1] = TEST[0]; 075 tmp[2] = TEST[1]; 076 077 switch ( TEST.length ) 078 { 079 case 3: 080 tmp[3] = TEST[2]; 081 break; 082 083 case 4: 084 tmp[3] = TEST[2]; 085 tmp[4] = TEST[3]; 086 break; 087 088 case 5: 089 tmp[3] = TEST[2]; 090 tmp[4] = TEST[3]; 091 tmp[5] = TEST[4]; 092 break; 093 094 default: 095 throw new RuntimeException( "don't understand TEST format with length" ); 096 } 097 098 data.add( tmp ); 099 } 100 return data; 101 } 102 103 /* 104 * =================================================================== Constructors 105 * =================================================================== 106 */ 107 public ArrayElementsTest( String name, Object root, String expressionString, Object expectedResult, 108 Object setValue, Object expectedAfterSetResult ) 109 { 110 super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult ); 111 } 112 113 /* 114 * =================================================================== Overridden methods 115 * =================================================================== 116 */ 117 @Override 118 @Before 119 public void setUp() 120 { 121 super.setUp(); 122 /** 123 * TypeConverter arrayConverter; 124 * arrayConverter = new DefaultTypeConverter() { public Object convertValue(Map context, Object target, Member 125 * member, String propertyName, Object value, Class toType) { if (value.getClass().isArray()) { if 126 * (!toType.isArray()) { value = Array.get(value, 0); } } return super.convertValue(context, target, member, 127 * propertyName, value, toType); } }; _context.setTypeConverter(arrayConverter); 128 */ 129 } 130 }