1   /*
2    $Id: PropertyTest.java,v 1.19 2005/06/10 13:42:22 cstein Exp $
3   
4    Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5   
6    Redistribution and use of this software and associated documentation
7    ("Software"), with or without modification, are permitted provided
8    that the following conditions are met:
9   
10   1. Redistributions of source code must retain copyright
11      statements and notices.  Redistributions must also contain a
12      copy of this document.
13  
14   2. Redistributions in binary form must reproduce the
15      above copyright notice, this list of conditions and the
16      following disclaimer in the documentation and/or other
17      materials provided with the distribution.
18  
19   3. The name "groovy" must not be used to endorse or promote
20      products derived from this Software without prior written
21      permission of The Codehaus.  For written permission,
22      please contact info@codehaus.org.
23  
24   4. Products derived from this Software may not be called "groovy"
25      nor may "groovy" appear in their names without prior written
26      permission of The Codehaus. "groovy" is a registered
27      trademark of The Codehaus.
28  
29   5. Due credit should be given to The Codehaus -
30      http://groovy.codehaus.org/
31  
32   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
36   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43   OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45   */
46  
47  package org.codehaus.groovy.runtime;
48  
49  import groovy.lang.MissingMethodException;
50  import groovy.util.GroovyTestCase;
51  import groovy.util.Node;
52  
53  import java.awt.HeadlessException;
54  import java.awt.Point;
55  import java.util.ArrayList;
56  import java.util.HashMap;
57  import java.util.List;
58  import java.util.Map;
59  
60  import javax.swing.JButton;
61  import javax.swing.JFrame;
62  import javax.swing.JPanel;
63  
64  /***
65   * Test the property access of the Invoker class
66   * 
67   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
68   * @version $Revision: 1.19 $
69   */
70  public class PropertyTest extends GroovyTestCase {
71  
72      protected Invoker invoker = new Invoker();
73  
74      public void testMapProperties() throws Exception {
75          Map map = new HashMap();
76          map.put("foo", "abc");
77          map.put("bar", new Integer(123));
78  
79          assertGetSetProperty(map, "foo", "abc", "def");
80          assertGetSetProperty(map, "bar", new Integer(123), new Double(12.34));
81      }
82  
83      public void testBeanProperties() throws Exception {
84          DummyBean bean = new DummyBean();
85  
86          assertGetSetProperty(bean, "name", "James", "Bob");
87          assertGetSetProperty(bean, "i", new Integer(123), new Integer(455));
88  
89          // dynamic properties
90          assertGetSetProperty(bean, "dynamicFoo", null, "aValue");
91          assertGetSetProperty(bean, "dynamicFoo", "aValue", "NewValue");
92      }
93  
94  /*** todo this is no longer possible in new groovy
95      public void testUsingMethodProperty() throws Exception {
96          DummyBean bean = new DummyBean();
97  
98          assertGetSetProperty(bean, "name", "James", "Bob");
99  
100         Object value = InvokerHelper.getProperty(bean, "getName");
101         assertTrue("Should have returned a closure: " + value, value instanceof Closure);
102         Closure closure = (Closure) value;
103         Object result = closure.call(null);
104         assertEquals("Result of call to closure", "Bob", result);
105     }
106 **/   
107     
108 
109     public void testStaticProperty() throws Exception {
110         Object value = InvokerHelper.getProperty(System.class, "out");
111         assertEquals("static property out", System.out, value);
112     }
113 
114     public void testClassProperty() throws Exception {
115         Class c = String.class;
116         Object value = InvokerHelper.getProperty(c, "name");
117         assertEquals("class name property", c.getName(), value);
118     }
119 
120     public void testMapEntryProperty() throws Exception {
121         HashMap map = new HashMap();
122         map.put("a", "x");
123         Object[] array = map.entrySet().toArray();
124         Object entry = array[0];
125 
126         Object key = InvokerHelper.getProperty(entry, "key");
127         assertEquals("key property", "a", key);
128 
129         Object value = InvokerHelper.getProperty(entry, "value");
130         assertEquals("value property", "x", value);
131     }
132 
133 /*** todo this is no longer possible in new groovy
134     public void testMethodProperty() throws Exception {
135         Object value = InvokerHelper.getProperty(this, "getCheese");
136         assertTrue("Should have returned a closure: " + value, value instanceof Closure);
137 
138         Object result = ((Closure) value).call();
139         assertEquals("result of closure call", getCheese(), result);
140 
141         System.out.println("Closure: " + value + " and cheese: " + result);
142     }
143 **/
144 
145     public void testListCoercionProperty() throws Exception {
146         DummyBean bean = new DummyBean();
147         List list = new ArrayList();
148         list.add(new Integer(10));
149         list.add(new Integer(20));
150 
151         InvokerHelper.setProperty(bean, "point", list);
152         assertEquals("Should have set a point", new Point(10, 20), bean.getPoint());
153     }
154 
155     public void testListCoercionPropertyOnJFrame() throws Exception {
156         try {
157 	        JFrame bean = new JFrame();
158 	        List list = new ArrayList();
159 	        list.add(new Integer(10));
160 	        list.add(new Integer(20));
161 	
162 	        InvokerHelper.setProperty(bean, "location", list);
163 	        assertEquals("Should have set a point", new Point(10, 20), bean.getLocation());
164         }
165         catch (HeadlessException e) {
166             // its fine to not run this test on headless environments
167         }
168         catch (MissingMethodException e) {
169             System.out.println("Failed with cause: " + e);
170             e.printStackTrace();
171             fail("Should not have throw: " + e);
172         }
173     }
174 
175     public void testListNavigationProperty() throws Exception {
176         List list = new ArrayList();
177         list.add(new DummyBean("James"));
178         list.add(new DummyBean("Bob"));
179 
180         List value = (List) InvokerHelper.getProperty(list, "name");
181         assertArrayEquals(new Object[] { "James", "Bob" }, value.toArray());
182     }
183 
184     public void testListOfListNavigationProperty() throws Exception {
185        List list = new ArrayList();
186        list.add(new DummyBean("James"));
187        list.add(new DummyBean("Bob"));
188 
189        List listOfList = new ArrayList();
190        listOfList.add(list);
191        
192        List value = (List) InvokerHelper.getProperty(listOfList, "name");
193        assertArrayEquals(new Object[] { "James", "Bob" }, value.toArray());
194    }
195 
196     public void testNodeNavigationProperty() throws Exception {
197         Node z = new Node(null, "z");
198         Node y = new Node(null, "y");
199 
200         List children = new ArrayList();
201         children.add(y);
202         children.add(z);
203 
204         Node x = new Node(null, "x", children);
205 
206         children = new ArrayList();
207         children.add(x);
208         Node b = new Node(null, "b", children);
209 
210         // @todo should try with just a node as the child
211 
212         List value = (List) InvokerHelper.getProperty(b, "x");
213         assertArrayEquals(new Object[] { x }, value.toArray());
214 
215         value = (List) InvokerHelper.getProperty(value, "z");
216         assertArrayEquals(new Object[] { z }, value.toArray());
217     }
218 
219     public void testUsingInPropertyOnProcessViaGroovyMethod() throws Exception {
220         Process process = DefaultGroovyMethods.execute("java -version");
221         Object value = InvokerHelper.getProperty(process, "in");
222         assertNotNull(value);
223         
224         System.out.println("Found in: " + value);
225         
226         process.destroy();
227     }
228     
229     public Object getCheese() {
230         return "cheddar";
231     }
232 
233     public void testComponentParent() {
234         JPanel panel = new JPanel();
235         JButton bean = new JButton();
236         
237         panel.add(bean);
238         
239         Object value = InvokerHelper.getProperty(bean, "parent");
240         assertTrue(value != null);
241     }
242     
243     // Implementation methods
244     //-------------------------------------------------------------------------
245 
246     protected void assertGetSetProperty(Object object, String property, Object currentValue, Object newValue) {
247         assertGetProperty(object, property, currentValue);
248 
249         InvokerHelper.setProperty(object, property, newValue);
250 
251         assertGetProperty(object, property, newValue);
252     }
253 
254     protected void assertGetProperty(Object object, String property, Object expected) {
255         Object value = InvokerHelper.getProperty(object, property);
256 
257         assertEquals("property: " + property + " of: " + object, expected, value);
258     }
259 }