1 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 ; 34 import javax.sql.DataSource ; 35 import java.sql.Driver ; 36 import java.sql.DriverManager ; 37 import java.sql.SQLException ; 38 import java.util.Properties ; 39 40 import junit.framework.Assert; 41 42 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 CF_NAME = "ds"; 53 public TestJavaxSqlDatasource(String 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 dcn = getConnectionDriverNameProperty(p); 63 String user = p.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME); 64 String pass = p.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, ""); 65 String 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 ) 76 Class.forName(dcn).newInstance()); 77 Object cf = new ConnectionSpecJDBC(url, dcn, user, pass); 78 InitialContext ic = new InitialContext (); 79 ic.rebind(CF_NAME, cf); 80 } catch (Exception 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 cf = pmf.getConnectionFactory(); 91 Assert.assertTrue("The connection factory is not a DataSource", 92 cf instanceof DataSource ); 93 try { 94 ((DataSource ) cf).getConnection().close(); 95 } catch (SQLException e) { 96 fail("Impossible to get a connection on the factory " + cf); 97 } 98 } 99 } | Popular Tags |