1 8 package org.picocontainer.defaults; 9 10 import java.io.FileNotFoundException ; 11 12 import org.jmock.MockObjectTestCase; 13 import org.picocontainer.MutablePicoContainer; 14 import org.picocontainer.Parameter; 15 import org.picocontainer.PicoIntrospectionException; 16 17 18 23 public class LifecycleVisitorTestCase extends MockObjectTestCase { 24 25 public abstract static class RecordingLifecycle { 26 private final StringBuffer recording; 27 28 protected RecordingLifecycle(StringBuffer recording) { 29 this.recording = recording; 30 } 31 32 public void demarrer() { 33 recording.append("<" + code()); 34 } 35 36 public void arreter() { 37 recording.append(code() + ">"); 38 } 39 40 public void ecraser() { 41 recording.append("!" + code()); 42 } 43 44 private String code() { 45 String name = getClass().getName(); 46 return name.substring(name.indexOf('$') + 1); 47 } 48 49 public void uncallableByVisitor(final String s) { 50 } 51 52 public void throwsAtVisit() throws FileNotFoundException { 53 throw new FileNotFoundException (); 54 } 55 56 private void callMe() { 57 } 58 } 59 60 public static class One extends RecordingLifecycle { 61 public One(StringBuffer sb) { 62 super(sb); 63 } 64 } 65 66 public static class Two extends RecordingLifecycle { 67 public Two(StringBuffer sb, One one) { 68 super(sb); 69 assertNotNull(one); 70 } 71 } 72 73 public static class Three extends RecordingLifecycle { 74 public Three(StringBuffer sb, One one, Two two) { 75 super(sb); 76 assertNotNull(one); 77 assertNotNull(two); 78 } 79 } 80 81 public static class Four extends RecordingLifecycle { 82 public Four(StringBuffer sb, Two two, Three three, One one) { 83 super(sb); 84 assertNotNull(one); 85 assertNotNull(two); 86 assertNotNull(three); 87 } 88 } 89 90 public void testShouldAllowCustomLifecycle() throws NoSuchMethodException { 91 LifecycleVisitor starter = new LifecycleVisitor( 92 RecordingLifecycle.class.getMethod("demarrer", null), RecordingLifecycle.class, true); 93 LifecycleVisitor stopper = new LifecycleVisitor( 94 RecordingLifecycle.class.getMethod("arreter", null), RecordingLifecycle.class, false); 95 LifecycleVisitor disposer = new LifecycleVisitor( 96 RecordingLifecycle.class.getMethod("ecraser", null), RecordingLifecycle.class, false); 97 98 MutablePicoContainer parent = new DefaultPicoContainer(); 99 MutablePicoContainer child = parent.makeChildContainer(); 100 parent.registerComponentImplementation("recording", StringBuffer .class); 101 child.registerComponentImplementation(Four.class); 102 parent.registerComponentImplementation(Two.class); 103 parent.registerComponentImplementation(One.class, One.class, new Parameter[]{ComponentParameter.DEFAULT}); 104 child.registerComponentImplementation(Three.class); 105 106 starter.traverse(parent); 107 stopper.traverse(parent); 108 disposer.traverse(parent); 109 110 assertEquals("<One<Two<Three<FourFour>Three>Two>One>!Four!Three!Two!One", parent.getComponentInstance( 111 "recording").toString()); 112 } 113 114 public void testPicoIntrospectionExceptionForInvalidMethod() throws NoSuchMethodException { 115 LifecycleVisitor visitor = new LifecycleVisitor(RecordingLifecycle.class.getMethod( 116 "uncallableByVisitor", new Class []{String .class}), RecordingLifecycle.class, true); 117 MutablePicoContainer pico = new DefaultPicoContainer(); 118 pico.registerComponentImplementation(StringBuffer .class); 119 pico.registerComponentImplementation(One.class); 120 try { 121 visitor.traverse(pico); 122 fail("PicoIntrospectionException expected"); 123 } catch (PicoIntrospectionException e) { 124 assertTrue(e.getCause() instanceof IllegalArgumentException ); 125 } 126 } 127 128 public void testPicoIntrospectionExceptionForThrownException() throws NoSuchMethodException { 129 LifecycleVisitor visitor = new LifecycleVisitor( 130 RecordingLifecycle.class.getMethod("throwsAtVisit", null), RecordingLifecycle.class, true); 131 MutablePicoContainer pico = new DefaultPicoContainer(); 132 pico.registerComponentImplementation(StringBuffer .class); 133 pico.registerComponentImplementation(One.class); 134 try { 135 visitor.traverse(pico); 136 fail("PicoIntrospectionException expected"); 137 } catch (PicoIntrospectionException e) { 138 assertTrue(e.getCause() instanceof FileNotFoundException ); 139 } 140 } 141 142 public void testPicoIntrospectionExceptionForInaccessibleMethod() throws NoSuchMethodException { 143 LifecycleVisitor visitor = new LifecycleVisitor( 144 RecordingLifecycle.class.getDeclaredMethod("callMe", null), RecordingLifecycle.class, true); 145 MutablePicoContainer pico = new DefaultPicoContainer(); 146 pico.registerComponentImplementation(StringBuffer .class); 147 pico.registerComponentImplementation(One.class); 148 try { 149 visitor.traverse(pico); 150 fail("PicoIntrospectionException expected"); 151 } catch (PicoIntrospectionException e) { 152 assertTrue(e.getCause() instanceof IllegalAccessException ); 153 } 154 } 155 } | Popular Tags |