1 package org.exoplatform.services.portletcontainer.imp; 2 3 import javax.portlet.PortletException; 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.net.MalformedURLException ; 7 import java.util.Enumeration ; 8 9 13 14 21 public class TestPortletContext extends BaseTest{ 22 23 public TestPortletContext(String s) { 24 super(s); 25 } 26 27 33 public void testPortletContextUnicityPerPortletApplication() throws PortletException { 34 } 36 37 44 public void testEqualityOfServletAndPortletContextParameters(){ 45 assertEquals("test-parame-value",portletContext.getInitParameter("test-param")); 46 } 47 48 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 70 public void testIdentitalResourcesFromPortletAndServletContext() throws IOException { 71 InputStream is1 = mockServletContext.getResourceAsStream("/WEB-INF/web.xml"); 72 InputStream 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 97 public void testContextAttributeAccess(){ 98 assertNotNull(portletContext.getAttribute("javax.servlet.context.tempdir")); 99 } 100 101 108 110 118 public void testCorrespondanceBetweenPortletAndServletContextMethods() throws MalformedURLException { 119 121 Enumeration e = mockServletContext.getAttributeNames(); 122 Enumeration e2 = portletContext.getAttributeNames(); 123 boolean equals = true; 124 while (e.hasMoreElements()) { 125 String attName = (String ) e.nextElement(); 126 if(!attName.equals(e2.nextElement())){ 127 equals = false; 128 break; 129 } 130 } 131 assertTrue(equals); 132 133 135 e = mockServletContext.getInitParameterNames(); 136 e2 = portletContext.getInitParameterNames(); 137 equals = true; 138 while (e.hasMoreElements()) { 139 String attName = (String ) 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 159 StringBuffer sB = new StringBuffer (); 160 sB.append("bad exception in log...."); 161 portletContext.log(sB.toString()); 162 assertEquals(mockServletContext.getLogBuffer(), sB.toString()); 163 164 sB = new StringBuffer (); 165 sB.append("bad exception in log....the come back"); 166 portletContext.log(sB.toString(), new Exception ("olala")); 167 assertEquals(mockServletContext.getLogBuffer(), sB.toString()+"olala"); 168 169 sB = new StringBuffer (); 170 sB.append("bad exception in log....the come back"); 171 portletContext.log(sB.toString(), new Throwable ("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 } 180 181 182 } 183 | Popular Tags |