Skip to content

Commit

Permalink
- Upgrade packages version
Browse files Browse the repository at this point in the history
- Add integration-test
- Update unit tests
- Correct double checked locking
  • Loading branch information
alexy committed Sep 5, 2020
1 parent a9b1a4b commit d2b9eab
Show file tree
Hide file tree
Showing 22 changed files with 787 additions and 350 deletions.
68 changes: 46 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.crazycake</groupId>
<artifactId>shiro-redis</artifactId>
<version>3.2.3</version>
<version>3.3.0</version>
<packaging>jar</packaging>

<name>shiro-redis</name>
Expand All @@ -26,56 +26,80 @@
</licenses>

<dependencies>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<version>3.3.0</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.2</version>
<version>1.6.0</version>
</dependency>

<!-- Testing -->
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.7</version>
<version>1.7.30</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.2</version>
<version>1.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<artifactId>mockito-core</artifactId>
<version>3.5.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.16</version>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>

<!-- Runtime -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<developers>
Expand Down Expand Up @@ -109,18 +133,18 @@
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<version>3.8.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
<compilerArgument>-nowarn</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
<version>3.1.0</version>
<executions>
<execution>
<id>checkstyle</id>
Expand Down Expand Up @@ -159,7 +183,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<version>1.6.8</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
Expand All @@ -170,7 +194,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -183,7 +207,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package org.crazycake.shiro;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.crazycake.shiro.common.SessionInMemory;
import org.crazycake.shiro.exception.SerializationException;
import org.crazycake.shiro.integration.fixture.model.FakeSession;
import org.crazycake.shiro.serializer.StringSerializer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;

import static org.crazycake.shiro.integration.fixture.TestFixture.*;

/**
* RedisSessionDAO integration test was put under org.crazycake.shiro
* is because I want to test protected method `doReadSession`
*/
public class RedisSessionDAOIntegrationTest {

private RedisSessionDAO redisSessionDAO;
private FakeSession session1;
private FakeSession session2;
private FakeSession emptySession;
private String name1;
private String prefix;
private void blast() {
blastRedis();
}

private void scaffold() {
prefix = scaffoldPrefix();
RedisManager redisManager = scaffoldStandaloneRedisManager();
redisSessionDAO = scaffoldRedisSessionDAO(redisManager, prefix);
session1 = scaffoldSession();
session2 = scaffoldSession();
emptySession = scaffoldEmptySession();
name1 = scaffoldUsername();
}

@BeforeEach
public void setUp() {
blast();
scaffold();
}

@AfterEach
public void tearDown() {
blast();
}

@Test
public void testDoCreateNull() {
Assertions.assertThrows(UnknownSessionException.class, () -> {
redisSessionDAO.doCreate(null);
});
}

@Test
public void testDoCreate() {
redisSessionDAO.doCreate(session1);
Session actualSession = redisSessionDAO.doReadSession(session1.getId());
assertSessionEquals(actualSession, session1);
}

@Test
public void testDoCreateWithSessionTimeout() {
doSetSessionDAOExpire(redisSessionDAO, -2);
redisSessionDAO.doCreate(session2);
assertEquals(getRedisTTL(prefix + session2.getId(), new StringSerializer()), 1800L);
}

@Test
public void testUpdateNull() {
Assertions.assertThrows(UnknownSessionException.class, () -> {
redisSessionDAO.update(null);
});
}

@Test
public void testUpdateEmptySession() {
Assertions.assertThrows(UnknownSessionException.class, () -> {
redisSessionDAO.update(emptySession);
});
}

@Test
public void testUpdate() {
redisSessionDAO.doCreate(session1);
redisSessionDAO.doReadSession(session1.getId());
doChangeSessionName(session1, name1);
redisSessionDAO.update(session1);
FakeSession actualSession = (FakeSession)redisSessionDAO.doReadSession(session1.getId());
assertEquals(actualSession.getName(), name1);
}

@Test
public void testUpdateWithoutSessionInMemory() {
redisSessionDAO.setSessionInMemoryEnabled(false);
redisSessionDAO.doCreate(session1);
redisSessionDAO.doReadSession(session1.getId());
doChangeSessionName(session1, name1);
redisSessionDAO.update(session1);
FakeSession actualSession = (FakeSession)redisSessionDAO.doReadSession(session1.getId());
assertEquals(actualSession.getName(), name1);
}

@Test
public void testDelete() {
redisSessionDAO.doCreate(session1);
redisSessionDAO.delete(session1);
assertRedisEmpty();
}

@Test
public void testGetActiveSessions() {
redisSessionDAO.doCreate(session1);
redisSessionDAO.doCreate(session2);
Collection<Session> activeSessions = redisSessionDAO.getActiveSessions();
assertEquals(activeSessions.size(), 2);
}

@Test
public void testRemoveExpiredSessionInMemory() throws InterruptedException, SerializationException {
redisSessionDAO.setSessionInMemoryTimeout(500L);
redisSessionDAO.doCreate(session1);
redisSessionDAO.doReadSession(session1.getId());
Thread.sleep(1000);
redisSessionDAO.doCreate(session2);
redisSessionDAO.doReadSession(session2.getId());
Map<Serializable, SessionInMemory> sessionMap = (Map<Serializable, SessionInMemory>) redisSessionDAO.getSessionsInThread().get();
assertEquals(sessionMap.size(), 1);
}
}
Loading

0 comments on commit d2b9eab

Please sign in to comment.