1 16 17 18 package org.apache.naming.config; 19 20 import java.sql.Connection ; 21 import java.sql.ResultSet ; 22 import java.sql.Statement ; 23 24 import javax.naming.Context ; 25 import javax.naming.InitialContext ; 26 import javax.sql.DataSource ; 27 28 import junit.framework.TestCase; 29 30 37 public class XmlConfiguratorTest extends TestCase 38 { 39 public XmlConfiguratorTest(String name) { 40 super(name); 41 } 42 43 46 protected void setUp() throws Exception { 47 super.setUp(); 48 } 49 50 53 protected void tearDown() throws Exception { 54 super.tearDown(); 55 XmlConfigurator.destroyInitialContext(); 56 } 57 58 63 protected static String DEFAULT_ROOT="java:comp/env"; 64 65 69 protected static String ALT_ROOT="alt/root/context"; 70 71 75 public void testEnvironment() throws Exception { 76 XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi.xml")); 77 checkEnvironment(DEFAULT_ROOT); 78 XmlConfigurator.destroyInitialContext(); 79 XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi2.xml")); 80 checkEnvironment(ALT_ROOT); 81 } 82 83 protected void checkEnvironment(String root) throws Exception { 84 Context ctx = new InitialContext (); 85 Context env = (Context ) ctx.lookup(root); 86 String host = (String ) env.lookup("config/host"); 87 Integer port = (Integer ) env.lookup("config/port"); 88 89 assertEquals("Check host", "www.apache.org", host); 90 assertEquals("Check port", new Integer (80), port); 91 92 Boolean trueBool = (Boolean ) env.lookup("config/mytruebool"); 93 Boolean falseBool = (Boolean ) env.lookup("config/myfalsebool"); 94 95 assertTrue("Check true boolean value", trueBool.booleanValue()); 96 assertTrue("Check false boolean value", !falseBool.booleanValue()); 97 98 trueBool = (Boolean ) ctx.lookup(root + "/config/mytruebool"); 99 assertTrue("Check true boolean value -- root lookup", trueBool.booleanValue()); 100 101 } 102 103 107 public void testDuplicateSubcontextName() throws Exception { 108 XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi.xml")); 109 checkDuplicateSubcontextName(DEFAULT_ROOT); 110 } 111 112 protected void checkDuplicateSubcontextName(String root) throws Exception { 113 Context ctx = new InitialContext (); 114 Context env = (Context ) ctx.lookup(root); 115 String user = (String ) env.lookup("jdbc/config/pool/user"); 116 assertEquals("Check user", "dbuser", user); 117 user = (String ) ctx.lookup(root + "/jdbc/config/pool/user"); 118 assertEquals("Check user -- root lookup", "dbuser", user); 119 } 120 121 126 public void testJdbc() throws Exception { 127 XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi.xml")); 128 checkJdbc(DEFAULT_ROOT); 129 XmlConfigurator.destroyInitialContext(); 130 XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi2.xml")); 131 checkJdbc(ALT_ROOT); 132 } 133 134 protected void checkJdbc(String root) throws Exception { 135 Context ctx = new InitialContext (); 136 Context env = (Context ) ctx.lookup(root); 137 DataSource ds = (DataSource ) env.lookup("jdbc/pool"); 138 Connection con = null; 139 Statement stat = null; 140 ResultSet rs = null; 141 try { 142 con = ds.getConnection(); 143 stat = con.createStatement(); 144 stat.executeUpdate("DROP TABLE DUAL IF EXISTS"); 145 stat.executeUpdate("CREATE TABLE DUAL(value char(50))"); 146 stat.executeUpdate("INSERT INTO DUAL VALUES(1)"); 147 rs = stat.executeQuery("SELECT * FROM DUAL"); 148 while (rs.next()) { 149 assertEquals("Check you get back what you put into the DB", 1, rs.getInt(1)); 150 } 151 } 152 finally { 153 if (rs != null) { 154 rs.close(); 155 } 156 if (stat != null) { 157 stat.close(); 158 } 159 if (con != null) { 160 con.close(); 161 } 162 } 163 } 164 } 165 166 | Popular Tags |