1 19 20 21 package org.netbeans.modules.j2ee.persistence.provider; 22 23 import java.io.File ; 24 import java.net.URL ; 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 39 public class ProviderUtilTest extends NbTestCase { 40 41 private PersistenceUnit persistenceUnit; 42 43 public ProviderUtilTest(String testName) { 44 super(testName); 45 } 46 47 protected void setUp() throws Exception { 48 this.persistenceUnit = new PersistenceUnit(); 49 } 50 51 protected void tearDown() throws Exception { 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 assertNoSuchProperty(originalProvider.getTableGenerationPropertyName()); 98 assertNoSuchProperty(originalProvider.getJdbcDriver()); 99 assertNoSuchProperty(originalProvider.getJdbcUrl()); 100 assertNoSuchProperty(originalProvider.getJdbcUsername()); 101 assertEquals(newProvider.getProviderClass(), persistenceUnit.getProvider()); 103 assertPropertyExists(newProvider.getJdbcDriver()); 104 assertPropertyExists(newProvider.getJdbcUrl()); 105 assertPropertyExists(newProvider.getJdbcUsername()); 106 assertPropertyExists(newProvider.getTableGenerationPropertyName()); 107 } 108 109 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 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 { 144 String invalidPersistenceXml = getDataDir().getAbsolutePath() + File.separator + "invalid_persistence.xml"; 145 FileObject invalidPersistenceFO = FileUtil.toFileObject(new File (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 157 protected void assertPropertyExists(String propertyName){ 158 if (!propertyExists(propertyName)){ 159 fail("Property " + propertyName + " was not found."); 160 } 161 assertTrue(true); 162 } 163 164 167 protected void assertNoSuchProperty(String propertyName){ 168 if (propertyExists(propertyName)){ 169 fail("Property " + propertyName + " was found."); 170 } 171 assertTrue(true); 172 } 173 174 protected void assertNoSuchValue(String value){ 175 if (valueExists(value)){ 176 fail("Property with value " + value + " was found"); 177 } 178 assertTrue(true); 179 } 180 181 protected void assertValueExists(String value){ 182 if (!valueExists(value)){ 183 fail("Property with value " + value + " was not found"); 184 } 185 assertTrue(true); 186 } 187 188 189 193 protected boolean propertyExists(String 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 207 protected boolean valueExists(String 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 []{}); 219 return DatabaseConnection.create(driver, "foo", "bar", "schema", "password", false); 220 } 221 222 } | Popular Tags |