KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > provider > ProviderUtilTest


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.provider;
22
23 import java.io.File JavaDoc;
24 import java.net.URL JavaDoc;
25 import junit.framework.*;
26 import org.netbeans.api.db.explorer.DatabaseConnection;
27 import org.netbeans.api.db.explorer.JDBCDriver;
28 import org.netbeans.junit.NbTestCase;
29 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
30 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Property;
31 import org.netbeans.modules.j2ee.persistence.unit.PUDataObject;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34
35 /**
36  * Tests for ProviderUtil.
37  * @author Erno Mononen
38  */

39 public class ProviderUtilTest extends NbTestCase {
40     
41     private PersistenceUnit persistenceUnit;
42     
43     public ProviderUtilTest(String JavaDoc testName) {
44         super(testName);
45     }
46     
47     protected void setUp() throws Exception JavaDoc {
48         this.persistenceUnit = new PersistenceUnit();
49     }
50     
51     protected void tearDown() throws Exception JavaDoc {
52     }
53     
54     public static Test suite() {
55         TestSuite suite = new TestSuite(ProviderUtilTest.class);
56         return suite;
57     }
58     
59     public void testGetProvider() {
60         persistenceUnit.setProvider(ProviderUtil.HIBERNATE_PROVIDER.getProviderClass());
61         assertEquals(ProviderUtil.HIBERNATE_PROVIDER, ProviderUtil.getProvider(persistenceUnit));
62     }
63     
64     public void testSetTableGeneration(){
65         Provider provider = ProviderUtil.TOPLINK_PROVIDER;
66         persistenceUnit.setProvider(provider.getProviderClass());
67         
68         ProviderUtil.setTableGeneration(persistenceUnit, Provider.TABLE_GENERATION_CREATE, provider);
69         assertPropertyExists(provider.getTableGenerationPropertyName());
70         assertValueExists(provider.getTableGenerationCreateValue());
71         assertNoSuchValue(provider.getTableGenerationDropCreateValue());
72         
73         ProviderUtil.setTableGeneration(persistenceUnit, Provider.TABLE_GENERATION_DROPCREATE, provider);
74         assertPropertyExists(provider.getTableGenerationPropertyName());
75         assertValueExists(provider.getTableGenerationDropCreateValue());
76         assertNoSuchValue(provider.getTableGenerationCreateValue());
77         
78     }
79     
80     public void testSetProvider(){
81         Provider provider = ProviderUtil.KODO_PROVIDER;
82         ProviderUtil.setProvider(persistenceUnit, provider, getConnection(), Provider.TABLE_GENERATTION_UNKOWN);
83         assertEquals(provider.getProviderClass(), persistenceUnit.getProvider());
84         assertPropertyExists(provider.getJdbcDriver());
85         assertPropertyExists(provider.getJdbcUrl());
86         assertPropertyExists(provider.getJdbcUsername());
87     }
88     
89     public void testChangeProvider(){
90         Provider originalProvider = ProviderUtil.HIBERNATE_PROVIDER;
91         ProviderUtil.setProvider(persistenceUnit, originalProvider, getConnection(), Provider.TABLE_GENERATION_CREATE);
92         assertEquals(originalProvider.getProviderClass(), persistenceUnit.getProvider());
93         
94         Provider newProvider = ProviderUtil.TOPLINK_PROVIDER;
95         ProviderUtil.setProvider(persistenceUnit, newProvider, getConnection(), Provider.TABLE_GENERATION_DROPCREATE);
96         // assert that old providers properties were removed
97
assertNoSuchProperty(originalProvider.getTableGenerationPropertyName());
98         assertNoSuchProperty(originalProvider.getJdbcDriver());
99         assertNoSuchProperty(originalProvider.getJdbcUrl());
100         assertNoSuchProperty(originalProvider.getJdbcUsername());
101         // assert that new providers properties are set
102
assertEquals(newProvider.getProviderClass(), persistenceUnit.getProvider());
103         assertPropertyExists(newProvider.getJdbcDriver());
104         assertPropertyExists(newProvider.getJdbcUrl());
105         assertPropertyExists(newProvider.getJdbcUsername());
106         assertPropertyExists(newProvider.getTableGenerationPropertyName());
107     }
108     
109     /**
110      * Tests that changing of provider preserves existing
111      * table generation value.
112      */

113     public void testTableGenerationPropertyIsPreserved(){
114         Provider originalProvider = ProviderUtil.KODO_PROVIDER;
115         ProviderUtil.setProvider(persistenceUnit, originalProvider, getConnection(), Provider.TABLE_GENERATION_CREATE);
116         
117         Provider newProvider = ProviderUtil.TOPLINK_PROVIDER;
118         ProviderUtil.setProvider(persistenceUnit, newProvider, getConnection(), Provider.TABLE_GENERATION_CREATE);
119         assertEquals(newProvider.getTableGenerationPropertyName(),
120                 ProviderUtil.getProperty(persistenceUnit, newProvider.getTableGenerationPropertyName()).getName());
121         assertEquals(newProvider.getTableGenerationCreateValue(),
122                 ProviderUtil.getProperty(persistenceUnit, newProvider.getTableGenerationPropertyName()).getValue());
123         
124         
125         
126     }
127     
128     public void testRemoveProviderProperties(){
129         Provider provider = ProviderUtil.KODO_PROVIDER;
130         PersistenceUnit persistenceUnit = new PersistenceUnit();
131         ProviderUtil.setProvider(persistenceUnit, provider, getConnection(), Provider.TABLE_GENERATION_CREATE);
132         // ProviderUtil.setTableGeneration(persistenceUnit, Provider.TABLE_GENERATION_CREATE, provider);
133

134         ProviderUtil.removeProviderProperties(persistenceUnit);
135         assertNoSuchProperty(provider.getTableGenerationPropertyName());
136         assertNoSuchProperty(provider.getJdbcDriver());
137         assertNoSuchProperty(provider.getJdbcUrl());
138         assertNoSuchProperty(provider.getJdbcUsername());
139         
140     }
141     
142     
143     public void testGetPUDataObject() throws Exception JavaDoc{
144         String JavaDoc invalidPersistenceXml = getDataDir().getAbsolutePath() + File.separator + "invalid_persistence.xml";
145         FileObject invalidPersistenceFO = FileUtil.toFileObject(new File JavaDoc(invalidPersistenceXml));
146         try{
147             ProviderUtil.getPUDataObject(invalidPersistenceFO);
148             fail("InvalidPersistenceXmlException should have been thrown");
149         } catch (InvalidPersistenceXmlException ipx){
150             assertEquals(invalidPersistenceXml, ipx.getPath());
151         }
152         
153     }
154     /**
155      * Asserts that property with given name exists in persistence unit.
156      */

157     protected void assertPropertyExists(String JavaDoc propertyName){
158         if (!propertyExists(propertyName)){
159             fail("Property " + propertyName + " was not found.");
160         }
161         assertTrue(true);
162     }
163     
164     /**
165      * Asserts that no property with given name exists in persistence unit.
166      */

167     protected void assertNoSuchProperty(String JavaDoc propertyName){
168         if (propertyExists(propertyName)){
169             fail("Property " + propertyName + " was found.");
170         }
171         assertTrue(true);
172     }
173     
174     protected void assertNoSuchValue(String JavaDoc value){
175         if (valueExists(value)){
176             fail("Property with value " + value + " was found");
177         }
178         assertTrue(true);
179     }
180     
181     protected void assertValueExists(String JavaDoc value){
182         if (!valueExists(value)){
183             fail("Property with value " + value + " was not found");
184         }
185         assertTrue(true);
186     }
187     
188     
189     /**
190      * @return true if property with given name exists in persistence unit,
191      * false otherwise.
192      */

193     protected boolean propertyExists(String JavaDoc propertyName){
194         Property[] properties = ProviderUtil.getProperties(persistenceUnit);
195         for (int i = 0; i < properties.length; i++) {
196             if (properties[i].getName().equals(propertyName)){
197                 return true;
198             }
199         }
200         return false;
201     }
202     
203     /**
204      * @return true if property with given value exists in persistence unit,
205      * false otherwise.
206      */

207     protected boolean valueExists(String JavaDoc propertyValue){
208         Property[] properties = ProviderUtil.getProperties(persistenceUnit);
209         for (int i = 0; i < properties.length; i++) {
210             if (properties[i].getValue().equals(propertyValue)){
211                 return true;
212             }
213         }
214         return false;
215     }
216     
217     private DatabaseConnection getConnection(){
218         JDBCDriver driver = JDBCDriver.create("driver", "driver", "foo.bar", new URL JavaDoc[]{});
219         return DatabaseConnection.create(driver, "foo", "bar", "schema", "password", false);
220     }
221     
222 }
Popular Tags