View Javadoc

1   /*
2    * $Id: ListPropertyAccessorTest.java 1104080 2011-05-17 09:22:09Z 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.accessors;
21  
22  import org.apache.commons.ognl.ListPropertyAccessor;
23  import org.apache.commons.ognl.Ognl;
24  import org.apache.commons.ognl.OgnlContext;
25  import org.apache.commons.ognl.enhance.ExpressionCompiler;
26  import org.apache.commons.ognl.test.objects.ListSource;
27  import org.apache.commons.ognl.test.objects.ListSourceImpl;
28  import org.apache.commons.ognl.test.objects.Root;
29  import org.junit.Test;
30  
31  import java.util.List;
32  
33  import static junit.framework.Assert.assertEquals;
34  import static junit.framework.Assert.assertNull;
35  
36  /**
37   * Tests functionality of various built in object accessors.
38   */
39  public class ListPropertyAccessorTest
40  {
41  
42      @Test
43      public void test_Get_Source_String_Number_Index()
44      {
45          ListPropertyAccessor pa = new ListPropertyAccessor();
46  
47          Root root = new Root();
48  
49          OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
50          context.setRoot( root );
51          context.setCurrentObject( root );
52          context.setCurrentType( Integer.TYPE );
53  
54          assertEquals( ".get(0)", pa.getSourceAccessor( context, root.getList(), "0" ) );
55  
56          assertEquals( List.class, context.getCurrentAccessor() );
57          assertEquals( Object.class, context.getCurrentType() );
58          assertEquals( Integer.TYPE, context.getPreviousType() );
59          assertEquals( null, context.getPreviousAccessor() );
60      }
61  
62      @Test
63      public void test_Get_Source_Object_Number_Index()
64      {
65          ListPropertyAccessor pa = new ListPropertyAccessor();
66  
67          Root root = new Root();
68  
69          OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
70          context.setRoot( root );
71          context.setCurrentObject( root );
72          context.setCurrentType( Integer.class );
73  
74          assertEquals( ".get(indexValue.intValue())", pa.getSourceAccessor( context, root.getList(), "indexValue" ) );
75  
76          assertEquals( List.class, context.getCurrentAccessor() );
77          assertEquals( Object.class, context.getCurrentType() );
78          assertEquals( Integer.class, context.getPreviousType() );
79          assertEquals( null, context.getPreviousAccessor() );
80      }
81  
82      @Test
83      public void test_List_To_Object_Property_Accessor_Read()
84          throws Exception
85      {
86          ListPropertyAccessor pa = new ListPropertyAccessor();
87  
88          ListSource list = new ListSourceImpl();
89  
90          OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
91          context.setRoot( list );
92          context.setCurrentObject( list );
93  
94          assertEquals( ".getTotal()", pa.getSourceAccessor( context, list, "total" ) );
95  
96          assertNull( context.get( ExpressionCompiler.PRE_CAST ) );
97          assertEquals( int.class, context.getCurrentType() );
98          assertEquals( ListSource.class, context.getCurrentAccessor() );
99      }
100 }