KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > beans > config > ModuleRegistrationTest


1 package info.magnolia.cms.beans.config;
2
3 import info.magnolia.cms.module.DependencyDefinition;
4 import info.magnolia.cms.module.ModuleDefinition;
5 import info.magnolia.cms.util.FactoryUtil;
6 import junit.framework.TestCase;
7
8 import java.io.File JavaDoc;
9 import java.net.URL JavaDoc;
10
11 /**
12  * @author fgiust
13  * @version $Revision: 7661 $ ($Author: gjoseph $)
14  */

15 public class ModuleRegistrationTest extends TestCase {
16     private static final File JavaDoc MGNL_CORE_ROOT = new File JavaDoc(ModuleRegistrationTest.class.getResource("/testresource.txt").getFile()).
17             getParentFile().getParentFile().getParentFile();
18
19     /**
20      * @see junit.framework.TestCase#setUp()
21      */

22     public void setUp() throws Exception JavaDoc {
23         FactoryUtil.setDefaultImplementation(ModuleRegistration.class, ModuleRegistration.class.getName());
24         super.setUp();
25     }
26
27     public void testGetModuleRootJar() {
28         URL JavaDoc xmlUrl = getClass().getResource("/testjar.jar");
29         File JavaDoc moduleRoot = new ModuleRegistration().getModuleRoot(xmlUrl);
30
31         assertNotNull("Unable to find jar", moduleRoot);
32         assertTrue("Jar " + moduleRoot.getAbsolutePath() + " does not exist", moduleRoot.exists());
33         assertEquals(MGNL_CORE_ROOT, moduleRoot);
34     }
35
36     public void testGetModuleRootJarWIthDot() {
37         URL JavaDoc xmlUrl = getClass().getResource("/.space test/testjar.jar");
38         File JavaDoc moduleRoot = new ModuleRegistration().getModuleRoot(xmlUrl);
39
40         assertNotNull("Unable to find jar", moduleRoot);
41         assertTrue("Jar " + moduleRoot.getAbsolutePath() + " does not exist", moduleRoot.exists());
42         assertEquals(new File JavaDoc(MGNL_CORE_ROOT, "target"), moduleRoot);
43     }
44
45     public void testGetModuleRootDirectory() {
46         File JavaDoc moduleRoot = new ModuleRegistration().getModuleRoot("/testresource.txt");
47
48         assertNotNull(moduleRoot);
49         assertTrue(moduleRoot.exists());
50         assertEquals(MGNL_CORE_ROOT, moduleRoot);
51     }
52
53     public void testGetModuleRootDirectoryWithDot() {
54         File JavaDoc moduleRoot = new ModuleRegistration().getModuleRoot("/.space test/testresource.txt");
55
56         assertNotNull(moduleRoot);
57         assertTrue(moduleRoot.exists());
58         assertEquals(new File JavaDoc(MGNL_CORE_ROOT, "target"), moduleRoot);
59     }
60
61     public void testCalcDepencyLevelWithNonOptionalDependencies() {
62         final ModuleDefinition modDefA = new ModuleDefinition("mod-a", "v1", "fake.Module");
63         final ModuleDefinition modDefB = new ModuleDefinition("mod-b", "v1", "fake.Module");
64         final ModuleDefinition modDefC = new ModuleDefinition("mod-c", "v1", "fake.Module");
65         final DependencyDefinition depOnA = new DependencyDefinition();
66         depOnA.setName("mod-a");
67         depOnA.setVersion("v1");
68         final DependencyDefinition depOnB = new DependencyDefinition();
69         depOnB.setName("mod-b");
70         depOnB.setVersion("v1");
71         modDefB.addDependency(depOnA);
72         modDefC.addDependency(depOnB);
73
74         final ModuleRegistration reg = new ModuleRegistration();
75         reg.getModuleDefinitions().put(modDefA.getName(), modDefA);
76         reg.getModuleDefinitions().put(modDefB.getName(), modDefB);
77         reg.getModuleDefinitions().put(modDefC.getName(), modDefC);
78
79         assertEquals(0, reg.calcDependencyLevel(modDefA));
80         assertEquals(1, reg.calcDependencyLevel(modDefB));
81         assertEquals(2, reg.calcDependencyLevel(modDefC));
82     }
83
84     public void testCalcDepencyLevelIgnoresUnregisteredOptionalDependencies() {
85         final ModuleDefinition modDefB = new ModuleDefinition("mod-b", "v1", "fake.Module");
86         final ModuleDefinition modDefC = new ModuleDefinition("mod-c", "v1", "fake.Module");
87         final DependencyDefinition depOnA = new DependencyDefinition();
88         depOnA.setName("mod-a");
89         depOnA.setVersion("v1");
90         depOnA.setOptional(true);
91         final DependencyDefinition depOnB = new DependencyDefinition();
92         depOnB.setName("mod-b");
93         depOnB.setVersion("v1");
94         modDefC.addDependency(depOnA);
95         modDefC.addDependency(depOnB);
96
97         final ModuleRegistration reg = new ModuleRegistration();
98         // mod-a is not registered in this case
99
reg.getModuleDefinitions().put(modDefB.getName(), modDefB);
100         reg.getModuleDefinitions().put(modDefC.getName(), modDefC);
101
102         assertEquals(0, reg.calcDependencyLevel(modDefB));
103         assertEquals(1, reg.calcDependencyLevel(modDefC));
104     }
105     
106     public void testCalcDepencyLevelDoesNotIgnoreRegisteredOptionalDependencies() {
107         final ModuleDefinition modDefA = new ModuleDefinition("mod-a", "v1", "fake.Module");
108         final ModuleDefinition modDefB = new ModuleDefinition("mod-b", "v1", "fake.Module");
109         final ModuleDefinition modDefC = new ModuleDefinition("mod-c", "v1", "fake.Module");
110         final DependencyDefinition depOnA = new DependencyDefinition();
111         depOnA.setName("mod-a");
112         depOnA.setVersion("v1");
113         depOnA.setOptional(true);
114         final DependencyDefinition depOnB = new DependencyDefinition();
115         depOnB.setName("mod-b");
116         depOnB.setVersion("v1");
117         modDefB.addDependency(depOnA);
118         modDefC.addDependency(depOnA);
119         modDefC.addDependency(depOnB);
120
121         final ModuleRegistration reg = new ModuleRegistration();
122         reg.getModuleDefinitions().put(modDefA.getName(), modDefA);
123         reg.getModuleDefinitions().put(modDefB.getName(), modDefB);
124         reg.getModuleDefinitions().put(modDefC.getName(), modDefC);
125
126         assertEquals(0, reg.calcDependencyLevel(modDefA));
127         assertEquals(1, reg.calcDependencyLevel(modDefB));
128         assertEquals(2, reg.calcDependencyLevel(modDefC));
129     }
130
131 }
132
Popular Tags