1   /*
2    *
3    * The Seasar Software License, Version 1.1
4    *
5    * Copyright (c) 2003-2004 The Seasar Project. All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or
8    * without modification, are permitted provided that the following
9    * conditions are met:
10   *
11   * 1. Redistributions of source code must retain the above
12   *    copyrightnotice, this list of conditions and the following
13   *    disclaimer.
14   *
15   * 2. Redistributions in binary form must reproduce the above
16   *    copyright notice, this list of conditions and the following
17   *    disclaimer in the documentation and/or other materials provided
18   *    with the distribution.
19   *
20   * 3. The end-user documentation included with the redistribution,
21   *    if any, must include the following acknowledgement:
22   *    "This product includes software developed by the
23   *    Seasar Project (http://www.seasar.org/)."
24   *    Alternately, this acknowledgement may appear in the software
25   *    itself, if and wherever such third-party acknowledgements
26   *    normally appear.
27   *
28   * 4. Neither the names "The Seasar Project" nor@the name of its
29   *    contributors ay be used to endour or promote products derived
30   *    from this software without specific prior written permission of
31   *    the Seasar Project.
32   *
33   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR
34   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35   * WARRANTIESOF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE SEASER@PROJECT
37   * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38   * INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42   * WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING
43   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
44   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package org.seasar.groovy;
48  
49  import java.util.List;
50  
51  import junit.framework.TestCase;
52  
53  import org.seasar.framework.container.S2Container;
54  import org.seasar.framework.container.factory.S2ContainerFactory;
55  import org.seasar.framework.container.impl.S2ContainerImpl;
56  
57  
58  /***
59   * @author takai
60   */
61  public class S2ContainerGroovyFactoryTest
62      extends TestCase
63  {
64      private static final String TEST_FILE =
65          "org/seasar/groovy/TestFactory.groovy";
66  
67      public void testBuild() {
68          S2ContainerGroovyFactory factory = new S2ContainerGroovyFactory();
69          S2Container container = factory.build(TEST_FILE);
70  
71          assertNotNull(container);
72          assertTrue(container.getComponent(List.class) instanceof List);
73      }
74  
75      public void testBuildWithClassLoader() {
76          MyLoader loader = new MyLoader(Thread.currentThread().getContextClassLoader());
77          S2ContainerGroovyFactory factory = new S2ContainerGroovyFactory();
78  
79          try {
80          	S2Container container = factory.build(TEST_FILE, loader);
81          	fail("should be thrown NoClassDefFoundError");
82          } catch (SeasarBuilderException e) {
83          	assertTrue(loader.visited);
84          }
85      }
86      
87      public void testBuildWithParent() {
88      	S2Container parent = new S2ContainerImpl();
89      	
90          S2ContainerGroovyFactory factory = new S2ContainerGroovyFactory();
91          S2Container container = factory.include(parent, TEST_FILE);
92  
93          assertNotNull(container);
94          assertTrue(container.getComponent(List.class) instanceof List);
95  	
96      }
97  
98      public void testCreate() {
99          S2Container container = S2ContainerGroovyFactory.create(TEST_FILE);
100         assertNotNull(container);
101         assertTrue(container.getComponent(List.class) instanceof List);
102 
103         try {
104             S2Container container2 =
105                 S2ContainerGroovyFactory.create("no/such/File");
106             fail("should be thrown SeasarBuilderException.");
107         } catch (SeasarBuilderException e) {
108         }
109 
110         try {
111             S2Container container2 =
112                 S2ContainerGroovyFactory.create("org/seasar/groovy/TestFactoryEmptyFile.groovy");
113             fail("should be thrown SeasarBuilderException.");
114         } catch (SeasarBuilderException e) {
115         }
116     }
117     
118     public void testS2Builder(){
119     	S2Container container = S2ContainerFactory.create("org/seasar/groovy/TestFactory.groovy");
120         assertNotNull(container);
121         assertTrue(container.getComponent(List.class) instanceof List);
122     }
123 
124     class MyLoader
125         extends ClassLoader
126     {
127         public boolean visited = false;
128         
129         public MyLoader(ClassLoader loader){
130         	super(loader);
131         }
132 
133         protected synchronized Class loadClass(String name, boolean resolve)
134             throws ClassNotFoundException
135         {
136             if (name.equals("java.util.ArrayList")) {
137                 visited = true;
138                 throw new ClassNotFoundException();
139             } else {
140             	return Class.forName(name);
141             }
142         }
143     }
144 }