KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > entitygenerator > DbSchemaEjbGeneratorTest


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.entitygenerator;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26 import org.netbeans.junit.NbTestCase;
27 import org.netbeans.modules.dbschema.SchemaElement;
28 import org.netbeans.modules.dbschema.SchemaElementUtil;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.URLMapper;
31
32 /**
33  *
34  * @author Andrei Badea
35  */

36 public class DbSchemaEjbGeneratorTest extends NbTestCase {
37
38     public DbSchemaEjbGeneratorTest(String JavaDoc testName) {
39         super(testName);
40     }
41
42     public void testIssue92031() throws Exception JavaDoc {
43         /*
44         create table part1 (id int not null primary key);
45         create table part2 (part1_id int not null references part1(id) primary key);
46          */

47         SchemaElement schema = SchemaElementUtil.forName(URLMapper.findFileObject(getClass().getResource("Issue92031.dbschema")));
48         DbSchemaEjbGenerator generator = new DbSchemaEjbGenerator(
49                 new GeneratedTablesImpl(new HashSet JavaDoc(Arrays.asList("PART1", "PART2"))),
50                 schema);
51
52         EntityClass[] beans = generator.getBeans();
53         EntityClass bean = getBeanByTableName(beans, "PART2");
54         assertNotNull(getFieldByName(bean, "part1"));
55         RelationshipRole role = (RelationshipRole)bean.getRoles().iterator().next();
56         assertEquals("part11", role.getFieldName());
57         assertNotNull("Should have CMR mapping for field part11", bean.getCMPMapping().getCmrFieldMapping().get("part11"));
58     }
59
60     private static EntityMember getFieldByName(EntityClass bean, String JavaDoc fieldName) {
61         for (Iterator JavaDoc i = bean.getFields().iterator(); i.hasNext();) {
62             EntityMember member = ((EntityMember)i.next());
63             if (fieldName.equals(member.getMemberName())) {
64                 return member;
65             }
66         }
67         return null;
68     }
69
70     private static EntityClass getBeanByTableName(EntityClass[] beans, String JavaDoc tableName) {
71         for (int i = 0; i < beans.length; i++) {
72             if (tableName.equals(beans[i].getTableName())) {
73                 return beans[i];
74             }
75         }
76         return null;
77     }
78
79     private static final class GeneratedTablesImpl implements GeneratedTables {
80
81         private final Set JavaDoc<String JavaDoc> tableNames;
82
83         public GeneratedTablesImpl(Set JavaDoc<String JavaDoc> tableNames) {
84             this.tableNames = tableNames;
85         }
86
87         public Set JavaDoc<String JavaDoc> getTableNames() {
88             return tableNames;
89         }
90
91         public FileObject getRootFolder(String JavaDoc tableName) {
92             return null;
93         }
94
95         public String JavaDoc getPackageName(String JavaDoc tableName) {
96             return null;
97         }
98
99         public String JavaDoc getClassName(String JavaDoc tableName) {
100             return tableName;
101         }
102     }
103 }
104
Popular Tags