KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > imp > BaseTest


1 /**
2  * Copyright 2001-2003 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.portletcontainer.imp;
6
7
8 import java.io.InputStream JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.net.URLClassLoader JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.HashMap JavaDoc;
13
14 import javax.portlet.PortletContext;
15 import javax.portlet.PortletMode;
16 import javax.portlet.WindowState;
17 import org.exoplatform.Constants;
18 import org.exoplatform.container.PortalContainer;
19 import org.exoplatform.container.RootContainer;
20 import org.exoplatform.container.SessionContainer;
21 import org.exoplatform.services.database.HibernateService;
22 import org.exoplatform.services.organization.OrganizationService;
23 import org.exoplatform.services.organization.User;
24 import org.exoplatform.services.portletcontainer.PortletApplicationRegister;
25 import org.exoplatform.services.portletcontainer.PortletContainerService;
26 import org.exoplatform.services.portletcontainer.impl.PortletContainerServiceImpl;
27 import org.exoplatform.services.portletcontainer.impl.monitor.PortletMonitor;
28 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.PortletAPIObjectFactory;
29 import org.exoplatform.services.portletcontainer.pci.*;
30 import org.exoplatform.services.portletcontainer.pci.model.PortletApp;
31 import org.exoplatform.services.portletcontainer.pci.model.XMLParser;
32 import org.exoplatform.services.portletcontainer.persistence.PortletPreferencesPersister;
33 import org.exoplatform.test.mocks.servlet.MockServletContext;
34
35 import junit.framework.TestCase;
36
37 /**
38  * Created by the Exo Development team.
39  * Author : Mestrallet Benjamin
40  * benjmestrallet@users.sourceforge.net
41  * Date: 11 nov. 2003
42  * Time: 22:08:31
43  */

44 public class BaseTest extends TestCase {
45
46   protected static final String JavaDoc PORTLET_APP_PATH = "file:./war_template";
47   protected PortletContainerServiceImpl portletContainer;
48   //protected PortletMonitor portletMonitor;
49
protected PortletApp portletApp_;
50   protected OrganizationService orgService_ ;
51   protected Collection JavaDoc roles;
52   protected URLClassLoader JavaDoc cl;
53   protected URLClassLoader JavaDoc cl2;
54   protected MockServletContext mockServletContext;
55   protected PortletContext portletContext;
56   static boolean initService_ = true;
57   protected RenderInput input;
58   protected ActionInput actionInput;
59   protected PortletMonitor portletMonitor;
60   protected PortletPreferencesPersister persister;
61   protected PortletApplicationRegister portletApplicationRegister;
62
63   public BaseTest(String JavaDoc s) {
64     super(s);
65   }
66
67   public void setUp() throws Exception JavaDoc {
68     PortalContainer portalContainer = PortalContainer.getInstance();
69     SessionContainer scontainer = portalContainer.createSessionContainer("sessioncontainer", "anon") ;
70     SessionContainer.setInstance(scontainer) ;
71     
72     persister = (PortletPreferencesPersister) portalContainer.getComponentInstanceOfType(PortletPreferencesPersister.class) ;
73     orgService_ =
74         (OrganizationService)portalContainer.getComponentInstanceOfType(OrganizationService.class) ;
75     User user = orgService_.findUserByName("exotest") ;
76     if(user == null) {
77         user = orgService_.createUserInstance() ;
78         user.setUserName("exotest") ;
79         user.setPassword("exo") ;
80         user.setFirstName("Exo") ;
81         user.setLastName("Platform") ;
82         user.setEmail("exo@exoportal.org") ;
83         orgService_.createUser(user);
84     }
85     URL JavaDoc url = new URL JavaDoc(PORTLET_APP_PATH + "/WEB-INF/portlet.xml");
86     InputStream JavaDoc is = url.openStream();
87     portletApp_ = XMLParser.parse(is);
88     
89     roles = new java.util.ArrayList JavaDoc() ;
90     roles.add("auth-user");
91
92     mockServletContext = new MockServletContext("hello", "./war_template");
93     mockServletContext.setInitParameter("test-param", "test-parame-value");
94
95     portletContainer = (PortletContainerServiceImpl) portalContainer.getComponentInstanceOfType(PortletContainerService.class);
96     portletApplicationRegister = (PortletApplicationRegister)portalContainer.getComponentInstanceOfType(PortletApplicationRegister.class);
97     portletApplicationRegister.registerPortletApplication(mockServletContext, portletApp_, roles);
98     portletMonitor = (PortletMonitor) RootContainer.getInstance().getComponentInstanceOfType(PortletMonitor.class);
99
100     portletContext = PortletAPIObjectFactory.getInstance().createPortletContext(mockServletContext);
101     ((MockServletContext)portalContainer.getComponentInstanceOfType(MockServletContext.class)).setName("hello");
102     //can be overidden for specific usages
103
ExoWindowID windowID = new ExoWindowID();
104     windowID.setOwner(Constants.ANON_USER);
105     windowID.setPortletApplicationName("hello");
106     windowID.setUniqueID("windowID");
107     windowID.setPersistenceId("persistenceID");
108
109
110     input = new RenderInput();
111     input.setBaseURL("exo/faces/portal/portal.jsp");
112     input.setWindowID(windowID);
113     input.setUserAttributes(new HashMap JavaDoc());
114     input.setPortletMode(PortletMode.VIEW);
115     input.setWindowState(WindowState.NORMAL);
116     input.setMarkup("text/html");
117     input.setRenderParameters(new HashMap JavaDoc());
118
119     actionInput = new ActionInput();
120     actionInput.setBaseURL("exo/faces/portal/portal.jsp");
121     actionInput.setWindowID(windowID);
122     actionInput.setUserAttributes(new HashMap JavaDoc());
123     actionInput.setPortletMode(PortletMode.VIEW);
124     actionInput.setWindowState(WindowState.NORMAL);
125     actionInput.setMarkup("text/html");
126   }
127
128   public void tearDown() throws Exception JavaDoc {
129     System.out.println("Tear down");
130     try {
131       portletApplicationRegister.removePortletApplication(mockServletContext);
132       PortalContainer manager = PortalContainer.getInstance();
133       HibernateService hservice =
134           (HibernateService) manager.getComponentInstanceOfType(HibernateService.class) ;
135       hservice.closeSession();
136     } catch (Exception JavaDoc e) {
137       e.printStackTrace();
138     }
139   }
140 }
Popular Tags