1 46 47 package org.codehaus.groovy.classgen; 48 49 import java.lang.reflect.Modifier ; 50 51 import org.codehaus.groovy.ast.ClassNode; 52 import org.codehaus.groovy.ast.PropertyNode; 53 54 59 public class PropertyTest extends TestSupport { 60 61 public void testFields() throws Exception { 62 ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, "java.lang.Object"); 63 classNode.addField("x", ACC_PUBLIC, "java.lang.Object", null); 64 classNode.addField("y", ACC_PUBLIC, "java.lang.Integer", null); 65 classNode.addField("z", ACC_PRIVATE, "java.lang.String", null); 66 67 Class fooClass = loadClass(classNode); 68 assertTrue("Loaded a new class", fooClass != null); 69 70 assertField(fooClass, "x", Modifier.PUBLIC, "java.lang.Object"); 71 assertField(fooClass, "y", Modifier.PUBLIC, "java.lang.Integer"); 72 assertField(fooClass, "z", Modifier.PRIVATE, "java.lang.String"); 73 } 74 75 public void testProperties() throws Exception { 76 ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC + ACC_SUPER, "java.lang.Object"); 77 classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, "java.lang.String", "Foo", null, null, null)); 78 79 Class fooClass = loadClass(classNode); 80 assertTrue("Loaded a new class", fooClass != null); 81 82 Object bean = fooClass.newInstance(); 83 assertTrue("Managed to create bean", bean != null); 84 85 assertField(fooClass, "bar", 0, "java.lang.String"); 86 87 assertGetProperty(bean, "bar", null); 88 assertSetProperty(bean, "bar", "newValue"); 89 } 90 91 public void testInheritedProperties() throws Exception { 92 ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC + ACC_SUPER, "org.codehaus.groovy.runtime.DummyBean"); 93 classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, "java.lang.String", "Foo", null, null, null)); 94 95 Class fooClass = loadClass(classNode); 96 assertTrue("Loaded a new class", fooClass != null); 97 98 Object bean = fooClass.newInstance(); 99 assertTrue("Managed to create bean", bean != null); 100 101 assertField(fooClass, "bar", 0, "java.lang.String"); 102 103 assertGetProperty(bean, "name", "James"); 104 assertSetProperty(bean, "name", "Bob"); 105 106 assertGetProperty(bean, "bar", null); 107 assertSetProperty(bean, "bar", "newValue"); 108 } 109 } 110 | Popular Tags |