1 8 package org.picocontainer.defaults; 9 10 import org.picocontainer.MutablePicoContainer; 11 import org.picocontainer.PicoContainer; 12 import org.picocontainer.PicoIntrospectionException; 13 import org.picocontainer.PicoVisitor; 14 15 import org.jmock.Mock; 16 import org.jmock.MockObjectTestCase; 17 18 19 23 public class PicoVisitorTestCase 24 extends MockObjectTestCase { 25 26 public void testVisitorThatMustBeInvokedUsingTraverse() { 27 MutablePicoContainer pico = new DefaultPicoContainer(); 28 try { 29 pico.accept(new VerifyingVisitor()); 30 fail("PicoVisitorTraversalException expected"); 31 } catch (PicoVisitorTraversalException e) { 32 assertTrue(e.getMessage().indexOf(VerifyingVisitor.class.getName()) >= 0); 33 } 34 } 35 36 private static class UnusualNode { 37 boolean visited; 38 39 public void accept(PicoVisitor visit) { 40 visited = true; 41 } 42 } 43 44 public void testUnusualTraverseNode() { 45 UnusualNode node = new UnusualNode(); 46 new VerifyingVisitor().traverse(node); 47 assertTrue(node.visited); 48 } 49 50 public void testIllegalTraverseNode() { 51 try { 52 new VerifyingVisitor().traverse("Gosh!"); 53 fail("IllegalArgumentException expected"); 54 } catch (IllegalArgumentException e) { 55 assertTrue(e.getMessage().indexOf(String .class.getName()) >= 0); 56 } 57 } 58 59 public void testThrownRuntimeExceptionIsUnwrapped() { 60 Mock mockPico = mock(PicoContainer.class); 61 PicoVisitor visitor = new VerifyingVisitor(); 62 Error exception = new Error ("junit"); 63 mockPico.expects(once()).method("accept").with(same(visitor)).will( 64 throwException(new PicoIntrospectionException("message", exception))); 65 try { 66 visitor.traverse(mockPico.proxy()); 67 fail("PicoIntrospectionException expected"); 68 } catch (RuntimeException e) { 69 assertEquals("message", e.getMessage()); 70 assertSame(exception, ((PicoIntrospectionException)e).getCause()); 71 } 72 } 73 74 public void testThrownErrorIsUnwrapped() { 75 Mock mockPico = mock(PicoContainer.class); 76 PicoVisitor visitor = new VerifyingVisitor(); 77 Error error = new InternalError ("junit"); 78 mockPico.expects(once()).method("accept").with(same(visitor)).id("1"); 79 mockPico.expects(once()).method("accept").with(same(visitor)).after("1").will(throwException(error)); 80 visitor.traverse(mockPico.proxy()); 81 try { 82 visitor.traverse(mockPico.proxy()); 83 fail("UndeclaredThrowableException expected"); 84 } catch(InternalError e) { 85 assertEquals("junit", e.getMessage()); 86 } 87 } 88 } 89 | Popular Tags |