james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [01/11] james-project git commit: JAMES-1746 Proof of concept with junit-contracts
Date Wed, 08 Jun 2016 11:35:59 GMT
Repository: james-project
Updated Branches:
  refs/heads/master 7c8a65bc3 -> e2b52479e


JAMES-1746 Proof of concept with junit-contracts


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/da5c0d8b
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/da5c0d8b
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/da5c0d8b

Branch: refs/heads/master
Commit: da5c0d8bfda0d56140ac0e7659ca46ab62043357
Parents: 7c8a65b
Author: Benoit Tellier <btellier@linagora.com>
Authored: Fri Jun 3 10:57:07 2016 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Wed Jun 8 17:09:00 2016 +0700

----------------------------------------------------------------------
 server/data/data-jmap-cassandra/pom.xml         |   5 +
 .../CassandraAccessTokenRepositoryTest.java     |  38 +++++--
 server/data/data-jmap/pom.xml                   |   5 +
 .../AbstractAccessTokenRepositoryTest.java      |  99 ----------------
 .../api/access/AccessTokenRepositoryTest.java   | 113 +++++++++++++++++++
 .../access/MemoryAccessTokenRepositoryTest.java |  29 ++++-
 server/pom.xml                                  |   5 +
 7 files changed, 179 insertions(+), 115 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/da5c0d8b/server/data/data-jmap-cassandra/pom.xml
----------------------------------------------------------------------
diff --git a/server/data/data-jmap-cassandra/pom.xml b/server/data/data-jmap-cassandra/pom.xml
index dec6ebc..8550bb1 100644
--- a/server/data/data-jmap-cassandra/pom.xml
+++ b/server/data/data-jmap-cassandra/pom.xml
@@ -234,6 +234,11 @@
                     <groupId>org.slf4j</groupId>
                     <artifactId>slf4j-api</artifactId>
                 </dependency>
+                <dependency>
+                    <groupId>org.xenei</groupId>
+                    <artifactId>junit-contracts</artifactId>
+                    <scope>test</scope>
+                </dependency>
             </dependencies>
         </profile>
         <profile>

http://git-wip-us.apache.org/repos/asf/james-project/blob/da5c0d8b/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepositoryTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepositoryTest.java
b/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepositoryTest.java
index 1217698..ccb44c4 100644
--- a/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepositoryTest.java
+++ b/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepositoryTest.java
@@ -20,22 +20,36 @@
 package org.apache.james.jmap.cassandra.access;
 
 import org.apache.james.backends.cassandra.CassandraCluster;
-import org.apache.james.jmap.api.access.AbstractAccessTokenRepositoryTest;
 import org.apache.james.jmap.api.access.AccessTokenRepository;
-import org.junit.After;
+import org.apache.james.jmap.api.access.AccessTokenRepositoryTest;
+import org.junit.runner.RunWith;
+import org.xenei.junit.contract.Contract;
+import org.xenei.junit.contract.ContractImpl;
+import org.xenei.junit.contract.ContractSuite;
+import org.xenei.junit.contract.IProducer;
 
