KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jdo > LocalPersistenceManagerFactoryTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.orm.jdo;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Properties JavaDoc;
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 /**
31  * @author Juergen Hoeller
32  */

33 public class LocalPersistenceManagerFactoryTests extends TestCase {
34
35     public void testLocalPersistenceManagerFactoryBean() throws IOException JavaDoc {
36         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
37         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
38         LocalPersistenceManagerFactoryBean pmfb = new LocalPersistenceManagerFactoryBean() {
39             protected PersistenceManagerFactory newPersistenceManagerFactory(Properties JavaDoc props) {
40                 return pmf;
41             }
42         };
43         pmfb.setJdoProperties(new Properties JavaDoc());
44         pmfb.afterPropertiesSet();
45         assertSame(pmf, pmfb.getObject());
46     }
47
48     public void testLocalPersistenceManagerFactoryBeanWithInvalidSettings() throws IOException JavaDoc {
49         LocalPersistenceManagerFactoryBean pmfb = new LocalPersistenceManagerFactoryBean();
50         try {
51             pmfb.afterPropertiesSet();
52             fail("Should have thrown IllegalArgumentException");
53         }
54         catch (IllegalArgumentException JavaDoc ex) {
55             // expected
56
}
57     }
58
59     public void testLocalPersistenceManagerFactoryBeanWithIncompleteProperties() throws IOException JavaDoc {
60         LocalPersistenceManagerFactoryBean pmfb = new LocalPersistenceManagerFactoryBean();
61         Properties JavaDoc props = new Properties JavaDoc();
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             // expected
70
}
71     }
72
73     public void testLocalPersistenceManagerFactoryBeanWithInvalidProperty() throws IOException JavaDoc {
74         LocalPersistenceManagerFactoryBean pmfb = new LocalPersistenceManagerFactoryBean() {
75             protected PersistenceManagerFactory newPersistenceManagerFactory(Properties JavaDoc props) {
76                 throw new IllegalArgumentException JavaDoc(props.getProperty("myKey"));
77             }
78         };
79         Properties JavaDoc props = new Properties JavaDoc();
80         props.setProperty("myKey", "myValue");
81         pmfb.setJdoProperties(props);
82         try {
83             pmfb.afterPropertiesSet();
84             fail("Should have thrown IllegalArgumentException");
85         }
86         catch (IllegalArgumentException JavaDoc ex) {
87             // expected
88
assertTrue("Correct exception", "myValue".equals(ex.getMessage()));
89         }
90     }
91
92     public void testLocalPersistenceManagerFactoryBeanWithFile() throws IOException JavaDoc {
93         LocalPersistenceManagerFactoryBean pmfb = new LocalPersistenceManagerFactoryBean() {
94             protected PersistenceManagerFactory newPersistenceManagerFactory(Properties JavaDoc prop) {
95                 throw new IllegalArgumentException JavaDoc(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 JavaDoc ex) {
104             // expected
105
assertTrue("Correct exception", "myValue".equals(ex.getMessage()));
106         }
107     }
108
109 }
110
Popular Tags