001 /*
002 * $Id: PropertyNotFoundTest.java 1104080 2011-05-17 09:22:09Z 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 java.util.ArrayList;
023 import java.util.Collection;
024 import java.util.Map;
025
026 import org.apache.commons.ognl.OgnlContext;
027 import org.apache.commons.ognl.OgnlException;
028 import org.apache.commons.ognl.OgnlRuntime;
029 import org.apache.commons.ognl.PropertyAccessor;
030 import org.junit.Before;
031 import org.junit.runner.RunWith;
032 import org.junit.runners.Parameterized;
033 import org.junit.runners.Parameterized.Parameters;
034
035 @RunWith(value = Parameterized.class)
036 public class PropertyNotFoundTest
037 extends OgnlTestCase
038 {
039 private static final Blah BLAH = new Blah();
040
041 private static Object[][] TESTS = { { BLAH, "webwork.token.name", OgnlException.class, "W value",
042 OgnlException.class }, };
043
044 /*
045 * =================================================================== Public static classes
046 * ===================================================================
047 */
048 public static class Blah
049 {
050 String x;
051
052 String y;
053
054 public String getX()
055 {
056 return x;
057 }
058
059 public void setX( String x )
060 {
061 this.x = x;
062 }
063
064 public String getY()
065 {
066 return y;
067 }
068
069 public void setY( String y )
070 {
071 this.y = y;
072 }
073 }
074
075 public static class BlahPropertyAccessor
076 implements PropertyAccessor
077 {
078 public void setProperty( Map context, Object target, Object name, Object value )
079 throws OgnlException
080 {
081 }
082
083 public Object getProperty( Map context, Object target, Object name )
084 throws OgnlException
085 {
086 if ( "x".equals( name ) || "y".equals( name ) )
087 {
088 return OgnlRuntime.getProperty( (OgnlContext) context, target, name );
089 }
090 return null;
091 }
092
093 public String getSourceAccessor( OgnlContext context, Object target, Object index )
094 {
095 return index.toString();
096 }
097
098 public String getSourceSetter( OgnlContext context, Object target, Object index )
099 {
100 return index.toString();
101 }
102 }
103
104 /*
105 * =================================================================== Public static methods
106 * ===================================================================
107 */
108 @Parameters
109 public static Collection<Object[]> data()
110 {
111 Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
112 for ( int i = 0; i < TESTS.length; i++ )
113 {
114 Object[] tmp = new Object[6];
115 tmp[0] = TESTS[i][1];
116 tmp[1] = TESTS[i][0];
117 tmp[2] = TESTS[i][1];
118
119 switch ( TESTS[i].length )
120 {
121 case 3:
122 tmp[3] = TESTS[i][2];
123 break;
124
125 case 4:
126 tmp[3] = TESTS[i][2];
127 tmp[4] = TESTS[i][3];
128 break;
129
130 case 5:
131 tmp[3] = TESTS[i][2];
132 tmp[4] = TESTS[i][3];
133 tmp[5] = TESTS[i][4];
134 break;
135
136 default:
137 throw new RuntimeException( "don't understand TEST format with length " + TESTS[i].length );
138 }
139
140 data.add( tmp );
141 }
142 return data;
143 }
144
145 /*
146 * =================================================================== Constructors
147 * ===================================================================
148 */
149 public PropertyNotFoundTest( String name, Object root, String expressionString, Object expectedResult,
150 Object setValue, Object expectedAfterSetResult )
151 {
152 super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
153 }
154
155 @Before
156 @Override
157 public void setUp()
158 {
159 super.setUp();
160 OgnlRuntime.setPropertyAccessor( Blah.class, new BlahPropertyAccessor() );
161 }
162 }