View Javadoc
1   /*
2    * Copyright 2002-2013 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.jpa.persistenceunit;
18  
19  import java.net.URL;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Properties;
23  import javax.persistence.spi.PersistenceUnitInfo;
24  import javax.persistence.spi.PersistenceUnitTransactionType;
25  import javax.sql.DataSource;
26  
27  import org.junit.Ignore;
28  import org.junit.Test;
29  
30  import org.springframework.core.io.ClassPathResource;
31  import org.springframework.core.io.Resource;
32  import org.springframework.core.io.UrlResource;
33  import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
34  import org.springframework.jdbc.datasource.DriverManagerDataSource;
35  import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
36  import org.springframework.jdbc.datasource.lookup.MapDataSourceLookup;
37  import org.springframework.tests.mock.jndi.SimpleNamingContextBuilder;
38  
39  import static org.junit.Assert.*;
40  
41  /**
42   * Unit and integration tests for the JPA XML resource parsing support.
43   *
44   * @author Costin Leau
45   * @author Juergen Hoeller
46   * @author Nicholas Williams
47   */
48  public class PersistenceXmlParsingTests {
49  
50  	@Test
51  	public void testMetaInfCase() throws Exception {
52  		PersistenceUnitReader reader = new PersistenceUnitReader(
53  				new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
54  		String resource = "/org/springframework/orm/jpa/META-INF/persistence.xml";
55  		PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
56  
57  		assertNotNull(info);
58  		assertEquals(1, info.length);
59  		assertEquals("OrderManagement", info[0].getPersistenceUnitName());
60  
61  		assertEquals(2, info[0].getJarFileUrls().size());
62  		assertEquals(new ClassPathResource("order.jar").getURL(), info[0].getJarFileUrls().get(0));
63  		assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), info[0].getJarFileUrls().get(1));
64  
65  		assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
66  	}
67  
68  	@Test
69  	public void testExample1() throws Exception {
70  		PersistenceUnitReader reader = new PersistenceUnitReader(
71  				new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
72  		String resource = "/org/springframework/orm/jpa/persistence-example1.xml";
73  		PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
74  
75  		assertNotNull(info);
76  		assertEquals(1, info.length);
77  		assertEquals("OrderManagement", info[0].getPersistenceUnitName());
78  
79  		assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
80  	}
81  
82  	@Test
83  	public void testExample2() throws Exception {
84  		PersistenceUnitReader reader = new PersistenceUnitReader(
85  				new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
86  		String resource = "/org/springframework/orm/jpa/persistence-example2.xml";
87  		PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
88  
89  		assertNotNull(info);
90  		assertEquals(1, info.length);
91  
92  		assertEquals("OrderManagement2", info[0].getPersistenceUnitName());
93  
94  		assertEquals(1, info[0].getMappingFileNames().size());
95  		assertEquals("mappings.xml", info[0].getMappingFileNames().get(0));
96  		assertEquals(0, info[0].getProperties().keySet().size());
97  
98  		assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
99  	}
100 
101 	@Test
102 	public void testExample3() throws Exception {
103 		PersistenceUnitReader reader = new PersistenceUnitReader(
104 				new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
105 		String resource = "/org/springframework/orm/jpa/persistence-example3.xml";
106 		PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
107 
108 		assertNotNull(info);
109 		assertEquals(1, info.length);
110 		assertEquals("OrderManagement3", info[0].getPersistenceUnitName());
111 
112 		assertEquals(2, info[0].getJarFileUrls().size());
113 		assertEquals(new ClassPathResource("order.jar").getURL(), info[0].getJarFileUrls().get(0));
114 		assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), info[0].getJarFileUrls().get(1));
115 
116 		assertEquals(0, info[0].getProperties().keySet().size());
117 		assertNull(info[0].getJtaDataSource());
118 		assertNull(info[0].getNonJtaDataSource());
119 
120 		assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
121 	}
122 
123 	@Test
124 	public void testExample4() throws Exception {
125 		SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
126 		DataSource ds = new DriverManagerDataSource();
127 		builder.bind("java:comp/env/jdbc/MyDB", ds);
128 
129 		PersistenceUnitReader reader = new PersistenceUnitReader(
130 				new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
131 		String resource = "/org/springframework/orm/jpa/persistence-example4.xml";
132 		PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
133 
134 		assertNotNull(info);
135 		assertEquals(1, info.length);
136 		assertEquals("OrderManagement4", info[0].getPersistenceUnitName());
137 
138 		assertEquals(1, info[0].getMappingFileNames().size());
139 		assertEquals("order-mappings.xml", info[0].getMappingFileNames().get(0));
140 
141 		assertEquals(3, info[0].getManagedClassNames().size());
142 		assertEquals("com.acme.Order", info[0].getManagedClassNames().get(0));
143 		assertEquals("com.acme.Customer", info[0].getManagedClassNames().get(1));
144 		assertEquals("com.acme.Item", info[0].getManagedClassNames().get(2));
145 
146 		assertTrue("Exclude unlisted should be true when no value.", info[0].excludeUnlistedClasses());
147 
148 		assertSame(PersistenceUnitTransactionType.RESOURCE_LOCAL, info[0].getTransactionType());
149 		assertEquals(0, info[0].getProperties().keySet().size());
150 
151 		builder.clear();
152 	}
153 
154 	@Test
155 	public void testExample5() throws Exception {
156 		PersistenceUnitReader reader = new PersistenceUnitReader(
157 				new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
158 		String resource = "/org/springframework/orm/jpa/persistence-example5.xml";
159 		PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
160 
161 		assertNotNull(info);
162 		assertEquals(1, info.length);
163 		assertEquals("OrderManagement5", info[0].getPersistenceUnitName());
164 
165 		assertEquals(2, info[0].getMappingFileNames().size());
166 		assertEquals("order1.xml", info[0].getMappingFileNames().get(0));
167 		assertEquals("order2.xml", info[0].getMappingFileNames().get(1));
168 
169 		assertEquals(2, info[0].getJarFileUrls().size());
170 		assertEquals(new ClassPathResource("order.jar").getURL(), info[0].getJarFileUrls().get(0));
171 		assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), info[0].getJarFileUrls().get(1));
172 
173 		assertEquals("com.acme.AcmePersistence", info[0].getPersistenceProviderClassName());
174 		assertEquals(0, info[0].getProperties().keySet().size());
175 
176 		assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
177 	}
178 
179 	@Test
180 	public void testExampleComplex() throws Exception {
181 		DataSource ds = new DriverManagerDataSource();
182 
183 		String resource = "/org/springframework/orm/jpa/persistence-complex.xml";
184 		MapDataSourceLookup dataSourceLookup = new MapDataSourceLookup();
185 		Map<String, DataSource> dataSources = new HashMap<String, DataSource>();
186 		dataSources.put("jdbc/MyPartDB", ds);
187 		dataSources.put("jdbc/MyDB", ds);
188 		dataSourceLookup.setDataSources(dataSources);
189 		PersistenceUnitReader reader = new PersistenceUnitReader(
190 				new PathMatchingResourcePatternResolver(), dataSourceLookup);
191 		PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
192 
193 		assertEquals(2, info.length);
194 
195 		PersistenceUnitInfo pu1 = info[0];
196 
197 		assertEquals("pu1", pu1.getPersistenceUnitName());
198 
199 		assertEquals("com.acme.AcmePersistence", pu1.getPersistenceProviderClassName());
200 
201 		assertEquals(1, pu1.getMappingFileNames().size());
202 		assertEquals("ormap2.xml", pu1.getMappingFileNames().get(0));
203 
204 		assertEquals(1, pu1.getJarFileUrls().size());
205 		assertEquals(new ClassPathResource("order.jar").getURL(), pu1.getJarFileUrls().get(0));
206 
207 		assertFalse(pu1.excludeUnlistedClasses());
208 
209 		assertSame(PersistenceUnitTransactionType.RESOURCE_LOCAL, pu1.getTransactionType());
210 
211 		Properties props = pu1.getProperties();
212 		assertEquals(2, props.keySet().size());
213 		assertEquals("on", props.getProperty("com.acme.persistence.sql-logging"));
214 		assertEquals("bar", props.getProperty("foo"));
215 
216 		assertNull(pu1.getNonJtaDataSource());
217 
218 		assertSame(ds, pu1.getJtaDataSource());
219 
220 		assertFalse("Exclude unlisted should default false in 1.0.", pu1.excludeUnlistedClasses());
221 
222 		PersistenceUnitInfo pu2 = info[1];
223 
224 		assertSame(PersistenceUnitTransactionType.JTA, pu2.getTransactionType());
225 		assertEquals("com.acme.AcmePersistence", pu2.getPersistenceProviderClassName());
226 
227 		assertEquals(1, pu2.getMappingFileNames().size());
228 		assertEquals("order2.xml", pu2.getMappingFileNames().get(0));
229 
230 		// the following assertions fail only during coverage runs
231 		// assertEquals(1, pu2.getJarFileUrls().size());
232 		// assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), pu2.getJarFileUrls().get(0));
233 
234 		assertTrue(pu2.excludeUnlistedClasses());
235 
236 		assertNull(pu2.getJtaDataSource());
237 		assertEquals(ds, pu2.getNonJtaDataSource());
238 
239 		assertTrue("Exclude unlisted should be true when no value.", pu2.excludeUnlistedClasses());
240 	}
241 
242 	@Test
243 	public void testExample6() throws Exception {
244 		PersistenceUnitReader reader = new PersistenceUnitReader(
245 				new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
246 		String resource = "/org/springframework/orm/jpa/persistence-example6.xml";
247 		PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
248 		assertEquals(1, info.length);
249 		assertEquals("pu", info[0].getPersistenceUnitName());
250 		assertEquals(0, info[0].getProperties().keySet().size());
251 
252 		assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
253 	}
254 
255 	@Ignore  // not doing schema parsing anymore for JPA 2.0 compatibility
256 	@Test
257 	public void testInvalidPersistence() throws Exception {
258 		PersistenceUnitReader reader = new PersistenceUnitReader(
259 				new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
260 		String resource = "/org/springframework/orm/jpa/persistence-invalid.xml";
261 		try {
262 			reader.readPersistenceUnitInfos(resource);
263 			fail("expected invalid document exception");
264 		}
265 		catch (RuntimeException expected) {
266 		}
267 	}
268 
269 	@Ignore  // not doing schema parsing anymore for JPA 2.0 compatibility
270 	@Test
271 	public void testNoSchemaPersistence() throws Exception {
272 		PersistenceUnitReader reader = new PersistenceUnitReader(
273 				new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
274 		String resource = "/org/springframework/orm/jpa/persistence-no-schema.xml";
275 		try {
276 			reader.readPersistenceUnitInfos(resource);
277 			fail("expected invalid document exception");
278 		}
279 		catch (RuntimeException expected) {
280 		}
281 	}
282 
283 	@Test
284 	public void testPersistenceUnitRootUrl() throws Exception {
285 		PersistenceUnitReader reader = new PersistenceUnitReader(
286 				new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
287 
288 		URL url = reader.determinePersistenceUnitRootUrl(new ClassPathResource(
289 				"/org/springframework/orm/jpa/persistence-no-schema.xml"));
290 		assertNull(url);
291 
292 		url = reader.determinePersistenceUnitRootUrl(new ClassPathResource("/org/springframework/orm/jpa/META-INF/persistence.xml"));
293 		assertTrue("the containing folder should have been returned", url.toString().endsWith("/org/springframework/orm/jpa"));
294 	}
295 
296 	@Test
297 	public void testPersistenceUnitRootUrlWithJar() throws Exception {
298 		PersistenceUnitReader reader = new PersistenceUnitReader(
299 				new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
300 
301 		ClassPathResource archive = new ClassPathResource("/org/springframework/orm/jpa/jpa-archive.jar");
302 		String newRoot = "jar:" + archive.getURL().toExternalForm() + "!/META-INF/persist.xml";
303 		Resource insideArchive = new UrlResource(newRoot);
304 		// make sure the location actually exists
305 		assertTrue(insideArchive.exists());
306 		URL url = reader.determinePersistenceUnitRootUrl(insideArchive);
307 		assertTrue("the archive location should have been returned", archive.getURL().sameFile(url));
308 	}
309 
310 	@Test
311 	public void testJpa1ExcludeUnlisted() throws Exception {
312 		PersistenceUnitReader reader = new PersistenceUnitReader(
313 				new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
314 		String resource = "/org/springframework/orm/jpa/persistence-exclude-1.0.xml";
315 		PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
316 
317 		assertNotNull(info);
318 		assertEquals("The number of persistence units is incorrect.", 4, info.length);
319 
320 		PersistenceUnitInfo noExclude = info[0];
321 		assertNotNull("noExclude should not be null.", noExclude);
322 		assertEquals("noExclude name is not correct.", "NoExcludeElement", noExclude.getPersistenceUnitName());
323 		assertFalse("Exclude unlisted should default false in 1.0.", noExclude.excludeUnlistedClasses());
324 
325 		PersistenceUnitInfo emptyExclude = info[1];
326 		assertNotNull("emptyExclude should not be null.", emptyExclude);
327 		assertEquals("emptyExclude name is not correct.", "EmptyExcludeElement", emptyExclude.getPersistenceUnitName());
328 		assertTrue("emptyExclude should be true.", emptyExclude.excludeUnlistedClasses());
329 
330 		PersistenceUnitInfo trueExclude = info[2];
331 		assertNotNull("trueExclude should not be null.", trueExclude);
332 		assertEquals("trueExclude name is not correct.", "TrueExcludeElement", trueExclude.getPersistenceUnitName());
333 		assertTrue("trueExclude should be true.", trueExclude.excludeUnlistedClasses());
334 
335 		PersistenceUnitInfo falseExclude = info[3];
336 		assertNotNull("falseExclude should not be null.", falseExclude);
337 		assertEquals("falseExclude name is not correct.", "FalseExcludeElement", falseExclude.getPersistenceUnitName());
338 		assertFalse("falseExclude should be false.", falseExclude.excludeUnlistedClasses());
339 	}
340 
341 	@Test
342 	public void testJpa2ExcludeUnlisted() throws Exception {
343 		PersistenceUnitReader reader = new PersistenceUnitReader(
344 				new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
345 		String resource = "/org/springframework/orm/jpa/persistence-exclude-2.0.xml";
346 		PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
347 
348 		assertNotNull(info);
349 		assertEquals("The number of persistence units is incorrect.", 4, info.length);
350 
351 		PersistenceUnitInfo noExclude = info[0];
352 		assertNotNull("noExclude should not be null.", noExclude);
353 		assertEquals("noExclude name is not correct.", "NoExcludeElement", noExclude.getPersistenceUnitName());
354 		assertFalse("Exclude unlisted still defaults to false in 2.0.", noExclude.excludeUnlistedClasses());
355 
356 		PersistenceUnitInfo emptyExclude = info[1];
357 		assertNotNull("emptyExclude should not be null.", emptyExclude);
358 		assertEquals("emptyExclude name is not correct.", "EmptyExcludeElement", emptyExclude.getPersistenceUnitName());
359 		assertTrue("emptyExclude should be true.", emptyExclude.excludeUnlistedClasses());
360 
361 		PersistenceUnitInfo trueExclude = info[2];
362 		assertNotNull("trueExclude should not be null.", trueExclude);
363 		assertEquals("trueExclude name is not correct.", "TrueExcludeElement", trueExclude.getPersistenceUnitName());
364 		assertTrue("trueExclude should be true.", trueExclude.excludeUnlistedClasses());
365 
366 		PersistenceUnitInfo falseExclude = info[3];
367 		assertNotNull("falseExclude should not be null.", falseExclude);
368 		assertEquals("falseExclude name is not correct.", "FalseExcludeElement", falseExclude.getPersistenceUnitName());
369 		assertFalse("falseExclude should be false.", falseExclude.excludeUnlistedClasses());
370 	}
371 
372 }