001 package org.picocontainer.monitors; 002 003 import junit.framework.TestCase; 004 import org.jmock.Expectations; 005 import org.jmock.Mockery; 006 import org.jmock.integration.junit4.JMock; 007 import org.junit.Test; 008 import org.junit.runner.RunWith; 009 import org.picocontainer.*; 010 011 import static org.picocontainer.Characteristics.USE_NAMES; 012 import static org.picocontainer.tck.MockFactory.mockeryWithCountingNamingScheme; 013 014 import org.picocontainer.composers.RegexComposer; 015 016 import java.lang.reflect.Constructor; 017 import java.util.List; 018 import java.util.ArrayList; 019 020 @RunWith(JMock.class) 021 public class RegexComposerTestCase extends TestCase { 022 023 private Mockery mockery = mockeryWithCountingNamingScheme(); 024 025 @Test 026 public void regexWorksInGetComponentCalls() { 027 MutablePicoContainer pico = new DefaultPicoContainer(new ComposingMonitor(new RegexComposer())); 028 pico.addComponent("apple1", "Braeburn"); 029 pico.addComponent("apple2", "Granny Smith"); 030 pico.addComponent("plum", "Victoria"); 031 032 List apples = (List) pico.getComponent("apple[1-9]"); 033 assertEquals("[Braeburn, Granny Smith]", apples.toString()); 034 } 035 036 @Test 037 public void canReturningDifferentListsForDifferentComposers() { 038 MutablePicoContainer pico = new DefaultPicoContainer( 039 new ComposingMonitor(new RegexComposer("apple[1-9]", "apples"), new RegexComposer("plum*", "plums"))); 040 pico.addComponent("apple1", "Braeburn") 041 .addComponent("apple2", "Granny Smith") 042 .addComponent("plumV", "Victoria"); 043 pico.as(USE_NAMES).addComponent(NeedsApples.class) 044 .as(USE_NAMES).addComponent(NeedsPlums.class); 045 046 assertEquals("[Braeburn, Granny Smith]", pico.getComponent(NeedsApples.class).apples.toString()); 047 048 assertEquals("[Victoria]", pico.getComponent(NeedsPlums.class).plums.toString()); 049 } 050 051 @Test 052 public void nonMatchingCanFallThroughToAnotherComponentMonitor() throws NoSuchMethodException { 053 054 final List<String> apples = new ArrayList<String>(); 055 apples.add("Cox's"); 056 057 final ComponentMonitor fallThru = mockery.mock(ComponentMonitor.class); 058 mockery.checking(new Expectations() {{ 059 one(fallThru).noComponentFound(with(any(MutablePicoContainer.class)), with(same(List.class))); 060 will(returnValue(null)); 061 one(fallThru).noComponentFound(with(any(MutablePicoContainer.class)), with(equal("apples"))); 062 will(returnValue(apples)); 063 one(fallThru).instantiating(with(any(MutablePicoContainer.class)), with(any(ComponentAdapter.class)), with(any(Constructor.class))); 064 will(returnValue(NeedsApples.class.getConstructor(List.class))); 065 one(fallThru).instantiated(with(any(MutablePicoContainer.class)), with(any(ComponentAdapter.class)), with(any(Constructor.class)), with(any(NeedsApples.class)), with(any(Object[].class)), with(any(int.class))); 066 }}); 067 068 final MutablePicoContainer pico = new DefaultPicoContainer(new ComposingMonitor(fallThru, new RegexComposer("qqq[1-9]", "qqq"))); 069 pico.addComponent("apple1", "Braeburn") 070 .addComponent("integer", 1) 071 .addComponent("apple2", "Granny Smith") 072 .addComponent("plum", "Victoria"); 073 074 pico.as(USE_NAMES).addComponent(NeedsApples.class); 075 076 assertEquals("[Cox's]", pico.getComponent(NeedsApples.class).apples.toString()); 077 } 078 079 public static class NeedsApples { 080 private List<String> apples; 081 082 public NeedsApples(List<String> apples) { 083 this.apples = apples; 084 } 085 } 086 087 public static class NeedsPlums { 088 private List<String> plums; 089 090 public NeedsPlums(List<String> plums) { 091 this.plums = plums; 092 } 093 } 094 095 096 }