View Javadoc

1   /*
2    * $Id: PropertyTest.java 1188000 2011-10-23 23:10:24Z mcucchiara $
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  package org.apache.commons.ognl.test;
21  
22  import org.apache.commons.ognl.test.objects.BaseBean;
23  import org.apache.commons.ognl.test.objects.Bean2;
24  import org.apache.commons.ognl.test.objects.FirstBean;
25  import org.apache.commons.ognl.test.objects.PropertyHolder;
26  import org.apache.commons.ognl.test.objects.Root;
27  import org.junit.runner.RunWith;
28  import org.junit.runners.Parameterized;
29  import org.junit.runners.Parameterized.Parameters;
30  
31  import java.text.SimpleDateFormat;
32  import java.util.ArrayList;
33  import java.util.Arrays;
34  import java.util.Collection;
35  
36  @RunWith(value = Parameterized.class)
37  public class PropertyTest
38      extends OgnlTestCase
39  {
40  
41      public static final SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat( "MM/dd/yyyy hh:mm a 'CST'" );
42  
43      public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat( "MM/dd/yyyy" );
44  
45      public static final String VALUE = "foo";
46  
47      private static Root ROOT = new Root();
48  
49      private static BaseBean BEAN = new FirstBean();
50  
51      private static PropertyHolder PROPERTY = new PropertyHolder();
52  
53      private static Object[][] TESTS =
54          {
55              { ROOT, "testString != null && !false", Boolean.TRUE },
56              { ROOT, "!getRenderNavigation() and !getReadonly()", Boolean.TRUE },
57              { ROOT, "!bean2.pageBreakAfter", Boolean.TRUE },
58              { ROOT, "map", ROOT.getMap() },
59              { ROOT, "map.test", ROOT },
60              { ROOT, "map[\"test\"]", ROOT },
61              { ROOT, "map[\"te\" + \"st\"]", ROOT },
62              { ROOT, "map[(\"s\" + \"i\") + \"ze\"]", ROOT.getMap().get( Root.SIZE_STRING ) },
63              { ROOT, "map[\"size\"]", ROOT.getMap().get( Root.SIZE_STRING ) },
64              { ROOT, "map[@org.apache.commons.ognl.test.objects.Root@SIZE_STRING]", ROOT.getMap().get( Root.SIZE_STRING ) },
65              { ROOT, "stringValue != null && stringValue.length() > 0", Boolean.FALSE },
66              { ROOT, "indexedStringValue != null && indexedStringValue.length() > 0", Boolean.TRUE },
67              { ROOT.getMap(), "list", ROOT.getList() },
68              { ROOT, "map.array[0]", new Integer( ROOT.getArray()[0] ) },
69              { ROOT, "map.list[1]", ROOT.getList().get( 1 ) },
70              { ROOT, "map[^]", new Integer( 99 ) },
71              { ROOT, "map[$]", null },
72              { ROOT.getMap(), "array[$]", new Integer( ROOT.getArray()[ROOT.getArray().length - 1] ) },
73              { ROOT, "[\"map\"]", ROOT.getMap() },
74              { ROOT.getArray(), "length", new Integer( ROOT.getArray().length ) },
75              { ROOT, "getMap().list[|]", ROOT.getList().get( ROOT.getList().size() / 2 ) },
76              { ROOT, "map.(array[2] + size())", new Integer( ROOT.getArray()[2] + ROOT.getMap().size() ) },
77              { ROOT, "map.(#this)", ROOT.getMap() },
78              { ROOT, "map.(#this != null ? #this['size'] : null)", ROOT.getMap().get( Root.SIZE_STRING ) },
79              { ROOT, "map[^].(#this == null ? 'empty' : #this)", new Integer( 99 ) },
80              { ROOT, "map[$].(#this == null ? 'empty' : #this)", "empty" },
81              { ROOT, "map[$].(#root == null ? 'empty' : #root)", ROOT },
82              { ROOT, "((selected != null) && (currLocale.toString() == selected.toString())) ? 'first' : 'second'",
83                  "first" },
84              { ROOT, "{stringValue, getMap()}", Arrays.asList( new Object[] { ROOT.getStringValue(), ROOT.getMap() } ) },
85              { ROOT, "{'stringValue', map[\"test\"].map[\"size\"]}",
86                  Arrays.asList( new Object[] { "stringValue", ROOT.getMap().get( "size" ) } ) },
87              { ROOT, "property.bean3.value + '(this.checked)'", "100(this.checked)" },
88              { ROOT, "getIndexedProperty(property.bean3.map[\"bar\"])", ROOT.getArray() },
89              { ROOT, "getProperty().getBean3()", ( (Bean2) ROOT.getProperty() ).getBean3() },
90              { ROOT, "intValue", new Integer( 0 ), new Integer( 2 ), new Integer( 2 ) },
91              { ROOT, "! booleanValue", Boolean.TRUE },
92              { ROOT, "booleanValue", Boolean.FALSE, Boolean.TRUE, Boolean.TRUE },
93              { ROOT, "! disabled", new Boolean( false ) },
94              { ROOT, "disabled || readonly", Boolean.TRUE },
95              { ROOT, "property.bean3.value != null", Boolean.TRUE },
96              { ROOT, "\"background-color:blue; width:\" + (currentLocaleVerbosity / 2) + \"px\"",
97                  "background-color:blue; width:43px" },
98              { ROOT, "renderNavigation ? '' : 'noborder'", "noborder" },
99              { 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 }