001: package org.jpox.plugin;
002:
003: import java.util.HashSet;
004: import java.util.Map;
005:
006: import junit.framework.TestCase;
007:
008: import org.apache.log4j.Appender;
009: import org.apache.log4j.Layout;
010: import org.apache.log4j.Level;
011: import org.apache.log4j.Logger;
012: import org.apache.log4j.spi.ErrorHandler;
013: import org.apache.log4j.spi.Filter;
014: import org.apache.log4j.spi.LoggingEvent;
015: import org.jpox.ClassLoaderResolver;
016: import org.jpox.JDOClassLoaderResolver;
017: import org.jpox.exceptions.JPOXException;
018: import org.jpox.exceptions.JPOXUserException;
019:
020: public class PluginParserTest extends TestCase {
021: public void testParseExtensionPoint() {
022: ClassLoaderResolver clr = new JDOClassLoaderResolver();
023: NonManagedPluginRegistry mgr = new NonManagedPluginRegistry(
024: clr, "EXCEPTION");
025: assertEquals(0, mgr.getExtensionPoints().length);
026: Bundle bundle0 = mgr.registerBundle(clr.getResource(
027: "/org/jpox/samples/plugin/MANIFEST0.MF", null));
028: mgr.registerPluginExtensions(clr.getResource(
029: "/org/jpox/samples/plugin/plugin1expoint.xml", null),
030: bundle0);
031: assertEquals(2, mgr.getExtensionPoints().length);
032: assertNull(mgr.getExtensionPoint("testID"));
033: assertNull(mgr.getExtensionPoint("testID2"));
034: assertNotNull(mgr.getExtensionPoint("org.jpox.testID"));
035: assertNotNull(mgr.getExtensionPoint("org.jpox.testID2"));
036: ExtensionPoint point = mgr.getExtensionPoint("org.jpox.testID");
037: assertEquals("testID", point.getId());
038: assertEquals("org.jpox.testID", point.getUniqueId());
039: assertEquals("testName", point.getName());
040: assertNotNull(clr.getResource(
041: "/org/jpox/samples/plugin/plugin1.xsd", null));
042: assertEquals(clr.getResource(
043: "/org/jpox/samples/plugin/plugin1.xsd", null), point
044: .getSchema());
045: assertEquals(0, point.getExtensions().length);
046: mgr.registerPluginExtensions(clr.getResource(
047: "/org/jpox/samples/plugin/plugin1.xml", null), bundle0);
048: assertEquals(2, point.getExtensions().length);
049: Extension[] exts = point.getExtensions();
050: assertEquals(exts[0].getPlugin(), exts[1].getPlugin());
051: assertEquals(2, exts[0].getConfigurationElements().length);
052:
053: ConfigurationElement[] level1 = exts[0]
054: .getConfigurationElements();
055: assertEquals(2, level1[0].getChildren().length);
056:
057: assertEquals("level1", level1[0].getName());
058: assertEquals(1, level1[0].getAttributeNames().length);
059: assertEquals("1", level1[0].getAttribute("attr11"));
060: assertNull(level1[0].getAttribute("attr11XXX"));
061:
062: ConfigurationElement[] level2 = level1[0].getChildren();
063: assertEquals(1, level2[0].getChildren().length);
064: assertEquals("level2", level2[0].getName());
065: assertEquals(2, level2[0].getAttributeNames().length);
066: assertEquals("attr21", level2[0].getAttributeNames()[0]);
067: assertEquals("attr22", level2[0].getAttributeNames()[1]);
068: assertEquals("2211", level2[0].getAttribute("attr21"));
069: assertEquals("2221", level2[0].getAttribute("attr22"));
070: assertNull(level2[0].getAttribute("attr11XXX"));
071:
072: assertEquals(0, level1[1].getChildren().length);
073: assertEquals("2", level1[1].getAttribute("attr11"));
074:
075: assertEquals(1, exts[1].getConfigurationElements().length);
076: level1 = exts[1].getConfigurationElements();
077: assertEquals("A", level1[0].getAttribute("attr11"));
078: assertEquals(0, level1[0].getChildren().length);
079: }
080:
081: public void testParseSymbolicName() {
082: ClassLoaderResolver clr = new JDOClassLoaderResolver();
083: NonManagedPluginRegistry mgr = new NonManagedPluginRegistry(
084: clr, "EXCEPTION");
085: Bundle bundle1 = mgr.registerBundle(clr.getResource(
086: "/org/jpox/samples/plugin/MANIFEST1.MF", null));
087: mgr.registerPluginExtensions(clr.getResource(
088: "/org/jpox/samples/plugin/plugin1expoint.xml", null),
089: bundle1);
090: assertEquals(2, mgr.getExtensionPoints().length);
091: assertNull(mgr.getExtensionPoint("testID"));
092: assertNull(mgr.getExtensionPoint("testID2"));
093: assertNotNull(mgr
094: .getExtensionPoint("org.jpox.plugin.test1.testID"));
095: assertNotNull(mgr
096: .getExtensionPoint("org.jpox.plugin.test1.testID2"));
097:
098: Bundle bundle2 = mgr.registerBundle(clr.getResource(
099: "/org/jpox/samples/plugin/MANIFEST2.MF", null));
100: mgr.registerPluginExtensions(clr.getResource(
101: "/org/jpox/samples/plugin/plugin1expoint.xml", null),
102: bundle2);
103: assertEquals(4, mgr.getExtensionPoints().length);
104: assertNotNull(mgr
105: .getExtensionPoint("org.jpox.plugin.test2.testID"));
106: assertNotNull(mgr
107: .getExtensionPoint("org.jpox.plugin.test2.testID2"));
108:
109: Extension[] ex = mgr.getExtensionPoint(
110: "org.jpox.plugin.test2.testID").getExtensions();
111: assertEquals(ex.length, 0);
112: /*assertEquals("org.jpox.plugin.test2",ex[ex.length-1].getPlugin().getSymbolicName());*/
113: }
114:
115: /**
116: * NonManagedPluginRegistry cannot handle multiple versions
117: * of the same plugin, so it must raise an exception
118: */
119: public void testDuplicatedBundleSymbolicNameSameOrDifferentVersion() {
120: ClassLoaderResolver clr = new JDOClassLoaderResolver();
121: NonManagedPluginRegistry mgr = new NonManagedPluginRegistry(
122: clr, "EXCEPTION");
123: mgr.registerBundle(clr.getResource(
124: "/org/jpox/samples/plugin/MANIFEST1.MF", null));
125: try {
126: mgr.registerBundle(clr.getResource(
127: "/org/jpox/samples/plugin/MANIFEST1-1.MF", null));
128: fail("Expected JPOXException");
129: } catch (JPOXException ex) {
130: //expected
131: }
132: }
133:
134: public void testParser() {
135: PluginParser.Parser p1 = new PluginParser.Parser(
136: "org.jpox,org.jpox.enhancer");
137: assertEquals("org.jpox", p1.parseSymbolicName());
138: assertEquals(0, p1.parseParameters().size());
139: assertEquals("org.jpox.enhancer", p1.parseSymbolicName());
140: assertNull(p1.parseSymbolicName());
141: assertEquals(0, p1.parseParameters().size());
142:
143: PluginParser.Parser p2 = new PluginParser.Parser(
144: "org.jpox;value=arg;value2=arg2;value3:=\"arg3\",org.jpox.enhancer,org.jpox.test;test=e,org.jpox.test2");
145: assertEquals("org.jpox", p2.parseSymbolicName());
146: Map p = p2.parseParameters();
147: assertEquals(3, p.size());
148: assertEquals("arg", p.get("value"));
149: assertEquals("arg3", p.get("value3"));
150: assertEquals("arg2", p.get("value2"));
151: assertEquals("org.jpox.enhancer", p2.parseSymbolicName());
152: assertEquals("org.jpox.test", p2.parseSymbolicName());
153: p = p2.parseParameters();
154: assertEquals(1, p.size());
155: assertEquals("e", p.get("test"));
156: assertEquals("org.jpox.test2", p2.parseSymbolicName());
157: }
158:
159: public void testParserInvalidCharsIgnored() {
160: PluginParser.Parser p1 = new PluginParser.Parser(
161: "org.jpox,org.jpox.enhancer,org.jpox.test**,org.jpox.tt");
162: assertEquals("org.jpox", p1.parseSymbolicName());
163: assertEquals("org.jpox.enhancer", p1.parseSymbolicName());
164: assertEquals("org.jpox.test", p1.parseSymbolicName());
165: try {
166: p1.parseSymbolicName();
167: fail("expected exception");
168: } catch (JPOXUserException e) {
169: }
170: }
171:
172: public void testRequireBundle() {
173: ClassLoaderResolver clr = new JDOClassLoaderResolver();
174: NonManagedPluginRegistry mgr = new NonManagedPluginRegistry(
175: clr, "EXCEPTION");
176: assertEquals(0, mgr.getExtensionPoints().length);
177: Bundle bundle3 = mgr.registerBundle(clr.getResource(
178: "/org/jpox/samples/plugin/MANIFEST3.MF", null));
179: Bundle bundle4 = mgr.registerBundle(clr.getResource(
180: "/org/jpox/samples/plugin/MANIFEST4.MF", null));
181: Bundle bundle5 = mgr.registerBundle(clr.getResource(
182: "/org/jpox/samples/plugin/MANIFEST5.MF", null));
183: assertEquals(1, bundle3.getRequireBundle().size());
184: assertEquals("org.jpox.plugin.test4",
185: ((Bundle.BundleDescription) bundle3.getRequireBundle()
186: .iterator().next()).getBundleSymbolicName());
187: assertEquals(0, bundle4.getRequireBundle().size());
188: assertEquals(2, bundle5.getRequireBundle().size());
189: assertEquals("org.jpox.plugin.test6",
190: ((Bundle.BundleDescription) bundle5.getRequireBundle()
191: .get(0)).getBundleSymbolicName());
192: assertEquals("org.jpox.plugin.test7",
193: ((Bundle.BundleDescription) bundle5.getRequireBundle()
194: .get(1)).getBundleSymbolicName());
195: assertEquals("org.jpox.plugin.test7",
196: ((Bundle.BundleDescription) bundle5.getRequireBundle()
197: .get(1)).getBundleSymbolicName());
198: assertEquals("optional", ((Bundle.BundleDescription) bundle5
199: .getRequireBundle().get(1)).getParameter("resolution"));
200: }
201:
202: public void testRequireBundleLogged() {
203: final java.util.Set messages = new HashSet();
204: Logger.getLogger("JPOX.Plugin").addAppender(new Appender() {
205:
206: public void setName(String arg0) {
207: }
208:
209: public void setLayout(Layout arg0) {
210: }
211:
212: public void setErrorHandler(ErrorHandler arg0) {
213: }
214:
215: public boolean requiresLayout() {
216: return false;
217: }
218:
219: public String getName() {
220: return "testappender123";
221: }
222:
223: public Layout getLayout() {
224: return null;
225: }
226:
227: public Filter getFilter() {
228: return null;
229: }
230:
231: public ErrorHandler getErrorHandler() {
232: return null;
233: }
234:
235: public void doAppend(LoggingEvent arg0) {
236: if (arg0.getLevel() != Level.DEBUG) {
237: messages.add(arg0.getRenderedMessage());
238: }
239: }
240:
241: public void close() {
242: }
243:
244: public void clearFilters() {
245: }
246:
247: public void addFilter(Filter arg0) {
248: }
249:
250: });
251: ClassLoaderResolver clr = new JDOClassLoaderResolver();
252: NonManagedPluginRegistry mgr = new NonManagedPluginRegistry(
253: clr, "EXCEPTION");
254: assertEquals(0, mgr.getExtensionPoints().length);
255: mgr.registerBundle(clr.getResource(
256: "/org/jpox/samples/plugin/MANIFEST3.MF", null));
257: mgr.registerBundle(clr.getResource(
258: "/org/jpox/samples/plugin/MANIFEST4.MF", null));
259: mgr.registerBundle(clr.getResource(
260: "/org/jpox/samples/plugin/MANIFEST5.MF", null));
261: mgr.resolveConstraints();
262: try {
263: assertEquals(2, messages.size());
264: assertTrue(messages
265: .contains("Bundle org.jpox.plugin.test5 requires org.jpox.plugin.test6 but it cannot be resolved."));
266: assertTrue(messages
267: .contains("Bundle org.jpox.plugin.test5 has an optional dependency to org.jpox.plugin.test7 but it cannot be resolved."));
268:
269: } finally {
270: Logger.getLogger("JPOX.Plugin").removeAppender(
271: "testappender123");
272: }
273: }
274:
275: public void testRequireBundleVersionLogged() {
276: final java.util.Set messages = new HashSet();
277: Logger.getLogger("JPOX.Plugin").addAppender(new Appender() {
278:
279: public void setName(String arg0) {
280: }
281:
282: public void setLayout(Layout arg0) {
283: }
284:
285: public void setErrorHandler(ErrorHandler arg0) {
286: }
287:
288: public boolean requiresLayout() {
289: return false;
290: }
291:
292: public String getName() {
293: return "testappender123";
294: }
295:
296: public Layout getLayout() {
297: return null;
298: }
299:
300: public Filter getFilter() {
301: return null;
302: }
303:
304: public ErrorHandler getErrorHandler() {
305: return null;
306: }
307:
308: public void doAppend(LoggingEvent arg0) {
309: if (arg0.getLevel() != Level.DEBUG) {
310: messages.add(arg0.getRenderedMessage());
311: }
312: }
313:
314: public void close() {
315: }
316:
317: public void clearFilters() {
318: }
319:
320: public void addFilter(Filter arg0) {
321: }
322:
323: });
324: ClassLoaderResolver clr = new JDOClassLoaderResolver();
325: NonManagedPluginRegistry mgr = new NonManagedPluginRegistry(
326: clr, "EXCEPTION");
327: assertEquals(0, mgr.getExtensionPoints().length);
328: mgr.registerBundle(clr.getResource(
329: "/org/jpox/samples/plugin/MANIFEST10.MF", null));
330: mgr.registerBundle(clr.getResource(
331: "/org/jpox/samples/plugin/MANIFEST11.MF", null));
332: mgr.registerBundle(clr.getResource(
333: "/org/jpox/samples/plugin/MANIFEST12.MF", null));
334: mgr.registerBundle(clr.getResource(
335: "/org/jpox/samples/plugin/MANIFEST13.MF", null));
336: mgr.registerBundle(clr.getResource(
337: "/org/jpox/samples/plugin/MANIFEST14.MF", null));
338: mgr.registerBundle(clr.getResource(
339: "/org/jpox/samples/plugin/MANIFEST15.MF", null));
340: mgr.registerBundle(clr.getResource(
341: "/org/jpox/samples/plugin/MANIFEST16.MF", null));
342: mgr.registerBundle(clr.getResource(
343: "/org/jpox/samples/plugin/MANIFEST17.MF", null));
344: mgr.resolveConstraints();
345: try {
346: assertEquals(3, messages.size());
347: assertTrue(messages
348: .contains("Bundle org.jpox.plugin.test12 requires org.jpox.plugin.test11 version (1.2.0.b2 but the resolved bundle has version 1.2.0.b2 which is outside the expected range."));
349: assertTrue(messages
350: .contains("Bundle org.jpox.plugin.test13 requires org.jpox.plugin.test11 version (1.2.0.c1 but the resolved bundle has version 1.2.0.b2 which is outside the expected range."));
351: assertTrue(messages
352: .contains("Bundle org.jpox.plugin.test15 requires org.jpox.plugin.test11 version (1.0.0.b2,1.2.0.b2) but the resolved bundle has version 1.2.0.b2 which is outside the expected range."));
353: } finally {
354: Logger.getLogger("JPOX.Plugin").removeAppender(
355: "testappender123");
356: }
357: }
358:
359: }
|