1 16 17 package org.springframework.orm.jdo; 18 19 import java.io.IOException ; 20 import java.util.Properties ; 21 22 import javax.jdo.JDOFatalUserException; 23 import javax.jdo.PersistenceManagerFactory; 24 25 import junit.framework.TestCase; 26 import org.easymock.MockControl; 27 28 import org.springframework.core.io.ClassPathResource; 29 30 33 public class LocalPersistenceManagerFactoryTests extends TestCase { 34 35 public void testLocalPersistenceManagerFactoryBean() throws IOException { 36 MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class); 37 final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock(); 38 LocalPersistenceManagerFactoryBean pmfb = new LocalPersistenceManagerFactoryBean() { 39 protected PersistenceManagerFactory newPersistenceManagerFactory(Properties props) { 40 return pmf; 41 } 42 }; 43 pmfb.setJdoProperties(new Properties ()); 44 pmfb.afterPropertiesSet(); 45 assertSame(pmf, pmfb.getObject()); 46 } 47 48 public void testLocalPersistenceManagerFactoryBeanWithInvalidSettings() throws IOException { 49 LocalPersistenceManagerFactoryBean pmfb = new LocalPersistenceManagerFactoryBean(); 50 try { 51 pmfb.afterPropertiesSet(); 52 fail("Should have thrown IllegalArgumentException"); 53 } 54 catch (IllegalArgumentException ex) { 55 } 57 } 58 59 public void testLocalPersistenceManagerFactoryBeanWithIncompleteProperties() throws IOException { 60 LocalPersistenceManagerFactoryBean pmfb = new LocalPersistenceManagerFactoryBean(); 61 Properties props = new Properties (); 62 props.setProperty("myKey", "myValue"); 63 pmfb.setJdoProperties(props); 64 try { 65 pmfb.afterPropertiesSet(); 66 fail("Should have thrown JDOFatalUserException"); 67 } 68 catch (JDOFatalUserException ex) { 69 } 71 } 72 73 public void testLocalPersistenceManagerFactoryBeanWithInvalidProperty() throws IOException { 74 LocalPersistenceManagerFactoryBean pmfb = new LocalPersistenceManagerFactoryBean() { 75 protected PersistenceManagerFactory newPersistenceManagerFactory(Properties props) { 76 throw new IllegalArgumentException (props.getProperty("myKey")); 77 } 78 }; 79 Properties props = new Properties (); 80 props.setProperty("myKey", "myValue"); 81 pmfb.setJdoProperties(props); 82 try { 83 pmfb.afterPropertiesSet(); 84 fail("Should have thrown IllegalArgumentException"); 85 } 86 catch (IllegalArgumentException ex) { 87 assertTrue("Correct exception", "myValue".equals(ex.getMessage())); 89 } 90 } 91 92 public void testLocalPersistenceManagerFactoryBeanWithFile() throws IOException { 93 LocalPersistenceManagerFactoryBean pmfb = new LocalPersistenceManagerFactoryBean() { 94 protected PersistenceManagerFactory newPersistenceManagerFactory(Properties prop) { 95 throw new IllegalArgumentException (prop.getProperty("myKey")); 96 } 97 }; 98 pmfb.setConfigLocation(new ClassPathResource("test.properties", getClass())); 99 try { 100 pmfb.afterPropertiesSet(); 101 fail("Should have thrown IllegalArgumentException"); 102 } 103 catch (IllegalArgumentException ex) { 104 assertTrue("Correct exception", "myValue".equals(ex.getMessage())); 106 } 107 } 108 109 } 110 | Popular Tags |