user data factory pattern
diff --git a/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java b/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java
index 1309458..306eade 100644
--- a/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java
+++ b/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java
@@ -695,13 +695,13 @@
}
public <T extends Userdata> T getUserData(User user, Class<T> clazz) {
- UserDataDbIface<T> dao = UserdataFactory.getInstance(clazz);
+ UserDataDbIface<T> dao = UserdataFactory.getDaoInstance(clazz);
return dao.get(user);
}
//todo: cache userdata outside of the user object!
- public void updateUserData(User user, Userdata data) {
- UserDataDbIface dao = UserdataFactory.getInstance(data.getClass());
+ public void updateUserData(Userdata data) {
+ UserDataDbIface dao = UserdataFactory.getDaoInstance(data.getClass());
dao.update(data);
}
diff --git a/src/main/java/de/ids_mannheim/korap/user/UserdataFactory.java b/src/main/java/de/ids_mannheim/korap/user/UserdataFactory.java
index 43000e1..6b02d28 100644
--- a/src/main/java/de/ids_mannheim/korap/user/UserdataFactory.java
+++ b/src/main/java/de/ids_mannheim/korap/user/UserdataFactory.java
@@ -39,10 +39,11 @@
return null;
}
- public static UserDataDbIface getInstance(Class<? extends Userdata> data) {
+ public static UserDataDbIface getDaoInstance(
+ Class<? extends Userdata> data) {
if (instances.get(data) == null) {
Class<? extends UserDataDbIface> cl = getClass(data);
- if (BeanConfiguration.hasContext()) {
+ if (BeanConfiguration.hasContext() && cl != null) {
try {
Constructor c = cl.getConstructor(PersistenceClient.class);
UserDataDbIface iface = (UserDataDbIface) c.newInstance(
@@ -53,10 +54,10 @@
}catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
return null;
}
-
}
+ throw new RuntimeException(
+ "No database class found for type " + data.getSimpleName());
}else
return instances.get(data);
- return null;
}
}
diff --git a/src/test/java/UserdataTest.java b/src/test/java/UserdataTest.java
index fc9e27e..a10a480 100644
--- a/src/test/java/UserdataTest.java
+++ b/src/test/java/UserdataTest.java
@@ -1,9 +1,9 @@
import de.ids_mannheim.korap.config.BeanConfiguration;
-import de.ids_mannheim.korap.user.KorAPUser;
-import de.ids_mannheim.korap.user.User;
-import de.ids_mannheim.korap.user.UserDetailsDao;
-import de.ids_mannheim.korap.user.Userdetails2;
-import org.junit.*;
+import de.ids_mannheim.korap.user.*;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
/**
* @author hanl
@@ -58,4 +58,21 @@
assert "value is a value".equals(d.get("key_1"));
}
+ @Test
+ public void testUserdatafactory() {
+ UserDataDbIface dao = UserdataFactory
+ .getDaoInstance(Userdetails2.class);
+ assert UserDetailsDao.class.equals(dao.getClass());
+ }
+
+ @Test(expected = RuntimeException.class)
+ public void testUserdatafactoryError() {
+ UserdataFactory.getDaoInstance(new Userdata(1) {
+ @Override
+ public String[] requiredFields() {
+ return new String[0];
+ }
+ }.getClass());
+ }
+
}
diff --git a/src/test/java/de/ids_mannheim/korap/config/ConfigTest.java b/src/test/java/de/ids_mannheim/korap/config/ConfigTest.java
index ded5cff..d4f2ad5 100644
--- a/src/test/java/de/ids_mannheim/korap/config/ConfigTest.java
+++ b/src/test/java/de/ids_mannheim/korap/config/ConfigTest.java
@@ -6,8 +6,6 @@
import de.ids_mannheim.korap.security.ac.ResourceFinder;
import de.ids_mannheim.korap.user.Attributes;
import de.ids_mannheim.korap.user.User;
-import de.ids_mannheim.korap.user.UserdataFactory;
-import de.ids_mannheim.korap.user.Userdetails2;
import de.ids_mannheim.korap.utils.ServiceVersion;
import de.ids_mannheim.korap.utils.TimeUtils;
import org.junit.After;
@@ -78,11 +76,6 @@
BeanConfiguration.getBeans().getEncryption()
.validateEntry(v, Attributes.EMAIL);
}
-
- @Test
- public void testTypeReference() {
- UserdataFactory.getInstance(Userdetails2.class);
- }
}