Skip to content

Commit 565e45b

Browse files
committed
Added the junit test for the ObjectmanagerFactory class.
1 parent de4e435 commit 565e45b

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/main/java/org/dataone/cn/indexer/object/ObjectManagerFactory.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
*/
1212
public class ObjectManagerFactory {
1313
private static volatile ObjectManager manager = null;
14-
private static String className = "org.dataone.cn.indexer.object.hashstore.HashStoreObjManager";
14+
private static final String DEFAULT_ClASS_NAME = "org.dataone.cn.indexer.object.hashstore"
15+
+ ".HashStoreObjManager";
1516
private static Log logger = LogFactory.getLog(ObjectManagerFactory.class);
1617
private static final String OBJECT_MANAGER_CLASSNAME_ENV =
1718
"DATAONE_INDEXER_OBJECT_MANAGER_CLASSNAME";
@@ -32,6 +33,7 @@ public static ObjectManager getObjectManager()
3233
throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
3334
InstantiationException, IllegalAccessException {
3435
String classNameFromEnv = System.getenv(OBJECT_MANAGER_CLASSNAME_ENV);
36+
String className = DEFAULT_ClASS_NAME;
3537
if (classNameFromEnv != null && !classNameFromEnv.isBlank()) {
3638
logger.debug("The ObjectManager class name form env variable "
3739
+ OBJECT_MANAGER_CLASSNAME_ENV + " is " + classNameFromEnv);
@@ -49,4 +51,11 @@ public static ObjectManager getObjectManager()
4951
}
5052
return manager;
5153
}
54+
55+
/**
56+
* This method is for testing only
57+
*/
58+
protected static void resetManagerNull() {
59+
manager = null;
60+
}
5261
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.dataone.cn.indexer.object;
2+
3+
import org.dataone.cn.indexer.object.hashstore.HashStoreObjManager;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import uk.org.webcompere.systemstubs.rules.EnvironmentVariablesRule;
7+
8+
import static org.junit.Assert.assertTrue;
9+
import static org.junit.Assert.fail;
10+
11+
/**
12+
* A Junit test class for ObjectManagerFactory
13+
* @author Tao
14+
*/
15+
public class ObjectManagerFactoryTest {
16+
17+
private static final String envName = "DATAONE_INDEXER_OBJECT_MANAGER_CLASSNAME";
18+
@Rule
19+
public EnvironmentVariablesRule environmentVariablesClassName =
20+
new EnvironmentVariablesRule(envName, null);
21+
/**
22+
* Test to create a HashStoreObjManager
23+
* @throws Exception
24+
*/
25+
@Test
26+
public void testHashStoreObjManager() throws Exception {
27+
environmentVariablesClassName.set(envName, null);
28+
ObjectManagerFactory.resetManagerNull();
29+
ObjectManager manager = ObjectManagerFactory.getObjectManager();
30+
assertTrue(manager instanceof HashStoreObjManager);
31+
}
32+
33+
/**
34+
* Test the failure with a wrong class name
35+
* @throws Exception
36+
*/
37+
@Test
38+
public void testWrongClassName() throws Exception {
39+
environmentVariablesClassName.set(envName, "foo.foo1.className");
40+
ObjectManagerFactory.resetManagerNull();
41+
try {
42+
ObjectManager manager = ObjectManagerFactory.getObjectManager();
43+
fail("Test shouldn't get here since the class doesn't exist with the given name.");
44+
} catch (Exception e) {
45+
assertTrue( e instanceof ClassNotFoundException);
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)