james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From matth...@apache.org
Subject svn commit: r1726774 - in /james/project/trunk/server/data: data-file/src/main/java/org/apache/james/mailrepository/file/ data-file/src/main/java/org/apache/james/user/file/ data-file/src/test/java/org/apache/james/user/file/ data-hbase/src/test/java/o...
Date Tue, 26 Jan 2016 11:17:06 GMT
Author: matthieu
Date: Tue Jan 26 11:17:06 2016
New Revision: 1726774

URL: http://svn.apache.org/viewvc?rev=1726774&view=rev
Log:
JAMES-1668 Enhance AbstractUsersRepositoryTest to match best test practice

	Contributed by Laura Royet

Modified:
    james/project/trunk/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/FileMailRepository.java
    james/project/trunk/server/data/data-file/src/main/java/org/apache/james/user/file/UsersFileRepository.java
    james/project/trunk/server/data/data-file/src/test/java/org/apache/james/user/file/UsersFileRepositoryTest.java
    james/project/trunk/server/data/data-hbase/src/test/java/org/apache/james/user/hbase/HBaseUsersRepositoryTest.java
    james/project/trunk/server/data/data-jcr/pom.xml
    james/project/trunk/server/data/data-jdbc/src/test/java/org/apache/james/user/jdbc/AbstractUsersJdbcRepositoryTest.java
    james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/api/ObjectRepository.java
    james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/api/StreamRepository.java
    james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/file/AbstractFileRepository.java
    james/project/trunk/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java
    james/project/trunk/server/data/data-library/src/test/java/org/apache/james/user/lib/AbstractUsersRepositoryTest.java

Modified: james/project/trunk/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/FileMailRepository.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/FileMailRepository.java?rev=1726774&r1=1726773&r2=1726774&view=diff
==============================================================================
--- james/project/trunk/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/FileMailRepository.java
(original)
+++ james/project/trunk/server/data/data-file/src/main/java/org/apache/james/mailrepository/file/FileMailRepository.java
Tue Jan 26 11:17:06 2016
@@ -19,6 +19,20 @@
 
 package org.apache.james.mailrepository.file;
 
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
 import org.apache.commons.configuration.DefaultConfigurationBuilder;
 import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.james.core.MimeMessageCopyOnWriteProxy;
@@ -29,20 +43,6 @@ import org.apache.james.repository.file.
 import org.apache.james.repository.file.FilePersistentStreamRepository;
 import org.apache.mailet.Mail;
 
-import javax.annotation.PostConstruct;
-import javax.inject.Inject;
-import javax.mail.MessagingException;
-import javax.mail.internet.MimeMessage;
-
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Set;
-
 /**
  * <p>
  * Implementation of a MailRepository on a FileSystem.
@@ -250,8 +250,9 @@ public class FileMailRepository extends
 
     @Override
     protected void internalRemove(String key) throws MessagingException {
-        if (keys != null)
+        if (keys != null) {
             keys.remove(key);
+        }
         streamRepository.remove(key);
         objectRepository.remove(key);
     }

Modified: james/project/trunk/server/data/data-file/src/main/java/org/apache/james/user/file/UsersFileRepository.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-file/src/main/java/org/apache/james/user/file/UsersFileRepository.java?rev=1726774&r1=1726773&r2=1726774&view=diff
==============================================================================
--- james/project/trunk/server/data/data-file/src/main/java/org/apache/james/user/file/UsersFileRepository.java
(original)
+++ james/project/trunk/server/data/data-file/src/main/java/org/apache/james/user/file/UsersFileRepository.java
Tue Jan 26 11:17:06 2016
@@ -206,7 +206,9 @@ public class UsersFileRepository extends
      * @see org.apache.james.user.api.UsersRepository#removeUser(java.lang.String)
      */
     public synchronized void removeUser(String name) throws UsersRepositoryException {
-        objectRepository.remove(name);
+        if (!objectRepository.remove(name)) {
+            throw new UsersRepositoryException("User " + name + " does not exist");
+        }
     }
 
     /**

Modified: james/project/trunk/server/data/data-file/src/test/java/org/apache/james/user/file/UsersFileRepositoryTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-file/src/test/java/org/apache/james/user/file/UsersFileRepositoryTest.java?rev=1726774&r1=1726773&r2=1726774&view=diff
==============================================================================
--- james/project/trunk/server/data/data-file/src/test/java/org/apache/james/user/file/UsersFileRepositoryTest.java
(original)
+++ james/project/trunk/server/data/data-file/src/test/java/org/apache/james/user/file/UsersFileRepositoryTest.java
Tue Jan 26 11:17:06 2016
@@ -35,6 +35,8 @@ import org.apache.james.user.lib.Abstrac
 import org.apache.james.user.lib.AbstractUsersRepositoryTest;
 import org.junit.After;
 import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
 import org.slf4j.LoggerFactory;
 
 /**
@@ -45,9 +47,6 @@ public class UsersFileRepositoryTest ext
     private static final String TARGET_REPOSITORY_FOLDER = "target/var/users";
 	private File targetRepositoryFolder;
 
-    /**
-     * @see junit.framework.TestCase#setUp()
-     */
     @Before
     @Override
     public void setUp() throws Exception {
@@ -61,12 +60,6 @@ public class UsersFileRepositoryTest ext
         FileUtils.forceDelete(targetRepositoryFolder);
     }
 
