001    /*****************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *****************************************************************************/
008    package org.picocontainer.defaults;
009    
010    import org.picocontainer.MutablePicoContainer;
011    import org.picocontainer.PicoContainer;
012    import org.picocontainer.PicoIntrospectionException;
013    import org.picocontainer.PicoVisitor;
014    
015    import org.jmock.Mock;
016    import org.jmock.MockObjectTestCase;
017    
018    
019    /**
020     * Test general PicoVisitor behaviour.
021     * @author Jörg Schaible
022     */
023    public class PicoVisitorTestCase
024            extends MockObjectTestCase {
025    
026        public void testVisitorThatMustBeInvokedUsingTraverse() {
027            MutablePicoContainer pico = new DefaultPicoContainer();
028            try {
029                pico.accept(new VerifyingVisitor());
030                fail("PicoVisitorTraversalException expected");
031            } catch (PicoVisitorTraversalException e) {
032                assertTrue(e.getMessage().indexOf(VerifyingVisitor.class.getName()) >= 0);
033            }
034        }
035    
036        private static class UnusualNode {
037            boolean visited;
038    
039            public void accept(PicoVisitor visit) {
040                visited = true;
041            }
042        }
043    
044        public void testUnusualTraverseNode() {
045            UnusualNode node = new UnusualNode();
046            new VerifyingVisitor().traverse(node);
047            assertTrue(node.visited);
048        }
049    
050        public void testIllegalTraverseNode() {
051            try {
052                new VerifyingVisitor().traverse("Gosh!");
053                fail("IllegalArgumentException expected");
054            } catch (IllegalArgumentException e) {
055                assertTrue(e.getMessage().indexOf(String.class.getName()) >= 0);
056            }
057        }
058    
059        public void testThrownRuntimeExceptionIsUnwrapped() {
060            Mock mockPico = mock(PicoContainer.class);
061            PicoVisitor visitor = new VerifyingVisitor();
062            Error exception = new Error("junit");
063            mockPico.expects(once()).method("accept").with(same(visitor)).will(
064                    throwException(new PicoIntrospectionException("message", exception)));
065            try {
066                visitor.traverse(mockPico.proxy());
067                fail("PicoIntrospectionException expected");
068            } catch (RuntimeException e) {
069                assertEquals("message", e.getMessage());
070                assertSame(exception, ((PicoIntrospectionException)e).getCause());
071            }
072        }
073    
074        public void testThrownErrorIsUnwrapped() {
075            Mock mockPico = mock(PicoContainer.class);
076            PicoVisitor visitor = new VerifyingVisitor();
077            Error error = new InternalError("junit");
078            mockPico.expects(once()).method("accept").with(same(visitor)).id("1");
079            mockPico.expects(once()).method("accept").with(same(visitor)).after("1").will(throwException(error));
080            visitor.traverse(mockPico.proxy());
081            try {
082                visitor.traverse(mockPico.proxy());
083                fail("UndeclaredThrowableException expected");
084            } catch(InternalError e) {
085                assertEquals("junit", e.getMessage());
086            }
087        }
088    }