KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > test > EntityAssociationResolverTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.refactoring.test;
21
22 import java.io.File JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
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 JavaDoc;
46 import org.netbeans.modules.j2ee.refactoring.EntityAssociationResolver;
47
48 /**
49  * Tests for <code>EntityAssociationResolver</code>.
50  * @author Erno Mononen
51  */

52 public class EntityAssociationResolverTest extends NbTestCase {
53     
54     private static final String JavaDoc CUSTOMER_ENTITY_NAME = "Customer.java";
55     private static final String JavaDoc ORDER_ENTITY_NAME = "Order.java";
56     private static final String JavaDoc DEPARTMENT_ENTITY_NAME = "Department.java";
57     private static final String JavaDoc EMPLOYEE_ENTITY_NAME = "Employee.java";
58     private static final String JavaDoc USER_ENTITY_NAME = "User.java";
59     private static final String JavaDoc 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 JavaDoc<JavaClass> allEntities;
69     
70     public EntityAssociationResolverTest(String JavaDoc 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 JavaDoc {
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 JavaDoc {
92         super.tearDown();
93         JMIUtils.endJmiTransaction();
94     }
95     
96     private JavaClass getJavaClass(String JavaDoc name){
97         String JavaDoc path = getDataDir().getAbsoluteFile() + "/entities/" + name;
98         FileObject fo = FileUtil.toFileObject(new File JavaDoc(path));
99         return (JavaClass) JavaModel.getResource(fo).getClassifiers().get(0);
100     }
101     
102     /**
103      * Tests that 'mappedBy' references are found when using property based access.
104      */

105     public void testGetMappedByReferencesPropertyAccess(){
106
107         EntityAssociationResolver resolver = new EntityAssociationResolver(order, allEntities);
108         List JavaDoc<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     /**
122      * Tests that 'mappedBy' references are found when using field based access.
123      */

124     public void testGetMappedReferencesFieldAccess(){
125
126         EntityAssociationResolver resolver = new EntityAssociationResolver(user, allEntities);
127         List JavaDoc<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 JavaDoc<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 JavaDoc<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