-    /**
-     * Create the repository to be tested.
-     * 
-     * @return the user repository
-     * @throws Exception
-     */
     @SuppressWarnings("deprecation")
     @Override
     protected AbstractUsersRepository getUsersRepository() throws Exception {
@@ -103,15 +96,12 @@ public class UsersFileRepositoryTest ext
         return res;
     }
     
-    /* Disable testUpperCaseSameUser test.
-     *
-     * @see org.apache.james.user.lib.AbstractUsersRepositoryTest#testUpperCaseSameUser()
-     */
     @Override
-    public void testUpperCaseSameUser() throws UsersRepositoryException {
+    @Ignore
+    @Test
+    public void addUserShouldThrowWhenSameUsernameWithDifferentCase() throws UsersRepositoryException
{
     }
 
-
     protected void disposeUsersRepository() throws UsersRepositoryException {
         if (this.usersRepository != null) {
             Iterator<String> i = this.usersRepository.list();
@@ -121,5 +111,4 @@ public class UsersFileRepositoryTest ext
             LifecycleUtil.dispose(this.usersRepository);
         }
     }
-
 }

Modified: james/project/trunk/server/data/data-hbase/src/test/java/org/apache/james/user/hbase/HBaseUsersRepositoryTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-hbase/src/test/java/org/apache/james/user/hbase/HBaseUsersRepositoryTest.java?rev=1726774&r1=1726773&r2=1726774&view=diff
==============================================================================
--- james/project/trunk/server/data/data-hbase/src/test/java/org/apache/james/user/hbase/HBaseUsersRepositoryTest.java
(original)
+++ james/project/trunk/server/data/data-hbase/src/test/java/org/apache/james/user/hbase/HBaseUsersRepositoryTest.java
Tue Jan 26 11:17:06 2016
@@ -29,6 +29,8 @@ import org.apache.james.user.lib.Abstrac
 import org.apache.james.user.lib.AbstractUsersRepositoryTest;
 import org.junit.Before;
 import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
 import org.slf4j.LoggerFactory;
 
 /**
@@ -79,4 +81,10 @@ public class HBaseUsersRepositoryTest ex
         userRepository.configure(new DefaultConfigurationBuilder());
         return userRepository;
     }
+    
+    @Override
+    @Ignore
+    @Test(expected = UsersRepositoryException.class)
+    public void removeUserShouldThrowWhenUserNotInRepository() throws UsersRepositoryException
{
+    }
 }

Modified: james/project/trunk/server/data/data-jcr/pom.xml
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-jcr/pom.xml?rev=1726774&r1=1726773&r2=1726774&view=diff
==============================================================================
--- james/project/trunk/server/data/data-jcr/pom.xml (original)
+++ james/project/trunk/server/data/data-jcr/pom.xml Tue Jan 26 11:17:06 2016
@@ -88,6 +88,12 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <version>${assertj-1.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.apache.jackrabbit</groupId>
             <artifactId>jackrabbit-core</artifactId>
             <scope>test</scope>

Modified: james/project/trunk/server/data/data-jdbc/src/test/java/org/apache/james/user/jdbc/AbstractUsersJdbcRepositoryTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-jdbc/src/test/java/org/apache/james/user/jdbc/AbstractUsersJdbcRepositoryTest.java?rev=1726774&r1=1726773&r2=1726774&view=diff
==============================================================================
--- james/project/trunk/server/data/data-jdbc/src/test/java/org/apache/james/user/jdbc/AbstractUsersJdbcRepositoryTest.java
(original)
+++ james/project/trunk/server/data/data-jdbc/src/test/java/org/apache/james/user/jdbc/AbstractUsersJdbcRepositoryTest.java
Tue Jan 26 11:17:06 2016
@@ -18,53 +18,16 @@
  ****************************************************************/
 package org.apache.james.user.jdbc;
 
