View Javadoc

1   package br.com.ibnetwork.guara.test;
2   
3   import java.io.File;
4   import java.net.URL;
5   import java.util.HashMap;
6   import java.util.Map;
7   
8   import javax.servlet.http.HttpServletRequest;
9   import javax.servlet.http.HttpServletResponse;
10  
11  import br.com.ibnetwork.guara.modules.ModuleLoader;
12  import br.com.ibnetwork.guara.rundata.RunData;
13  import br.com.ibnetwork.guara.rundata.RunDataPool;
14  import br.com.ibnetwork.guara.test.mock.MockHttpServletRequest;
15  import br.com.ibnetwork.guara.test.mock.MockHttpServletResponse;
16  import br.com.ibnetwork.guara.view.Context;
17  import br.com.ibnetwork.guara.view.TemplateEngine;
18  import br.com.ibnetwork.xingu.container.Container;
19  import br.com.ibnetwork.xingu.container.ContainerUtils;
20  import junit.framework.TestCase;
21  
22  /***
23   * @author leandro
24   */
25  public abstract class GuaraTestCase
26      extends TestCase
27  {
28      private static Container container;
29  
30      protected RunDataPool runDataPool;
31      
32      protected ModuleLoader loader;
33      
34      protected TemplateEngine templateEngine;
35      
36      public GuaraTestCase(String name)
37      {
38          super(name);
39      }
40  
41      public GuaraTestCase()
42      {
43          super();
44      }
45  
46      protected Container getContainer()
47          throws Exception
48      {
49          if(container == null)
50          {
51              String fileName = getContainerFile();
52              URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);
53              container = ContainerUtils.createContainer(url.getFile());
54          }
55          return container;
56      }
57  
58      protected String getContainerFile()
59      {
60          return "guaraTest.xml";
61      }
62      
63      protected void backupResources() throws Exception {}
64  
65      protected void restoreResources() throws Exception {}
66      
67      protected void setUp()
68      	throws Exception
69      {
70          Container c = getContainer();
71          runDataPool = (RunDataPool) c.lookup(RunDataPool.class);
72          loader = (ModuleLoader) c.lookup(ModuleLoader.class);
73          templateEngine = (TemplateEngine) c.lookup(TemplateEngine.class);
74          backupResources();
75      }
76  
77      protected void tearDown()
78  		throws Exception
79  	{
80          runDataPool = null;
81          loader = null;
82          templateEngine = null;
83          restoreResources();
84  	}
85  
86      protected Context createContext()
87      {
88          return templateEngine.createContext();
89      }
90      
91      protected RunData createRunData()
92      {
93          Map map = new HashMap();
94          String path = "";
95          return createRunData(map,path);
96      }
97      
98      protected RunData createRunData(Map map, String path)
99      {
100         HttpServletRequest request = new MockHttpServletRequest(map,path);
101         HttpServletResponse response = new MockHttpServletResponse();
102         RunData runData = runDataPool.create(request,response,null);
103         return runData;
104     }
105 
106     public static File loadFile(String fileName)
107     {
108         URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);
109         return new File(url.getFile());
110     }
111 }