1   /*
2    * SpringGuiceComparison - Comparing Guice, Pico and Spring
3    * Copyright (C) 2007 Christian Schenk
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    * 
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18   */
19  package org.christianschenk.guicepicospringcomparison;
20  
21  import org.christianschenk.guicepicospringcomparison.managers.ComputationManager;
22  import org.christianschenk.guicepicospringcomparison.managers.OutputManager1;
23  import org.christianschenk.guicepicospringcomparison.managers.OutputManager2;
24  import org.christianschenk.guicepicospringcomparison.managers.SuperManager;
25  import org.christianschenk.guicepicospringcomparison.util.ComputationHelper;
26  import org.christianschenk.guicepicospringcomparison.util.OutputHelper;
27  import org.christianschenk.guicepicospringcomparison.util.output.LoggerOutput;
28  import org.christianschenk.guicepicospringcomparison.util.output.SystemOutOutput;
29  import org.junit.Test;
30  import org.picocontainer.MutablePicoContainer;
31  import org.picocontainer.defaults.DefaultPicoContainer;
32  import org.springframework.beans.factory.BeanFactory;
33  import org.springframework.beans.factory.xml.XmlBeanFactory;
34  import org.springframework.core.io.ClassPathResource;
35  
36  import com.google.inject.Binder;
37  import com.google.inject.Guice;
38  import com.google.inject.Injector;
39  import com.google.inject.Module;
40  
41  public class ComparisonTest {
42  
43  	/*
44  	 * Guice
45  	 */
46  	@Test
47  	public void guice() {
48  		final Injector injector = Guice.createInjector(new GuiceBinderModule());
49  		final SuperManager mgr = injector.getInstance(SuperManager.class);
50  		mgr.manage1();
51  		mgr.manage2();
52  	}
53  
54  	private final class GuiceBinderModule implements Module {
55  		public void configure(final Binder binder) {
56  			binder.bind(OutputManager1.class).toInstance(new OutputManager1(new OutputHelper(new SystemOutOutput())));
57  			binder.bind(OutputManager2.class).toInstance(new OutputManager2(new OutputHelper(new LoggerOutput())));
58  		}
59  	}
60  
61  	/*
62  	 * PicoContainer
63  	 */
64  	@Test
65  	public void picocontainer() {
66  		final MutablePicoContainer pico = new DefaultPicoContainer();
67  		pico.registerComponentImplementation(SuperManager.class);
68  		pico.registerComponentInstance(new OutputManager1(new OutputHelper(new SystemOutOutput())));
69  		pico.registerComponentInstance(new OutputManager2(new OutputHelper(new LoggerOutput())));
70  		pico.registerComponentImplementation(ComputationManager.class);
71  		pico.registerComponentImplementation(ComputationHelper.class);
72  
73  		final SuperManager mgr = (SuperManager) pico.getComponentInstance(SuperManager.class);
74  		mgr.manage1();
75  		mgr.manage2();
76  	}
77  
78  	/*
79  	 * Spring
80  	 */
81  	@Test
82  	public void spring() {
83  		final BeanFactory factory = new XmlBeanFactory(new ClassPathResource("spring.xml"));
84  		final SuperManager mgr = (SuperManager) factory.getBean("SuperManager");
85  		mgr.manage1();
86  		mgr.manage2();
87  	}
88  }