+import org.apache.james.user.api.UsersRepositoryException;
 import org.apache.james.user.lib.AbstractUsersRepositoryTest;
+import org.junit.Ignore;
+import org.junit.Test;
 
 public abstract class AbstractUsersJdbcRepositoryTest extends AbstractUsersRepositoryTest
{
     
-    /* Deactivate this test for the Jdbc implementation
-     * Should be disable via @Ignore with JUnit4
-     * 
-     * @see org.apache.james.user.lib.AbstractUsersRepositoryTest#testUsersRepositoryEmpty()
-     */
     @Override
-    public void testUserListing() {
+    @Ignore
+    @Test(expected = UsersRepositoryException.class)
+    public void addUserShouldThrowWhenSameUsernameWithDifferentCase() throws UsersRepositoryException
{
     }
-
-    /* Deactivate this test for the Jdbc implementation
-     * Should be disable via @Ignore with JUnit4
-     * 
-     * @see org.apache.james.user.lib.AbstractUsersRepositoryTest#testUsersRepositoryEmpty()
-     */
-    @Override
-    public void testUpperCaseSameUser() {
-    }
-
-    /* Deactivate this test for the Jdbc implementation
-     * Should be disable via @Ignore with JUnit4
-     * 
-     * @see org.apache.james.user.lib.AbstractUsersRepositoryTest#testUsersRepositoryEmpty()
-     */
-    @Override
-    public void testUserAddedIsFound() {
-    }
-
-    /* Deactivate this test for the Jdbc implementation
-     * Should be disable via @Ignore with JUnit4
-     * 
-     * @see org.apache.james.user.lib.AbstractUsersRepositoryTest#testUsersRepositoryEmpty()
-     */
-    @Override
-    public void testUserPassword() {
-    }
-
-    /* Deactivate this test for the Jdbc implementation
-     * Should be disable via @Ignore with JUnit4
-     * 
-     * @see org.apache.james.user.lib.AbstractUsersRepositoryTest#testUsersRepositoryEmpty()
-     */
-    @Override
-    public void testUserAddRemoveCycle() {
-    }
-
 }

Modified: james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/api/ObjectRepository.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/api/ObjectRepository.java?rev=1726774&r1=1726773&r2=1726774&view=diff
==============================================================================
--- james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/api/ObjectRepository.java
(original)
+++ james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/api/ObjectRepository.java
Tue Jan 26 11:17:06 2016
@@ -33,5 +33,5 @@ public interface ObjectRepository extend
 
     void put(String key, Object value);
 
-    void remove(String key);
+    boolean remove(String key);
 }

Modified: james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/api/StreamRepository.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/api/StreamRepository.java?rev=1726774&r1=1726773&r2=1726774&view=diff
==============================================================================
--- james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/api/StreamRepository.java
(original)
+++ james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/api/StreamRepository.java
Tue Jan 26 11:17:06 2016
@@ -31,5 +31,5 @@ public interface StreamRepository extend
 
     OutputStream put(String key);
 
-    void remove(String key);
+    boolean remove(String key);
 }

