View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jexl3;
18  
19  import org.junit.Assert;
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  /**
24   * Tests public field set/get.
25   */
26  @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
27  public class PublicFieldsTest extends JexlTestCase {
28      // some constants
29      private static final String LOWER42 = "fourty-two";
30      private static final String UPPER42 = "FOURTY-TWO";
31      /**
32       * An Inner class.
33       */
34      public static class Inner {
35          public double aDouble = 42.0;
36          public static double NOT42 = -42.0;
37      }
38  
39      /**
40       * A Struct, all fields public
41       */
42      public static class Struct {
43          public Inner inner = new Inner();
44          public int anInt = 42;
45          public String aString = LOWER42;
46      }
47  
48      // a pub instance
49      private Struct pub;
50      // the JexlContext to use
51      private JexlContext ctxt;
52  
53      public PublicFieldsTest() {
54          super("PublicFieldsTest");
55      }
56  
57      @Before
58      @Override
59      public void setUp() {
60          pub = new Struct();
61          ctxt = new MapContext();
62          ctxt.set("pub", pub);
63      }
64  
65      @Test
66      public void testGetInt() throws Exception {
67          final JexlExpression get = JEXL.createExpression("pub.anInt");
68          Assert.assertEquals(42, get.evaluate(ctxt));
69          JEXL.setProperty(pub, "anInt", -42);
70          Assert.assertEquals(-42, get.evaluate(ctxt));
71      }
72  
73      @Test
74      public void testSetInt() throws Exception {
75          final JexlExpression set = JEXL.createExpression("pub.anInt = value");
76          ctxt.set("value", -42);
77          Assert.assertEquals(-42, set.evaluate(ctxt));
78          Assert.assertEquals(-42, JEXL.getProperty(pub, "anInt"));
79          ctxt.set("value", 42);
80          Assert.assertEquals(42, set.evaluate(ctxt));
81          Assert.assertEquals(42, JEXL.getProperty(pub, "anInt"));
82          try {
83              ctxt.set("value", UPPER42);
84              Assert.assertNull(set.evaluate(ctxt));
85              Assert.fail("should have thrown");
86          } catch(final JexlException xjexl) {}
87      }
88  
89      @Test
90      public void testGetString() throws Exception {
91          final JexlExpression get = JEXL.createExpression("pub.aString");
92          Assert.assertEquals(LOWER42, get.evaluate(ctxt));
93          JEXL.setProperty(pub, "aString", UPPER42);
94          Assert.assertEquals(UPPER42, get.evaluate(ctxt));
95      }
96  
97      @Test
98      public void testSetString() throws Exception {
99          final JexlExpression set = JEXL.createExpression("pub.aString = value");
100         ctxt.set("value", UPPER42);
101         Assert.assertEquals(UPPER42, set.evaluate(ctxt));
102         Assert.assertEquals(UPPER42, JEXL.getProperty(pub, "aString"));
103         ctxt.set("value", LOWER42);
104         Assert.assertEquals(LOWER42, set.evaluate(ctxt));
105         Assert.assertEquals(LOWER42, JEXL.getProperty(pub, "aString"));
106     }
107 
108     @Test
109     public void testGetInnerDouble() throws Exception {
110         final JexlExpression get = JEXL.createExpression("pub.inner.aDouble");
111         Assert.assertEquals(42.0, get.evaluate(ctxt));
112         JEXL.setProperty(pub, "inner.aDouble", -42);
113         Assert.assertEquals(-42.0, get.evaluate(ctxt));
114     }
115 
116     @Test
117     public void testSetInnerDouble() throws Exception {
118         final JexlExpression set = JEXL.createExpression("pub.inner.aDouble = value");
119         ctxt.set("value", -42.0);
120         Assert.assertEquals(-42.0, set.evaluate(ctxt));
121         Assert.assertEquals(-42.0, JEXL.getProperty(pub, "inner.aDouble"));
122         ctxt.set("value", 42.0);
123         Assert.assertEquals(42.0, set.evaluate(ctxt));
124         Assert.assertEquals(42.0, JEXL.getProperty(pub, "inner.aDouble"));
125         try {
126             ctxt.set("value", UPPER42);
127             Assert.assertNull(set.evaluate(ctxt));
128             Assert.fail("should have thrown");
129         } catch(final JexlException xjexl) {}
130     }
131 
132     public enum Gender { MALE, FEMALE }
133 
134     @Test
135     public void testGetEnum() throws Exception {
136         ctxt.set("com.jexl.gender", Gender.class);
137         final String src = "x = com.jexl.gender.FEMALE";
138         final JexlScript script = JEXL.createScript(src);
139         final Object result = script.execute(ctxt);
140         Assert.assertEquals(Gender.FEMALE, result);
141         Assert.assertEquals(Gender.FEMALE, ctxt.get("x"));
142     }
143 
144     @Test
145     public void testGetStaticField() throws Exception {
146         ctxt.set("com.jexl", Inner.class);
147         final String src = "x = com.jexl.NOT42";
148         final JexlScript script = JEXL.createScript(src);
149         final Object result = script.execute(ctxt);
150         Assert.assertEquals(Inner.NOT42, result);
151         Assert.assertEquals(Inner.NOT42, ctxt.get("x"));
152     }
153 }