1   /*
2    $Id: PropertyTest.java,v 1.18 2005/04/04 23:02:10 blackdrag 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.Closure;
50  import groovy.lang.MissingMethodException;
51  import groovy.util.GroovyTestCase;
52  import groovy.util.Node;
53  
54  import java.awt.HeadlessException;
55  import java.awt.Point;
56  import java.util.ArrayList;
57  import java.util.HashMap;
58  import java.util.List;
59  import java.util.Map;
60  
61  import javax.swing.JButton;
62  import javax.swing.JFrame;
63  import javax.swing.JPanel;
64  
65  /***
66   * Test the property access of the Invoker class
67   * 
68   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
69   * @version $Revision: 1.18 $
70   */
71  public class PropertyTest extends GroovyTestCase {
72  
73      protected Invoker invoker = new Invoker();
74  
75      public void testMapProperties() throws Exception {
76          Map map = new HashMap();
77          map.put("foo", "abc");
78          map.put("bar", new Integer(123));
79  
80          assertGetSetProperty(map, "foo", "abc", "def");
81          assertGetSetProperty(map, "bar", new Integer(123), new Double(12.34));
82      }
83  
84      public void testBeanProperties() throws Exception {
85          DummyBean bean = new DummyBean();
86  
87          assertGetSetProperty(bean, "name", "James", "Bob");
88          assertGetSetProperty(bean, "i", new Integer(123), new Integer(455));
89  
90          // dynamic properties
91          assertGetSetProperty(bean, "dynamicFoo", null, "aValue");
92          assertGetSetProperty(bean, "dynamicFoo", "aValue", "NewValue");
93      }
94  
95  /*** todo this is no longer possible in new groovy
96      public void testUsingMethodProperty() throws Exception {
97          DummyBean bean = new DummyBean();
98  
99          assertGetSetProperty(bean, "name", "James", "Bob");
100 
101         Object value = InvokerHelper.getProperty(bean, "getName");
102         assertTrue("Should have returned a closure: " + value, value instanceof Closure);
103         Closure closure = (Closure) value;
104         Object result = closure.call(null);
105         assertEquals("Result of call to closure", "Bob", result);
106     }
107 **/   
108     
109 
110     public void testStaticProperty() throws Exception {
111         Object value = InvokerHelper.getProperty(System.class, "out");
112         assertEquals("static property out", System.out, value);
113     }
114 
115     public void testClassProperty() throws Exception {
116         Class c = String.class;
117         Object value = InvokerHelper.getProperty(c, "name");
118         assertEquals("class name property", c.getName(), value);
119     }
120 
121     public void testMapEntryProperty() throws Exception {
122         HashMap map = new HashMap();
123         map.put("a", "x");
124         Object[] array = map.entrySet().toArray();
125         Object entry = array[0];
126 
127         Object key = InvokerHelper.getProperty(entry, "key");
128         assertEquals("key property", "a", key);
129 
130         Object value = InvokerHelper.getProperty(entry, "value");
131         assertEquals("value property", "x", value);
132     }
133 
134 /*** todo this is no longer possible in new groovy
135     public void testMethodProperty() throws Exception {
136         Object value = InvokerHelper.getProperty(this, "getCheese");
137         assertTrue("Should have returned a closure: " + value, value instanceof Closure);
138 
139         Object result = ((Closure) value).call();
140         assertEquals("result of closure call", getCheese(), result);
141 
142         System.out.println("Closure: " + value + " and cheese: " + result);
143     }
144 **/
145 
146     public void testListCoercionProperty() throws Exception {
147         DummyBean bean = new DummyBean();
148         List list = new ArrayList();
149         list.add(new Integer(10));
150         list.add(new Integer(20));
151 
152         InvokerHelper.setProperty(bean, "point", list);
153         assertEquals("Should have set a point", new Point(10, 20), bean.getPoint());
154     }
155 
156     public void testListCoercionPropertyOnJFrame() throws Exception {
157         try {
158 	        JFrame bean = new JFrame();
159 	        List list = new ArrayList();
160 	        list.add(new Integer(10));
161 	        list.add(new Integer(20));
162 	
163 	        InvokerHelper.setProperty(bean, "location", list);
164 	        assertEquals("Should have set a point", new Point(10, 20), bean.getLocation());
165         }
166         catch (HeadlessException e) {
167             // its fine to not run this test on headless environments
168         }
169         catch (MissingMethodException e) {
170             System.out.println("Failed with cause: " + e);
171             e.printStackTrace();
172             fail("Should not have throw: " + e);
173         }
174     }
175 
176     public void testListNavigationProperty() throws Exception {
177         List list = new ArrayList();
178         list.add(new DummyBean("James"));
179         list.add(new DummyBean("Bob"));
180 
181         List value = (List) InvokerHelper.getProperty(list, "name");
182         assertArrayEquals(new Object[] { "James", "Bob" }, value.toArray());
183     }
184 
185     public void testListOfListNavigationProperty() throws Exception {
186        List list = new ArrayList();
187        list.add(new DummyBean("James"));
188        list.add(new DummyBean("Bob"));
189 
190        List listOfList = new ArrayList();
191        listOfList.add(list);
192        
193        List value = (List) InvokerHelper.getProperty(listOfList, "name");
194        assertArrayEquals(new Object[] { "James", "Bob" }, value.toArray());
195    }
196 
197     public void testNodeNavigationProperty() throws Exception {
198         Node z = new Node(null, "z");
199         Node y = new Node(null, "y");
200 
201         List children = new ArrayList();
202         children.add(y);
203         children.add(z);
204 
205         Node x = new Node(null, "x", children);
206 
207         children = new ArrayList();
208         children.add(x);
209         Node b = new Node(null, "b", children);
210 
211         // @todo should try with just a node as the child
212 
213         List value = (List) InvokerHelper.getProperty(b, "x");
214         assertArrayEquals(new Object[] { x }, value.toArray());
215 
216         value = (List) InvokerHelper.getProperty(value, "z");
217         assertArrayEquals(new Object[] { z }, value.toArray());
218     }
219 
220     public void testUsingInPropertyOnProcessViaGroovyMethod() throws Exception {
221         Process process = DefaultGroovyMethods.execute("java -version");
222         Object value = InvokerHelper.getProperty(process, "in");
223         assertNotNull(value);
224         
225         System.out.println("Found in: " + value);
226         
227         process.destroy();
228     }
229     
230     public Object getCheese() {
231         return "cheddar";
232     }
233 
234     public void testComponentParent() {
235         JPanel panel = new JPanel();
236         JButton bean = new JButton();
237         
238         panel.add(bean);
239         
240         Object value = InvokerHelper.getProperty(bean, "parent");
241         assertTrue(value != null);
242     }
243     
244     // Implementation methods
245     //-------------------------------------------------------------------------
246 
247     protected void assertGetSetProperty(Object object, String property, Object currentValue, Object newValue) {
248         assertGetProperty(object, property, currentValue);
249 
250         InvokerHelper.setProperty(object, property, newValue);
251 
252         assertGetProperty(object, property, newValue);
253     }
254 
255     protected void assertGetProperty(Object object, String property, Object expected) {
256         Object value = InvokerHelper.getProperty(object, property);
257 
258         assertEquals("property: " + property + " of: " + object, expected, value);
259     }
260 }