james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e...@apache.org
Subject svn commit: r1153881 - in /james/server/trunk: data-library/src/test/java/org/apache/james/domainlist/ data-library/src/test/java/org/apache/james/domainlist/lib/ jpa/src/test/java/org/apache/james/domainlist/jpa/
Date Thu, 04 Aug 2011 13:32:38 GMT
Author: eric
Date: Thu Aug  4 13:32:38 2011
New Revision: 1153881

URL: http://svn.apache.org/viewvc?rev=1153881&view=rev
Log:
Factor JPADomainListTest to AbstractDomainListTest (JAMES-1293)

Added:
    james/server/trunk/data-library/src/test/java/org/apache/james/domainlist/
    james/server/trunk/data-library/src/test/java/org/apache/james/domainlist/lib/
    james/server/trunk/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListTest.java
Modified:
    james/server/trunk/jpa/src/test/java/org/apache/james/domainlist/jpa/JPADomainListTest.java

Added: james/server/trunk/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListTest.java?rev=1153881&view=auto
==============================================================================
--- james/server/trunk/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListTest.java
(added)
+++ james/server/trunk/data-library/src/test/java/org/apache/james/domainlist/lib/AbstractDomainListTest.java
Thu Aug  4 13:32:38 2011
@@ -0,0 +1,148 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.domainlist.lib;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import junit.framework.TestCase;
+
+import org.apache.james.dnsservice.api.DNSService;
+import org.apache.james.dnsservice.api.mock.MockDNSService;
+import org.apache.james.domainlist.api.DomainList;
+import org.apache.james.domainlist.api.DomainListException;
+
+/**
+ * Test the implementation of the DomainList.
+ */
+public abstract class AbstractDomainListTest extends TestCase {
+
+    // Domains we will play with.
+    private final String DOMAIN_1 = "domain1.tld";
+    private final String DOMAIN_2 = "domain2.tld";
+    private final String DOMAIN_3 = "domain3.tld";
+    private final String DOMAIN_4 = "domain4.tld";
+    private final String DOMAIN_5 = "domain5.tld";
+
+    /**
+     * The JPA DomainList service.
+     */
+    private DomainList domainList;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        domainList = createDomainList();
+        deleteAll();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    /**
+     * Add 3 domains and list them.
+     * 
+     * @throws DomainListException
+     */
+    public void createListDomains() throws DomainListException {
+        domainList.addDomain(DOMAIN_3);
+        domainList.addDomain(DOMAIN_4);
+        domainList.addDomain(DOMAIN_5);
+        assertEquals(3, domainList.getDomains().length);
+    }
+
+    /**
+     * Add a domain and check it is present.
+     * 
+     * @throws DomainListException
+     */
+    public void testAddContainsDomain() throws DomainListException {
+        domainList.addDomain(DOMAIN_2);
+        domainList.containsDomain(DOMAIN_2);
+    }
+
+    /**
+     * Add and remove a domain, and check database is empty.
+     * 
+     * @throws DomainListException
+     */
+    public void testAddRemoveContainsSameDomain() throws DomainListException {
+        domainList.addDomain(DOMAIN_1);
+        domainList.removeDomain(DOMAIN_1);
+        assertEquals(null, domainList.getDomains());
+    }
+
+    /**
+     * Add a domain and remove another domain, and check first domain is still
+     * present.
+     * 
+     * @throws DomainListException
+     */
+    public void testAddRemoveContainsDifferentDomain() throws DomainListException {
+        domainList.addDomain(DOMAIN_1);
+        domainList.removeDomain(DOMAIN_2);
+        assertEquals(1, domainList.getDomains().length);
+        assertEquals(true, domainList.containsDomain(DOMAIN_1));
+    }
+
+    /**
+     * Delete all possible domains from database.
+     * 
+     * @throws DomainListException
+     */
+    private void deleteAll() throws DomainListException {
+        domainList.removeDomain(DOMAIN_1);
+        domainList.removeDomain(DOMAIN_2);
+        domainList.removeDomain(DOMAIN_3);
+        domainList.removeDomain(DOMAIN_4);
+        domainList.removeDomain(DOMAIN_5);
+    }
+
+    /**
+     * Return a fake DNSServer.
+     * 
+     * @param hostName
+     * @return
+     */
+    protected DNSService getDNSServer(final String hostName) {
+        DNSService dns = new MockDNSService() {
+            public String getHostName(InetAddress inet) {
+                return hostName;
+            }
+            public InetAddress[] getAllByName(String name) throws UnknownHostException {
+                return new InetAddress[] { InetAddress.getByName("127.0.0.1") };
+            }
+            public InetAddress getLocalHost() throws UnknownHostException {
+                return InetAddress.getLocalHost();
+            }
+        };
+        return dns;
+    }
+    
+    /**
+     * Implementing test classes must provide the corresponding implement
+     * of the DomainList interface.
+     * 
+     * @return an implementation of DomainList
+     */
+    protected abstract DomainList createDomainList();
+
+}

