|
Struts example source code file (EmbeddedJSPResultTest.java)
The Struts EmbeddedJSPResultTest.java source code/* * $Id$ * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.struts2; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.conversion.impl.XWorkConverter; import com.opensymphony.xwork2.inject.Container; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.util.finder.ClassLoaderInterface; import com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate; import junit.framework.TestCase; import org.apache.commons.lang.StringUtils; import org.easymock.EasyMock; import org.easymock.IAnswer; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockServletContext; import javax.servlet.Servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class EmbeddedJSPResultTest extends TestCase { private HttpServletRequest request; private MockHttpServletResponse response; private MockServletContext context; private EmbeddedJSPResult result; public void testScriptlet() throws Exception { result.setLocation("org/apache/struts2/scriptlet.jsp"); result.execute(null); assertEquals("Saynotoscriptlets", StringUtils.deleteWhitespace(response.getContentAsString())); } public void testEmbedded() throws Exception { //the jsp is inside jsps.jar result.setLocation("dir/all.jsp"); result.execute(null); assertEquals("helloJGWhoamI?XXXXXXXXXXXYThissessionisnotsecure.", StringUtils.deleteWhitespace(response.getContentAsString())); } public void testFilesAreReadOnlyOnce() throws Exception { //make sure that files are not read multiple times String jsp = "org/apache/struts2/dont-use.jsp"; CountingClassLoaderInterface classLoaderInterface = new CountingClassLoaderInterface(this.getClass().getClassLoader()); context.setAttribute(ClassLoaderInterface.CLASS_LOADER_INTERFACE, classLoaderInterface); result.setLocation(jsp); result.execute(null); Integer counter0 = classLoaderInterface.counters.get(jsp); assertNotNull(counter0); result.execute(null); Integer counter1 = classLoaderInterface.counters.get(jsp); assertNotNull(counter1); assertEquals(counter0, counter1); } public void testEmbeddedAbsolutePath() throws Exception { //the jsp is inside jsps.jar result.setLocation("/dir/all.jsp"); result.execute(null); assertEquals("helloJGWhoamI?XXXXXXXXXXXYThissessionisnotsecure.", StringUtils.deleteWhitespace(response.getContentAsString())); } public void testSimple() throws Exception { result.setLocation("org/apache/struts2/simple0.jsp"); result.execute(null); assertEquals("hello", response.getContentAsString()); } //ok i give up..i don't know why this doesn't work from maven /* public void testKeyInContext() throws Exception { result.setLocation("org/apache/struts2/scriptlet.jsp"); result.execute(null); String key = JspApplicationContextImpl.class.getName() + "@" + this.getClass().getClassLoader().hashCode(); assertNotNull(context.getAttribute(key)); }*/ public void testEL() throws Exception { result.setLocation("org/apache/struts2/el.jsp"); result.execute(null); assertEquals("somethingelseText", response.getContentAsString()); } public void tesAbsolutePatht() throws Exception { result.setLocation("/org/apache/struts2/simple0.jsp"); result.execute(null); assertEquals("hello", response.getContentAsString()); } public void testTag0() throws Exception { result.setLocation("org/apache/struts2/tag0.jsp"); result.execute(null); assertEquals("Thissessionisnotsecure.OtherText", StringUtils.deleteWhitespace(response.getContentAsString())); } public void testIncludeSimple() throws Exception { result.setLocation("org/apache/struts2/includes0.jsp"); result.execute(null); assertEquals("helloTest", StringUtils.deleteWhitespace(response.getContentAsString())); } public void testIncludeSimpleWithDirective() throws Exception { result.setLocation("org/apache/struts2/includes3.jsp"); result.execute(null); assertEquals("helloTest", StringUtils.deleteWhitespace(response.getContentAsString())); } public void testIncludeWithSubdir() throws Exception { result.setLocation("org/apache/struts2/includes1.jsp"); result.execute(null); assertEquals("subTest", StringUtils.deleteWhitespace(response.getContentAsString())); } public void testIncludeWithParam() throws Exception { result.setLocation("org/apache/struts2/includes2.jsp"); result.execute(null); assertEquals("JGTest", StringUtils.deleteWhitespace(response.getContentAsString())); } public void testBroken0() throws Exception { try { result.setLocation("org/apache/struts2/broken0.jsp"); result.execute(null); fail("should have failed with broken jsp"); } catch (IllegalStateException ex) { //ok } } public void testJSTL() throws Exception { result.setLocation("org/apache/struts2/jstl.jsp"); result.execute(null); assertEquals("XXXXXXXXXXXY", StringUtils.deleteWhitespace(response.getContentAsString())); } public void testCachedInstances() throws InterruptedException { ServletCache cache = new ServletCache(); Servlet servlet1 = cache.get("org/apache/struts2/simple0.jsp"); Servlet servlet2 = cache.get("org/apache/struts2/simple0.jsp"); assertSame(servlet1, servlet2); } public void testCacheInstanceWithManyThreads() throws BrokenBarrierException, InterruptedException { //start a bunch of thread at the same time using CyclicBarrier and hit the cache //then wait for all the threads to end and check that they all got a reference to the same object //and the cache size should be 1 DummyServletCache cache = new DummyServletCache(); int numThreads = 70; CyclicBarrier startBarrier = new CyclicBarrier(numThreads + 1); CyclicBarrier endBarrier = new CyclicBarrier(numThreads + 1); List<ServletGetRunnable> runnables = new ArrayList Other Struts examples (source code examples)Here is a short list of links related to this Struts EmbeddedJSPResultTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.