001 /*
002 * $Id: PropertyTest.java 1188000 2011-10-23 23:10:24Z 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.BaseBean;
023 import org.apache.commons.ognl.test.objects.Bean2;
024 import org.apache.commons.ognl.test.objects.FirstBean;
025 import org.apache.commons.ognl.test.objects.PropertyHolder;
026 import org.apache.commons.ognl.test.objects.Root;
027 import org.junit.runner.RunWith;
028 import org.junit.runners.Parameterized;
029 import org.junit.runners.Parameterized.Parameters;
030
031 import java.text.SimpleDateFormat;
032 import java.util.ArrayList;
033 import java.util.Arrays;
034 import java.util.Collection;
035
036 @RunWith(value = Parameterized.class)
037 public class PropertyTest
038 extends OgnlTestCase
039 {
040
041 public static final SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat( "MM/dd/yyyy hh:mm a 'CST'" );
042
043 public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat( "MM/dd/yyyy" );
044
045 public static final String VALUE = "foo";
046
047 private static Root ROOT = new Root();
048
049 private static BaseBean BEAN = new FirstBean();
050
051 private static PropertyHolder PROPERTY = new PropertyHolder();
052
053 private static Object[][] TESTS =
054 {
055 { ROOT, "testString != null && !false", Boolean.TRUE },
056 { ROOT, "!getRenderNavigation() and !getReadonly()", Boolean.TRUE },
057 { ROOT, "!bean2.pageBreakAfter", Boolean.TRUE },
058 { ROOT, "map", ROOT.getMap() },
059 { ROOT, "map.test", ROOT },
060 { ROOT, "map[\"test\"]", ROOT },
061 { ROOT, "map[\"te\" + \"st\"]", ROOT },
062 { ROOT, "map[(\"s\" + \"i\") + \"ze\"]", ROOT.getMap().get( Root.SIZE_STRING ) },
063 { ROOT, "map[\"size\"]", ROOT.getMap().get( Root.SIZE_STRING ) },
064 { ROOT, "map[@org.apache.commons.ognl.test.objects.Root@SIZE_STRING]", ROOT.getMap().get( Root.SIZE_STRING ) },
065 { ROOT, "stringValue != null && stringValue.length() > 0", Boolean.FALSE },
066 { ROOT, "indexedStringValue != null && indexedStringValue.length() > 0", Boolean.TRUE },
067 { ROOT.getMap(), "list", ROOT.getList() },
068 { ROOT, "map.array[0]", new Integer( ROOT.getArray()[0] ) },
069 { ROOT, "map.list[1]", ROOT.getList().get( 1 ) },
070 { ROOT, "map[^]", new Integer( 99 ) },
071 { ROOT, "map[$]", null },
072 { ROOT.getMap(), "array[$]", new Integer( ROOT.getArray()[ROOT.getArray().length - 1] ) },
073 { ROOT, "[\"map\"]", ROOT.getMap() },
074 { ROOT.getArray(), "length", new Integer( ROOT.getArray().length ) },
075 { ROOT, "getMap().list[|]", ROOT.getList().get( ROOT.getList().size() / 2 ) },
076 { ROOT, "map.(array[2] + size())", new Integer( ROOT.getArray()[2] + ROOT.getMap().size() ) },
077 { ROOT, "map.(#this)", ROOT.getMap() },
078 { ROOT, "map.(#this != null ? #this['size'] : null)", ROOT.getMap().get( Root.SIZE_STRING ) },
079 { ROOT, "map[^].(#this == null ? 'empty' : #this)", new Integer( 99 ) },
080 { ROOT, "map[$].(#this == null ? 'empty' : #this)", "empty" },
081 { ROOT, "map[$].(#root == null ? 'empty' : #root)", ROOT },
082 { ROOT, "((selected != null) && (currLocale.toString() == selected.toString())) ? 'first' : 'second'",
083 "first" },
084 { ROOT, "{stringValue, getMap()}", Arrays.asList( new Object[] { ROOT.getStringValue(), ROOT.getMap() } ) },
085 { ROOT, "{'stringValue', map[\"test\"].map[\"size\"]}",
086 Arrays.asList( new Object[] { "stringValue", ROOT.getMap().get( "size" ) } ) },
087 { ROOT, "property.bean3.value + '(this.checked)'", "100(this.checked)" },
088 { ROOT, "getIndexedProperty(property.bean3.map[\"bar\"])", ROOT.getArray() },
089 { ROOT, "getProperty().getBean3()", ( (Bean2) ROOT.getProperty() ).getBean3() },
090 { ROOT, "intValue", new Integer( 0 ), new Integer( 2 ), new Integer( 2 ) },
091 { ROOT, "! booleanValue", Boolean.TRUE },
092 { ROOT, "booleanValue", Boolean.FALSE, Boolean.TRUE, Boolean.TRUE },
093 { ROOT, "! disabled", new Boolean( false ) },
094 { ROOT, "disabled || readonly", Boolean.TRUE },
095 { ROOT, "property.bean3.value != null", Boolean.TRUE },
096 { ROOT, "\"background-color:blue; width:\" + (currentLocaleVerbosity / 2) + \"px\"",
097 "background-color:blue; width:43px" },
098 { ROOT, "renderNavigation ? '' : 'noborder'", "noborder" },
099 { ROOT, "format('key', array)", "formatted" },
100 { ROOT, "format('key', intValue)", "formatted" },
101 { ROOT, "format('key', map.size)", "formatted" },
102 { ROOT,
103 "'disableButton(this,\"' + map.get('button-testing') + '\");clearElement("testFtpMessage")'",
104 "disableButton(this,'null');clearElement('testFtpMessage')" },
105 { ROOT.getMap(), "!disableWarning", Boolean.TRUE },
106 { ROOT.getMap(), "get('value').bean3.value",
107 new Integer( ( (Bean2) ROOT.getMap().get( "value" ) ).getBean3().getValue() ) },
108 { ROOT.getMap(), "\"Tapestry\".toCharArray()[2]", new Character( 'p' ) },
109 { ROOT.getMap(), "nested.deep.last", Boolean.TRUE },
110 { ROOT, "'last ' + getCurrentClass(@org.apache.commons.ognl.test.PropertyTest@VALUE)", "last foo stop" },
111 { ROOT, "@org.apache.commons.ognl.test.PropertyTest@formatValue(property.millis, true, true)",
112 formatValue( (int) ( (Bean2) ROOT.getProperty() ).getMillis(), true, true ) },
113 { ROOT, "nullObject || !readonly", Boolean.TRUE },
114 { ROOT,
115 "testDate == null ? '-' : @org.apache.commons.ognl.test.PropertyTest@DATETIME_FORMAT.format(testDate)",
116 DATETIME_FORMAT.format( ROOT.getTestDate() ) },
117 { ROOT, "disabled ? 'disabled' : 'othernot'", "disabled" },
118 { BEAN, "two.getMessage(active ? 'ACT' : 'INA')", "[ACT]" },
119 { BEAN, "hasChildren('aaa')", Boolean.TRUE },
120 { BEAN, "two.hasChildren('aa')", Boolean.FALSE },
121 { BEAN, "two.hasChildren('a')", Boolean.FALSE },
122 { ROOT, "sorted ? (readonly ? 'currentSortDesc' : 'currentSortAsc') : 'currentSortNone'", "currentSortAsc" },
123 { ROOT, "getAsset( (width?'Yes':'No')+'Icon' )", "YesIcon" },
124 { ROOT, "flyingMonkey", Boolean.TRUE },
125 { ROOT,
126 "expiration == null ? '' : @org.apache.commons.ognl.test.PropertyTest@DATE_FORMAT.format(expiration)",
127 "" }, { ROOT, "printDelivery ? 'javascript:toggle(' + bean2.id + ');' : ''", "javascript:toggle(1);" },
128 { ROOT, "openTransitionWin", Boolean.FALSE }, { ROOT, "b.methodOfB(a.methodOfA(b)-1)", new Integer( 0 ) },
129 { ROOT, "disabled", Boolean.TRUE }, { PROPERTY, "value", "" }, { PROPERTY, "search", "foo" } };
130
131 public static String formatValue( int millis, boolean b1, boolean b2 )
132 {
133 return millis + "-formatted";
134 }
135
136 /*
137 * =================================================================== Public static methods
138 * ===================================================================
139 */
140 @Parameters
141 public static Collection<Object[]> data()
142 {
143 Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
144 for ( int i = 0; i < TESTS.length; i++ )
145 {
146 Object[] tmp = new Object[6];
147 tmp[0] = TESTS[i][1];
148 tmp[1] = TESTS[i][0];
149 tmp[2] = TESTS[i][1];
150 tmp[3] = TESTS[i][2];
151
152 if ( TESTS[i].length == 5 )
153 {
154 tmp[4] = TESTS[i][3];
155 tmp[5] = TESTS[i][4];
156 }
157
158 data.add( tmp );
159 }
160 return data;
161 }
162
163 /*
164 * =================================================================== Constructors
165 * ===================================================================
166 */
167 public PropertyTest( String name, Object root, String expressionString, Object expectedResult, Object setValue,
168 Object expectedAfterSetResult )
169 {
170 super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
171 }
172 }