Modified: james/server/trunk/jpa/src/test/java/org/apache/james/domainlist/jpa/JPADomainListTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/jpa/src/test/java/org/apache/james/domainlist/jpa/JPADomainListTest.java?rev=1153881&r1=1153880&r2=1153881&view=diff
==============================================================================
--- james/server/trunk/jpa/src/test/java/org/apache/james/domainlist/jpa/JPADomainListTest.java
(original)
+++ james/server/trunk/jpa/src/test/java/org/apache/james/domainlist/jpa/JPADomainListTest.java
Thu Aug  4 13:32:38 2011
@@ -18,17 +18,11 @@
  ****************************************************************/
 package org.apache.james.domainlist.jpa;
 
-import java.net.InetAddress;
-import java.net.UnknownHostException;
 import java.util.HashMap;
 
-import junit.framework.TestCase;
-
-import org.apache.james.dnsservice.api.DNSService;
-import org.apache.james.dnsservice.api.mock.MockDNSService;
-import org.apache.james.domainlist.api.DomainListException;
-import org.apache.james.domainlist.jpa.JPADomainList;
+import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.domainlist.jpa.model.JPADomain;
+import org.apache.james.domainlist.lib.AbstractDomainListTest;
 import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
 import org.apache.openjpa.persistence.OpenJPAPersistence;
 import org.slf4j.LoggerFactory;
@@ -36,37 +30,27 @@ import org.slf4j.LoggerFactory;
 /**
  * Test the JPA implementation of the DomainList.
  */
