KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > basic > TestJavaxSqlDatasource


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27 package org.objectweb.speedo.runtime.basic;
28
29 import org.objectweb.speedo.api.SpeedoProperties;
30 import org.objectweb.util.monolog.api.BasicLevel;
31 import org.objectweb.jorm.mapper.rdb.lib.ConnectionSpecJDBC;
32
33 import javax.naming.InitialContext JavaDoc;
34 import javax.sql.DataSource JavaDoc;
35 import java.sql.Driver JavaDoc;
36 import java.sql.DriverManager JavaDoc;
37 import java.sql.SQLException JavaDoc;
38 import java.util.Properties JavaDoc;
39
40 import junit.framework.Assert;
41
42 /**
43  * @author S.Chassande-Barrioz
44  */

45 public class TestJavaxSqlDatasource extends TestBasicA {
46
47     static {
48         System.setProperty("java.naming.factory.initial",
49             "org.objectweb.speedo.runtime.basic.SpeedoInitialNamingFactory");
50     }
51
52     public final static String JavaDoc CF_NAME = "ds";
53     public TestJavaxSqlDatasource(String JavaDoc s) {
54         super(s);
55     }
56
57
58     public Properties getPMFProperties() {
59         Properties p = super.getPMFProperties();
60         p.setProperty("javax.jdo.option.ConnectionFactoryName", CF_NAME);
61         if (pmf == null) {
62             String JavaDoc dcn = getConnectionDriverNameProperty(p);
63             String JavaDoc user = p.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME);
64             String JavaDoc pass = p.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, "");
65             String JavaDoc url = p.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL);
66             if (user == null) {
67                 System.out.println(p);
68                 fail("No user name specified");
69             }
70             if (url == null) {
71                 System.out.println(p);
72                 fail("No database url specified");
73             }
74             try {
75                 DriverManager.registerDriver((Driver JavaDoc)
76                             Class.forName(dcn).newInstance());
77                 Object JavaDoc cf = new ConnectionSpecJDBC(url, dcn, user, pass);
78                 InitialContext JavaDoc ic = new InitialContext JavaDoc();
79                 ic.rebind(CF_NAME, cf);
80             } catch (Exception JavaDoc e) {
81                 e.printStackTrace();
82                 fail(e.getMessage());
83             }
84         }
85         return p;
86     }
87
88     public void testGetSqlConnection() {
89         logger.log(BasicLevel.DEBUG, "testGetSqlConnection");
90         Object JavaDoc cf = pmf.getConnectionFactory();
91         Assert.assertTrue("The connection factory is not a DataSource",
92             cf instanceof DataSource JavaDoc);
93         try {
94             ((DataSource JavaDoc) cf).getConnection().close();
95         } catch (SQLException JavaDoc e) {
96             fail("Impossible to get a connection on the factory " + cf);
97         }
98     }
99 }
Popular Tags