KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > domain > JDBCDomainListTest


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20
21
22 package org.apache.james.domain;
23
24 import java.net.InetAddress JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26 import java.sql.Connection JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
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 JavaDoc repos = "db://maildb/";
50     private String JavaDoc table = "costumTable";
51     private DataSourceSelector dataSource;
52     private DataSourceComponent data;
53     
54     public void setUp() throws Exception JavaDoc {
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 JavaDoc {
62         sqlQuery("drop table " + table);
63     }
64
65     private boolean sqlQuery(String JavaDoc query){
66         Connection JavaDoc conn = null;
67         PreparedStatement JavaDoc mappingStmt = null;
68
69         try {
70             conn = data.getConnection();
71             mappingStmt = conn.prepareStatement(query);
72
73             ResultSet JavaDoc mappingRS = null;
74             try {
75              
76                 if(mappingStmt.executeUpdate() >0) {
77                     return true;
78                 }
79             } finally {
80                 theJDBCUtil.closeJDBCResultSet(mappingRS);
81             }
82         } catch (SQLException JavaDoc e) {
83             System.err.println(e.getMessage());
84         } finally {
85             theJDBCUtil.closeJDBCStatement(mappingStmt);
86             theJDBCUtil.closeJDBCConnection(conn);
87         }
88         return false;
89     }
90     
91     /**
92      * The JDBCUtil helper class
93      */

94     private final JDBCUtil theJDBCUtil = new JDBCUtil() {
95         protected void delegatedLog(String JavaDoc logString) {}
96     };
97     
98     private Configuration setUpConfiguration(String JavaDoc 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 JavaDoc hostName) {
112         DNSServer dns = new AbstractDNSServer() {
113             public String JavaDoc getHostName(InetAddress JavaDoc inet) {
114                 return hostName;
115             }
116             
117             public InetAddress JavaDoc[] getAllByName(String JavaDoc name) throws UnknownHostException JavaDoc {
118                 return new InetAddress JavaDoc[] { InetAddress.getByName("127.0.0.1")};
119             }
120             
121             public InetAddress JavaDoc getLocalHost() throws UnknownHostException JavaDoc {
122             return InetAddress.getLocalHost();
123             }
124         };
125         return dns;
126     }
127     
128     private MockServiceManager setUpServiceManager(DNSServer dns) throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
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