1 6 package org.logicalcobwebs.proxool.configuration; 7 8 import org.logicalcobwebs.proxool.AbstractProxoolTest; 9 import org.logicalcobwebs.proxool.ProxoolConstants; 10 import org.logicalcobwebs.proxool.ProxoolException; 11 import org.logicalcobwebs.proxool.ProxoolFacade; 12 import org.logicalcobwebs.proxool.TestHelper; 13 import org.xml.sax.InputSource ; 14 15 import java.io.FileInputStream ; 16 import java.io.FileNotFoundException ; 17 import java.sql.DriverManager ; 18 import java.sql.SQLException ; 19 20 29 public class JAXPConfiguratorTest extends AbstractProxoolTest { 30 31 34 public JAXPConfiguratorTest(String name) { 35 super(name); 36 } 37 38 45 public void testNoNamspaces() throws ProxoolException, SQLException { 46 final String xmlFile = "src/java-test/org/logicalcobwebs/proxool/configuration/test-no-ns.xml"; 47 JAXPConfigurator.configure(xmlFile, false); 48 try { 49 assertNotNull("2nd (deeply nested) pool was not configured.", ProxoolFacade.getConnectionPoolDefinition("xml-test-2")); 50 } catch (ProxoolException e) { 51 fail("2nd (deeply nested) pool was not configured."); 52 } 53 TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("xml-test")); 54 TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("xml-test-2")); 55 } 56 57 64 public void testWithNamspaces() throws ProxoolException, SQLException { 65 final String xmlFile = "src/java-test/org/logicalcobwebs/proxool/configuration/test-ns.xml"; 66 JAXPConfigurator.configure(xmlFile, false); 67 try { 68 assertNotNull("2nd (deeply nested) pool was not configured.", ProxoolFacade.getConnectionPoolDefinition("xml-test-ns-2")); 69 } catch (ProxoolException e) { 70 fail("2nd (deeply nested) pool was not configured."); 71 } 72 TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("xml-test-ns")); 73 TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("xml-test-ns-2")); 74 } 75 76 84 public void testValidiation() throws ProxoolException, SQLException , FileNotFoundException { 85 final String xmlFile = "src/java-test/org/logicalcobwebs/proxool/configuration/test-valid.xml"; 86 final InputSource inputSource = new InputSource (new FileInputStream (xmlFile)); 87 inputSource.setSystemId(getWorkingDirectoryUrl()); 88 JAXPConfigurator.configure(inputSource, true); 89 try { 90 assertNotNull("2nd (deeply nested) pool was not configured.", ProxoolFacade.getConnectionPoolDefinition("xml-test-validating-2")); 91 } catch (ProxoolException e) { 92 fail("2nd (deeply nested) pool was not configured."); 93 } 94 TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("xml-test-validating")); 95 TestHelper.equalsCompleteAlternativeProperties(ProxoolFacade.getConnectionPoolDefinition("xml-test-validating-2")); 96 } 97 98 104 public void testWithAlias() throws ProxoolException, SQLException , FileNotFoundException { 105 final String xmlFile = "src/java-test/org/logicalcobwebs/proxool/configuration/test-valid.xml"; 106 final InputSource inputSource = new InputSource (new FileInputStream (xmlFile)); 107 inputSource.setSystemId(getWorkingDirectoryUrl()); 108 JAXPConfigurator.configure(inputSource, true); 109 110 final String alias = "xml-test-validating"; 111 DriverManager.getConnection(ProxoolConstants.PROXOOL + "." + alias).close(); 112 } 113 114 119 public void testValidiationFailure() throws SQLException , FileNotFoundException , ProxoolException { 120 final String xmlFile = "src/java-test/org/logicalcobwebs/proxool/configuration/test-not-valid.xml"; 121 final InputSource inputSource = new InputSource (new FileInputStream (xmlFile)); 122 inputSource.setSystemId(getWorkingDirectoryUrl()); 123 boolean failure = false; 124 try { 125 JAXPConfigurator.configure(inputSource, true); 126 } catch (ProxoolException e) { 127 failure = true; 128 } 129 assertTrue("Configuration did not fail on unvalid xml document.", failure); 130 } 131 132 private static String getWorkingDirectoryUrl() { 133 String userDir = System.getProperty("user.dir"); 134 String toReplace = "\\"; 135 String replaceWith = "/"; 136 int pos = 0; 137 if (!toReplace.equals(replaceWith)) { 138 while (true) { 139 pos = userDir.indexOf(toReplace, pos); 140 if (pos == -1) { 141 break; 142 } 143 userDir = userDir.substring(0, pos) + replaceWith + userDir.substring(pos + toReplace.length()); 144 pos += replaceWith.length(); 145 } 146 } 147 if (!userDir.startsWith("/")) { 148 userDir = "/" + userDir; 149 } 150 if (!userDir.endsWith("/")) { 151 userDir = userDir + "/"; 152 } 153 return "file://" + userDir; 154 } 155 156 } 157 158 213 | Popular Tags |