KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.exoplatform.services.portletcontainer.imp;
2
3 import javax.portlet.PortletException;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.net.MalformedURLException JavaDoc;
7 import java.util.Enumeration JavaDoc;
8
9 /**
10  * Copyright 2001-2003 The eXo platform SARL All rights reserved.
11  * Please look at license.txt in info directory for more license detail.
12  **/

13
14 /**
15  * Created by the Exo Development team.
16  * Author : Mestrallet Benjamin
17  * benjmestrallet@users.sourceforge.net
18  * Date: 15 nov. 2003
19  * Time: 13:58:26
20  */

21 public class TestPortletContext extends BaseTest{
22
23     public TestPortletContext(String JavaDoc s) {
24         super(s);
25     }
26
27     /**
28      * test (xli) : There is one instance of the PortletContext interface associated with
29      * each portlet application deployed into a portlet container.
30      *
31      * PLT.10.1
32      */

33   public void testPortletContextUnicityPerPortletApplication() throws PortletException {
34     //TODO find a way to test
35
}
36
37     /**
38      * test (xliii) : The initialization parameters accessible through the PortletContext
39      * must be the same that are accessible through the ServletContext of
40      * the portlet application.
41      *
42      * PLT.10.3
43      */

44     public void testEqualityOfServletAndPortletContextParameters(){
45     assertEquals("test-parame-value",portletContext.getInitParameter("test-param"));
46     }
47
48     /**
49      * test (xliv) : Context attributes set using the PortletContext must be stored in the
50      * ServletContext of the portlet application. A direct consequence of this
51      * is that data stored in the ServletContext by servlets or JSPs is accessible
52      * to portlets through the PortletContext and vice versa.
53      *
54      * PLT.10.3
55      */

56     public void testAttributesShareBetweenPortletAndServlets(){
57     mockServletContext.setAttribute("testAtt1", "attValue1");
58         assertEquals("attValue1", portletContext.getAttribute("testAtt1"));
59
60         portletContext.setAttribute("testAtt2", "attValue2");
61         assertEquals("attValue2", mockServletContext.getAttribute("testAtt2"));
62     }
63
64     /**
65      * test (xlv) : The PortletContext must offer access to the same set of resources the
66      * ServletContext exposes.
67      *
68      * PLT.10.3
69      */

70     public void testIdentitalResourcesFromPortletAndServletContext() throws IOException JavaDoc {
71     InputStream JavaDoc is1 = mockServletContext.getResourceAsStream("/WEB-INF/web.xml");
72         InputStream JavaDoc is2 = portletContext.getResourceAsStream("/WEB-INF/web.xml");
73     byte[] byteArray1 = new byte[1024];
74         byte[] byteArray2 = new byte[1024];
75         is1.read(byteArray1);
76         is2.read(byteArray2);
77         System.out.println(byteArray1);
78         boolean equals = true;
79         for (int i = 0; i < byteArray1.length; i++) {
80             byte b = byteArray1[i];
81             if(b != byteArray2[i]){
82                 equals = false;
83                 break;
84             }
85         }
86         assertTrue(equals);
87     }
88
89     /**
90      * test (xlvi) : The PortletContext must handle the same temporary working directory the
91      * ServletContext handles. It must be accessible as a context attribute
92      * using the same constant defined in the Servlet Specification 2.3 SVR 3
93      * Servlet Context Chapter, javax.servlet.context.tempdir.
94      *
95      * PLT.10.3
96      */

97   public void testContextAttributeAccess(){
98     assertNotNull(portletContext.getAttribute("javax.servlet.context.tempdir"));
99     }
100
101     /**
102      * test (xlvii) : The portlet context must follow the same behavior and functionality that
103      * the servlet context has for virtual hosting and reloading considerations.
104      * (see Servlet Specification 2.3 SVR 3 Servlet Context Chapter)
105      *
106      * PLT.10.3
107      */

108   //TODO find a way to test that
109

110     /**
111      * test : The following methods of the PortletContext should provide the same functionality
112      * as the methods of the ServletContext of similar name: getAttribute, getAttributeNames,
113      * getInitParameter, getInitParameterNames, getMimeType, getRealPath, getResource,
114      * getResourcePaths, getResourceAsStream, log, removeAttribute and setAttribute.
115      *
116      * PLT.10.3.1
117      */

118   public void testCorrespondanceBetweenPortletAndServletContextMethods() throws MalformedURLException JavaDoc {
119         //getAttribute has already been tested
120

121         Enumeration JavaDoc e = mockServletContext.getAttributeNames();
122         Enumeration JavaDoc e2 = portletContext.getAttributeNames();
123         boolean equals = true;
124         while (e.hasMoreElements()) {
125             String JavaDoc attName = (String JavaDoc) e.nextElement();
126             if(!attName.equals(e2.nextElement())){
127                 equals = false;
128                 break;
129             }
130         }
131     assertTrue(equals);
132
133         //getInitParameter has already been tested
134

135         e = mockServletContext.getInitParameterNames();
136         e2 = portletContext.getInitParameterNames();
137         equals = true;
138         while (e.hasMoreElements()) {
139             String JavaDoc attName = (String JavaDoc) e.nextElement();
140             if(!attName.equals(e2.nextElement())){
141                 equals = false;
142                 break;
143             }
144         }
145     assertTrue(equals);
146
147         assertEquals(mockServletContext.getMimeType("blbla"), portletContext.getMimeType("blabla"));
148
149         assertEquals(mockServletContext.getRealPath("blabla"), portletContext.getRealPath("blabla"));
150
151         assertEquals(mockServletContext.getResource("/WEB-INF/web.xml"),
152                         portletContext.getResource("/WEB-INF/web.xml"));
153
154     assertEquals(mockServletContext.getResourcePaths("/"),
155                         portletContext.getResourcePaths("/"));
156
157         //getResourceAsStream has already been tested
158

159         StringBuffer JavaDoc sB = new StringBuffer JavaDoc();
160         sB.append("bad exception in log....");
161         portletContext.log(sB.toString());
162         assertEquals(mockServletContext.getLogBuffer(), sB.toString());
163
164         sB = new StringBuffer JavaDoc();
165         sB.append("bad exception in log....the come back");
166         portletContext.log(sB.toString(), new Exception JavaDoc("olala"));
167         assertEquals(mockServletContext.getLogBuffer(), sB.toString()+"olala");
168
169         sB = new StringBuffer JavaDoc();
170         sB.append("bad exception in log....the come back");
171         portletContext.log(sB.toString(), new Throwable JavaDoc("olala"));
172         assertEquals(mockServletContext.getLogBuffer(), sB.toString()+"olala");
173
174         portletContext.setAttribute("testAtt2", "attValue2");
175         mockServletContext.removeAttribute("testAtt2");
176         assertNull(portletContext.getAttribute("testAtt2"));
177
178         //setAttribute has already been tested
179
}
180
181
182 }
183
Popular Tags