-public class CassandraAccessTokenRepositoryTest extends AbstractAccessTokenRepositoryTest
{
+@RunWith(ContractSuite.class)
+@ContractImpl(CassandraAccessTokenRepository.class)
+public class CassandraAccessTokenRepositoryTest {
 
-    private CassandraCluster cassandra;
+    private IProducer<AccessTokenRepository> producer = new IProducer<AccessTokenRepository>()
{
 
-    @Override
-    protected AccessTokenRepository createAccessTokenRepository() {
-        cassandra = CassandraCluster.create(new CassandraAccessModule());
-        return new CassandraAccessTokenRepository(new CassandraAccessTokenDAO(cassandra.getConf(),
TTL_IN_MS));
-    }
+        private CassandraCluster cassandra;
+
+        @Override
+        public CassandraAccessTokenRepository newInstance() {
+            cassandra = CassandraCluster.create(new CassandraAccessModule());
+            return new CassandraAccessTokenRepository(new CassandraAccessTokenDAO(cassandra.getConf(),
AccessTokenRepositoryTest.TTL_IN_MS));
+        }
+
+        @Override
+        public void cleanUp() {
+            cassandra.clearAllTables();
+        }
+    };
 
-    @After
-    public void tearDown() {
-        cassandra.clearAllTables();
+    @Contract.Inject
+    public IProducer<AccessTokenRepository> getProducer() {
+        return producer;
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/da5c0d8b/server/data/data-jmap/pom.xml
----------------------------------------------------------------------
diff --git a/server/data/data-jmap/pom.xml b/server/data/data-jmap/pom.xml
index 55e569f..a13efc9 100644
--- a/server/data/data-jmap/pom.xml
+++ b/server/data/data-jmap/pom.xml
@@ -223,6 +223,11 @@
                     <artifactId>slf4j-simple</artifactId>
                     <scope>test</scope>
                 </dependency>
+                <dependency>
+                    <groupId>org.xenei</groupId>
+                    <artifactId>junit-contracts</artifactId>
+                    <scope>test</scope>
+                </dependency>
             </dependencies>
         </profile>
         <profile>

http://git-wip-us.apache.org/repos/asf/james-project/blob/da5c0d8b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/access/AbstractAccessTokenRepositoryTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/access/AbstractAccessTokenRepositoryTest.java
b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/access/AbstractAccessTokenRepositoryTest.java
deleted file mode 100644
index 34de4f5..0000000
--- a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/access/AbstractAccessTokenRepositoryTest.java
+++ /dev/null
@@ -1,99 +0,0 @@
-/****************************************************************
- * 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.jmap.api.access;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-
-import java.util.concurrent.CompletionException;
-
-import org.apache.james.jmap.api.access.exceptions.InvalidAccessToken;
-import org.junit.Before;
-import org.junit.Test;
-
-public abstract class AbstractAccessTokenRepositoryTest {
-
-    private static final AccessToken TOKEN = AccessToken.generate();
-    private static final String USERNAME = "username";
-    protected static final long TTL_IN_MS = 1000;
-
-    private AccessTokenRepository accessTokenRepository;
-
-    @Before
-    public void setUp() {
-        accessTokenRepository = createAccessTokenRepository();
-    }
-
-    protected abstract AccessTokenRepository createAccessTokenRepository();
-
-    @Test
-    public void validTokenMustBeRetrieved() throws Throwable {
-        accessTokenRepository.addToken(USERNAME, TOKEN).join();
-        assertThat(accessTokenRepository.getUsernameFromToken(TOKEN).join()).isEqualTo(USERNAME);
-    }
-
-    @Test
-    public void absentTokensMustBeInvalid() throws Exception {
-        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).isInstanceOf(CompletionException.class);
-        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).hasCauseInstanceOf(InvalidAccessToken.class);
-    }
-
-    @Test
-    public void removedTokensMustBeInvalid() throws Exception {
-        accessTokenRepository.addToken(USERNAME, TOKEN).join();
-        accessTokenRepository.removeToken(TOKEN).join();
-        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).isInstanceOf(CompletionException.class);
-        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).hasCauseInstanceOf(InvalidAccessToken.class);
-    }
-
-    @Test
-    public void outDatedTokenMustBeInvalid() throws Exception {
-        accessTokenRepository.addToken(USERNAME, TOKEN).join();
-        Thread.sleep(2 * TTL_IN_MS);
-        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).isInstanceOf(CompletionException.class);
-        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).hasCauseInstanceOf(InvalidAccessToken.class);
-    }
-
-    @Test(expected = NullPointerException.class)
-    public void addTokenMustThrowWhenUsernameIsNull() throws Exception {
-        accessTokenRepository.addToken(null, TOKEN);
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void addTokenMustThrowWhenUsernameIsEmpty() throws Exception {
-        accessTokenRepository.addToken("", TOKEN);
-    }
-
-    @Test(expected = NullPointerException.class)
-    public void addTokenMustThrowWhenTokenIsNull() throws Exception {
-        accessTokenRepository.addToken(USERNAME, null);
-    }
-
-    @Test(expected = NullPointerException.class)
-    public void removeTokenTokenMustThrowWhenTokenIsNull() throws Exception {
-        accessTokenRepository.removeToken(null);
-    }
-
-    @Test(expected = NullPointerException.class)
-    public void getUsernameFromTokenMustThrowWhenTokenIsNull() throws Exception {
-        accessTokenRepository.getUsernameFromToken(null);
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/james-project/blob/da5c0d8b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/access/AccessTokenRepositoryTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/access/AccessTokenRepositoryTest.java
b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/access/AccessTokenRepositoryTest.java
new file mode 100644
index 0000000..9914883
--- /dev/null
+++ b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/access/AccessTokenRepositoryTest.java
@@ -0,0 +1,113 @@
+/****************************************************************
+ * 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.jmap.api.access;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.concurrent.CompletionException;
+
+import org.apache.james.jmap.api.access.exceptions.InvalidAccessToken;
+import org.junit.After;
+import org.xenei.junit.contract.Contract;
+import org.xenei.junit.contract.ContractTest;
+import org.xenei.junit.contract.IProducer;
+
+@Contract(AccessTokenRepository.class)
+public class AccessTokenRepositoryTest<T extends AccessTokenRepository> {
+
+    private static final AccessToken TOKEN = AccessToken.generate();
+    private static final String USERNAME = "username";
+    public static final long TTL_IN_MS = 1000;
+
+    private IProducer<T> producer;
+
+    private AccessTokenRepository accessTokenRepository;
+
+    @Contract.Inject
+    public final void setProducer(IProducer<T> producer) {
+        this.producer = producer;
+        this.accessTokenRepository = producer.newInstance();
+    }
+
+    @After
+    public void tearDown() {
+        producer.cleanUp();
+    }
+
+    @ContractTest
+    public void validTokenMustBeRetrieved() throws Throwable {
+        accessTokenRepository.addToken(USERNAME, TOKEN).join();
+        assertThat(accessTokenRepository.getUsernameFromToken(TOKEN).join()).isEqualTo(USERNAME);
+    }
+
+    @ContractTest
+    public void absentTokensMustBeInvalid() throws Exception {
+        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).isInstanceOf(CompletionException.class);
+        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).hasCauseInstanceOf(InvalidAccessToken.class);
+    }
+
+    @ContractTest
+    public void removedTokensMustBeInvalid() throws Exception {
+        accessTokenRepository.addToken(USERNAME, TOKEN).join();
+        accessTokenRepository.removeToken(TOKEN).join();
+        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).isInstanceOf(CompletionException.class);
+        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).hasCauseInstanceOf(InvalidAccessToken.class);
+    }
+
+    @ContractTest
+    public void outDatedTokenMustBeInvalid() throws Exception {
+        accessTokenRepository.addToken(USERNAME, TOKEN).join();
+        Thread.sleep(2 * TTL_IN_MS);
+        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).isInstanceOf(CompletionException.class);
+        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).hasCauseInstanceOf(InvalidAccessToken.class);
+    }
+
+    @ContractTest
+    public void addTokenMustThrowWhenUsernameIsNull() throws Exception {
+        assertThatThrownBy(() -> accessTokenRepository.addToken(null, TOKEN))
+            .isInstanceOf(NullPointerException.class);
+    }
+
+    @ContractTest
+    public void addTokenMustThrowWhenUsernameIsEmpty() throws Exception {
+        assertThatThrownBy(() -> accessTokenRepository.addToken("", TOKEN))
+            .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @ContractTest
+    public void addTokenMustThrowWhenTokenIsNull() throws Exception {
+        assertThatThrownBy(() -> accessTokenRepository.addToken(USERNAME, null))
+            .isInstanceOf(NullPointerException.class);
+    }
+
+    @ContractTest
+    public void removeTokenTokenMustThrowWhenTokenIsNull() throws Exception {
+        assertThatThrownBy(() -> accessTokenRepository.removeToken(null))
+            .isInstanceOf(NullPointerException.class);
+    }
+
+    @ContractTest
+    public void getUsernameFromTokenMustThrowWhenTokenIsNull() throws Exception {
+        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(null))
+            .isInstanceOf(NullPointerException.class);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/da5c0d8b/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepositoryTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepositoryTest.java
b/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepositoryTest.java
index cc01ff7..c40ab39 100644
--- a/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepositoryTest.java
+++ b/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepositoryTest.java
@@ -19,12 +19,33 @@
 
 package org.apache.james.jmap.memory.access;
 
-import org.apache.james.jmap.api.access.AbstractAccessTokenRepositoryTest;
+import org.apache.james.jmap.api.access.AccessTokenRepository;
+import org.apache.james.jmap.api.access.AccessTokenRepositoryTest;
+import org.junit.runner.RunWith;
+import org.xenei.junit.contract.Contract;
+import org.xenei.junit.contract.ContractImpl;
+import org.xenei.junit.contract.ContractSuite;
+import org.xenei.junit.contract.IProducer;
 
-public class MemoryAccessTokenRepositoryTest extends AbstractAccessTokenRepositoryTest {
+@RunWith(ContractSuite.class)
+@ContractImpl(MemoryAccessTokenRepository.class)
+public class MemoryAccessTokenRepositoryTest {
 
-    protected MemoryAccessTokenRepository createAccessTokenRepository() {
-        return new MemoryAccessTokenRepository(TTL_IN_MS);
+    private IProducer<AccessTokenRepository> producer = new IProducer<AccessTokenRepository>()
{
+        @Override
+        public MemoryAccessTokenRepository newInstance() {
+            return new MemoryAccessTokenRepository(AccessTokenRepositoryTest.TTL_IN_MS);
+        }
+
+        @Override
+        public void cleanUp() {
+
+        }
+    };
+
+    @Contract.Inject
+    public IProducer<AccessTokenRepository> getProducer() {
+        return producer;
     }
 
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/da5c0d8b/server/pom.xml
----------------------------------------------------------------------
diff --git a/server/pom.xml b/server/pom.xml
index 6f582bb..28baa57 100644
--- a/server/pom.xml
+++ b/server/pom.xml
@@ -1049,6 +1049,11 @@
                 <scope>test</scope>
             </dependency>
             <dependency>
+                <groupId>org.xenei</groupId>
+                <artifactId>junit-contracts</artifactId>
+                <version>0.1.5</version>
+            </dependency>
+            <dependency>
                 <groupId>pl.pragmatists</groupId>
                 <artifactId>JUnitParams</artifactId>
                 <version>1.0.4</version>


---------------------------------------------------------------------
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