001: package org.objectweb.celtix.bus.resource;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.InputStream;
005: import junit.framework.Test;
006: import junit.framework.TestCase;
007: import junit.framework.TestSuite;
008: import junit.textui.TestRunner;
009: import org.easymock.classextension.EasyMock;
010: import org.objectweb.celtix.Bus;
011: import org.objectweb.celtix.configuration.Configuration;
012: import org.objectweb.celtix.configuration.types.StringListType;
013: import org.objectweb.celtix.resource.ClassLoaderResolver;
014: import org.objectweb.celtix.resource.ClasspathResolver;
015: import org.objectweb.celtix.resource.ResourceManager;
016: import org.objectweb.celtix.resource.ResourceResolver;
017:
018: public class ResourceManagerImplTest extends TestCase {
019:
020: private static final String TEST_RESOURCE = "this is the found resource";
021: private static final String TEST_RESOURCE_NAME = "testResource";
022: private static final int DEFAULT_RESOLVER_COUNT = 2;
023:
024: private ResourceManager mgr = new ResourceManagerImpl();
025: private final ResourceResolver resolver1 = EasyMock
026: .createMock(ResourceResolver.class);
027: private final ResourceResolver resolver2 = EasyMock
028: .createMock(ResourceResolver.class);
029:
030: public void setUp() {
031: mgr.addResourceResolver(resolver2);
032: mgr.addResourceResolver(resolver1);
033: }
034:
035: public void testDefaultResolvers() {
036:
037: mgr = new ResourceManagerImpl();
038: assertNotNull(mgr.getResourceResolvers());
039: assertEquals(DEFAULT_RESOLVER_COUNT, mgr.getResourceResolvers()
040: .size());
041: assertEquals(ClassLoaderResolver.class, mgr
042: .getResourceResolvers().get(0).getClass());
043: assertEquals(ClasspathResolver.class, mgr
044: .getResourceResolvers().get(1).getClass());
045: }
046:
047: public void testGetResourceAsStream() {
048:
049: ByteArrayInputStream resourceStream = new ByteArrayInputStream(
050: TEST_RESOURCE.getBytes());
051:
052: resolver1.getAsStream(TEST_RESOURCE_NAME);
053: EasyMock.expectLastCall().andReturn(resourceStream);
054: EasyMock.replay(resolver1);
055: EasyMock.replay(resolver2);
056:
057: InputStream ret = mgr.getResourceAsStream(TEST_RESOURCE_NAME);
058: assertSame(resourceStream, ret);
059:
060: EasyMock.verify(resolver1);
061: EasyMock.verify(resolver2);
062:
063: EasyMock.reset(resolver1);
064: EasyMock.reset(resolver2);
065:
066: // do it again but let the second resolver handler it
067:
068: resolver1.getAsStream(TEST_RESOURCE_NAME);
069: EasyMock.expectLastCall().andReturn(null);
070: resolver2.getAsStream(TEST_RESOURCE_NAME);
071: EasyMock.expectLastCall().andReturn(resourceStream);
072:
073: EasyMock.replay(resolver1);
074: EasyMock.replay(resolver2);
075:
076: ret = mgr.getResourceAsStream(TEST_RESOURCE_NAME);
077: assertSame(resourceStream, ret);
078:
079: EasyMock.verify(resolver1);
080: EasyMock.verify(resolver2);
081: }
082:
083: public void testResolveResource() {
084:
085: resolver1.resolve(TEST_RESOURCE_NAME, String.class);
086: EasyMock.expectLastCall().andReturn(TEST_RESOURCE);
087: EasyMock.replay(resolver1);
088: EasyMock.replay(resolver2);
089:
090: Object ret = mgr.resolveResource(TEST_RESOURCE_NAME,
091: String.class);
092: assertEquals(TEST_RESOURCE, ret);
093:
094: EasyMock.verify(resolver1);
095: EasyMock.verify(resolver2);
096:
097: EasyMock.reset(resolver1);
098: EasyMock.reset(resolver2);
099:
100: // do it again but let the second resolver handler it
101:
102: resolver1.resolve(TEST_RESOURCE_NAME, String.class);
103: EasyMock.expectLastCall().andReturn(null);
104: EasyMock.replay(resolver1);
105: resolver2.resolve(TEST_RESOURCE_NAME, String.class);
106: EasyMock.expectLastCall().andReturn(TEST_RESOURCE);
107: EasyMock.replay(resolver2);
108:
109: ret = mgr.resolveResource(TEST_RESOURCE_NAME, String.class);
110: assertEquals(TEST_RESOURCE, ret);
111:
112: EasyMock.verify(resolver1);
113: EasyMock.verify(resolver2);
114: }
115:
116: public void testAddRemoveResourceResolver() {
117:
118: // use a clean one for this test
119: mgr = new ResourceManagerImpl();
120: assertEquals(DEFAULT_RESOLVER_COUNT, mgr.getResourceResolvers()
121: .size());
122: mgr.addResourceResolver(resolver1);
123: assertEquals(1 + DEFAULT_RESOLVER_COUNT, mgr
124: .getResourceResolvers().size());
125: mgr.addResourceResolver(resolver1);
126: assertEquals(1 + DEFAULT_RESOLVER_COUNT, mgr
127: .getResourceResolvers().size());
128:
129: mgr.removeResourceResolver(resolver1);
130: assertEquals(DEFAULT_RESOLVER_COUNT, mgr.getResourceResolvers()
131: .size());
132:
133: mgr.addResourceResolver(resolver1);
134: mgr.addResourceResolver(resolver2);
135: assertEquals(2 + DEFAULT_RESOLVER_COUNT, mgr
136: .getResourceResolvers().size());
137: mgr.removeResourceResolver(resolver1);
138: assertEquals(1 + DEFAULT_RESOLVER_COUNT, mgr
139: .getResourceResolvers().size());
140: mgr.removeResourceResolver(resolver2);
141: assertEquals(DEFAULT_RESOLVER_COUNT, mgr.getResourceResolvers()
142: .size());
143:
144: mgr.removeResourceResolver(resolver1);
145: assertEquals(DEFAULT_RESOLVER_COUNT, mgr.getResourceResolvers()
146: .size());
147: }
148:
149: public void testCtorWithBus() throws Exception {
150:
151: Bus bus = EasyMock.createMock(Bus.class);
152: Configuration conf = EasyMock.createMock(Configuration.class);
153:
154: StringListType resolverList = new StringListType();
155: resolverList.getItem().add(ClassLoaderResolver.class.getName());
156: resolverList.getItem().add(ClasspathResolver.class.getName());
157:
158: new ClassLoaderResolver();
159:
160: bus.getConfiguration();
161: EasyMock.expectLastCall().andReturn(conf);
162: conf.getObject("resourceResolvers");
163: EasyMock.expectLastCall().andReturn(resolverList);
164:
165: EasyMock.replay(bus);
166: EasyMock.replay(conf);
167:
168: ResourceManagerImpl rmi = new ResourceManagerImpl(bus);
169:
170: assertNotNull(rmi.getResourceResolvers());
171: assertEquals(2, rmi.getResourceResolvers().size());
172: assertEquals(ClassLoaderResolver.class, rmi
173: .getResourceResolvers().get(0).getClass());
174: assertEquals(ClasspathResolver.class, rmi
175: .getResourceResolvers().get(1).getClass());
176:
177: EasyMock.verify(bus);
178: EasyMock.verify(conf);
179: }
180:
181: public static Test suite() {
182: return new TestSuite(ResourceManagerImplTest.class);
183: }
184:
185: public static void main(String[] args) {
186: TestRunner.main(new String[] { ResourceManagerImplTest.class
187: .getName() });
188: }
189: }
|