View Javadoc

1   /*
2    * $Id: StaticsAndConstructorsTest.java 1198683 2011-11-07 09:52:34Z 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.Root;
23  import org.apache.commons.ognl.test.objects.Simple;
24  import org.junit.Test;
25  import org.junit.runner.RunWith;
26  import org.junit.runners.Parameterized;
27  import org.junit.runners.Parameterized.Parameters;
28  
29  import java.util.ArrayList;
30  import java.util.Collection;
31  
32  @RunWith(value = Parameterized.class)
33  public class StaticsAndConstructorsTest
34      extends OgnlTestCase
35  {
36      private static Root ROOT = new Root();
37  
38      private static Object[][] TESTS =
39          {
40              { "@java.lang.Class@forName(\"java.lang.Object\")", Object.class },
41              { "@java.lang.Integer@MAX_VALUE", Integer.MAX_VALUE },
42              { "@@max(3,4)", 4 },
43              { "new java.lang.StringBuilder().append(55).toString()", "55" },
44              { "class", ROOT.getClass() },
45              { "@org.apache.commons.ognl.test.objects.Root@class", ROOT.getClass() },
46              { "class.getName()", ROOT.getClass().getName() },
47              { "@org.apache.commons.ognl.test.objects.Root@class.getName()", ROOT.getClass().getName() },
48              { "@org.apache.commons.ognl.test.objects.Root@class.name", ROOT.getClass().getName() },
49              { "class.getSuperclass()", ROOT.getClass().getSuperclass() },
50              { "class.superclass", ROOT.getClass().getSuperclass() },
51              { "class.name", ROOT.getClass().getName() },
52              { "getStaticInt()", Root.getStaticInt() },
53              { "@org.apache.commons.ognl.test.objects.Root@getStaticInt()", Root.getStaticInt() },
54              { "new org.apache.commons.ognl.test.objects.Simple(property).getStringValue()",
55                  new Simple().getStringValue() },
56              { "new org.apache.commons.ognl.test.objects.Simple(map['test'].property).getStringValue()",
57                  new Simple().getStringValue() },
58              { "map.test.getCurrentClass(@org.apache.commons.ognl.test.StaticsAndConstructorsTest@KEY.toString())",
59                  "size stop" },
60              { "new org.apache.commons.ognl.test.StaticsAndConstructorsTest$IntWrapper(index)",
61                  new IntWrapper( ROOT.getIndex() ) },
62              { "new org.apache.commons.ognl.test.StaticsAndConstructorsTest$IntObjectWrapper(index)",
63                  new IntObjectWrapper( ROOT.getIndex() ) },
64              { "new org.apache.commons.ognl.test.StaticsAndConstructorsTest$A(#root)", new A( ROOT ) },
65              { "@org.apache.commons.ognl.test.StaticsAndConstructorsTest$Animals@values().length != 2", Boolean.TRUE },
66              { "isOk(@org.apache.commons.ognl.test.objects.SimpleEnum@ONE, null)", Boolean.TRUE }, };
67  
68      public static final String KEY = "size";
69  
70      public static class IntWrapper
71      {
72          public IntWrapper( int value )
73          {
74              this.value = value;
75          }
76  
77          private final int value;
78  
79          public String toString()
80          {
81              return Integer.toString( value );
82          }
83  
84          public boolean equals( Object o )
85          {
86              if ( this == o )
87                  return true;
88              if ( o == null || getClass() != o.getClass() )
89                  return false;
90  
91              IntWrapper that = (IntWrapper) o;
92  
93              return value == that.value;
94          }
95      }
96  
97      public static class IntObjectWrapper
98      {
99  
100         public IntObjectWrapper( Integer value )
101         {
102             this.value = value;
103         }
104 
105         private final Integer value;
106 
107         public String toString()
108         {
109             return value.toString();
110         }
111 
112         public boolean equals( Object o )
113         {
114             if ( this == o )
115                 return true;
116             if ( o == null || getClass() != o.getClass() )
117                 return false;
118 
119             IntObjectWrapper that = (IntObjectWrapper) o;
120 
121             return value.equals( that.value );
122         }
123     }
124 
125     public static class A
126     {
127         String key = "A";
128 
129         public A( Root root )
130         {
131 
132         }
133 
134         public boolean equals( Object o )
135         {
136             if ( this == o )
137                 return true;
138             if ( o == null || getClass() != o.getClass() )
139                 return false;
140 
141             A a = (A) o;
142 
143             return !( key != null ? !key.equals( a.key ) : a.key != null );
144 
145         }
146     }
147 
148     public enum Animals
149     {
150 
151         Dog, Cat, Wallabee, Bear
152     }
153 
154     /*
155      * =================================================================== Public static methods
156      * ===================================================================
157      */
158     @Parameters
159     public static Collection<Object[]> data()
160     {
161         Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
162         for ( Object[] TEST : TESTS )
163         {
164             Object[] tmp = new Object[6];
165             tmp[0] = TEST[0] + " (" + TEST[1] + ")";
166             tmp[1] = ROOT;
167             tmp[2] = TEST[0];
168             tmp[3] = TEST[1];
169 
170             data.add( tmp );
171         }
172         return data;
173     }
174 
175     /*
176      * =================================================================== Constructors
177      * ===================================================================
178      */
179     public StaticsAndConstructorsTest( String name, Object root, String expressionString, Object expectedResult,
180                                        Object setValue, Object expectedAfterSetResult )
181     {
182         super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
183     }
184 
185     @Test
186 
187     @Override
188     public void runTest()
189         throws Exception
190     {
191         super.runTest();
192     }
193 }