Modified: james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/file/AbstractFileRepository.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/file/AbstractFileRepository.java?rev=1726774&r1=1726773&r2=1726774&view=diff
==============================================================================
--- james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/file/AbstractFileRepository.java
(original)
+++ james/project/trunk/server/data/data-library/src/main/java/org/apache/james/repository/file/AbstractFileRepository.java
Tue Jan 26 11:17:06 2016
@@ -261,19 +261,19 @@ public abstract class AbstractFileReposi
      * 
      * @param key
      *            the key to remove
+     * @throws IOException 
      */
-    public synchronized void remove(final String key) {
+    
+    public synchronized boolean remove(final String key) {
         try {
             FileUtils.forceDelete(getFile(key));
-            if (DEBUG)
-                getLogger().debug("removed key " + key);
-        } catch (FileNotFoundException e) {
-            getLogger().debug("File for " + key + " not found: wasn't able to remove");
+            return true;
+        } catch (FileNotFoundException e) { 
+            return false;
         } catch (Exception e) {
             throw new RuntimeException("Exception caught while removing" + " an object: "
+ e);
         }
     }
-
     /**
      * 
      * Indicates if the given key is associated to a contained object

Modified: james/project/trunk/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java?rev=1726774&r1=1726773&r2=1726774&view=diff
==============================================================================
--- james/project/trunk/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java
(original)
+++ james/project/trunk/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java
Tue Jan 26 11:17:06 2016
@@ -60,7 +60,6 @@ public abstract class AbstractUsersRepos
     }
 
     protected void doConfigure(HierarchicalConfiguration config) throws ConfigurationException
{
-
     }
 
     public void setEnableVirtualHosting(boolean virtualHosting) {

Modified: james/project/trunk/server/data/data-library/src/test/java/org/apache/james/user/lib/AbstractUsersRepositoryTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-library/src/test/java/org/apache/james/user/lib/AbstractUsersRepositoryTest.java?rev=1726774&r1=1726773&r2=1726774&view=diff
==============================================================================
--- james/project/trunk/server/data/data-library/src/test/java/org/apache/james/user/lib/AbstractUsersRepositoryTest.java
(original)
+++ james/project/trunk/server/data/data-library/src/test/java/org/apache/james/user/lib/AbstractUsersRepositoryTest.java
Tue Jan 26 11:17:06 2016
@@ -18,12 +18,7 @@
  ****************************************************************/
 package org.apache.james.user.lib;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.assertj.core.api.Assertions.assertThat;
 
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -36,16 +31,12 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
-/**
- * Test basic behaviors of UsersFileRepository
- */
+
 public abstract class AbstractUsersRepositoryTest {
 
-    private static final String DOMAIN = "domain";
-    /**
-     * Users repository
-     */
-    protected AbstractUsersRepository usersRepository;
+    private static final String DOMAIN = "domain"; 
+
+    protected AbstractUsersRepository usersRepository; 
 
     /**
      * Create the repository to be tested.
@@ -59,11 +50,8 @@ public abstract class AbstractUsersRepos
     private String user2;
     private String user3;
     
-    /**
-     * @see junit.framework.TestCase#setUp()
-     */
-    @Before
-    public void setUp() throws Exception {
+    @Before 
+    public void setUp() throws Exception { 
         this.usersRepository = getUsersRepository();
         SimpleDomainList domainList = new SimpleDomainList();
         domainList.addDomain(DOMAIN);
@@ -73,14 +61,11 @@ public abstract class AbstractUsersRepos
         user3 = login("username3");
     }
 
-    /**
-     * @see junit.framework.TestCase#tearDown()
-     */
     @After
     public void tearDown() throws Exception {
         disposeUsersRepository();
     }
-
+    
     private String login(String login) throws UsersRepositoryException {
         if (usersRepository.supportVirtualHosting()) {
             return login + '@' + DOMAIN;
@@ -90,47 +75,42 @@ public abstract class AbstractUsersRepos
     }
     
     @Test
-    public void testUsersRepositoryEmpty() throws UsersRepositoryException {
-        assertEquals("users repository not empty", 0, usersRepository.countUsers());
-        assertFalse("users repository not empty", usersRepository.list().hasNext());
+    public void countUsersShouldReturnZeroWhenEmptyRepository() throws UsersRepositoryException
{
+        //Given
+        int expected = 0;
+        //When
+        int actual = usersRepository.countUsers();
+        //Then
+        assertThat(actual).isEqualTo(expected);
     }
-
+    
     @Test
-    public void testAddUserOnce() throws UsersRepositoryException {
-        usersRepository.addUser(user1, "password");
-        try {
-            usersRepository.addUser(user1, "password2");
-            fail("User added twice!");
-        } catch (UsersRepositoryException e) {
-            // UsersRepositoryException must be thrown by implementation.
-        }
-        try {
-            usersRepository.addUser(user2, "password2");
-            assertTrue(usersRepository.contains(user2));
-            usersRepository.addUser(user3, "password3");
-            assertTrue(usersRepository.contains(user3));
-        } catch (UnsupportedOperationException e) {
+    public void countUsersShouldReturnNumberOfUsersWhenNotEmptyRepository() throws UsersRepositoryException
{
+        //Given
+        ArrayList<String> keys = new ArrayList<String>(3);
+        keys.add(user1);
+        keys.add(user2);
+        keys.add(user3);
+        for (String username : keys) {
+            usersRepository.addUser(username, username);
         }
+        //When
+        int actual = usersRepository.countUsers();
+        //Then
+        assertThat(actual).isEqualTo(keys.size());
     }
-
+    
     @Test
-    public void testUserAddedIsFound() throws UsersRepositoryException {
-        usersRepository.addUser(user1, "password");
-        User user = usersRepository.getUserByName(user1);
-        assertNotNull(user);
-        assertEquals("username does not match", user.getUserName(), user1);
-        assertTrue("user not contained in the repository", usersRepository.contains(user1));
-
-        User u = usersRepository.getUserByName("uSERNAMe");
-        assertNull("found the user searching for a different case!", u);
-
-        // String realname = usersRepository.getRealName("uSERNAMe");
-        // assertNull("name is not null", realname);
-        // assertEquals("name is different", user1, realname);
+    public void listShouldReturnEmptyIteratorWhenEmptyRepository() throws UsersRepositoryException
{
+        //When
+        Iterator<String> actual = usersRepository.list();
+        //Then
+        assertThat(actual).isEmpty();
     }
-
+    
     @Test
-    public void testUserListing() throws UsersRepositoryException {
+    public void listShouldReturnExactlyUsersInRepository() throws UsersRepositoryException
{
+        //Given
         ArrayList<String> keys = new ArrayList<String>(3);
         keys.add(user1);
         keys.add(user2);
@@ -138,98 +118,188 @@ public abstract class AbstractUsersRepos
         for (String username : keys) {
             usersRepository.addUser(username, username);
         }
-        assertEquals("Wrong number of users found", keys.size(), usersRepository.countUsers());
-
-        // check list return all and only the expected users
-        ArrayList<String> check = new ArrayList<String>(keys);
-        for (Iterator<String> i = usersRepository.list(); i.hasNext();) {
-            String username = i.next();
-            if (getPasswordsEnabled()) {
-                assertTrue(usersRepository.test(username, username));
-                User u = usersRepository.getUserByName(username);
-                u.setPassword("newpass");
-                usersRepository.updateUser(u);
-            }
-            assertTrue(check.contains(username));
-            check.remove(username);
-        }
-        assertEquals("Some user has not be found", 0, check.size());
+        //When
+        Iterator<String> actual = usersRepository.list();
+        //Then
+        assertThat(actual).containsOnly(user1, user2, user3);
     }
-
+    
     @Test
-    public void testUpperCaseSameUser() throws UsersRepositoryException {
+    public void addUserShouldAddAUserWhenEmptyRepository() throws UsersRepositoryException
{
+        //When
+        usersRepository.addUser(user2, "password2");
+        //Then
+        assertThat(usersRepository.contains(user2)).isTrue();
+    }
+    
+    @Test 
+    public void addUserShouldAddAUserWhenNotEmptyRepository() throws UsersRepositoryException
{
+        //Given
+        usersRepository.addUser(user2, "password2");
+        //When
+        usersRepository.addUser(user3, "password3");
+        //Then
+        assertThat(usersRepository.contains(user3)).isTrue();
+    }
+    
+    @Test(expected = UsersRepositoryException.class)
+    public void addUserShouldThrowWhenSameUsernameWithDifferentCase() throws UsersRepositoryException
{
+        //Given
         usersRepository.addUser(login("myUsername"), "password");
-        try {
-            usersRepository.addUser(login("MyUsername"), "password");
-            fail("We should not be able to insert same users, even with different cases");
-        } catch (UsersRepositoryException e) {
-            assertTrue("The exception message must contain the username value but was=" +
e.getMessage(), e.
-                    getMessage().contains("MyUsername"));
-        }
+        //When
+        usersRepository.addUser(login("MyUsername"), "password"); 
+    }
+    
+    @Test(expected = UsersRepositoryException.class)
+    public void addUserShouldThrowWhenUserAlreadyPresentInRepository() throws UsersRepositoryException
{
+        //Given
+        usersRepository.addUser(user1, "password");
+       //When
+        usersRepository.addUser(user1, "password2");
+    }
+    
+    @Test
+    public void getUserByNameShouldReturnAUserWhenContainedInRepository () throws UsersRepositoryException
{
+        //Given
+        usersRepository.addUser(user1, "password");
+        //When
+        User actual = usersRepository.getUserByName(user1);
+        //Then
+        assertThat(actual).isNotNull();
+        assertThat(actual.getUserName()).isEqualTo(user1);
     }
 
     @Test
-    public void testUserPassword() throws UsersRepositoryException {
+    public void getUserByNameShouldReturnNullWhenDifferentCase() throws UsersRepositoryException
{
+        //Given
+        usersRepository.addUser(login("username"), "password");
+        //When
+        User actual = usersRepository.getUserByName(login("uSERNAMe"));
+        //Then
+        assertThat(actual).isNull();
+    }
+   
+    @Test
+    public void testShouldReturnTrueWhenAUserHasACorrectPassword() throws UsersRepositoryException
{ 
+        //Given
         usersRepository.addUser(user1, "password");
-        assertEquals("didn't accept the correct password ", usersRepository.test(user1, "password"),
-                getPasswordsEnabled());
-        assertFalse("accepted the wrong password #1", usersRepository.test(user1, "password2"));
-        assertFalse("accepted the wrong password #2", usersRepository.test("username2", "password"));
-        assertFalse("accepted the wrong password #3", usersRepository.test(user1, "Password"));
-        assertFalse("accepted the wrong password #4", usersRepository.test(user1, "passwords"));
-        assertFalse("accepted the wrong password #5", usersRepository.test("userName", "password"));
+        //When
+        boolean actual = usersRepository.test(user1, "password");
+        //Then
+        assertThat(actual).isTrue();
+    }
+    
+    @Test
+    public void testShouldReturnFalseWhenAUserHasAnIncorrectPassword() throws UsersRepositoryException
{ 
+        //Given
+        usersRepository.addUser(user1, "password");
+        //When
+        boolean actual = usersRepository.test(user1, "password2");
+        //Then
+        assertThat(actual).isFalse();
+    }
+    
+    @Test
+    public void testShouldReturnFalseWhenAUserHasAnIncorrectCasePassword() throws UsersRepositoryException
{ 
+        //Given
+        usersRepository.addUser(user1, "password");
+        //When
+        boolean actual = usersRepository.test(user1, "Password");
+        //Then
+        assertThat(actual).isFalse();
+    }
+    
+    @Test
+    public void testShouldReturnFalseWhenAUserIsNotInRepository() throws UsersRepositoryException
{ 
+        //Given
+        usersRepository.addUser(login("username"), "password");
+        //When
+        boolean actual = usersRepository.test(login("username2"), "password"); 
+        //Then
+        assertThat(actual).isFalse();
     }
 
     @Test
-    public void testUserAddRemoveCycle() throws UsersRepositoryException {
-        assertFalse("accepted login when no user existed", usersRepository.test(user1, "password"));
-        try {
-            usersRepository.removeUser(user1);
-            // UsersFileRepository accept this call for every argument
-            // fail("removing an unknown user didn't fail!");
-        } catch (UsersRepositoryException e) {
-            // Do nothing, we should come here if test works.
-        }
+    public void testShouldReturnFalseWhenAUserHasAnIncorrectCaseName() throws UsersRepositoryException
{
+        //Given
+        usersRepository.addUser(login("username"), "password");
+        //When
+        boolean actual= usersRepository.test(login("userName"), "password");
+        //Then
+        assertThat(actual).isFalse();
+    }
+
+    @Test
+    public void testShouldReturnFalseWhenEmptyRepository() throws UsersRepositoryException
{
+        //When
+        boolean actual = usersRepository.test(user1, "password");
+        //Then
+        assertThat(actual).isFalse();
+    }
+
+    @Test
+    public void testShouldReturnFalseWhenAUserIsRemovedFromRepository() throws UsersRepositoryException
{
+        //Given
+        usersRepository.addUser(user1, "password");
+        usersRepository.removeUser(user1);
+        //When
+        boolean actual = usersRepository.test(user1, "password");
+        //Then
+        assertThat(actual).isFalse();
+    }
+    
+    @Test
+    public void removeUserShouldRemoveAUserWhenPresentInRepository() throws UsersRepositoryException
{
+        //Given
+        usersRepository.addUser(user1, "password");
+        //When
+        usersRepository.removeUser(user1);
+        //Then
+        assertThat(usersRepository.contains(user1)).isFalse();
+    }
+    
+    @Test(expected = UsersRepositoryException.class)
+    public void removeUserShouldThrowWhenUserNotInRepository() throws UsersRepositoryException
{
+        //When
+        usersRepository.removeUser(user1);
+    }
+    
+    @Test
+    public void updateUserShouldAllowToAuthenticateWithNewPassword() throws UsersRepositoryException
{ 
+      //Given
         usersRepository.addUser(user1, "password");
-        assertEquals("didn't accept the correct password", getPasswordsEnabled(), usersRepository.test(user1,
"password"));
         User user = usersRepository.getUserByName(user1);
         user.setPassword("newpass");
-        try {
-            usersRepository.updateUser(user);
-            assertEquals("new password accepted", getPasswordsEnabled(), usersRepository.test(user1,
"newpass"));
-            assertFalse("old password rejected", usersRepository.test(user1, "password"));
-        } catch (UnsupportedOperationException e) {
-            // if updating users is not allowed check that this is a repository
-            // without password checking
-            assertFalse(getPasswordsEnabled());
-        }
-        try {
-            usersRepository.removeUser(user1);
-        } catch (Exception e) {
-            e.printStackTrace();
-            fail("removing the user failed!");
-        }
-        assertFalse("user not existing", usersRepository.contains(user1));
-        assertFalse("new password rejected", usersRepository.test(user1, "newpass"));
-        try {
-            usersRepository.updateUser(user);
-            fail();
-        } catch (UsersRepositoryException e) {
-        }
+        //When
+        usersRepository.updateUser(user);
+        //Then
+        assertThat(usersRepository.test(user1, "newpass")).isTrue();
     }
-
-    /**
-     * Dispose the repository
-     *
-     * @throws UsersRepositoryException
-     */
-    protected void disposeUsersRepository() throws UsersRepositoryException {
-        if (usersRepository != null) {
-            LifecycleUtil.dispose(this.usersRepository);
-        }
+   
+    @Test
+    public void updateUserShouldNotAllowToAuthenticateWithOldPassword() throws UsersRepositoryException
{
+        //Given
+        usersRepository.addUser(user1, "password");
+        User user = usersRepository.getUserByName(user1);
+        user.setPassword("newpass");
+        //When
+        usersRepository.updateUser(user);
+        //Then
+        assertThat(usersRepository.test(user1, "password")).isFalse();
+    }
+    
+    @Test(expected = UsersRepositoryException.class)
+    public void updateUserShouldThrowWhenAUserIsNoMoreInRepository() throws UsersRepositoryException
{
+        //Given
+        usersRepository.addUser(user1, "password");
+        User user = usersRepository.getUserByName(user1);
+        usersRepository.removeUser(user1);
+        //When
+        usersRepository.updateUser(user);
     }
 
-    private boolean getPasswordsEnabled() {
-        return true;
+
+    protected void disposeUsersRepository() throws UsersRepositoryException {
+        LifecycleUtil.dispose(this.usersRepository);
     }
 }



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