1   // 
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //	 this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //	 this list of conditions and the following disclaimer in the documentation  
12  //	 and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //	 nor the names of its contributors may be used to endorse or promote products  
15  //	 derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  // 
28  package org.objectledge.selector;
29  
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  import junit.framework.TestCase;
34  
35  /***
36   * 
37   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
38   * @version $Id: IntrospectionVariablesTest.java,v 1.4 2005/02/10 17:47:00 rafal Exp $
39   */
40  public class IntrospectionVariablesTest extends TestCase
41  {
42      /***
43       * Constructor for IntrospectionVariablesTest.
44       * @param arg0
45       */
46      public IntrospectionVariablesTest(String arg0)
47      {
48          super(arg0);
49      }
50      
51      public void testIsDefined()
52          throws Exception
53      {
54          Variables variables;
55          
56          variables = new IntrospectionVariables(new PlainObject());
57          assertTrue(variables.isDefined("publicMethod"));
58          assertTrue(variables.isDefined("publicBooleanMethod"));
59          assertTrue(variables.isDefined("publicBooleanPrimitiveMethod"));
60          assertFalse(variables.isDefined("undefined"));
61          assertFalse(variables.isDefined("voidPublicMethod"));
62          assertFalse(variables.isDefined("nonBooleanIsMethod"));
63          
64          variables = new IntrospectionVariables(new StringGetObject());
65          assertTrue(variables.isDefined("defined"));
66          // TODO caveat!
67          // assertFalse(variables.isDefined("undefined"));
68          
69          Map map = new HashMap();
70          map.put("defined", "defined");
71          map.put("null", null);
72          variables = new IntrospectionVariables(map);
73          assertTrue(variables.isDefined("defined"));
74          // TODO caveat!
75          // assertFalse(variables.isDefined("null"));
76          // assertFalse(variables.isDefined("undefined"));
77      }
78      
79      public void testNesting()
80          throws Exception
81      {
82          Variables variables;
83          variables = new IntrospectionVariables(new PlainObject());
84          assertTrue(variables.isDefined("nested.publicMethod"));
85          assertTrue(variables.isDefined("nested.publicBooleanMethod"));
86          assertTrue(variables.isDefined("nested.publicBooleanPrimitiveMethod"));
87          assertFalse(variables.isDefined("nested.undefined"));
88          assertFalse(variables.isDefined("nested.voidPublicMethod"));
89          assertFalse(variables.isDefined("nested.nonBooleanIsMethod"));
90      }
91      
92      public void testGet()
93          throws Exception
94      {
95          Variables variables;
96          variables = new IntrospectionVariables(new PlainObject());
97          assertEquals("publicMethod", variables.get("nested.publicMethod"));
98          assertEquals(Boolean.TRUE, variables.get("publicBooleanMethod"));
99          assertEquals(Boolean.TRUE, variables.get("publicBooleanPrimitiveMethod"));
100         
101         assertEquals("publicMethod", variables.get("nested.publicMethod"));
102         assertEquals(Boolean.TRUE, variables.get("nested.publicBooleanMethod"));
103         assertEquals(Boolean.TRUE, variables.get("nested.publicBooleanPrimitiveMethod"));
104         
105         try
106         {
107             variables.get("undefined");
108             fail("exception expected");
109         }
110         catch(Exception e)
111         {
112             assertEquals("Undefined variable undefined", e.getMessage());
113         }
114 
115         try
116         {
117             variables.get("nested.undefined");
118             fail("exception expected");
119         }
120         catch(Exception e)
121         {
122             assertEquals("Undefined variable nested.undefined", e.getMessage());
123         }
124     }
125     
126     public void testExceptions()
127     {
128         Variables variables;
129         variables = new IntrospectionVariables(new PlainObject());
130         try
131         {
132             variables.get("failing");
133             fail("exception expected");
134         }
135         catch(Exception e)
136         {
137             assertEquals(EvaluationException.class, e.getClass());
138             assertEquals(IllegalStateException.class, e.getCause().getClass());
139         }
140         variables = new IntrospectionVariables(new StringGetObject());
141         try
142         {
143             variables.get("failing");
144             fail("exception expected");
145         }
146         catch(Exception e)
147         {
148             assertEquals(EvaluationException.class, e.getClass());
149             assertEquals(IllegalStateException.class, e.getCause().getClass());
150         }
151 
152         variables = new IntrospectionVariables(new FailingObjectGetObject());
153         try
154         {
155             variables.get("failing");
156             fail("exception expected");
157         }
158         catch(Exception e)
159         {
160             assertEquals(EvaluationException.class, e.getClass());
161             assertEquals(IllegalStateException.class, e.getCause().getClass());
162         }
163     }
164     
165     private static class PlainObject
166     {
167         public String getPublicMethod()
168         {
169             return "publicMethod";
170         }
171         
172         public void getVoidPublicMethod()
173         {
174             // does nothing
175         }
176         
177         public String isNonBooleanIsMethod()
178         {
179             return null;
180         }
181         
182         public boolean isPublicBooleanPrimitiveMethod()
183         {
184             return true;
185         }
186         
187         public Boolean isPublicBooleanMethod()
188         {
189             return Boolean.TRUE;
190         }
191         
192         public PlainObject getNested()
193         {
194             return this;
195         }
196         
197         public Object getFailing()
198         {
199             throw new IllegalStateException("failed");
200         }
201     }
202     
203     private static class StringGetObject
204     {
205         public String get(String name)
206         {
207             if(name.equals("defined"))
208             {
209                 return "defined";
210             }
211             else if(name.equals("failing"))
212             {
213                 throw new IllegalStateException("failed");
214             }
215             else
216             {
217                 return null;
218             }
219         }
220     }
221     
222     private static class FailingObjectGetObject
223     {
224         public Object get(Object param)
225         {
226             throw new IllegalStateException("failed");
227         }
228     }
229 }