001: /*
002: $Id: PropertyTest.java 2272 2005-06-10 13:42:22Z cstein $
003:
004: Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046:
047: package org.codehaus.groovy.runtime;
048:
049: import groovy.lang.MissingMethodException;
050: import groovy.util.GroovyTestCase;
051: import groovy.util.Node;
052:
053: import java.awt.HeadlessException;
054: import java.awt.Point;
055: import java.util.ArrayList;
056: import java.util.HashMap;
057: import java.util.List;
058: import java.util.Map;
059:
060: import javax.swing.JButton;
061: import javax.swing.JFrame;
062: import javax.swing.JPanel;
063:
064: /**
065: * Test the property access of the Invoker class
066: *
067: * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
068: * @version $Revision: 2272 $
069: */
070: public class PropertyTest extends GroovyTestCase {
071:
072: protected Invoker invoker = new Invoker();
073:
074: public void testMapProperties() throws Exception {
075: Map map = new HashMap();
076: map.put("foo", "abc");
077: map.put("bar", new Integer(123));
078:
079: assertGetSetProperty(map, "foo", "abc", "def");
080: assertGetSetProperty(map, "bar", new Integer(123), new Double(
081: 12.34));
082: }
083:
084: public void testBeanProperties() throws Exception {
085: DummyBean bean = new DummyBean();
086:
087: assertGetSetProperty(bean, "name", "James", "Bob");
088: assertGetSetProperty(bean, "i", new Integer(123), new Integer(
089: 455));
090:
091: // dynamic properties
092: assertGetSetProperty(bean, "dynamicFoo", null, "aValue");
093: assertGetSetProperty(bean, "dynamicFoo", "aValue", "NewValue");
094: }
095:
096: /** todo this is no longer possible in new groovy
097: public void testUsingMethodProperty() throws Exception {
098: DummyBean bean = new DummyBean();
099:
100: assertGetSetProperty(bean, "name", "James", "Bob");
101:
102: Object value = InvokerHelper.getProperty(bean, "getName");
103: assertTrue("Should have returned a closure: " + value, value instanceof Closure);
104: Closure closure = (Closure) value;
105: Object result = closure.call(null);
106: assertEquals("Result of call to closure", "Bob", result);
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
154: .getPoint());
155: }
156:
157: public void testListCoercionPropertyOnJFrame() throws Exception {
158: try {
159: JFrame bean = new JFrame();
160: List list = new ArrayList();
161: list.add(new Integer(10));
162: list.add(new Integer(20));
163:
164: InvokerHelper.setProperty(bean, "location", list);
165: assertEquals("Should have set a point", new Point(10, 20),
166: bean.getLocation());
167: } catch (HeadlessException e) {
168: // its fine to not run this test on headless environments
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
183: .toArray());
184: }
185:
186: public void testListOfListNavigationProperty() throws Exception {
187: List list = new ArrayList();
188: list.add(new DummyBean("James"));
189: list.add(new DummyBean("Bob"));
190:
191: List listOfList = new ArrayList();
192: listOfList.add(list);
193:
194: List value = (List) InvokerHelper.getProperty(listOfList,
195: "name");
196: assertArrayEquals(new Object[] { "James", "Bob" }, value
197: .toArray());
198: }
199:
200: public void testNodeNavigationProperty() throws Exception {
201: Node z = new Node(null, "z");
202: Node y = new Node(null, "y");
203:
204: List children = new ArrayList();
205: children.add(y);
206: children.add(z);
207:
208: Node x = new Node(null, "x", children);
209:
210: children = new ArrayList();
211: children.add(x);
212: Node b = new Node(null, "b", children);
213:
214: // @todo should try with just a node as the child
215:
216: List value = (List) InvokerHelper.getProperty(b, "x");
217: assertArrayEquals(new Object[] { x }, value.toArray());
218:
219: value = (List) InvokerHelper.getProperty(value, "z");
220: assertArrayEquals(new Object[] { z }, value.toArray());
221: }
222:
223: public void testUsingInPropertyOnProcessViaGroovyMethod()
224: throws Exception {
225: Process process = DefaultGroovyMethods.execute("java -version");
226: Object value = InvokerHelper.getProperty(process, "in");
227: assertNotNull(value);
228:
229: System.out.println("Found in: " + value);
230:
231: process.destroy();
232: }
233:
234: public Object getCheese() {
235: return "cheddar";
236: }
237:
238: public void testComponentParent() {
239: JPanel panel = new JPanel();
240: JButton bean = new JButton();
241:
242: panel.add(bean);
243:
244: Object value = InvokerHelper.getProperty(bean, "parent");
245: assertTrue(value != null);
246: }
247:
248: // Implementation methods
249: //-------------------------------------------------------------------------
250:
251: protected void assertGetSetProperty(Object object, String property,
252: Object currentValue, Object newValue) {
253: assertGetProperty(object, property, currentValue);
254:
255: InvokerHelper.setProperty(object, property, newValue);
256:
257: assertGetProperty(object, property, newValue);
258: }
259:
260: protected void assertGetProperty(Object object, String property,
261: Object expected) {
262: Object value = InvokerHelper.getProperty(object, property);
263:
264: assertEquals("property: " + property + " of: " + object,
265: expected, value);
266: }
267: }
|