Source Code Cross Referenced for AutoConfigPersistenceUnitsTest.java in  » J2EE » openejb3 » org » apache » openejb » config » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » openejb3 » org.apache.openejb.config 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         * Licensed to the Apache Software Foundation (ASF) under one or more
0003:         * contributor license agreements.  See the NOTICE file distributed with
0004:         * this work for additional information regarding copyright ownership.
0005:         * The ASF licenses this file to You under the Apache License, Version 2.0
0006:         * (the "License"); you may not use this file except in compliance with
0007:         * the License.  You may obtain a copy of the License at
0008:         *
0009:         *     http://www.apache.org/licenses/LICENSE-2.0
0010:         *
0011:         *  Unless required by applicable law or agreed to in writing, software
0012:         *  distributed under the License is distributed on an "AS IS" BASIS,
0013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         *  See the License for the specific language governing permissions and
0015:         *  limitations under the License.
0016:         */package org.apache.openejb.config;
0017:
0018:        import junit.framework.TestCase;
0019:        import org.apache.openejb.OpenEJBException;
0020:        import org.apache.openejb.assembler.classic.AppInfo;
0021:        import org.apache.openejb.assembler.classic.Assembler;
0022:        import org.apache.openejb.assembler.classic.OpenEjbConfiguration;
0023:        import org.apache.openejb.assembler.classic.PersistenceUnitInfo;
0024:        import org.apache.openejb.assembler.classic.ProxyFactoryInfo;
0025:        import org.apache.openejb.assembler.classic.ResourceInfo;
0026:        import org.apache.openejb.assembler.classic.SecurityServiceInfo;
0027:        import org.apache.openejb.assembler.classic.TransactionServiceInfo;
0028:        import org.apache.openejb.config.sys.Resource;
0029:        import org.apache.openejb.jee.jpa.unit.Persistence;
0030:        import org.apache.openejb.jee.jpa.unit.PersistenceUnit;
0031:        import org.apache.openejb.loader.SystemInstance;
0032:
0033:        import javax.naming.NamingException;
0034:        import java.io.IOException;
0035:        import java.sql.Connection;
0036:        import java.sql.DriverPropertyInfo;
0037:        import java.sql.SQLException;
0038:        import java.util.Properties;
0039:        import java.util.List;
0040:
0041:        /**
0042:         * @version $Revision: 632667 $ $Date: 2008-03-01 14:32:11 -0800 (Sat, 01 Mar 2008) $
0043:         */
0044:        public class AutoConfigPersistenceUnitsTest extends TestCase {
0045:            private ConfigurationFactory config;
0046:            private Assembler assembler;
0047:            private List<ResourceInfo> resources;
0048:
0049:            protected void setUp() throws Exception {
0050:                config = new ConfigurationFactory();
0051:                assembler = new Assembler();
0052:
0053:                assembler.createProxyFactory(config
0054:                        .configureService(ProxyFactoryInfo.class));
0055:                assembler.createTransactionManager(config
0056:                        .configureService(TransactionServiceInfo.class));
0057:                assembler.createSecurityService(config
0058:                        .configureService(SecurityServiceInfo.class));
0059:
0060:                OpenEjbConfiguration configuration = SystemInstance.get()
0061:                        .getComponent(OpenEjbConfiguration.class);
0062:                resources = configuration.facilities.resources;
0063:            }
0064:
0065:            /**
0066:             * Existing data source "Orange", jta managed
0067:             * Existing data source "OrangeUnmanaged", not jta managed
0068:             *
0069:             * Persistence xml like so:
0070:             *
0071:             * <persistence-unit name="orange-unit">
0072:             *      <jta-data-source>Orange</jta-data-source>
0073:             *      <non-jta-data-source>OrangeUnamanged</non-jta-data-source>
0074:             * </persistence-unit>
0075:             *
0076:             * This is the happy path.
0077:             *
0078:             * @throws Exception
0079:             */
0080:            public void test() throws Exception {
0081:
0082:                ResourceInfo jta = addDataSource("Orange", OrangeDriver.class,
0083:                        "jdbc:orange:some:stuff", true);
0084:                ResourceInfo nonJta = addDataSource("OrangeUnmanaged",
0085:                        OrangeDriver.class, "jdbc:orange:some:stuff", false);
0086:
0087:                assertSame(jta, resources.get(0));
0088:                assertSame(nonJta, resources.get(1));
0089:
0090:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0091:                        "orange-unit", "Orange", "OrangeUnmanaged");
0092:
0093:                assertNotNull(unitInfo);
0094:
0095:                assertEquals(jta.id, unitInfo.jtaDataSource);
0096:                assertEquals(nonJta.id, unitInfo.nonJtaDataSource);
0097:            }
0098:
0099:            /**
0100:             * Existing data source "Orange", not controlled by us
0101:             * Existing data source "OrangeUnmanaged", also not controlled by us
0102:             *
0103:             * Persistence xml like so:
0104:             *
0105:             * <persistence-unit name="orange-unit">
0106:             *      <jta-data-source>Orange</jta-data-source>
0107:             *      <non-jta-data-source>OrangeUnamanged</non-jta-data-source>
0108:             * </persistence-unit>
0109:             *
0110:             * @throws Exception
0111:             */
0112:            public void testThirdPartyDataSources() throws Exception {
0113:
0114:                ResourceInfo jta = addDataSource("Orange", OrangeDriver.class,
0115:                        "jdbc:orange:some:stuff", null);
0116:                ResourceInfo nonJta = addDataSource("OrangeUnmanaged",
0117:                        OrangeDriver.class, "jdbc:orange:some:stuff", null);
0118:
0119:                assertSame(jta, resources.get(0));
0120:                assertSame(nonJta, resources.get(1));
0121:
0122:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0123:                        "orange-unit", "Orange", "OrangeUnmanaged");
0124:
0125:                assertNotNull(unitInfo);
0126:
0127:                assertEquals(jta.id, unitInfo.jtaDataSource);
0128:                assertEquals(nonJta.id, unitInfo.nonJtaDataSource);
0129:            }
0130:
0131:            /**
0132:             * Existing data source "Orange", not controlled by us
0133:             *
0134:             * Persistence xml like so:
0135:             *
0136:             * <persistence-unit name="orange-unit">
0137:             *      <jta-data-source>Orange</jta-data-source>
0138:             * </persistence-unit>
0139:             *
0140:             * Here we should just let them try and get by with
0141:             * just the one data source.
0142:             *
0143:             * @throws Exception
0144:             */
0145:            public void testThirdPartyDataSources2() throws Exception {
0146:
0147:                ResourceInfo dataSource = addDataSource("Orange",
0148:                        OrangeDriver.class, "jdbc:orange:some:stuff", null);
0149:
0150:                assertSame(dataSource, resources.get(0));
0151:
0152:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0153:                        "orange-unit", "Orange", null);
0154:
0155:                assertNotNull(unitInfo);
0156:
0157:                assertEquals(dataSource.id, unitInfo.jtaDataSource);
0158:                assertNull(unitInfo.nonJtaDataSource);
0159:            }
0160:
0161:            /**
0162:             * Existing data source "Orange", not controlled by us
0163:             *
0164:             * Persistence xml like so:
0165:             *
0166:             * <persistence-unit name="orange-unit">
0167:             *      <non-jta-data-source>Orange</non-jta-data-source>
0168:             * </persistence-unit>
0169:             *
0170:             * Here we should just let them try and get by with
0171:             * just the one data source.
0172:             *
0173:             * @throws Exception
0174:             */
0175:            public void testThirdPartyDataSources3() throws Exception {
0176:
0177:                ResourceInfo dataSource = addDataSource("Orange",
0178:                        OrangeDriver.class, "jdbc:orange:some:stuff", null);
0179:
0180:                assertSame(dataSource, resources.get(0));
0181:
0182:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0183:                        "orange-unit", null, "Orange");
0184:
0185:                assertNotNull(unitInfo);
0186:
0187:                assertNull(unitInfo.jtaDataSource);
0188:                assertEquals(dataSource.id, unitInfo.nonJtaDataSource);
0189:            }
0190:
0191:            /**
0192:             * Existing data source "Orange", not controlled by us
0193:             *
0194:             * Persistence xml like so:
0195:             *
0196:             * <persistence-unit name="orange-unit">
0197:             *      <jta-data-source>Orange</jta-data-source>
0198:             *      <non-jta-data-source>Orange</non-jta-data-source>
0199:             * </persistence-unit>
0200:             *
0201:             * Here we should just let them try and get by with
0202:             * both jta and non-jta references pointed at the same
0203:             * data source.
0204:             *
0205:             * @throws Exception
0206:             */
0207:            public void testThirdPartyDataSources4() throws Exception {
0208:
0209:                ResourceInfo dataSource = addDataSource("Orange",
0210:                        OrangeDriver.class, "jdbc:orange:some:stuff", null);
0211:
0212:                assertSame(dataSource, resources.get(0));
0213:
0214:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0215:                        "orange-unit", "Orange", "Orange");
0216:
0217:                assertNotNull(unitInfo);
0218:
0219:                assertEquals(dataSource.id, unitInfo.jtaDataSource);
0220:                assertEquals(dataSource.id, unitInfo.nonJtaDataSource);
0221:            }
0222:
0223:            /**
0224:             * Existing data source "Orange", jta managed
0225:             *
0226:             * Persistence xml like so:
0227:             *
0228:             * <persistence-unit name="orange-unit">
0229:             *      <jta-data-source>Orange</jta-data-source>
0230:             *      <non-jta-data-source>Orange</non-jta-data-source>
0231:             * </persistence-unit>
0232:             *
0233:             * They used the same data source for both the
0234:             * jta-data-source and non-jta-data-source and we
0235:             * can determine the data source will not work as
0236:             * a non-jta-data-source
0237:             *
0238:             * We should generate the missing data source for them
0239:             * based on the one they supplied.
0240:             *
0241:             * @throws Exception
0242:             */
0243:            public void testSameDataSourceForBoth1() throws Exception {
0244:
0245:                ResourceInfo supplied = addDataSource("Orange",
0246:                        OrangeDriver.class, "jdbc:orange:some:stuff", true);
0247:
0248:                assertSame(supplied, resources.get(0));
0249:
0250:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0251:                        "orange-unit", "Orange", "Orange");
0252:
0253:                assertNotNull(unitInfo);
0254:
0255:                ResourceInfo generated = resources.get(1);
0256:
0257:                assertEquals(supplied.id, unitInfo.jtaDataSource);
0258:                assertEquals(generated.id, unitInfo.nonJtaDataSource);
0259:
0260:                assertNotNull(generated);
0261:                assertEquals(supplied.id + "NonJta", generated.id);
0262:                assertEquals(supplied.service, generated.service);
0263:                assertEquals(supplied.className, generated.className);
0264:                assertEquals(supplied.properties.get("JdbcDriver"),
0265:                        generated.properties.get("JdbcDriver"));
0266:                assertEquals(supplied.properties.get("JdbcUrl"),
0267:                        generated.properties.get("JdbcUrl"));
0268:                assertEquals("false", generated.properties.get("JtaManaged"));
0269:
0270:            }
0271:
0272:            /**
0273:             * Existing data source "Orange", not jta managed
0274:             *
0275:             * Persistence xml like so:
0276:             *
0277:             * <persistence-unit name="orange-unit">
0278:             *      <jta-data-source>Orange</jta-data-source>
0279:             *      <non-jta-data-source>Orange</non-jta-data-source>
0280:             * </persistence-unit>
0281:             *
0282:             * They used the same data source for both the
0283:             * jta-data-source and non-jta-data-source and we
0284:             * can determine the data source will not work as
0285:             * a jta-data-source
0286:             *
0287:             * We should generate the missing data source for them
0288:             * based on the one they supplied.
0289:             *
0290:             * @throws Exception
0291:             */
0292:            public void testSameDataSourceForBoth2() throws Exception {
0293:
0294:                ResourceInfo supplied = addDataSource("Orange",
0295:                        OrangeDriver.class, "jdbc:orange:some:stuff", false);
0296:
0297:                assertSame(supplied, resources.get(0));
0298:
0299:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0300:                        "orange-unit", "Orange", "Orange");
0301:
0302:                assertNotNull(unitInfo);
0303:
0304:                ResourceInfo generated = resources.get(1);
0305:
0306:                assertEquals(generated.id, unitInfo.jtaDataSource);
0307:                assertEquals(supplied.id, unitInfo.nonJtaDataSource);
0308:
0309:                assertNotNull(generated);
0310:                assertEquals(supplied.id + "Jta", generated.id);
0311:                assertEquals(supplied.service, generated.service);
0312:                assertEquals(supplied.className, generated.className);
0313:                assertEquals(supplied.properties.get("JdbcDriver"),
0314:                        generated.properties.get("JdbcDriver"));
0315:                assertEquals(supplied.properties.get("JdbcUrl"),
0316:                        generated.properties.get("JdbcUrl"));
0317:                assertEquals("true", generated.properties.get("JtaManaged"));
0318:
0319:            }
0320:
0321:            /**
0322:             * Existing data source "OrangeOne", jta managed
0323:             * Existing data source "OrangeTwo", not jta managed
0324:             *
0325:             * Persistence xml like so:
0326:             *
0327:             * <persistence-unit name="orange-unit">
0328:             *      <jta-data-source>OrangeOne</jta-data-source>
0329:             *      <non-jta-data-source>OrangeOne</non-jta-data-source>
0330:             * </persistence-unit>
0331:             *
0332:             * They used the same data source for both the
0333:             * jta-data-source and non-jta-data-source and we
0334:             * can determine the data source will not work as
0335:             * a non-jta-data-source
0336:             * BUT
0337:             * they have explicitly configured a data source
0338:             * that nearly matches the named datasource and
0339:             * would be identical to what we would auto-create
0340:             *
0341:             * @throws Exception
0342:             */
0343:            public void testSameDataSourceForBoth3() throws Exception {
0344:
0345:                ResourceInfo jta = addDataSource("OrangeOne",
0346:                        OrangeDriver.class, "jdbc:orange:some:stuff", true);
0347:                ResourceInfo nonJta = addDataSource("OrangeTwo",
0348:                        OrangeDriver.class, "jdbc:orange:some:stuff", false);
0349:
0350:                assertSame(jta, resources.get(0));
0351:                assertSame(nonJta, resources.get(1));
0352:
0353:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0354:                        "orange-unit", "OrangeOne", "OrangeOne");
0355:
0356:                assertNotNull(unitInfo);
0357:
0358:                assertEquals(jta.id, unitInfo.jtaDataSource);
0359:                assertEquals(nonJta.id, unitInfo.nonJtaDataSource);
0360:            }
0361:
0362:            /**
0363:             * Existing data source "OrangeOne", jta managed
0364:             * Existing data source "OrangeTwo", not jta managed
0365:             *
0366:             * Persistence xml like so:
0367:             *
0368:             * <persistence-unit name="orange-unit">
0369:             *      <jta-data-source>OrangeTwo</jta-data-source>
0370:             *      <non-jta-data-source>OrangeTwo</non-jta-data-source>
0371:             * </persistence-unit>
0372:             *
0373:             * They used the same data source for both the
0374:             * jta-data-source and non-jta-data-source and we
0375:             * can determine the data source will not work as
0376:             * a jta-data-source
0377:             * BUT
0378:             * they have explicitly configured a data source
0379:             * that nearly matches the named datasource and
0380:             * would be identical to what we would auto-create
0381:             *
0382:             * @throws Exception
0383:             */
0384:            public void testSameDataSourceForBoth4() throws Exception {
0385:
0386:                ResourceInfo jta = addDataSource("OrangeOne",
0387:                        OrangeDriver.class, "jdbc:orange:some:stuff", true);
0388:                ResourceInfo nonJta = addDataSource("OrangeTwo",
0389:                        OrangeDriver.class, "jdbc:orange:some:stuff", false);
0390:
0391:                assertSame(jta, resources.get(0));
0392:                assertSame(nonJta, resources.get(1));
0393:
0394:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0395:                        "orange-unit", "OrangeTwo", "OrangeTwo");
0396:
0397:                assertNotNull(unitInfo);
0398:
0399:                assertEquals(jta.id, unitInfo.jtaDataSource);
0400:                assertEquals(nonJta.id, unitInfo.nonJtaDataSource);
0401:            }
0402:
0403:            /**
0404:             * Existing data source "Orange", jta managed
0405:             * Existing data source "OrangeUnmanaged", not jta managed
0406:             *
0407:             * Persistence xml like so:
0408:             *
0409:             * <persistence-unit name="orange-unit">
0410:             *      <jta-data-source>java:foo/bar/baz/Orange</jta-data-source>
0411:             *      <non-jta-data-source>java:foo/bar/baz/OrangeUnamanged</non-jta-data-source>
0412:             * </persistence-unit>
0413:             *
0414:             * The datasources should be mapped correctly despite the
0415:             * vendor specific prefix.
0416:             *
0417:             * @throws Exception
0418:             */
0419:            public void testShortName() throws Exception {
0420:
0421:                ResourceInfo jta = addDataSource("Orange", OrangeDriver.class,
0422:                        "jdbc:orange:some:stuff", true);
0423:                ResourceInfo nonJta = addDataSource("OrangeUnmanaged",
0424:                        OrangeDriver.class, "jdbc:orange:some:stuff", false);
0425:
0426:                assertSame(jta, resources.get(0));
0427:                assertSame(nonJta, resources.get(1));
0428:
0429:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0430:                        "orange-unit", "java:foo/bar/baz/Orange",
0431:                        "java:foo/bar/baz/OrangeUnmanaged");
0432:
0433:                assertNotNull(unitInfo);
0434:
0435:                assertEquals(jta.id, unitInfo.jtaDataSource);
0436:                assertEquals(nonJta.id, unitInfo.nonJtaDataSource);
0437:            }
0438:
0439:            /**
0440:             * Existing data source "Orange", jta managed
0441:             * Existing data source "OrangeUnmanaged", not jta managed
0442:             *
0443:             * Persistence xml like so:
0444:             *
0445:             * <persistence-unit name="orange-unit">
0446:             *      <jta-data-source>DoesNotExist</jta-data-source>
0447:             *      <non-jta-data-source>AlsoDoesNotExist</non-jta-data-source>
0448:             * </persistence-unit>
0449:             *
0450:             * We should automatically hook them up to the configured
0451:             * datasources that do match
0452:             *
0453:             * @throws Exception
0454:             */
0455:            public void testInvalidRefs() throws Exception {
0456:
0457:                ResourceInfo jta = addDataSource("Orange", OrangeDriver.class,
0458:                        "jdbc:orange:some:stuff", true);
0459:                ResourceInfo nonJta = addDataSource("OrangeUnmanaged",
0460:                        OrangeDriver.class, "jdbc:orange:some:stuff", false);
0461:
0462:                assertSame(jta, resources.get(0));
0463:                assertSame(nonJta, resources.get(1));
0464:
0465:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0466:                        "orange-unit", "DoesNotExist", "AlsoDoesNotExist");
0467:
0468:                assertNotNull(unitInfo);
0469:
0470:                assertEquals(jta.id, unitInfo.jtaDataSource);
0471:                assertEquals(nonJta.id, unitInfo.nonJtaDataSource);
0472:            }
0473:
0474:            // ---
0475:
0476:            /**
0477:             * Existing data source "OrangeOne", not jta managed
0478:             * Existing data source "OrangeTwo", not jta managed
0479:             *
0480:             * Persistence xml like so:
0481:             *
0482:             * <persistence-unit name="orange-unit">
0483:             *      <jta-data-source>OrangeOne</jta-data-source>
0484:             *      <non-jta-data-source>OrangeTwo</non-jta-data-source>
0485:             * </persistence-unit>
0486:             *
0487:             * This configuration should be rejected
0488:             *
0489:             * @throws Exception
0490:             */
0491:            public void testJtaRefToContrarilyConfiguredDataSource()
0492:                    throws Exception {
0493:
0494:                ResourceInfo nonJta1 = addDataSource("OrangeOne",
0495:                        OrangeDriver.class, "jdbc:orange:some:stuff", false);
0496:                ResourceInfo nonJta2 = addDataSource("OrangeTwo",
0497:                        OrangeDriver.class, "jdbc:orange:some:stuff", false);
0498:
0499:                assertSame(nonJta1, resources.get(0));
0500:                assertSame(nonJta2, resources.get(1));
0501:
0502:                try {
0503:                    addPersistenceUnit("orange-unit", "OrangeOne", "OrangeTwo");
0504:                    fail("Configuration should be rejected");
0505:                } catch (OpenEJBException e) {
0506:                    // pass
0507:                }
0508:            }
0509:
0510:            /**
0511:             * Existing data source "OrangeOne", not jta managed
0512:             * Existing data source "OrangeTwo", not jta managed
0513:             *
0514:             * Persistence xml like so:
0515:             *
0516:             * <persistence-unit name="orange-unit">
0517:             *      <jta-data-source>OrangeOne</jta-data-source>
0518:             *      <non-jta-data-source>OrangeTwo</non-jta-data-source>
0519:             * </persistence-unit>
0520:             *
0521:             * This configuration should be rejected
0522:             *
0523:             * @throws Exception
0524:             */
0525:            public void testNonJtaRefToContrarilyConfiguredDataSource()
0526:                    throws Exception {
0527:
0528:                ResourceInfo jta1 = addDataSource("OrangeOne",
0529:                        OrangeDriver.class, "jdbc:orange:some:stuff", true);
0530:                ResourceInfo jta2 = addDataSource("OrangeTwo",
0531:                        OrangeDriver.class, "jdbc:orange:some:stuff", true);
0532:
0533:                assertSame(jta1, resources.get(0));
0534:                assertSame(jta2, resources.get(1));
0535:
0536:                try {
0537:                    addPersistenceUnit("orange-unit", "OrangeOne", "OrangeTwo");
0538:                    fail("Configuration should be rejected");
0539:                } catch (OpenEJBException e) {
0540:                    // pass
0541:                }
0542:            }
0543:
0544:            /**
0545:             * Existing data source "OrangeOne", not jta managed
0546:             *
0547:             * Persistence xml like so:
0548:             *
0549:             * <persistence-unit name="orange-unit">
0550:             *      <jta-data-source>OrangeOne</jta-data-source>
0551:             * </persistence-unit>
0552:             *
0553:             * This configuration should be rejected
0554:             *
0555:             * @throws Exception
0556:             */
0557:            public void testJtaRefToContrarilyConfiguredDataSource2()
0558:                    throws Exception {
0559:
0560:                ResourceInfo jta = addDataSource("OrangeOne",
0561:                        OrangeDriver.class, "jdbc:orange:some:stuff", false);
0562:
0563:                assertSame(jta, resources.get(0));
0564:
0565:                try {
0566:                    addPersistenceUnit("orange-unit", "OrangeOne", null);
0567:                    fail("Configuration should be rejected");
0568:                } catch (OpenEJBException e) {
0569:                    // pass
0570:                }
0571:            }
0572:
0573:            /**
0574:             * Existing data source "OrangeOne", not jta managed
0575:             *
0576:             * Persistence xml like so:
0577:             *
0578:             * <persistence-unit name="orange-unit">
0579:             *      <non-jta-data-source>OrangeOne</non-jta-data-source>
0580:             * </persistence-unit>
0581:             *
0582:             * This configuration should be rejected
0583:             *
0584:             * @throws Exception
0585:             */
0586:            public void testNonJtaRefToContrarilyConfiguredDataSource2()
0587:                    throws Exception {
0588:
0589:                ResourceInfo jta = addDataSource("OrangeOne",
0590:                        OrangeDriver.class, "jdbc:orange:some:stuff", true);
0591:
0592:                assertSame(jta, resources.get(0));
0593:
0594:                try {
0595:                    addPersistenceUnit("orange-unit", null, "OrangeOne");
0596:                    fail("Configuration should be rejected");
0597:                } catch (OpenEJBException e) {
0598:                    // pass
0599:                }
0600:            }
0601:
0602:            // ---
0603:
0604:            /**
0605:             * Existing data source "Orange" not jta managed
0606:             *
0607:             * Persistence xml like so:
0608:             *
0609:             * <persistence-unit name="orange-unit">
0610:             *      <non-jta-data-source>Orange</non-jta-data-source>
0611:             * </persistence-unit>
0612:             *
0613:             * We should generate a <jta-data-source> based on
0614:             * the <non-jta-data-source>
0615:             *
0616:             * @throws Exception
0617:             */
0618:            public void testMissingJtaDataSource() throws Exception {
0619:
0620:                ResourceInfo supplied = addDataSource("Orange",
0621:                        OrangeDriver.class, "jdbc:orange:some:stuff", false);
0622:                assertSame(supplied, resources.get(0));
0623:
0624:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0625:                        "orange-unit", null, "Orange");
0626:
0627:                assertNotNull(unitInfo);
0628:
0629:                ResourceInfo generated = resources.get(1);
0630:
0631:                assertEquals(supplied.id, unitInfo.nonJtaDataSource);
0632:                assertEquals(generated.id, unitInfo.jtaDataSource);
0633:
0634:                assertNotNull(generated);
0635:                assertEquals(supplied.id + "Jta", generated.id);
0636:                assertEquals(supplied.service, generated.service);
0637:                assertEquals(supplied.className, generated.className);
0638:                assertEquals(supplied.properties.get("JdbcDriver"),
0639:                        generated.properties.get("JdbcDriver"));
0640:                assertEquals(supplied.properties.get("JdbcUrl"),
0641:                        generated.properties.get("JdbcUrl"));
0642:                assertEquals("true", generated.properties.get("JtaManaged"));
0643:            }
0644:
0645:            /**
0646:             * Existing data source "Orange" not jta managed
0647:             *
0648:             * Persistence xml like so:
0649:             *
0650:             * <persistence-unit name="orange-unit">
0651:             *      <non-jta-data-source>Orange</non-jta-data-source>
0652:             * </persistence-unit>
0653:             *
0654:             * We should generate a <jta-data-source> based on
0655:             * the <non-jta-data-source>
0656:             *
0657:             * @throws Exception
0658:             */
0659:            public void testMissingNonJtaDataSource() throws Exception {
0660:
0661:                ResourceInfo supplied = addDataSource("Orange",
0662:                        OrangeDriver.class, "jdbc:orange:some:stuff", true);
0663:                assertSame(supplied, resources.get(0));
0664:
0665:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0666:                        "orange-unit", "Orange", null);
0667:
0668:                assertNotNull(unitInfo);
0669:
0670:                ResourceInfo generated = resources.get(1);
0671:
0672:                assertEquals(supplied.id, unitInfo.jtaDataSource);
0673:                assertEquals(generated.id, unitInfo.nonJtaDataSource);
0674:
0675:                assertEquals(supplied.id + "NonJta", generated.id);
0676:                assertEquals(supplied.service, generated.service);
0677:                assertEquals(supplied.className, generated.className);
0678:                assertEquals(supplied.properties.get("JdbcDriver"),
0679:                        generated.properties.get("JdbcDriver"));
0680:                assertEquals(supplied.properties.get("JdbcUrl"),
0681:                        generated.properties.get("JdbcUrl"));
0682:                assertEquals("false", generated.properties.get("JtaManaged"));
0683:            }
0684:
0685:            // ---
0686:
0687:            /**
0688:             * Existing data source "Orange", not jta managed
0689:             * Existing data source "Lime", jta managed
0690:             *
0691:             * Persistence xml like so:
0692:             *
0693:             * <persistence-unit name="orange-unit">
0694:             *      <non-jta-data-source>Orange</non-jta-data-source>
0695:             * </persistence-unit>
0696:             *
0697:             * We should generate a <jta-data-source> based on
0698:             * the <non-jta-data-source>
0699:             *
0700:             * @throws Exception
0701:             */
0702:            public void testInvalidOptionsJta() throws Exception {
0703:
0704:                ResourceInfo supplied = addDataSource("Orange",
0705:                        OrangeDriver.class, "jdbc:orange:some:stuff", false);
0706:                ResourceInfo badMatch = addDataSource("Lime", LimeDriver.class,
0707:                        "jdbc:lime:some:stuff", true);
0708:
0709:                assertSame(supplied, resources.get(0));
0710:                assertSame(badMatch, resources.get(1));
0711:
0712:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0713:                        "orange-unit", null, "Orange");
0714:
0715:                assertNotNull(unitInfo);
0716:
0717:                ResourceInfo generated = resources.get(2);
0718:
0719:                assertEquals(generated.id, unitInfo.jtaDataSource);
0720:                assertEquals(supplied.id, unitInfo.nonJtaDataSource);
0721:
0722:                assertEquals(supplied.id + "Jta", generated.id);
0723:                assertEquals(supplied.service, generated.service);
0724:                assertEquals(supplied.className, generated.className);
0725:                assertEquals(supplied.properties.get("JdbcDriver"),
0726:                        generated.properties.get("JdbcDriver"));
0727:                assertEquals(supplied.properties.get("JdbcUrl"),
0728:                        generated.properties.get("JdbcUrl"));
0729:                assertEquals("true", generated.properties.get("JtaManaged"));
0730:            }
0731:
0732:            /**
0733:             * Existing data source "Orange", not jta managed
0734:             * Existing data source "Lime", jta managed
0735:             *
0736:             * Persistence xml like so:
0737:             *
0738:             * <persistence-unit name="orange-unit">
0739:             *      <jta-data-source>Orange</jta-data-source>
0740:             * </persistence-unit>
0741:             *
0742:             * We should generate a <non-jta-data-source> based on
0743:             * the <jta-data-source>
0744:             *
0745:             * @throws Exception
0746:             */
0747:            public void testInvalidOptionsNonJta() throws Exception {
0748:
0749:                ResourceInfo supplied = addDataSource("Orange",
0750:                        OrangeDriver.class, "jdbc:orange:some:stuff", true);
0751:                ResourceInfo badMatch = addDataSource("Lime", LimeDriver.class,
0752:                        "jdbc:lime:some:stuff", false);
0753:
0754:                assertSame(supplied, resources.get(0));
0755:                assertSame(badMatch, resources.get(1));
0756:
0757:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0758:                        "orange-unit", "Orange", null);
0759:
0760:                assertNotNull(unitInfo);
0761:
0762:                ResourceInfo generated = resources.get(2);
0763:
0764:                assertEquals(supplied.id, unitInfo.jtaDataSource);
0765:                assertEquals(generated.id, unitInfo.nonJtaDataSource);
0766:
0767:                assertEquals(supplied.id + "NonJta", generated.id);
0768:                assertEquals(supplied.service, generated.service);
0769:                assertEquals(supplied.className, generated.className);
0770:                assertEquals(supplied.properties.get("JdbcDriver"),
0771:                        generated.properties.get("JdbcDriver"));
0772:                assertEquals(supplied.properties.get("JdbcUrl"),
0773:                        generated.properties.get("JdbcUrl"));
0774:                assertEquals("false", generated.properties.get("JtaManaged"));
0775:            }
0776:
0777:            // ---
0778:
0779:            /**
0780:             * Existing data source "Orange", not jta managed
0781:             * Existing data source "Lime", jta managed
0782:             *
0783:             * Persistence xml like so:
0784:             *
0785:             * <persistence-unit name="orange-unit">
0786:             *      <non-jta-data-source>Orange</non-jta-data-source>
0787:             * </persistence-unit>
0788:             *
0789:             * We should select the <jta-data-source> based on
0790:             * the closest match to the <non-jta-data-source>
0791:             *
0792:             * @throws Exception
0793:             */
0794:            public void testPossiblyAmbiguousJtaOptions() throws Exception {
0795:
0796:                ResourceInfo supplied = addDataSource("Orange",
0797:                        OrangeDriver.class, "jdbc:orange:some:stuff", false);
0798:                ResourceInfo badMatch = addDataSource("Lime", LimeDriver.class,
0799:                        "jdbc:lime:some:stuff", true);
0800:                ResourceInfo goodMatch = addDataSource("JtaOrange",
0801:                        OrangeDriver.class, "jdbc:orange:some:stuff", true);
0802:
0803:                assertSame(supplied, resources.get(0));
0804:                assertSame(badMatch, resources.get(1));
0805:                assertSame(goodMatch, resources.get(2));
0806:
0807:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0808:                        "orange-unit", null, "Orange");
0809:
0810:                assertNotNull(unitInfo);
0811:
0812:                assertEquals(goodMatch.id, unitInfo.jtaDataSource);
0813:                assertEquals(supplied.id, unitInfo.nonJtaDataSource);
0814:            }
0815:
0816:            /**
0817:             * Existing data source "Orange", not jta managed
0818:             * Existing data source "Lime", jta managed
0819:             *
0820:             * Persistence xml like so:
0821:             *
0822:             * <persistence-unit name="orange-unit">
0823:             *      <jta-data-source>Orange</jta-data-source>
0824:             * </persistence-unit>
0825:             *
0826:             * We should select the <non-jta-data-source> based on
0827:             * the closest match to the <jta-data-source>
0828:             *
0829:             * @throws Exception
0830:             */
0831:            public void testPossiblyAmbiguousNonJtaOptions() throws Exception {
0832:
0833:                ResourceInfo supplied = addDataSource("Orange",
0834:                        OrangeDriver.class, "jdbc:orange:some:stuff", true);
0835:                ResourceInfo badMatch = addDataSource("Lime", LimeDriver.class,
0836:                        "jdbc:lime:some:stuff", false);
0837:                ResourceInfo goodMatch = addDataSource("OrangeUnmanaged",
0838:                        OrangeDriver.class, "jdbc:orange:some:stuff", false);
0839:
0840:                assertSame(supplied, resources.get(0));
0841:                assertSame(badMatch, resources.get(1));
0842:                assertSame(goodMatch, resources.get(2));
0843:
0844:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0845:                        "orange-unit", "Orange", null);
0846:
0847:                assertNotNull(unitInfo);
0848:
0849:                assertEquals(supplied.id, unitInfo.jtaDataSource);
0850:                assertEquals(goodMatch.id, unitInfo.nonJtaDataSource);
0851:            }
0852:
0853:            // ---
0854:
0855:            /**
0856:             * Existing data source "Orange", not jta managed
0857:             *
0858:             * Persistence xml like so:
0859:             *
0860:             * <persistence-unit name="orange-unit">
0861:             * </persistence-unit>
0862:             *
0863:             * The <non-jta-data-source> should be auto linked
0864:             * to the Orange data source
0865:             *
0866:             * We should generate a <jta-data-source> based on
0867:             * the <non-jta-data-source>
0868:             *
0869:             * @throws Exception
0870:             */
0871:            public void testEmptyUnitOneAvailableNonJtaDataSource()
0872:                    throws Exception {
0873:
0874:                ResourceInfo supplied = addDataSource("Orange",
0875:                        OrangeDriver.class, "jdbc:orange:some:stuff", false);
0876:
0877:                assertSame(supplied, resources.get(0));
0878:
0879:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0880:                        "orange-unit", null, null);
0881:
0882:                assertNotNull(unitInfo);
0883:
0884:                ResourceInfo generated = resources.get(1);
0885:
0886:                assertEquals(generated.id, unitInfo.jtaDataSource);
0887:                assertEquals(supplied.id, unitInfo.nonJtaDataSource);
0888:
0889:                assertEquals(supplied.id + "Jta", generated.id);
0890:                assertEquals(supplied.service, generated.service);
0891:                assertEquals(supplied.className, generated.className);
0892:                assertEquals(supplied.properties.get("JdbcDriver"),
0893:                        generated.properties.get("JdbcDriver"));
0894:                assertEquals(supplied.properties.get("JdbcUrl"),
0895:                        generated.properties.get("JdbcUrl"));
0896:                assertEquals("true", generated.properties.get("JtaManaged"));
0897:            }
0898:
0899:            /**
0900:             * Existing data source "Orange", jta managed
0901:             *
0902:             * Persistence xml like so:
0903:             *
0904:             * <persistence-unit name="orange-unit">
0905:             * </persistence-unit>
0906:             *
0907:             * The <jta-data-source> should be auto linked
0908:             * to the Orange data source
0909:             *
0910:             * We should generate a <non-jta-data-source> based on
0911:             * the <jta-data-source>
0912:             *
0913:             * @throws Exception
0914:             */
0915:            public void testEmptyUnitOneAvailableJtaDataSource()
0916:                    throws Exception {
0917:
0918:                ResourceInfo supplied = addDataSource("Orange",
0919:                        OrangeDriver.class, "jdbc:orange:some:stuff", true);
0920:
0921:                assertSame(supplied, resources.get(0));
0922:
0923:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0924:                        "orange-unit", null, null);
0925:
0926:                assertNotNull(unitInfo);
0927:
0928:                ResourceInfo generated = resources.get(1);
0929:
0930:                assertEquals(supplied.id, unitInfo.jtaDataSource);
0931:                assertEquals(generated.id, unitInfo.nonJtaDataSource);
0932:
0933:                assertEquals(supplied.id + "NonJta", generated.id);
0934:                assertEquals(supplied.service, generated.service);
0935:                assertEquals(supplied.className, generated.className);
0936:                assertEquals(supplied.properties.get("JdbcDriver"),
0937:                        generated.properties.get("JdbcDriver"));
0938:                assertEquals(supplied.properties.get("JdbcUrl"),
0939:                        generated.properties.get("JdbcUrl"));
0940:                assertEquals("false", generated.properties.get("JtaManaged"));
0941:            }
0942:
0943:            // ---
0944:
0945:            /**
0946:             * Existing data source "Orange", not jta managed
0947:             *
0948:             * Persistence xml like so:
0949:             *
0950:             * <persistence-unit name="orange-unit">
0951:             * </persistence-unit>
0952:             *
0953:             * The <non-jta-data-source> should be auto linked
0954:             * to the Orange data source
0955:             *
0956:             * We should generate a <jta-data-source> based on
0957:             * the <non-jta-data-source>
0958:             *
0959:             * @throws Exception
0960:             */
0961:            public void testEmptyUnitNoAvailableDataSources() throws Exception {
0962:
0963:                assertEquals(0, resources.size());
0964:
0965:                PersistenceUnitInfo unitInfo = addPersistenceUnit(
0966:                        "orange-unit", null, null);
0967:
0968:                assertNotNull(unitInfo);
0969:
0970:                ResourceInfo jta = resources.get(0);
0971:                ResourceInfo nonJta = resources.get(1);
0972:
0973:                assertEquals("Default JDBC Database", jta.id);
0974:                assertEquals("Default Unmanaged JDBC Database", nonJta.id);
0975:
0976:                assertEquals(jta.id, unitInfo.jtaDataSource);
0977:                assertEquals(nonJta.id, unitInfo.nonJtaDataSource);
0978:            }
0979:
0980:            // --------------------------------------------------------------------------------------------
0981:            //  Convenience methods
0982:            // --------------------------------------------------------------------------------------------
0983:
0984:            private PersistenceUnitInfo addPersistenceUnit(String unitName,
0985:                    String jtaDataSource, String nonJtaDataSource)
0986:                    throws OpenEJBException, IOException, NamingException {
0987:                PersistenceUnit unit = new PersistenceUnit(unitName);
0988:                unit.setJtaDataSource(jtaDataSource);
0989:                unit.setNonJtaDataSource(nonJtaDataSource);
0990:
0991:                AppModule app = new AppModule(this .getClass().getClassLoader(),
0992:                        unitName + "-app");
0993:                app.getPersistenceModules().add(
0994:                        new PersistenceModule("root", new Persistence(unit)));
0995:
0996:                // Create app
0997:
0998:                AppInfo appInfo = config.configureApplication(app);
0999:                assembler.createApplication(appInfo);
1000:
1001:                // Check results
1002:
1003:                return appInfo.persistenceUnits.get(0);
1004:            }
1005:
1006:            private ResourceInfo addDataSource(String id, Class driver,
1007:                    String url, Boolean managed) throws OpenEJBException {
1008:                Resource resource = new Resource(id, "DataSource");
1009:                resource.getProperties().put("JdbcDriver", driver.getName());
1010:                resource.getProperties().put("JdbcUrl", url);
1011:                resource.getProperties().put("JtaManaged", managed + "");
1012:
1013:                ResourceInfo resourceInfo = config.configureService(resource,
1014:                        ResourceInfo.class);
1015:
1016:                if (managed == null) {
1017:                    // Strip out the JtaManaged property so we can mimic
1018:                    // datasources that we don't control
1019:                    resourceInfo.properties.remove("JtaManaged");
1020:                }
1021:
1022:                assembler.createResource(resourceInfo);
1023:                return resourceInfo;
1024:            }
1025:
1026:            public static class Driver implements  java.sql.Driver {
1027:                public boolean acceptsURL(String url) throws SQLException {
1028:                    return false;
1029:                }
1030:
1031:                public Connection connect(String url, Properties info)
1032:                        throws SQLException {
1033:                    return null;
1034:                }
1035:
1036:                public int getMajorVersion() {
1037:                    return 0;
1038:                }
1039:
1040:                public int getMinorVersion() {
1041:                    return 0;
1042:                }
1043:
1044:                public DriverPropertyInfo[] getPropertyInfo(String url,
1045:                        Properties info) throws SQLException {
1046:                    return new DriverPropertyInfo[0];
1047:                }
1048:
1049:                public boolean jdbcCompliant() {
1050:                    return false;
1051:                }
1052:            }
1053:
1054:            public static class OrangeDriver extends Driver {
1055:            }
1056:
1057:            public static class LimeDriver extends Driver {
1058:            }
1059:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.