1 19 20 21 22 package org.apache.james.domain; 23 24 import java.net.InetAddress ; 25 import java.net.UnknownHostException ; 26 import java.sql.Connection ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 31 import junit.framework.TestCase; 32 33 import org.apache.avalon.cornerstone.services.datasources.DataSourceSelector; 34 import org.apache.avalon.excalibur.datasource.DataSourceComponent; 35 import org.apache.avalon.framework.configuration.Configuration; 36 import org.apache.avalon.framework.configuration.ConfigurationException; 37 import org.apache.avalon.framework.configuration.DefaultConfiguration; 38 import org.apache.avalon.framework.container.ContainerUtil; 39 import org.apache.james.services.AbstractDNSServer; 40 import org.apache.james.services.DNSServer; 41 import org.apache.james.services.FileSystem; 42 import org.apache.james.test.mock.avalon.MockLogger; 43 import org.apache.james.test.mock.avalon.MockServiceManager; 44 import org.apache.james.test.mock.james.MockFileSystem; 45 import org.apache.james.test.util.Util; 46 import org.apache.james.util.JDBCUtil; 47 48 public class JDBCDomainListTest extends TestCase { 49 private String repos = "db://maildb/"; 50 private String table = "costumTable"; 51 private DataSourceSelector dataSource; 52 private DataSourceComponent data; 53 54 public void setUp() throws Exception { 55 dataSource = Util.getDataSourceSelector(); 56 data = (DataSourceComponent) dataSource.select("maildb"); 57 58 sqlQuery("create table " + table + " (domain VARCHAR (255))"); 59 } 60 61 public void tearDown() throws Exception { 62 sqlQuery("drop table " + table); 63 } 64 65 private boolean sqlQuery(String query){ 66 Connection conn = null; 67 PreparedStatement mappingStmt = null; 68 69 try { 70 conn = data.getConnection(); 71 mappingStmt = conn.prepareStatement(query); 72 73 ResultSet mappingRS = null; 74 try { 75 76 if(mappingStmt.executeUpdate() >0) { 77 return true; 78 } 79 } finally { 80 theJDBCUtil.closeJDBCResultSet(mappingRS); 81 } 82 } catch (SQLException e) { 83 System.err.println(e.getMessage()); 84 } finally { 85 theJDBCUtil.closeJDBCStatement(mappingStmt); 86 theJDBCUtil.closeJDBCConnection(conn); 87 } 88 return false; 89 } 90 91 94 private final JDBCUtil theJDBCUtil = new JDBCUtil() { 95 protected void delegatedLog(String logString) {} 96 }; 97 98 private Configuration setUpConfiguration(String url) { 99 DefaultConfiguration configuration = new DefaultConfiguration("test"); 100 DefaultConfiguration reposConf = new DefaultConfiguration("repositoryPath"); 101 reposConf.setValue(url); 102 configuration.addChild(reposConf); 103 104 DefaultConfiguration sqlConf = new DefaultConfiguration("sqlFile"); 105 sqlConf.setValue("file://conf/sqlResources.xml"); 106 configuration.addChild(sqlConf); 107 108 return configuration; 109 } 110 111 private DNSServer setUpDNSServer(final String hostName) { 112 DNSServer dns = new AbstractDNSServer() { 113 public String getHostName(InetAddress inet) { 114 return hostName; 115 } 116 117 public InetAddress [] getAllByName(String name) throws UnknownHostException { 118 return new InetAddress [] { InetAddress.getByName("127.0.0.1")}; 119 } 120 121 public InetAddress getLocalHost() throws UnknownHostException { 122 return InetAddress.getLocalHost(); 123 } 124 }; 125 return dns; 126 } 127 128 private MockServiceManager setUpServiceManager(DNSServer dns) throws Exception { 129 MockServiceManager service = new MockServiceManager(); 130 service.put(DNSServer.ROLE, dns); 131 service.put(FileSystem.ROLE, new MockFileSystem()); 132 service.put(DataSourceSelector.ROLE, dataSource); 133 return service; 134 } 135 136 public void testAddRemoveGetDomains() throws Exception { 137 138 139 JDBCDomainList dom = new JDBCDomainList(); 140 ContainerUtil.enableLogging(dom,new MockLogger()); 141 dom.service(setUpServiceManager(setUpDNSServer("localhost"))); 142 dom.configure(setUpConfiguration(repos + table)); 143 dom.initialize(); 144 dom.addDomain("domain1."); 145 146 assertEquals("two domain found",dom.getDomains().size(),2); 147 148 dom.removeDomain("domain1."); 149 assertNull("two domain found",dom.getDomains()); 150 151 } 152 153 154 public void testThrowConfigurationException() throws Exception { 155 boolean exception = false; 156 boolean exception2 = false; 157 JDBCDomainList dom = new JDBCDomainList(); 158 ContainerUtil.enableLogging(dom,new MockLogger()); 159 dom.service(setUpServiceManager(setUpDNSServer("localhost"))); 160 try { 161 dom.configure(new DefaultConfiguration("invalid")); 162 dom.initialize(); 163 } catch (ConfigurationException e) { 164 exception = true; 165 } 166 167 assertTrue("Exception thrown",exception); 168 169 try { 170 dom.configure(setUpConfiguration(null)); 171 } catch (ConfigurationException e) { 172 exception2 = true; 173 } 174 175 assertTrue("Exception thrown",exception2); 176 } 177 } 178 | Popular Tags |