KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > unit > PersistenceUnitDataObjectTest


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
21 package org.netbeans.modules.j2ee.persistence.unit;
22
23 import junit.framework.*;
24 import junit.textui.TestRunner;
25 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
26
27 /**
28  * Tests for the persistence unit data object.
29  * @author Erno Mononen
30  */

31 public class PersistenceUnitDataObjectTest extends PersistenceEditorTestBase{
32     
33     public PersistenceUnitDataObjectTest(String JavaDoc testName) {
34         super(testName);
35     }
36     
37     public static Test suite() {
38         TestSuite suite = new TestSuite(PersistenceUnitDataObjectTest.class);
39         return suite;
40     }
41     
42     public void testAddPersistenceUnit() throws Exception JavaDoc{
43         PersistenceUnit persistenceUnit = new PersistenceUnit();
44         persistenceUnit.setName("em3");
45         persistenceUnit.setJtaDataSource("jdbc/__default");
46         dataObject.addPersistenceUnit(persistenceUnit);
47         
48         assertTrue(containsUnit(persistenceUnit));
49         assertTrue(dataCacheContains("\"em3\""));
50         assertTrue(dataCacheContains("<jta-data-source>jdbc/__default"));
51     }
52     
53     public void testRemovePersistenceUnit()throws Exception JavaDoc{
54         int originalSize = dataObject.getPersistence().getPersistenceUnit().length;
55         PersistenceUnit toBeRemoved = dataObject.getPersistence().getPersistenceUnit(0);
56         String JavaDoc name = toBeRemoved.getName();
57         dataObject.removePersistenceUnit(toBeRemoved);
58         assertFalse(containsUnit(toBeRemoved));
59         assertTrue(dataObject.getPersistence().getPersistenceUnit().length == originalSize -1);
60         assertFalse(dataCacheContains("name=\"" + name + "\""));
61     }
62     
63     public void testChangeName() throws Exception JavaDoc{
64         PersistenceUnit persistenceUnit = dataObject.getPersistence().getPersistenceUnit(0);
65         String JavaDoc oldName = persistenceUnit.getName();
66         String JavaDoc newName = "new name";
67         persistenceUnit.setName(newName);
68         dataObject.modelUpdatedFromUI();
69         assertTrue(dataCacheContains("\"" + newName + "\""));
70         assertFalse(dataCacheContains("\"" + oldName + "\""));
71     }
72
73     public void testChangeDatasource() throws Exception JavaDoc{
74         PersistenceUnit persistenceUnit = dataObject.getPersistence().getPersistenceUnit(0);
75         String JavaDoc newDatasource = "jdbc/new_datasource";
76         persistenceUnit.setJtaDataSource(newDatasource);
77         dataObject.modelUpdatedFromUI();
78         assertEquals(newDatasource, persistenceUnit.getJtaDataSource());
79         assertTrue(dataCacheContains(newDatasource));
80     }
81
82     public void testAddClass() throws Exception JavaDoc{
83         PersistenceUnit persistenceUnit = dataObject.getPersistence().getPersistenceUnit(0);
84         String JavaDoc clazz = "com.foo.bar.FooClass";
85         dataObject.addClass(persistenceUnit, clazz);
86         assertTrue(dataCacheContains(clazz));
87     }
88     
89     public void testRemoveClass() throws Exception JavaDoc {
90         PersistenceUnit persistenceUnit = dataObject.getPersistence().getPersistenceUnit(0);
91         String JavaDoc clazz = "com.foo.bar.FooClass";
92         dataObject.addClass(persistenceUnit, clazz);
93         assertTrue(dataCacheContains(clazz));
94         dataObject.removeClass(persistenceUnit, clazz);
95         assertFalse(dataCacheContains(clazz));
96     }
97
98     public void testAddMultipleClasses() throws Exception JavaDoc {
99         PersistenceUnit persistenceUnit = dataObject.getPersistence().getPersistenceUnit(0);
100         String JavaDoc clazz = "com.foo.bar.FooClass";
101         String JavaDoc clazz2 = "com.foo.bar.FooClass2";
102         String JavaDoc clazz3 = "com.foo.bar.FooClass3";
103         dataObject.addClass(persistenceUnit, clazz);
104         dataObject.addClass(persistenceUnit, clazz2);
105         dataObject.addClass(persistenceUnit, clazz3);
106         assertTrue(dataCacheContains(clazz));
107         assertTrue(dataCacheContains(clazz2));
108         assertTrue(dataCacheContains(clazz3));
109     }
110     
111     public static void main(String JavaDoc[] args) {
112         TestRunner.run(suite());
113     }
114
115     
116     
117 }
118
Popular Tags