1 19 20 package org.netbeans.modules.j2ee.refactoring.test; 21 22 import java.io.File ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import junit.framework.*; 28 import org.netbeans.jmi.javamodel.Feature; 29 import org.netbeans.jmi.javamodel.JavaClass; 30 import org.netbeans.jmi.javamodel.Method; 31 import org.netbeans.jmi.javamodel.StringLiteral; 32 import org.netbeans.junit.NbTestCase; 33 import org.netbeans.modules.j2ee.common.JMIUtils; 34 import org.netbeans.modules.j2ee.refactoring.*; 35 import org.netbeans.modules.javacore.api.JavaModel; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileSystem; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.filesystems.MultiFileSystem; 40 import org.openide.filesystems.Repository; 41 import org.openide.filesystems.XMLFileSystem; 42 import org.openide.util.Lookup; 43 import org.openide.util.lookup.Lookups; 44 import org.openide.util.lookup.ProxyLookup; 45 import org.xml.sax.SAXException ; 46 import org.netbeans.modules.j2ee.refactoring.EntityAssociationResolver; 47 48 52 public class EntityAssociationResolverTest extends NbTestCase { 53 54 private static final String CUSTOMER_ENTITY_NAME = "Customer.java"; 55 private static final String ORDER_ENTITY_NAME = "Order.java"; 56 private static final String DEPARTMENT_ENTITY_NAME = "Department.java"; 57 private static final String EMPLOYEE_ENTITY_NAME = "Employee.java"; 58 private static final String USER_ENTITY_NAME = "User.java"; 59 private static final String GROUP_ENTITY_NAME = "Group.java"; 60 61 private JavaClass customer; 62 private JavaClass order; 63 private JavaClass department; 64 private JavaClass employee; 65 private JavaClass user; 66 private JavaClass group; 67 68 private List <JavaClass> allEntities; 69 70 public EntityAssociationResolverTest(String name){ 71 super(name); 72 } 73 74 public static Test suite() { 75 TestSuite suite = new TestSuite(EntityAssociationResolverTest.class); 76 return suite; 77 } 78 79 protected void setUp() throws Exception { 80 super.setUp(); 81 JMIUtils.beginJmiTransaction(true); 82 this.customer = getJavaClass(CUSTOMER_ENTITY_NAME); 83 this.order = getJavaClass(ORDER_ENTITY_NAME); 84 this.employee = getJavaClass(EMPLOYEE_ENTITY_NAME); 85 this.department = getJavaClass(DEPARTMENT_ENTITY_NAME); 86 this.user = getJavaClass(USER_ENTITY_NAME); 87 this.group = getJavaClass(GROUP_ENTITY_NAME); 88 this.allEntities = Arrays.asList(new JavaClass[]{customer, order, employee, department, user, group}); 89 } 90 91 protected void tearDown()throws Exception { 92 super.tearDown(); 93 JMIUtils.endJmiTransaction(); 94 } 95 96 private JavaClass getJavaClass(String name){ 97 String path = getDataDir().getAbsoluteFile() + "/entities/" + name; 98 FileObject fo = FileUtil.toFileObject(new File (path)); 99 return (JavaClass) JavaModel.getResource(fo).getClassifiers().get(0); 100 } 101 102 105 public void testGetMappedByReferencesPropertyAccess(){ 106 107 EntityAssociationResolver resolver = new EntityAssociationResolver(order, allEntities); 108 List <EntityAnnotationReference> refs = resolver.getMappedByReferences(order.getMethod("getCustomer", Collections.EMPTY_LIST, false)); 109 110 assertEquals(1, refs.size()); 111 112 EntityAnnotationReference association = refs.get(0); 113 114 assertEquals("OneToMany", association.getAnnotation().getTypeName().getName()); 115 assertEquals("Customer", association.getReferring().getName()); 116 assertEquals("mappedBy", association.getAttributeValue().getName()); 117 StringLiteral literal = (StringLiteral) association.getAttributeValue().getValue(); 118 assertEquals("customer", literal.getValue()); 119 } 120 121 124 public void testGetMappedReferencesFieldAccess(){ 125 126 EntityAssociationResolver resolver = new EntityAssociationResolver(user, allEntities); 127 List <EntityAnnotationReference> refs = resolver.getMappedByReferences(user.getField("groups", false)); 128 129 assertEquals(1, refs.size()); 130 131 EntityAnnotationReference association = refs.get(0); 132 133 assertEquals("ManyToMany", association.getAnnotation().getTypeName().getName()); 134 assertEquals("Group", association.getReferring().getName()); 135 assertEquals("mappedBy", association.getAttributeValue().getName()); 136 StringLiteral literal = (StringLiteral) association.getAttributeValue().getValue(); 137 assertEquals("groups", literal.getValue()); 138 } 139 140 public void testGetMappedByReferencesNotFound(){ 141 EntityAssociationResolver resolver = new EntityAssociationResolver(order, allEntities); 142 List <EntityAnnotationReference> refs = resolver.getMappedByReferences(order.getMethod("getId", Collections.EMPTY_LIST, false)); 143 assertTrue(refs.isEmpty()); 144 } 145 146 public void testChangeAttributeValue(){ 147 EntityAssociationResolver resolver = new EntityAssociationResolver(order, allEntities); 148 List <EntityAnnotationReference> refs = resolver.getMappedByReferences(order.getMethod("getCustomer", Collections.EMPTY_LIST, false)); 149 150 assertEquals(1, refs.size()); 151 152 EntityAnnotationReference association = refs.get(0); 153 StringLiteral literal = (StringLiteral) association.getAttributeValue().getValue(); 154 assertEquals("customer", literal.getValue()); 155 156 literal.setValue("newValue"); 157 assertEquals("newValue", literal.getValue()); 158 } 159 160 } 161 | Popular Tags |