1 package org.picocontainer.defaults; 2 3 import org.picocontainer.tck.AbstractPicoContainerTestCase; 4 import org.picocontainer.Parameter; 5 import org.picocontainer.PicoException; 6 import org.picocontainer.PicoInitializationException; 7 import org.picocontainer.MutablePicoContainer; 8 9 import java.util.Map ; 10 import java.util.HashMap ; 11 import java.io.IOException ; 12 import java.awt.event.ActionListener ; 13 import java.awt.event.ActionEvent ; 14 15 19 public abstract class AbstractImplementationHidingPicoContainerTestCase extends AbstractPicoContainerTestCase { 20 21 22 public void testInstanceIsNotAutomaticallyHidden() { 23 MutablePicoContainer pc = createImplementationHidingPicoContainer(); 24 pc.registerComponentInstance(Map .class, new HashMap ()); 25 Map map = (Map ) pc.getComponentInstance(Map .class); 26 assertNotNull(map); 27 assertTrue(map instanceof HashMap ); 28 } 29 30 protected abstract MutablePicoContainer createImplementationHidingPicoContainer(); 31 32 33 public void testImplementaionIsAutomaticallyHidden() { 34 MutablePicoContainer pc = createImplementationHidingPicoContainer(); 35 pc.registerComponentImplementation(Map .class, HashMap .class); 36 Map map = (Map ) pc.getComponentInstance(Map .class); 37 assertNotNull(map); 38 assertFalse(map instanceof HashMap ); 39 } 40 41 public void testNonInterfaceImplementaionIsAutomaticallyHidden() { 42 MutablePicoContainer pc = createImplementationHidingPicoContainer(); 43 pc.registerComponentImplementation(HashMap .class, HashMap .class); 44 Map map = (Map ) pc.getComponentInstance(HashMap .class); 45 assertNotNull(map); 46 assertTrue(map instanceof HashMap ); 47 } 48 49 public void testNonInterfaceImplementaionWithParametersIsAutomaticallyHidden() { 50 MutablePicoContainer pc = createImplementationHidingPicoContainer(); 51 pc.registerComponentImplementation(HashMap .class, HashMap .class, new Parameter[0]); 52 Map map = (Map ) pc.getComponentInstance(HashMap .class); 53 assertNotNull(map); 54 assertTrue(map instanceof HashMap ); 55 } 56 57 58 public void testImplementaionWithParametersIsAutomaticallyHidden() { 59 MutablePicoContainer pc = createImplementationHidingPicoContainer(); 60 pc.registerComponentImplementation(Map .class, HashMap .class, new Parameter[0]); 61 Map map = (Map ) pc.getComponentInstance(Map .class); 62 assertNotNull(map); 63 assertFalse(map instanceof HashMap ); 64 } 65 66 public void testSerializedContainerCanRetrieveImplementation() throws PicoException, PicoInitializationException, 67 IOException , ClassNotFoundException { 68 try { 69 super.testSerializedContainerCanRetrieveImplementation(); 70 fail("The ImplementationHidingPicoContainer should not be able to retrieve the component impl"); 71 } catch (ClassCastException cce) { 72 } 74 } 75 76 public void testExceptionThrowingFromHiddenComponent() { 77 MutablePicoContainer pc = createImplementationHidingPicoContainer(); 78 pc.registerComponentImplementation(ActionListener .class, Burp.class); 79 try { 80 ActionListener ac = (ActionListener ) pc.getComponentInstance(ActionListener .class); 81 ac.actionPerformed(null); 82 fail("Oh no."); 83 } catch (RuntimeException e) { 84 assertEquals("woohoo", e.getMessage()); 85 } 86 } 87 88 public static class Burp implements ActionListener { 89 90 public void actionPerformed(ActionEvent e) { 91 throw new RuntimeException ("woohoo"); 92 } 93 } 94 95 } 96 | Popular Tags |