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