001 /*
002 * $Id: ArrayCreationTest.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.ExpressionSyntaxException;
023 import org.apache.commons.ognl.test.objects.Entry;
024 import org.apache.commons.ognl.test.objects.Root;
025 import org.apache.commons.ognl.test.objects.Simple;
026 import org.junit.runner.RunWith;
027 import org.junit.runners.Parameterized;
028 import org.junit.runners.Parameterized.Parameters;
029
030 import java.util.ArrayList;
031 import java.util.Collection;
032
033 @RunWith(value = Parameterized.class)
034 public class ArrayCreationTest
035 extends OgnlTestCase
036 {
037
038 private static Root ROOT = new Root();
039
040 private static Object[][] TESTS =
041 {
042 // Array creation
043 { ROOT, "new String[] { \"one\", \"two\" }", new String[] { "one", "two" } },
044 { ROOT, "new String[] { 1, 2 }", new String[] { "1", "2" } },
045 { ROOT, "new Integer[] { \"1\", 2, \"3\" }",
046 new Integer[] { 1, 2, 3 } },
047 { ROOT, "new String[10]", new String[10] },
048 { ROOT, "new Object[4] { #root, #this }", ExpressionSyntaxException.class },
049 { ROOT, "new Object[4]", new Object[4] },
050 { ROOT, "new Object[] { #root, #this }", new Object[] { ROOT, ROOT } },
051 {
052 ROOT,
053 "new org.apache.commons.ognl.test.objects.Simple[] { new org.apache.commons.ognl.test.objects.Simple(), new org.apache.commons.ognl.test.objects.Simple(\"foo\", 1.0f, 2) }",
054 new Simple[] { new Simple(), new Simple( "foo", 1.0f, 2 ) } },
055 { ROOT, "new org.apache.commons.ognl.test.objects.Simple[5]", new Simple[5] },
056 { ROOT, "new org.apache.commons.ognl.test.objects.Simple(new Object[5])", new Simple( new Object[5] ) },
057 { ROOT, "new org.apache.commons.ognl.test.objects.Simple(new String[5])", new Simple( new String[5] ) },
058 {
059 ROOT,
060 "objectIndex ? new org.apache.commons.ognl.test.objects.Entry[] { new org.apache.commons.ognl.test.objects.Entry(), new org.apache.commons.ognl.test.objects.Entry()} "
061 + ": new org.apache.commons.ognl.test.objects.Entry[] { new org.apache.commons.ognl.test.objects.Entry(), new org.apache.commons.ognl.test.objects.Entry()} ",
062 new Entry[] { new Entry(), new Entry() } } };
063
064 /*
065 * =================================================================== Public static methods
066 * ===================================================================
067 */
068 @Parameters
069 public static Collection<Object[]> data()
070 {
071 Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
072 for ( Object[] TEST : TESTS )
073 {
074 Object[] tmp = new Object[6];
075 tmp[0] = TEST[1];
076 tmp[1] = TEST[0];
077 tmp[2] = TEST[1];
078
079 switch ( TEST.length )
080 {
081 case 3:
082 tmp[3] = TEST[2];
083 break;
084
085 case 4:
086 tmp[3] = TEST[2];
087 tmp[4] = TEST[3];
088 break;
089
090 case 5:
091 tmp[3] = TEST[2];
092 tmp[4] = TEST[3];
093 tmp[5] = TEST[4];
094 break;
095
096 default:
097 throw new RuntimeException( "don't understand TEST format with length" );
098 }
099
100 data.add( tmp );
101 }
102 return data;
103 }
104
105 /*
106 * =================================================================== Constructors
107 * ===================================================================
108 */
109 public ArrayCreationTest( String name, Object root, String expressionString, Object expectedResult,
110 Object setValue, Object expectedAfterSetResult )
111 {
112 super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
113 }
114 }