KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > bsh > BeanShellComponentAdapterTestCase


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by Leo Simons *
9  *****************************************************************************/

10 package org.nanocontainer.script.bsh;
11
12 import java.util.ArrayList JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import org.picocontainer.ComponentAdapter;
17 import org.picocontainer.MutablePicoContainer;
18 import org.picocontainer.defaults.DefaultPicoContainer;
19
20 /**
21  * <a HREF="http://www.junit.org/">JUnit</a>
22  * {@link junit.framework.TestCase testcase} for
23  * BeanShellComponentAdapter.
24  *
25  * @author <a HREF="mail at leosimons dot com">Leo Simons</a>
26  * @author Nick Sieger
27  * @version $Id: BeanShellComponentAdapterTestCase.java 3144 2006-12-26 10:12:19Z mauro $
28  */

29 public class BeanShellComponentAdapterTestCase extends TestCase {
30
31     private MutablePicoContainer pico;
32
33     ComponentAdapter setupComponentAdapter(Class JavaDoc implementation) {
34         pico = new DefaultPicoContainer();
35         pico.registerComponentImplementation("whatever", ArrayList JavaDoc.class);
36
37         ComponentAdapter adapter = new BeanShellComponentAdapter("thekey", implementation, null);
38         pico.registerComponent(adapter);
39         return adapter;
40     }
41
42     public void testGetComponentInstance() {
43         ComponentAdapter adapter = setupComponentAdapter(ScriptableDemoBean.class);
44
45         ScriptableDemoBean bean = (ScriptableDemoBean) adapter.getComponentInstance(pico);
46
47         assertEquals("Bsh demo script should have set the key", "thekey", bean.key);
48
49         assertTrue(bean.whatever instanceof ArrayList JavaDoc);
50     }
51
52     public void testGetComponentInstanceBadScript() {
53         ComponentAdapter adapter = setupComponentAdapter(BadScriptableDemoBean.class);
54
55         try {
56             adapter.getComponentInstance(pico);
57             fail("did not throw exception on missing 'instance' variable");
58         } catch (BeanShellScriptInitializationException bssie) {
59             // success
60
}
61     }
62
63 }
64
Popular Tags