001    /*
002     * $Id: StaticsAndConstructorsTest.java 1198683 2011-11-07 09:52:34Z 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.Root;
023    import org.apache.commons.ognl.test.objects.Simple;
024    import org.junit.Test;
025    import org.junit.runner.RunWith;
026    import org.junit.runners.Parameterized;
027    import org.junit.runners.Parameterized.Parameters;
028    
029    import java.util.ArrayList;
030    import java.util.Collection;
031    
032    @RunWith(value = Parameterized.class)
033    public class StaticsAndConstructorsTest
034        extends OgnlTestCase
035    {
036        private static Root ROOT = new Root();
037    
038        private static Object[][] TESTS =
039            {
040                { "@java.lang.Class@forName(\"java.lang.Object\")", Object.class },
041                { "@java.lang.Integer@MAX_VALUE", Integer.MAX_VALUE },
042                { "@@max(3,4)", 4 },
043                { "new java.lang.StringBuilder().append(55).toString()", "55" },
044                { "class", ROOT.getClass() },
045                { "@org.apache.commons.ognl.test.objects.Root@class", ROOT.getClass() },
046                { "class.getName()", ROOT.getClass().getName() },
047                { "@org.apache.commons.ognl.test.objects.Root@class.getName()", ROOT.getClass().getName() },
048                { "@org.apache.commons.ognl.test.objects.Root@class.name", ROOT.getClass().getName() },
049                { "class.getSuperclass()", ROOT.getClass().getSuperclass() },
050                { "class.superclass", ROOT.getClass().getSuperclass() },
051                { "class.name", ROOT.getClass().getName() },
052                { "getStaticInt()", Root.getStaticInt() },
053                { "@org.apache.commons.ognl.test.objects.Root@getStaticInt()", Root.getStaticInt() },
054                { "new org.apache.commons.ognl.test.objects.Simple(property).getStringValue()",
055                    new Simple().getStringValue() },
056                { "new org.apache.commons.ognl.test.objects.Simple(map['test'].property).getStringValue()",
057                    new Simple().getStringValue() },
058                { "map.test.getCurrentClass(@org.apache.commons.ognl.test.StaticsAndConstructorsTest@KEY.toString())",
059                    "size stop" },
060                { "new org.apache.commons.ognl.test.StaticsAndConstructorsTest$IntWrapper(index)",
061                    new IntWrapper( ROOT.getIndex() ) },
062                { "new org.apache.commons.ognl.test.StaticsAndConstructorsTest$IntObjectWrapper(index)",
063                    new IntObjectWrapper( ROOT.getIndex() ) },
064                { "new org.apache.commons.ognl.test.StaticsAndConstructorsTest$A(#root)", new A( ROOT ) },
065                { "@org.apache.commons.ognl.test.StaticsAndConstructorsTest$Animals@values().length != 2", Boolean.TRUE },
066                { "isOk(@org.apache.commons.ognl.test.objects.SimpleEnum@ONE, null)", Boolean.TRUE }, };
067    
068        public static final String KEY = "size";
069    
070        public static class IntWrapper
071        {
072            public IntWrapper( int value )
073            {
074                this.value = value;
075            }
076    
077            private final int value;
078    
079            public String toString()
080            {
081                return Integer.toString( value );
082            }
083    
084            public boolean equals( Object o )
085            {
086                if ( this == o )
087                    return true;
088                if ( o == null || getClass() != o.getClass() )
089                    return false;
090    
091                IntWrapper that = (IntWrapper) o;
092    
093                return value == that.value;
094            }
095        }
096    
097        public static class IntObjectWrapper
098        {
099    
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    }