001 package org.picocontainer.defaults.issues;
002
003 import org.picocontainer.MutablePicoContainer;
004 import org.picocontainer.DefaultPicoContainer;
005 import org.picocontainer.behaviors.Caching;
006 import org.picocontainer.parameters.ComponentParameter;
007 import org.junit.Ignore;
008 import org.junit.Test;
009
010 import java.util.ArrayList;
011 import java.util.List;
012
013 import static junit.framework.Assert.*;
014
015 public class Issue0332TestCase {
016
017 /**
018 * Sample class that demonstrates literal collection handling.
019 */
020 public static class Searcher {
021 private final List<String> searchPath;
022
023 public Searcher(List<String> searchPath) {
024 this.searchPath = searchPath;
025 }
026
027 public List<String> getSearchPath() {
028 return searchPath;
029 }
030 }
031
032
033 /**
034 * TODO Revisit this for Pico 3.
035 */
036 @Ignore
037 @Test
038 public void canInstantiateAutowiredCollectionThatAreDefinedImplicitly() {
039 MutablePicoContainer pico = new DefaultPicoContainer(new Caching());
040 List<String> searchPath = new ArrayList<String>();
041 searchPath.add("a");
042 searchPath.add("b");
043
044 List<Integer> conflictingList = new ArrayList<Integer>();
045 conflictingList.add(1);
046 conflictingList.add(2);
047 pico.addComponent("conflict", conflictingList);
048
049 pico.addComponent("searchPath",searchPath)
050 .addComponent(Searcher.class);
051
052 assertNotNull(pico.getComponent(Searcher.class));
053 assertNotNull(pico.getComponent(Searcher.class).getSearchPath());
054 }
055
056 @Test
057 public void canInstantiateExplicitCollectionWithComponentParameter() {
058 MutablePicoContainer pico = new DefaultPicoContainer(new Caching());
059 List<String> searchPath = new ArrayList<String>();
060 searchPath.add("a");
061 searchPath.add("b");
062
063 pico.addComponent("searchPath",searchPath);
064 pico.addComponent(Searcher.class, Searcher.class, new ComponentParameter("searchPath"));
065
066 assertNotNull(pico.getComponent(Searcher.class));
067 assertNotNull(pico.getComponent(Searcher.class).getSearchPath());
068 }
069
070 @SuppressWarnings("serial")
071 public static class StringArrayList extends ArrayList<String> {
072 }
073
074 @Test
075 public void canInstantiateAutowiredCollectionThatAreDefinedExplicitlyAnotherWay() {
076 MutablePicoContainer pico = new DefaultPicoContainer(new Caching());
077 List<String> searchPath = new StringArrayList();
078 searchPath.add("a");
079 searchPath.add("b");
080
081 pico.addComponent(searchPath)
082 .addComponent(Searcher.class);
083
084 assertNotNull(pico.getComponent(Searcher.class));
085 List<String> list = pico.getComponent(Searcher.class).getSearchPath();
086 assertNotNull(list);
087 assertEquals("a", list.get(0));
088 assertEquals("b", list.get(1));
089 }
090
091
092
093 }