View Javadoc

1   /*
2    * joey-test and its relative products are published under the terms
3    * of the Apache Software License.
4    * 
5    * Created on 2004/11/28 1:28:16
6    */
7   package org.asyrinx.joey.test.dao;
8   
9   import org.seasar.framework.container.ComponentDef;
10  import org.seasar.framework.container.S2Container;
11  import org.seasar.framework.container.factory.S2ContainerFactory;
12  import org.seasar.framework.container.impl.S2ContainerImpl;
13  import org.seasar.framework.util.ResourceUtil;
14  
15  import junit.framework.TestCase;
16  
17  /***
18   * @author takeshi
19   */
20  public abstract class S2TestCaseBase extends TestCase {
21  
22      /***
23       *  
24       */
25      public S2TestCaseBase(String name) {
26          super(name);
27      }
28  
29      protected S2Container container_;
30  
31      public S2Container getContainer() {
32          return container_;
33      }
34  
35      public Object getComponent(String componentName) {
36          return container_.getComponent(componentName);
37      }
38  
39      public Object getComponent(Class componentClass) {
40          return container_.getComponent(componentClass);
41      }
42  
43      public ComponentDef getComponentDef(String componentName) {
44          return container_.getComponentDef(componentName);
45      }
46  
47      public ComponentDef getComponentDef(Class componentClass) {
48          return container_.getComponentDef(componentClass);
49      }
50  
51      public void register(Class componentClass) {
52          container_.register(componentClass);
53      }
54  
55      public void register(Class componentClass, String componentName) {
56          container_.register(componentClass, componentName);
57      }
58  
59      public void register(Object component) {
60          container_.register(component);
61      }
62  
63      public void register(Object component, String componentName) {
64          container_.register(component, componentName);
65      }
66  
67      public void register(ComponentDef componentDef) {
68          container_.register(componentDef);
69      }
70  
71      public void include(String path) {
72          container_.include(S2ContainerFactory.create(convertPath(path)));
73      }
74  
75      private String convertPath(String path) {
76          if (ResourceUtil.getResourceNoException(path) != null)
77              return path;
78          final String prefix = getClass().getPackage().getName().replace('.', '/');
79          return prefix + "/" + path;
80      }
81  
82      /*
83       * (non-Javadoc)
84       * 
85       * @see junit.framework.TestCase#runBare()
86       */
87      public void runBare() throws Throwable {
88          container_ = new S2ContainerImpl();
89          setUpContainer();
90          container_.init();
91          try {
92              afterSetupContainer();
93              executeRunBare();
94          } finally {
95              container_.destroy();
96          }
97      }
98  
99      /***
100      * @throws Throwable
101      *  
102      */
103     protected void afterSetupContainer() throws Throwable {
104         //do nothing
105     }
106 
107     /***
108      * @throws Throwable
109      *  
110      */
111     protected void executeRunBare() throws Throwable {
112         super.runBare();
113     }
114 
115     /***
116      * includeメソッドを呼び出すtemplateメソッド
117      */
118     protected abstract void setUpContainer();
119 
120 }