Source Code Cross Referenced for JRubyContainerBuilderTestCase.java in  » Inversion-of-Control » nanocontainer » org » nanocontainer » script » jruby » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Inversion of Control » nanocontainer » org.nanocontainer.script.jruby 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.nanocontainer.script.jruby;
002:
003:        import java.io.File;
004:        import java.io.IOException;
005:        import java.io.Reader;
006:        import java.io.StringReader;
007:        import java.io.StringWriter;
008:        import java.net.URL;
009:        import java.net.URLClassLoader;
010:
011:        import org.jmock.Mock;
012:        import org.jruby.exceptions.RaiseException;
013:        import org.nanocontainer.NanoPicoContainer;
014:        import org.nanocontainer.TestHelper;
015:        import org.nanocontainer.integrationkit.PicoCompositionException;
016:        import org.nanocontainer.reflection.DefaultNanoPicoContainer;
017:        import org.nanocontainer.script.AbstractScriptedContainerBuilderTestCase;
018:        import org.nanocontainer.script.NanoContainerMarkupException;
019:        import org.nanocontainer.testmodel.A;
020:        import org.nanocontainer.testmodel.B;
021:        import org.nanocontainer.testmodel.HasParams;
022:        import org.nanocontainer.testmodel.ParentAssemblyScope;
023:        import org.nanocontainer.testmodel.SomeAssemblyScope;
024:        import org.nanocontainer.testmodel.X;
025:        import org.picocontainer.ComponentAdapter;
026:        import org.picocontainer.MutablePicoContainer;
027:        import org.picocontainer.PicoContainer;
028:        import org.picocontainer.defaults.ComponentAdapterFactory;
029:        import org.picocontainer.defaults.DefaultPicoContainer;
030:        import org.picocontainer.defaults.InstanceComponentAdapter;
031:        import org.picocontainer.defaults.SetterInjectionComponentAdapter;
032:        import org.picocontainer.defaults.SetterInjectionComponentAdapterFactory;
033:        import org.picocontainer.defaults.UnsatisfiableDependenciesException;
034:
035:        /**
036:         * @author Nick Sieger
037:         * @author Paul Hammant
038:         * @author Chris Bailey
039:         * @author Mauro Talevi
040:         */
041:        public class JRubyContainerBuilderTestCase extends
042:                AbstractScriptedContainerBuilderTestCase {
043:            private static final String ASSEMBLY_SCOPE = "SOME_SCOPE";
044:
045:            public void testContainerCanBeBuiltWithParentGlobal() {
046:                Reader script = new StringReader(
047:                        "StringBuffer = java.lang.StringBuffer\n"
048:                                + "container(:parent => $parent) { \n"
049:                                + "  component(StringBuffer)\n" + "}");
050:                PicoContainer parent = new DefaultPicoContainer();
051:                PicoContainer pico = buildContainer(script, parent,
052:                        ASSEMBLY_SCOPE);
053:                //PicoContainer.getParent() is now ImmutablePicoContainer
054:                assertNotNull(pico.getParent());
055:                assertNotSame(parent, pico.getParent());
056:                assertEquals(StringBuffer.class, pico.getComponentInstance(
057:                        StringBuffer.class).getClass());
058:            }
059:
060:            public void testContainerCanBeBuiltWithComponentImplementation() {
061:                X.reset();
062:                Reader script = new StringReader(
063:                        "A = org.nanocontainer.testmodel.A\n" + "container {\n"
064:                                + "    component(A)\n" + "}");
065:
066:                PicoContainer pico = buildContainer(script, null,
067:                        ASSEMBLY_SCOPE);
068:                // LifecyleContainerBuilder starts the container
069:                pico.dispose();
070:
071:                assertEquals("Should match the expression", "<A!A",
072:                        X.componentRecorder);
073:            }
074:
075:            public void testContainerCanBeBuiltWithComponentInstance() {
076:                Reader script = new StringReader("container { \n"
077:                        + "  component(:key => 'string', :instance => 'foo')\n"
078:                        + "}");
079:
080:                PicoContainer pico = buildContainer(script, null, "SOME_SCOPE");
081:
082:                assertEquals("foo", pico.getComponentInstance("string"));
083:            }
084:
085:            public void testBuildingWithPicoSyntax() {
086:                Reader script = new StringReader(
087:                        "$parent.registerComponentImplementation('foo', Java::JavaClass.for_name('java.lang.String'))\n"
088:                                + "DefaultPicoContainer = org.picocontainer.defaults.DefaultPicoContainer\n"
089:                                + "pico = DefaultPicoContainer.new($parent)\n"
090:                                + "pico.registerComponentImplementation(Java::JavaClass.for_name('org.nanocontainer.testmodel.A'))\n"
091:                                + "pico");
092:
093:                PicoContainer parent = new DefaultPicoContainer();
094:                PicoContainer pico = buildContainer(script, parent,
095:                        "SOME_SCOPE");
096:
097:                assertNotSame(parent, pico.getParent());
098:                assertNotNull(pico.getComponentInstance(A.class));
099:                assertNotNull(pico.getComponentInstance("foo"));
100:            }
101:
102:            public void testContainerBuiltWithMultipleComponentInstances() {
103:                Reader script = new StringReader("container {\n"
104:                        + "    component(:key => 'a', :instance => 'apple')\n"
105:                        + "    component(:key => 'b', :instance => 'banana')\n"
106:                        + "    component(:instance => 'noKeySpecified')\n"
107:                        + "}");
108:
109:                PicoContainer pico = buildContainer(script, null,
110:                        ASSEMBLY_SCOPE);
111:                assertEquals("apple", pico.getComponentInstance("a"));
112:                assertEquals("banana", pico.getComponentInstance("b"));
113:                assertEquals("noKeySpecified", pico
114:                        .getComponentInstance(String.class));
115:            }
116:
117:            public void testShouldFailWhenNeitherClassNorInstanceIsSpecifiedForComponent() {
118:                Reader script = new StringReader("container {\n"
119:                        + "  component(:key => 'a')\n" + "}");
120:
121:                try {
122:                    buildContainer(script, null, ASSEMBLY_SCOPE);
123:                    fail("NanoContainerMarkupException should have been raised");
124:                } catch (NanoContainerMarkupException e) {
125:                    // expected
126:                }
127:            }
128:
129:            public void testAcceptsConstantParametersForComponent() {
130:                Reader script = new StringReader(
131:                        "HasParams = org.nanocontainer.testmodel.HasParams\n"
132:                                + "container {\n"
133:                                + "    component(:key => 'byClass', :class => HasParams, :parameters => [ 'a', 'b', constant('c')])\n"
134:                                + "}");
135:
136:                PicoContainer pico = buildContainer(script, null,
137:                        ASSEMBLY_SCOPE);
138:                HasParams byClass = (HasParams) pico
139:                        .getComponentInstance("byClass");
140:                assertEquals("abc", byClass.getParams());
141:            }
142:
143:            public void testAcceptsComponentClassNameAsString() {
144:                Reader script = new StringReader(
145:                        "container {\n"
146:                                + "    component(:key => 'byClassString', :class => 'org.nanocontainer.testmodel.HasParams', :parameters => [ 'c', 'a', 't' ])\n"
147:                                + "}");
148:
149:                PicoContainer pico = buildContainer(script, null,
150:                        ASSEMBLY_SCOPE);
151:                HasParams byClassString = (HasParams) pico
152:                        .getComponentInstance("byClassString");
153:                assertEquals("cat", byClassString.getParams());
154:            }
155:
156:            public void testAcceptsComponentParametersForComponent() {
157:                Reader script = new StringReader(
158:                        "A = org.nanocontainer.testmodel.A\n"
159:                                + "B = org.nanocontainer.testmodel.B\n"
160:                                + "container {\n"
161:                                + "    component(:key => 'a1', :class => A)\n"
162:                                + "    component(:key => 'a2', :class => A)\n"
163:                                + "    component(:key => 'b1', :class => B, :parameters => [ key('a1') ])\n"
164:                                + "    component(:key => 'b2', :class => B, :parameters => key('a2'))\n"
165:                                + "}");
166:
167:                PicoContainer pico = buildContainer(script, null,
168:                        ASSEMBLY_SCOPE);
169:                A a1 = (A) pico.getComponentInstance("a1");
170:                A a2 = (A) pico.getComponentInstance("a2");
171:                B b1 = (B) pico.getComponentInstance("b1");
172:                B b2 = (B) pico.getComponentInstance("b2");
173:
174:                assertNotNull(a1);
175:                assertNotNull(a2);
176:                assertNotNull(b1);
177:                assertNotNull(b2);
178:
179:                assertSame(a1, b1.getA());
180:                assertSame(a2, b2.getA());
181:                assertNotSame(a1, a2);
182:                assertNotSame(b1, b2);
183:            }
184:
185:            public void testAcceptsComponentParameterWithClassNameKey() {
186:                Reader script = new StringReader(
187:                        "A = org.nanocontainer.testmodel.A\n"
188:                                + "B = org.nanocontainer.testmodel.B\n"
189:                                + "container {\n"
190:                                + "    component(:class => A)\n"
191:                                + "    component(:key => B, :class => B, :parameters => key(A))\n"
192:                                + "}");
193:
194:                PicoContainer pico = buildContainer(script, null,
195:                        ASSEMBLY_SCOPE);
196:                A a = (A) pico.getComponentInstance(A.class);
197:                B b = (B) pico.getComponentInstance(B.class);
198:
199:                assertNotNull(a);
200:                assertNotNull(b);
201:                assertSame(a, b.getA());
202:            }
203:
204:            public void testInstantiateBasicComponentInDeeperTree() {
205:                X.reset();
206:                Reader script = new StringReader(
207:                        "A = org.nanocontainer.testmodel.A\n" + "container {\n"
208:                                + "  container {\n" + "    component(A)\n"
209:                                + "  }\n" + "}");
210:
211:                PicoContainer pico = buildContainer(script, null,
212:                        ASSEMBLY_SCOPE);
213:                pico.dispose();
214:                assertEquals("Should match the expression", "<A!A",
215:                        X.componentRecorder);
216:            }
217:
218:            public void testCustomComponentAdapterFactoryCanBeSpecified() {
219:                Reader script = new StringReader(
220:                        "A = org.nanocontainer.testmodel.A\n"
221:                                + "container(:component_adapter_factory => $assembly_scope) {\n"
222:                                + "    component(A)\n" + "}");
223:
224:                A a = new A();
225:                Mock cafMock = mock(ComponentAdapterFactory.class);
226:                cafMock.expects(once()).method("createComponentAdapter").with(
227:                        same(A.class), same(A.class), eq(null)).will(
228:                        returnValue(new InstanceComponentAdapter(A.class, a)));
229:                PicoContainer pico = buildContainer(script, null, cafMock
230:                        .proxy());
231:                assertSame(a, pico.getComponentInstanceOfType(A.class));
232:            }
233:
234:            public void testCustomComponentMonitorCanBeSpecified() {
235:                Reader script = new StringReader(
236:                        "A = org.nanocontainer.testmodel.A\n"
237:                                + "StringWriter = java.io.StringWriter\n"
238:                                + "WriterComponentMonitor = org.picocontainer.monitors.WriterComponentMonitor\n"
239:                                + "writer = StringWriter.new\n"
240:                                + "monitor = WriterComponentMonitor.new(writer) \n"
241:                                + "container(:component_monitor => monitor) {\n"
242:                                + "    component(A)\n"
243:                                + "    component(:key => StringWriter, :instance => writer)\n"
244:                                + "}");
245:
246:                PicoContainer pico = buildContainer(script, null,
247:                        ASSEMBLY_SCOPE);
248:                StringWriter writer = (StringWriter) pico
249:                        .getComponentInstanceOfType(StringWriter.class);
250:                assertTrue(writer.toString().length() > 0);
251:            }
252:
253:            public void testCustomComponentMonitorCanBeSpecifiedWhenCAFIsSpecified() {
254:                Reader script = new StringReader(
255:                        "A = org.nanocontainer.testmodel.A\n"
256:                                + "StringWriter = java.io.StringWriter\n"
257:                                + "WriterComponentMonitor = org.picocontainer.monitors.WriterComponentMonitor\n"
258:                                + "DefaultComponentAdapterFactory = org.picocontainer.defaults.DefaultComponentAdapterFactory\n"
259:                                + "writer = StringWriter.new\n"
260:                                + "monitor = WriterComponentMonitor.new(writer) \n"
261:                                + "container(:component_adapter_factory => DefaultComponentAdapterFactory.new, :component_monitor => monitor) {\n"
262:                                + "    component(A)\n"
263:                                + "    component(:key => StringWriter, :instance => writer)\n"
264:                                + "}");
265:
266:                PicoContainer pico = buildContainer(script, null,
267:                        ASSEMBLY_SCOPE);
268:                StringWriter writer = (StringWriter) pico
269:                        .getComponentInstanceOfType(StringWriter.class);
270:                assertTrue(writer.toString().length() > 0);
271:            }
272:
273:            public void testCustomComponentMonitorCanBeSpecifiedWhenParentIsSpecified() {
274:                DefaultNanoPicoContainer parent = new DefaultNanoPicoContainer();
275:                Reader script = new StringReader(
276:                        "A = org.nanocontainer.testmodel.A\n"
277:                                + "StringWriter = java.io.StringWriter\n"
278:                                + "WriterComponentMonitor = org.picocontainer.monitors.WriterComponentMonitor\n"
279:                                + "writer = StringWriter.new\n"
280:                                + "monitor = WriterComponentMonitor.new(writer) \n"
281:                                + "container(:parent => $parent, :component_monitor => monitor) {\n"
282:                                + "    component(A)\n"
283:                                + "    component(:key => StringWriter, :instance => writer)\n"
284:                                + "}");
285:
286:                PicoContainer pico = buildContainer(script, parent,
287:                        ASSEMBLY_SCOPE);
288:                StringWriter writer = (StringWriter) pico
289:                        .getComponentInstanceOfType(StringWriter.class);
290:                assertTrue(writer.toString().length() > 0);
291:            }
292:
293:            public void testCustomComponentMonitorCanBeSpecifiedWhenParentAndCAFAreSpecified() {
294:                DefaultNanoPicoContainer parent = new DefaultNanoPicoContainer();
295:                Reader script = new StringReader(
296:                        "A = org.nanocontainer.testmodel.A\n"
297:                                + "StringWriter = java.io.StringWriter\n"
298:                                + "WriterComponentMonitor = org.picocontainer.monitors.WriterComponentMonitor\n"
299:                                + "DefaultComponentAdapterFactory = org.picocontainer.defaults.DefaultComponentAdapterFactory\n"
300:                                + "writer = StringWriter.new\n"
301:                                + "monitor = WriterComponentMonitor.new(writer) \n"
302:                                + "container(:parent => $parent, :component_adapter_factory => DefaultComponentAdapterFactory.new, :component_monitor => monitor) {\n"
303:                                + "    component(A)\n"
304:                                + "    component(:key => StringWriter, :instance => writer)\n"
305:                                + "}");
306:
307:                PicoContainer pico = buildContainer(script, parent,
308:                        ASSEMBLY_SCOPE);
309:                StringWriter writer = (StringWriter) pico
310:                        .getComponentInstanceOfType(StringWriter.class);
311:                assertTrue(writer.toString().length() > 0);
312:            }
313:
314:            public void testInstantiateWithImpossibleComponentDependenciesConsideringTheHierarchy() {
315:                X.reset();
316:                Reader script = new StringReader(
317:                        "A = org.nanocontainer.testmodel.A\n"
318:                                + "B = org.nanocontainer.testmodel.B\n"
319:                                + "C = org.nanocontainer.testmodel.C\n"
320:                                + "container {\n" + "    component(B)\n"
321:                                + "    container() {\n"
322:                                + "        component(A)\n" + "    }\n"
323:                                + "    component(C)\n" + "}");
324:
325:                try {
326:                    buildContainer(script, null, ASSEMBLY_SCOPE);
327:                    fail("Should not have been able to instansiate component tree due to visibility/parent reasons.");
328:                } catch (UnsatisfiableDependenciesException expected) {
329:                }
330:            }
331:
332:            public void testInstantiateWithChildContainerAndStartStopAndDisposeOrderIsCorrect() {
333:                X.reset();
334:                Reader script = new StringReader(
335:                        "A = org.nanocontainer.testmodel.A\n"
336:                                + "B = org.nanocontainer.testmodel.B\n"
337:                                + "C = org.nanocontainer.testmodel.C\n"
338:                                + "container {\n" + "    component(A)\n"
339:                                + "    container() {\n"
340:                                + "         component(B)\n" + "    }\n"
341:                                + "    component(C)\n" + "}\n");
342:
343:                // A and C have no no dependancies. B Depends on A.
344:                PicoContainer pico = buildContainer(script, null,
345:                        ASSEMBLY_SCOPE);
346:                pico.stop();
347:                pico.dispose();
348:
349:                assertEquals("Should match the expression",
350:                        "<A<C<BB>C>A>!B!C!A", X.componentRecorder);
351:            }
352:
353:            public void testBuildContainerWithParentAttribute() {
354:                DefaultNanoPicoContainer parent = new DefaultNanoPicoContainer();
355:                parent.registerComponentInstance("hello", "world");
356:
357:                Reader script = new StringReader(
358:                        "A = org.nanocontainer.testmodel.A\n"
359:                                + "container(:parent => $parent) {\n"
360:                                + "    component(A)\n" + "}\n");
361:
362:                PicoContainer pico = buildContainer(script, parent,
363:                        ASSEMBLY_SCOPE);
364:                // Should be able to get instance that was registered in the parent container
365:                assertEquals("world", pico.getComponentInstance("hello"));
366:            }
367:
368:            public void testBuildContainerWithParentDependencyAndAssemblyScope()
369:                    throws Exception {
370:                DefaultNanoPicoContainer parent = new DefaultNanoPicoContainer();
371:                parent.registerComponentImplementation("a", A.class);
372:
373:                String source = "B = org.nanocontainer.testmodel.B\n"
374:                        + "SomeAssemblyScope = org.nanocontainer.testmodel.SomeAssemblyScope\n"
375:                        + "container(:parent => $parent) {\n"
376:                        + "  if $assembly_scope.kind_of?(SomeAssemblyScope)\n "
377:                        + "    component(B)\n" + "  end\n " + "}\n";
378:
379:                Reader script = new StringReader(source);
380:
381:                PicoContainer pico = buildContainer(script, parent,
382:                        new SomeAssemblyScope());
383:                assertNotNull(pico.getComponentInstanceOfType(B.class));
384:
385:                script = new StringReader(source);
386:                pico = buildContainer(script, parent, ASSEMBLY_SCOPE);
387:                assertNull(pico.getComponentInstanceOfType(B.class));
388:            }
389:
390:            public void testBuildContainerWithParentAndChildAssemblyScopes()
391:                    throws IOException {
392:                String scriptValue = "A = org.nanocontainer.testmodel.A\n"
393:                        + "B = org.nanocontainer.testmodel.B\n"
394:                        + "ParentAssemblyScope = org.nanocontainer.testmodel.ParentAssemblyScope\n"
395:                        + "SomeAssemblyScope = org.nanocontainer.testmodel.SomeAssemblyScope\n"
396:                        + "container(:parent => $parent) {\n"
397:                        + "  puts 'assembly_scope:'+$assembly_scope.inspect\n "
398:                        + "  case $assembly_scope\n"
399:                        + "  when ParentAssemblyScope\n "
400:                        + "    puts 'parent scope'\n "
401:                        + "    component(A)\n"
402:                        + "  when SomeAssemblyScope\n "
403:                        + "    puts 'child scope'\n "
404:                        + "    component(B)\n"
405:                        + "  else \n"
406:                        + "     raise 'Invalid Scope: ' +  $assembly_scope.inspect\n"
407:                        + "  end\n " + "}\n";
408:
409:                Reader script = new StringReader(scriptValue);
410:                NanoPicoContainer parent = new DefaultNanoPicoContainer(
411:                        buildContainer(script, null, new ParentAssemblyScope()));
412:                assertNotNull(parent.getComponentAdapterOfType(A.class));
413:
414:                script = new StringReader(scriptValue);
415:                PicoContainer pico = buildContainer(script, parent,
416:                        new SomeAssemblyScope());
417:                assertNotNull(pico.getComponentInstance(B.class));
418:            }
419:
420:            public void FAILING_testBuildContainerWithParentAttributesPropagatesComponentAdapterFactory() {
421:                DefaultNanoPicoContainer parent = new DefaultNanoPicoContainer(
422:                        new SetterInjectionComponentAdapterFactory());
423:                Reader script = new StringReader(
424:                        "container(:parent => $parent)\n");
425:
426:                MutablePicoContainer pico = (MutablePicoContainer) buildContainer(
427:                        script, parent, ASSEMBLY_SCOPE);
428:                // Should be able to get instance that was registered in the parent container
429:                ComponentAdapter componentAdapter = pico
430:                        .registerComponentImplementation(String.class);
431:                assertTrue(
432:                        "ComponentAdapter should be originally defined by parent",
433:                        componentAdapter instanceof  SetterInjectionComponentAdapter);
434:            }
435:
436:            public void testExceptionThrownWhenParentAttributeDefinedWithinChild() {
437:                DefaultNanoPicoContainer parent = new DefaultNanoPicoContainer(
438:                        new SetterInjectionComponentAdapterFactory());
439:                Reader script = new StringReader(
440:                        "A = org.nanocontainer.testmodel.A\n"
441:                                + "B = org.nanocontainer.testmodel.B\n"
442:                                + "container() {\n" + "    component(A)\n"
443:                                + "    container(:parent => $parent) {\n"
444:                                + "         component(B)\n" + "    }\n" + "}\n");
445:
446:                try {
447:                    buildContainer(script, parent, ASSEMBLY_SCOPE);
448:                    fail("NanoContainerMarkupException should have been thrown.");
449:                } catch (NanoContainerMarkupException ignore) {
450:                    // expected
451:                }
452:            }
453:
454:            //TODO
455:            public void testSpuriousAttributes() {
456:                DefaultNanoPicoContainer parent = new DefaultNanoPicoContainer();
457:
458:                Reader script = new StringReader(
459:                        "container(:jim => 'Jam', :foo => 'bar')");
460:                try {
461:                    buildContainer(script, parent, ASSEMBLY_SCOPE);
462:                    //fail("Should throw exception upon spurious attributes?");
463:                } catch (NanoContainerMarkupException ex) {
464:                    //ok?
465:                }
466:            }
467:
468:            public void testWithDynamicClassPathThatDoesNotExist() {
469:                DefaultNanoPicoContainer parent = new DefaultNanoPicoContainer();
470:                try {
471:                    Reader script = new StringReader(
472:                            "container {\n"
473:                                    + "  classPathElement(:path => 'this/path/does/not/exist.jar')\n"
474:                                    + "  component(:class => \"FooBar\")\n"
475:                                    + "}");
476:
477:                    buildContainer(script, parent, ASSEMBLY_SCOPE);
478:                    fail("should have barfed with bad path exception");
479:                } catch (NanoContainerMarkupException e) {
480:                    // excpected
481:                }
482:
483:            }
484:
485:            public void testWithDynamicClassPath() {
486:                DefaultNanoPicoContainer parent = new DefaultNanoPicoContainer();
487:                Reader script = new StringReader(
488:                        "TestHelper = org.nanocontainer.TestHelper\n"
489:                                + "testCompJar = TestHelper.getTestCompJarFile()\n"
490:                                + "compJarPath = testCompJar.getCanonicalPath()\n"
491:                                + "container {\n"
492:                                + "  classPathElement(:path => compJarPath)\n"
493:                                + "  component(:class => \"TestComp\")\n" + "}");
494:
495:                MutablePicoContainer pico = (MutablePicoContainer) buildContainer(
496:                        script, parent, ASSEMBLY_SCOPE);
497:
498:                assertEquals(1, pico.getComponentInstances().size());
499:                assertEquals("TestComp", pico.getComponentInstances().get(0)
500:                        .getClass().getName());
501:            }
502:
503:            public void testWithDynamicClassPathWithPermissions() {
504:                DefaultNanoPicoContainer parent = new DefaultNanoPicoContainer();
505:                Reader script = new StringReader(
506:                        "TestHelper = org.nanocontainer.TestHelper\n"
507:                                + "SocketPermission = java.net.SocketPermission\n"
508:                                + "testCompJar = TestHelper.getTestCompJarFile()\n"
509:                                + "compJarPath = testCompJar.getCanonicalPath()\n"
510:                                + "container {\n"
511:                                + "  classPathElement(:path => compJarPath) {\n"
512:                                + "    grant(:perm => SocketPermission.new('google.com','connect'))\n"
513:                                + "  }\n"
514:                                + "  component(:class => \"TestComp\")\n" + "}");
515:
516:                MutablePicoContainer pico = (MutablePicoContainer) buildContainer(
517:                        script, parent, ASSEMBLY_SCOPE);
518:
519:                assertEquals(1, pico.getComponentInstances().size());
520:                // can't actually test the permission under JUNIT control. We're just
521:                // testing the syntax here.
522:            }
523:
524:            public void testGrantPermissionInWrongPlace() {
525:                DefaultNanoPicoContainer parent = new DefaultNanoPicoContainer();
526:                try {
527:                    Reader script = new StringReader(
528:                            "TestHelper = org.nanocontainer.TestHelper\n"
529:                                    + "SocketPermission = java.net.SocketPermission\n"
530:                                    + "testCompJar = TestHelper.getTestCompJarFile()\n"
531:                                    + "container {\n"
532:                                    + "  grant(:perm => SocketPermission.new('google.com','connect'))\n"
533:                                    + "}");
534:
535:                    buildContainer(script, parent, ASSEMBLY_SCOPE);
536:                    fail("should barf with RaiseException");
537:                } catch (PicoCompositionException e) {
538:                    assertNotNull(e.getCause());
539:                    assertTrue(e.getCause() instanceof  RaiseException);
540:                }
541:            }
542:
543:            public void testWithParentClassPathPropagatesWithNoParentContainer()
544:                    throws IOException {
545:                File testCompJar = TestHelper.getTestCompJarFile();
546:
547:                URLClassLoader classLoader = new URLClassLoader(
548:                        new URL[] { testCompJar.toURL() }, this .getClass()
549:                                .getClassLoader());
550:                Class testComp = null;
551:
552:                try {
553:                    testComp = classLoader.loadClass("TestComp");
554:                } catch (ClassNotFoundException ex) {
555:                    fail("Unable to load test component from the jar using a url classloader");
556:                }
557:                Reader script = new StringReader(
558:                        "container(:parent => $parent) {\n"
559:                                + "  component(:class => \"TestComp\")\n" + "}");
560:
561:                PicoContainer pico = buildContainer(new JRubyContainerBuilder(
562:                        script, classLoader), null, null);
563:                assertNotNull(pico);
564:                Object testCompInstance = pico.getComponentInstance(testComp
565:                        .getName());
566:                assertSame(testCompInstance.getClass(), testComp);
567:
568:            }
569:
570:            //    public void testExceptionThrownWhenParentAttributeDefinedWithinChild() {
571:            //        DefaultNanoPicoContainer parent = new DefaultNanoPicoContainer(new SetterInjectionComponentAdapterFactory() );
572:            //        Reader script = new StringReader("" +
573:            //                "package org.nanocontainer.testmodel\n" +
574:            //                "nano = new GroovyNodeBuilder().container() {\n" +
575:            //                "    component(A)\n" +
576:            //                "    container(parent:parent) {\n" +
577:            //                "         component(B)\n" +
578:            //                "    }\n" +
579:            //                "}\n");
580:            //
581:            //        try {
582:            //            buildContainer(script, parent, ASSEMBLY_SCOPE);
583:            //            fail("NanoContainerMarkupException should have been thrown.");
584:            //        } catch (NanoContainerMarkupException ignore) {
585:            //            // ignore
586:            //        }
587:            //    }
588:
589:            private PicoContainer buildContainer(Reader script,
590:                    PicoContainer parent, Object scope) {
591:                return buildContainer(new JRubyContainerBuilder(script,
592:                        getClass().getClassLoader()), parent, scope);
593:            }
594:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.