-public class JPADomainListTest extends TestCase {
-
-    // Domains we will play with.
-    private final String DOMAIN_1 = "domain1.tld";
-    private final String DOMAIN_2 = "domain2.tld";
-    private final String DOMAIN_3 = "domain3.tld";
-    private final String DOMAIN_4 = "domain4.tld";
-    private final String DOMAIN_5 = "domain5.tld";
+public class JPADomainListTest extends AbstractDomainListTest {
 
     /**
      * The OpenJPA Entity Manager used for the tests.
      */
     private OpenJPAEntityManagerFactory factory;
 
-    /**
-     * The properties for the OpenJPA Entity Manager.
-     */
-    private HashMap<String, String> properties;
-
-    /**
-     * The JPA DomainList service.
-     */
-    private JPADomainList jpaDomainList;
-
     @Override
     protected void setUp() throws Exception {
-
         super.setUp();
+    }
 
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    @Override
+    protected DomainList createDomainList() {
         // Use a memory database.
-        properties = new HashMap<String, String>();
+        HashMap<String, String> properties = new HashMap<String, String>();
         properties.put("openjpa.ConnectionDriverName", org.apache.derby.jdbc.EmbeddedDriver.class.getName());
         properties.put("openjpa.ConnectionURL", "jdbc:derby:memory:JPADomainListTestDB;create=true");
         properties.put("openjpa.Log", "JDBC=WARN, SQL=WARN, Runtime=WARN");
@@ -76,103 +60,15 @@ public class JPADomainListTest extends T
         factory = OpenJPAPersistence.getEntityManagerFactory(properties);
 
         // Initialize the JPADomainList (no autodetect,...).
-        jpaDomainList = new JPADomainList();
+        JPADomainList jpaDomainList = new JPADomainList();
         jpaDomainList.setLog(LoggerFactory.getLogger("JPADomainListMockLog"));
-        jpaDomainList.setDNSService(setUpDNSServer("localhost"));
+        jpaDomainList.setDNSService(getDNSServer("localhost"));
         jpaDomainList.setAutoDetect(false);
         jpaDomainList.setAutoDetectIP(false);
         jpaDomainList.setEntityManagerFactory(factory);
+        
+        return jpaDomainList;
 
-        // Always delete everything before running any tests.
-        deleteAll();
-
-    }
-
-    @Override
-    protected void tearDown() throws Exception {
-        super.tearDown();
-    }
-
-    /**
-     * Add 3 domains and list them.
-     * 
-     * @throws DomainListException
-     */
-    public void createListDomains() throws DomainListException {
-        jpaDomainList.addDomain(DOMAIN_3);
-        jpaDomainList.addDomain(DOMAIN_4);
-        jpaDomainList.addDomain(DOMAIN_5);
-        assertEquals(3, jpaDomainList.getDomains().length);
-    }
-
-    /**
-     * Add a domain and check it is present.
-     * 
-     * @throws DomainListException
-     */
-    public void testAddContainsDomain() throws DomainListException {
-        jpaDomainList.addDomain(DOMAIN_2);
-        jpaDomainList.containsDomain(DOMAIN_2);
-    }
-
-    /**
-     * Add and remove a domain, and check database is empty.
-     * 
-     * @throws DomainListException
-     */
-    public void testAddRemoveContainsSameDomain() throws DomainListException {
-        jpaDomainList.addDomain(DOMAIN_1);
-        jpaDomainList.removeDomain(DOMAIN_1);
-        assertEquals(null, jpaDomainList.getDomains());
-    }
-
-    /**
-     * Add a domain and remove another domain, and check first domain is still
-     * present.
-     * 
-     * @throws DomainListException
-     */
-    public void testAddRemoveContainsDifferentDomain() throws DomainListException {
-        jpaDomainList.addDomain(DOMAIN_1);
-        jpaDomainList.removeDomain(DOMAIN_2);
-        assertEquals(1, jpaDomainList.getDomains().length);
-        assertEquals(true, jpaDomainList.containsDomain(DOMAIN_1));
-    }
-
-    /**
-     * Delete all possible domains from database.
-     * 
-     * @throws DomainListException
-     */
-    private void deleteAll() throws DomainListException {
-        jpaDomainList.removeDomain(DOMAIN_1);
-        jpaDomainList.removeDomain(DOMAIN_2);
-        jpaDomainList.removeDomain(DOMAIN_3);
-        jpaDomainList.removeDomain(DOMAIN_4);
-        jpaDomainList.removeDomain(DOMAIN_5);
-    }
-
-    /**
-     * Return a fake DNSServer.
-     * 
-     * @param hostName
-     * @return
-     */
-    private DNSService setUpDNSServer(final String hostName) {
-        DNSService dns = new MockDNSService() {
-            public String getHostName(InetAddress inet) {
-                return hostName;
-            }
-
-            public InetAddress[] getAllByName(String name) throws UnknownHostException {
-                return new InetAddress[] { InetAddress.getByName("127.0.0.1") };
-            }
-
-            public InetAddress getLocalHost() throws UnknownHostException {
-                return InetAddress.getLocalHost();
-            }
-        };
-        return dns;
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message