Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/xpn/xwiki/test/ServletTest.java


1   /**
2    * User: ludovic
3    * Date: 13 mars 2004
4    * Time: 15:04:19
5    * ===================================================================
6    *
7    * Copyright (c) 2003 Ludovic Dubost, All rights reserved.
8    *
9    * This program is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; either version 2
12   * of the License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details, published at
18   * http://www.gnu.org/copyleft/gpl.html or in gpl.txt in the
19   * root folder of this distribution.
20   *
21   */
22  
23  package com.xpn.xwiki.test;
24  
25  import com.xpn.xwiki.XWiki;
26  import com.xpn.xwiki.XWikiContext;
27  import com.xpn.xwiki.XWikiException;
28  import com.xpn.xwiki.store.XWikiCacheStoreInterface;
29  import com.xpn.xwiki.store.XWikiHibernateStore;
30  import com.xpn.xwiki.store.XWikiStoreInterface;
31  import org.hibernate.HibernateException;
32  import org.apache.cactus.ServletTestCase;
33  import org.apache.cactus.WebRequest;
34  import org.apache.struts.action.ActionServlet;
35  
36  import javax.servlet.ServletContext;
37  import javax.servlet.ServletException;
38  import javax.servlet.http.HttpSession;
39  import java.io.File;
40  import java.util.Enumeration;
41  import java.util.Vector;
42  
43  
44  public abstract class ServletTest extends ServletTestCase {
45      public String hibpath = "hibernate-test.cfg.xml";
46      public XWikiContext context = new XWikiContext();
47      public XWiki xwiki;
48  
49      public void setUp() throws Exception {
50          super.setUp();
51          flushCache();
52      };
53  
54      public XWikiHibernateStore getHibStore() {
55          XWikiStoreInterface store = xwiki.getStore();
56          if (store instanceof XWikiCacheStoreInterface)
57              return (XWikiHibernateStore)((XWikiCacheStoreInterface)store).getStore();
58          else
59              return (XWikiHibernateStore) store;
60      }
61  
62      public void cleanUp() {
63      };
64  
65      public void clientSetUp(XWikiStoreInterface store) throws XWikiException {
66          xwiki = new XWiki("./xwiki.cfg", context);
67          context.setWiki(xwiki);
68      }
69  
70      public void clientTearDown() throws HibernateException {
71          getHibStore().shutdownHibernate(context);
72          xwiki = null;
73          context = null;
74          System.gc();
75      }
76  
77      public static void setUrl(WebRequest webRequest, String action, String docname) {
78          setUrl(webRequest, action, docname, "");
79      }
80  
81      public static void setUrl(WebRequest webRequest, String action, String docname, String query) {
82          setUrl(webRequest, action, "Main", docname, query);
83      }
84  
85      public static void setUrl(WebRequest webRequest, String action, String web, String docname, String query) {
86          webRequest.setURL("127.0.0.1:9080", "/xwiki" , "/testbin", "/" + action + "/" + web + "/" + docname, query);
87      }
88  
89      public static void setVirtualUrl(WebRequest webRequest, String host, String appname, String action, String docname, String query) {
90          webRequest.setURL(host + ":9080", "/" + appname , "/testbin", "/" + action + "/Main/" + docname, query);
91      }
92  
93      public String getHibpath() {
94          // Usefull in case we need to understand where we are
95          String path = (new File(".")).getAbsolutePath();
96          System.out.println("Current Directory is: " + path);
97  
98          File file = new File(hibpath);
99          if (file.exists())
100             return hibpath;
101 
102         file = new File("WEB-INF", hibpath);
103         if (file.exists())
104             return "./WEB-INF/" + hibpath;
105 
106         file = new File("test", hibpath);
107         if (file.exists())
108             return "./test/" + hibpath;
109 
110         if (config!=null)
111         {
112             ServletContext context = config.getServletContext();
113             if (context!=null)
114                 return context.getRealPath("WEB-INF/" + hibpath);
115         }
116 
117         return hibpath;
118     }
119 
120     public void cleanSession(HttpSession session) {
121         Vector names = new Vector();
122         Enumeration enumeration = session.getAttributeNames();
123         while (enumeration.hasMoreElements()) {
124             String name = (String) enumeration.nextElement();
125             names.add(name);
126         }
127 
128         for (int i=0;i<names.size();i++)
129         {
130             session.removeAttribute((String)names.get(i));
131         }
132     }
133 
134     public void flushCache() {
135         // We need to flush the server cache before running our tests
136         // because we are modifiying the database behind the scenes
137         // so if we are running the tests twice we won't necessarly
138         // get the same results..
139         try {
140             XWiki xwiki = (XWiki) config.getServletContext().getAttribute("xwikitest");
141             if (xwiki!=null)
142                 xwiki.flushCache();
143             xwiki = (XWiki) config.getServletContext().getAttribute("xwiki");
144             if (xwiki!=null)
145              xwiki.flushCache();
146         } catch (Exception e) {
147             e.printStackTrace();
148         }
149     }
150 
151     public void launchTest() throws Throwable {
152 
153       // by default we don't check the number of active connections
154       launchTest(false);
155     }
156 
157     public void launchTest(boolean checkActive) throws Throwable {
158         try {
159             ActionServlet servlet = new ActionServlet();
160             servlet.init(config);
161             servlet.service(request, response);
162 
163             if (checkActive) {
164                 // Let's verify that we didn't let any connections behind..
165                 XWiki xwiki = (XWiki) config.getServletContext().getAttribute("xwiki");
166                 if (xwiki != null) {
167                     XWikiHibernateStore store = (XWikiHibernateStore) ((XWikiCacheStoreInterface) xwiki.getStore()).getStore();
168                     assertEquals("Active connections in xwiki should be zero", 0, store.getConnections().size());
169                 }
170 
171                 xwiki = (XWiki) config.getServletContext().getAttribute("xwikitest");
172                 if (xwiki != null) {
173                     XWikiHibernateStore store = (XWikiHibernateStore) ((XWikiCacheStoreInterface) xwiki.getStore()).getStore();
174                     assertEquals("Active connections in xwikitest should be zero", 0, store.getConnections().size());
175                 }
176             }
177 
178             cleanSession(session);
179         } catch (ServletException e) {
180             e.getRootCause().printStackTrace();
181             throw e.getRootCause();
182         }
183     }
184 
185 }