Skip to content

Commit

Permalink
Merge pull request #90 from theo/support-string
Browse files Browse the repository at this point in the history
#89 Add support for strings being the Principal
  • Loading branch information
alexxiyang authored Feb 14, 2019
2 parents 8ca8e11 + 24f5d85 commit e2f4cdd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/crazycake/shiro/RedisCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ private String getStringRedisKey(K key) {

private String getRedisKeyFromPrincipalIdField(PrincipalCollection key) {
Object principalObject = key.getPrimaryPrincipal();
if (principalObject instanceof String) {
return principalObject.toString();
}
Method pincipalIdGetter = getPrincipalIdGetter(principalObject);
return getIdObj(principalObject, pincipalIdGetter);
}
Expand Down
29 changes: 23 additions & 6 deletions src/test/java/org/crazycake/shiro/RedisCacheTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package org.crazycake.shiro;

import static fixture.TestFixture.*;
import static org.junit.Assert.fail;

import java.util.Properties;
import java.util.Set;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.crazycake.shiro.exception.CacheManagerPrincipalIdNotAssignedException;
import org.crazycake.shiro.exception.PrincipalInstanceException;
import org.crazycake.shiro.model.*;
import org.crazycake.shiro.model.FakeAuth;
import org.crazycake.shiro.model.UserInfo;
import org.crazycake.shiro.serializer.ObjectSerializer;
import org.crazycake.shiro.serializer.StringSerializer;
import org.junit.Before;
import org.junit.Test;
import java.util.Properties;
import java.util.Set;

import static fixture.TestFixture.turnUserToFakeAuth;
import static org.junit.Assert.fail;
import static fixture.TestFixture.*;
import com.github.javafaker.Faker;

/**
* input key, value (java)
Expand All @@ -25,10 +29,14 @@ public class RedisCacheTest {
private RedisCache<PrincipalCollection, FakeAuth> redisCache;
private RedisCache<PrincipalCollection, FakeAuth> redisCacheWithPrincipalIdFieldName;
private RedisCache<PrincipalCollection, FakeAuth> redisCacheWithEmptyPrincipalIdFieldName;
private RedisCache<PrincipalCollection, String> redisCacheWithStrings;

private Properties properties = loadProperties("shiro-standalone.ini");
private PrincipalCollection user1;
private PrincipalCollection user2;
private PrincipalCollection user3;
private PrincipalCollection user4;

private Set users1_2_3;
private String prefix;

Expand All @@ -42,9 +50,11 @@ private void scaffold() {
redisCache = scaffoldRedisCache(redisManager, new StringSerializer(), new ObjectSerializer(), prefix, NumberUtils.toInt(properties.getProperty("cacheManager.expire")), RedisCacheManager.DEFAULT_PRINCIPAL_ID_FIELD_NAME);
redisCacheWithPrincipalIdFieldName = scaffoldRedisCache(redisManager, new StringSerializer(), new ObjectSerializer(), prefix, NumberUtils.toInt(properties.getProperty("cacheManager.expire")), properties.getProperty("cacheManager.principalIdFieldName"));
redisCacheWithEmptyPrincipalIdFieldName = scaffoldRedisCache(redisManager, new StringSerializer(), new ObjectSerializer(), prefix, NumberUtils.toInt(properties.getProperty("cacheManager.expire")), "");
redisCacheWithStrings = scaffoldRedisCache(redisManager, new StringSerializer(), new ObjectSerializer(), prefix, NumberUtils.toInt(properties.getProperty("cacheManager.expire")), properties.getProperty("cacheManager.principalIdFieldName"));
user1 = scaffoldAuthKey(scaffoldUser());
user2 = scaffoldAuthKey(scaffoldUser());
user3 = scaffoldAuthKey(scaffoldUser());
user4 = new SimplePrincipalCollection(Faker.instance().gameOfThrones().character(), Faker.instance().gameOfThrones().city());
users1_2_3 = scaffoldKeys(user1, user2, user3);
}

Expand Down Expand Up @@ -94,6 +104,13 @@ public void testPut() {
FakeAuth fakeAuth = redisCache.get(user1);
assertAuthEquals(fakeAuth, turnUserToFakeAuth((UserInfo)user1.getPrimaryPrincipal()));
}

@Test
public void testPutString() {
redisCacheWithStrings.put(user4, user4.getPrimaryPrincipal().toString());
String auth = redisCacheWithStrings.get(user4);
assertEquals(auth, user4.getPrimaryPrincipal());
}

@Test
public void testSize() throws InterruptedException {
Expand Down

0 comments on commit e2f4cdd

Please sign in to comment.