You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2016/01/14 00:07:10 UTC
svn commit: r1724523 [2/3] - in /sling/trunk: ./
bundles/extensions/servlet-helpers/ bundles/extensions/servlet-helpers/src/
bundles/extensions/servlet-helpers/src/main/
bundles/extensions/servlet-helpers/src/main/java/
bundles/extensions/servlet-helpe...
Added: sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletResponse.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletResponse.java?rev=1724523&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletResponse.java (added)
+++ sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletResponse.java Wed Jan 13 23:07:09 2016
@@ -0,0 +1,282 @@
+/*
+ * 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.sling.servlethelpers;
+
+import java.io.PrintWriter;
+import java.util.Collection;
+import java.util.Locale;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.adapter.SlingAdaptable;
+
+import aQute.bnd.annotation.ConsumerType;
+
+/**
+ * Mock {@link SlingHttpServletResponse} implementation.
+ */
+@ConsumerType
+public class MockSlingHttpServletResponse extends SlingAdaptable implements SlingHttpServletResponse {
+
+ static final String CHARSET_SEPARATOR = ";charset=";
+
+ private String contentType;
+ private String characterEncoding;
+ private int contentLength;
+ private int status = HttpServletResponse.SC_OK;
+ private int bufferSize = 1024 * 8;
+ private boolean isCommitted;
+ private final HeaderSupport headerSupport = new HeaderSupport();
+ private final ResponseBodySupport bodySupport = new ResponseBodySupport();
+ private final CookieSupport cookieSupport = new CookieSupport();
+
+ @Override
+ public String getContentType() {
+ if (this.contentType == null) {
+ return null;
+ } else {
+ return this.contentType
+ + (StringUtils.isNotBlank(characterEncoding) ? CHARSET_SEPARATOR + characterEncoding : "");
+ }
+ }
+
+ @Override
+ public void setContentType(String type) {
+ this.contentType = type;
+ if (StringUtils.contains(this.contentType, CHARSET_SEPARATOR)) {
+ this.characterEncoding = StringUtils.substringAfter(this.contentType, CHARSET_SEPARATOR);
+ this.contentType = StringUtils.substringBefore(this.contentType, CHARSET_SEPARATOR);
+ }
+ }
+
+ @Override
+ public void setCharacterEncoding(String charset) {
+ this.characterEncoding = charset;
+ }
+
+ @Override
+ public String getCharacterEncoding() {
+ return this.characterEncoding;
+ }
+
+ @Override
+ public void setContentLength(int len) {
+ this.contentLength = len;
+ }
+
+ public int getContentLength() {
+ return this.contentLength;
+ }
+
+ @Override
+ public void setStatus(int sc, String sm) {
+ setStatus(sc);
+ }
+
+ @Override
+ public void setStatus(int sc) {
+ this.status = sc;
+ }
+
+ @Override
+ public int getStatus() {
+ return this.status;
+ }
+
+ @Override
+ public void sendError(int sc, String msg) {
+ setStatus(sc);
+ }
+
+ @Override
+ public void sendError(int sc) {
+ setStatus(sc);
+ }
+
+ @Override
+ public void sendRedirect(String location) {
+ setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
+ setHeader("Location", location);
+ }
+
+ @Override
+ public void addHeader(String name, String value) {
+ headerSupport.addHeader(name, value);
+ }
+
+ @Override
+ public void addIntHeader(String name, int value) {
+ headerSupport.addIntHeader(name, value);
+ }
+
+ @Override
+ public void addDateHeader(String name, long date) {
+ headerSupport.addDateHeader(name, date);
+ }
+
+ @Override
+ public void setHeader(String name, String value) {
+ headerSupport.setHeader(name, value);
+ }
+
+ @Override
+ public void setIntHeader(String name, int value) {
+ headerSupport.setIntHeader(name, value);
+ }
+
+ @Override
+ public void setDateHeader(String name, long date) {
+ headerSupport.setDateHeader(name, date);
+ }
+
+ @Override
+ public boolean containsHeader(String name) {
+ return headerSupport.containsHeader(name);
+ }
+
+ @Override
+ public String getHeader(String name) {
+ return headerSupport.getHeader(name);
+ }
+
+ @Override
+ public Collection<String> getHeaders(String name) {
+ return headerSupport.getHeaders(name);
+ }
+
+ @Override
+ public Collection<String> getHeaderNames() {
+ return headerSupport.getHeaderNames();
+ }
+
+ @Override
+ public PrintWriter getWriter() {
+ return bodySupport.getWriter(getCharacterEncoding());
+ }
+
+ @Override
+ public ServletOutputStream getOutputStream() {
+ return bodySupport.getOutputStream();
+ }
+
+ @Override
+ public void reset() {
+ if (isCommitted()) {
+ throw new IllegalStateException("Response already committed.");
+ }
+ bodySupport.reset();
+ headerSupport.reset();
+ cookieSupport.reset();
+ status = HttpServletResponse.SC_OK;
+ contentLength = 0;
+ }
+
+ @Override
+ public void resetBuffer() {
+ if (isCommitted()) {
+ throw new IllegalStateException("Response already committed.");
+ }
+ bodySupport.reset();
+ }
+
+ @Override
+ public int getBufferSize() {
+ return this.bufferSize;
+ }
+
+ @Override
+ public void setBufferSize(int size) {
+ this.bufferSize = size;
+ }
+
+ @Override
+ public void flushBuffer() {
+ isCommitted = true;
+ }
+
+ @Override
+ public boolean isCommitted() {
+ return isCommitted;
+ }
+
+ public byte[] getOutput() {
+ return bodySupport.getOutput();
+ }
+
+ public String getOutputAsString() {
+ return bodySupport.getOutputAsString(getCharacterEncoding());
+ }
+
+ @Override
+ public void addCookie(Cookie cookie) {
+ cookieSupport.addCookie(cookie);
+ }
+
+ /**
+ * Get cookie
+ * @param name Cookie name
+ * @return Cookie or null
+ */
+ public Cookie getCookie(String name) {
+ return cookieSupport.getCookie(name);
+ }
+
+ /**
+ * Get cookies
+ * @return Cookies array or null if no cookie defined
+ */
+ public Cookie[] getCookies() {
+ return cookieSupport.getCookies();
+ }
+
+ // --- unsupported operations ---
+ @Override
+ public Locale getLocale() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setLocale(Locale loc) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public String encodeRedirectUrl(String url) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public String encodeRedirectURL(String url) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public String encodeUrl(String url) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public String encodeURL(String url) {
+ throw new UnsupportedOperationException();
+ }
+}
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletResponse.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletResponse.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Wed Jan 13 23:07:09 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/MockSlingHttpServletResponse.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/ResponseBodySupport.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/ResponseBodySupport.java?rev=1724523&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/ResponseBodySupport.java (added)
+++ sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/ResponseBodySupport.java Wed Jan 13 23:07:09 2016
@@ -0,0 +1,100 @@
+/*
+ * 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.sling.servlethelpers;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
+
+import javax.servlet.ServletOutputStream;
+
+import org.apache.commons.lang3.CharEncoding;
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * Manage response body content.
+ */
+class ResponseBodySupport {
+
+ private ByteArrayOutputStream outputStream;
+ private ServletOutputStream servletOutputStream;
+ private PrintWriter printWriter;
+
+ public ResponseBodySupport() {
+ reset();
+ }
+
+ public void reset() {
+ outputStream = new ByteArrayOutputStream();
+ servletOutputStream = null;
+ printWriter = null;
+ }
+
+ public ServletOutputStream getOutputStream() {
+ if (servletOutputStream == null) {
+ servletOutputStream = new ServletOutputStream() {
+ @Override
+ public void write(int b) throws IOException {
+ outputStream.write(b);
+ }
+ };
+ }
+ return servletOutputStream;
+ }
+
+ public PrintWriter getWriter(String charset) {
+ if (printWriter == null) {
+ try {
+ printWriter = new PrintWriter(new OutputStreamWriter(getOutputStream(), defaultCharset(charset)));
+ } catch (UnsupportedEncodingException ex) {
+ throw new RuntimeException("Unsupported encoding: " + defaultCharset(charset), ex);
+ }
+ }
+ return printWriter;
+ }
+
+ public byte[] getOutput() {
+ if (servletOutputStream != null) {
+ try {
+ servletOutputStream.flush();
+ } catch (IOException ex) {
+ // ignore
+ }
+ }
+ return outputStream.toByteArray();
+ }
+
+ public String getOutputAsString(String charset) {
+ if (printWriter != null) {
+ printWriter.flush();
+ }
+ try {
+ return new String(getOutput(), defaultCharset(charset));
+ } catch (UnsupportedEncodingException ex) {
+ throw new RuntimeException("Unsupported encoding: " + defaultCharset(charset), ex);
+ }
+ }
+
+ private String defaultCharset(String charset) {
+ return StringUtils.defaultString(charset, CharEncoding.UTF_8);
+ }
+
+}
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/ResponseBodySupport.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/ResponseBodySupport.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Wed Jan 13 23:07:09 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/ResponseBodySupport.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/package-info.java?rev=1724523&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/package-info.java (added)
+++ sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/package-info.java Wed Jan 13 23:07:09 2016
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+/**
+ * Mock implementation of selected Servlet-related Sling APIs.
+ */
+@aQute.bnd.annotation.Version("1.0")
+package org.apache.sling.servlethelpers;
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/package-info.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/package-info.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Wed Jan 13 23:07:09 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/main/java/org/apache/sling/servlethelpers/package-info.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockHttpSessionTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockHttpSessionTest.java?rev=1724523&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockHttpSessionTest.java (added)
+++ sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockHttpSessionTest.java Wed Jan 13 23:07:09 2016
@@ -0,0 +1,103 @@
+/*
+ * 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.sling.servlethelpers;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.sling.servlethelpers.MockHttpSession;
+import org.junit.Before;
+import org.junit.Test;
+
+public class MockHttpSessionTest {
+
+ private MockHttpSession httpSession;
+
+ @Before
+ public void setUp() throws Exception {
+ httpSession = new MockHttpSession();
+ }
+
+ @Test
+ public void testServletContext() {
+ assertNotNull(httpSession.getServletContext());
+ }
+
+ @Test
+ public void testId() {
+ assertNotNull(httpSession.getId());
+ }
+
+ @Test
+ public void testCreationTime() {
+ assertNotNull(httpSession.getCreationTime());
+ }
+
+ @Test
+ public void testAttributes() {
+ httpSession.setAttribute("attr1", "value1");
+ assertTrue(httpSession.getAttributeNames().hasMoreElements());
+ assertEquals("value1", httpSession.getAttribute("attr1"));
+ httpSession.removeAttribute("attr1");
+ assertFalse(httpSession.getAttributeNames().hasMoreElements());
+ }
+
+ @Test
+ public void testValues() {
+ httpSession.putValue("attr1", "value1");
+ assertEquals(1, httpSession.getValueNames().length);
+ assertEquals("value1", httpSession.getValue("attr1"));
+ httpSession.removeValue("attr1");
+ assertEquals(0, httpSession.getValueNames().length);
+ }
+
+ @Test
+ public void testInvalidate() {
+ httpSession.invalidate();
+ assertTrue(httpSession.isInvalidated());
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testInvalidateStateCheck() {
+ httpSession.invalidate();
+ httpSession.getAttribute("attr1");
+ }
+
+ @Test
+ public void testIsNew() {
+ assertTrue(httpSession.isNew());
+ httpSession.setNew(false);
+ assertFalse(httpSession.isNew());
+ }
+
+ @Test
+ public void testGetLastAccessedTime() {
+ assertNotNull(httpSession.getLastAccessedTime());
+ }
+
+ @Test
+ public void testGetMaxInactiveInterval() {
+ assertTrue(httpSession.getMaxInactiveInterval() > 0);
+ httpSession.setMaxInactiveInterval(123);
+ assertEquals(123, httpSession.getMaxInactiveInterval());
+ }
+
+}
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockHttpSessionTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockHttpSessionTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Wed Jan 13 23:07:09 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockHttpSessionTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockRequestPathInfoTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockRequestPathInfoTest.java?rev=1724523&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockRequestPathInfoTest.java (added)
+++ sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockRequestPathInfoTest.java Wed Jan 13 23:07:09 2016
@@ -0,0 +1,69 @@
+/*
+ * 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.sling.servlethelpers;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.apache.sling.servlethelpers.MockRequestPathInfo;
+import org.junit.Before;
+import org.junit.Test;
+
+public class MockRequestPathInfoTest {
+
+ private MockRequestPathInfo requestPathInfo;
+
+ @Before
+ public void setUp() throws Exception {
+ this.requestPathInfo = new MockRequestPathInfo();
+ }
+
+ @Test
+ public void testExtension() {
+ assertNull(this.requestPathInfo.getExtension());
+ this.requestPathInfo.setExtension("ext");
+ assertEquals("ext", this.requestPathInfo.getExtension());
+ }
+
+ @Test
+ public void testResourcePath() {
+ assertNull(this.requestPathInfo.getResourcePath());
+ this.requestPathInfo.setResourcePath("/path");
+ assertEquals("/path", this.requestPathInfo.getResourcePath());
+ }
+
+ @Test
+ public void testSelector() {
+ assertNull(this.requestPathInfo.getSelectorString());
+ assertEquals(0, this.requestPathInfo.getSelectors().length);
+ this.requestPathInfo.setSelectorString("aa.bb");
+ assertEquals("aa.bb", this.requestPathInfo.getSelectorString());
+ assertEquals(2, this.requestPathInfo.getSelectors().length);
+ assertArrayEquals(new String[] { "aa", "bb" }, this.requestPathInfo.getSelectors());
+ }
+
+ @Test
+ public void testSuffix() {
+ assertNull(this.requestPathInfo.getSuffix());
+ this.requestPathInfo.setSuffix("/suffix");
+ assertEquals("/suffix", this.requestPathInfo.getSuffix());
+ }
+
+}
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockRequestPathInfoTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockRequestPathInfoTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Wed Jan 13 23:07:09 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockRequestPathInfoTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockServletContextTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockServletContextTest.java?rev=1724523&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockServletContextTest.java (added)
+++ sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockServletContextTest.java Wed Jan 13 23:07:09 2016
@@ -0,0 +1,43 @@
+/*
+ * 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.sling.servlethelpers;
+
+import static org.junit.Assert.assertEquals;
+
+import javax.servlet.ServletContext;
+
+import org.apache.sling.servlethelpers.MockServletContext;
+import org.junit.Before;
+import org.junit.Test;
+
+public class MockServletContextTest {
+
+ private ServletContext servletContext;
+
+ @Before
+ public void setUp() throws Exception {
+ this.servletContext = new MockServletContext();
+ }
+
+ @Test
+ public void testGetMimeType() {
+ assertEquals("application/octet-stream", this.servletContext.getMimeType("any"));
+ }
+
+}
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockServletContextTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockServletContextTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Wed Jan 13 23:07:09 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockServletContextTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java?rev=1724523&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java (added)
+++ sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java Wed Jan 13 23:07:09 2016
@@ -0,0 +1,328 @@
+/*
+ * 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.sling.servlethelpers;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.Calendar;
+import java.util.Enumeration;
+import java.util.LinkedHashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.ResourceBundle;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpSession;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.CharEncoding;
+import org.apache.sling.api.request.RequestDispatcherOptions;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.servlets.HttpConstants;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class MockSlingHttpServletRequestTest {
+
+ @Mock
+ private ResourceResolver resourceResolver;
+ @Mock
+ private Resource resource;
+
+ private MockSlingHttpServletRequest request;
+
+ @Before
+ public void setUp() throws Exception {
+ request = new MockSlingHttpServletRequest(resourceResolver);
+ }
+
+ @Test
+ public void testResourceResolver() {
+ assertSame(resourceResolver, request.getResourceResolver());
+ }
+
+ @Test
+ public void testDefaultResourceResolver() {
+ assertNotNull(request.getResourceResolver());
+ }
+
+ @Test
+ public void testSession() {
+ HttpSession session = request.getSession(false);
+ assertNull(session);
+ session = request.getSession();
+ assertNotNull(session);
+ }
+
+ @Test
+ public void testRequestPathInfo() {
+ assertNotNull(request.getRequestPathInfo());
+ }
+
+ @Test
+ public void testAttributes() {
+ request.setAttribute("attr1", "value1");
+ assertTrue(request.getAttributeNames().hasMoreElements());
+ assertEquals("value1", request.getAttribute("attr1"));
+ request.removeAttribute("attr1");
+ assertFalse(request.getAttributeNames().hasMoreElements());
+ }
+
+ @Test
+ public void testResource() {
+ assertNull(request.getResource());
+ request.setResource(resource);
+ assertSame(resource, request.getResource());
+ }
+
+ @Test
+ public void testContextPath() {
+ assertNull(request.getContextPath());
+ request.setContextPath("/ctx");
+ assertEquals("/ctx", request.getContextPath());
+ }
+
+ @Test
+ public void testLocale() {
+ assertEquals(Locale.US, request.getLocale());
+ }
+
+ @Test
+ public void testQueryString() throws UnsupportedEncodingException {
+ assertNull(request.getQueryString());
+ assertEquals(0, request.getParameterMap().size());
+ assertFalse(request.getParameterNames().hasMoreElements());
+
+ request.setQueryString("param1=123¶m2=" + URLEncoder.encode("äöüÃâ¬!:!", CharEncoding.UTF_8)
+ + "¶m3=a¶m3=b");
+
+ assertNotNull(request.getQueryString());
+ assertEquals(3, request.getParameterMap().size());
+ assertTrue(request.getParameterNames().hasMoreElements());
+ assertEquals("123", request.getParameter("param1"));
+ assertEquals("äöüÃâ¬!:!", request.getParameter("param2"));
+ assertArrayEquals(new String[] { "a", "b" }, request.getParameterValues("param3"));
+
+ Map<String, Object> paramMap = new LinkedHashMap<String, Object>();
+ paramMap.put("p1", "a");
+ paramMap.put("p2", new String[] { "b", "c" });
+ paramMap.put("p3", null);
+ paramMap.put("p4", new String[] { null });
+ paramMap.put("p5", 22);
+ request.setParameterMap(paramMap);
+
+ assertEquals("p1=a&p2=b&p2=c&p4=&p5=22", request.getQueryString());
+ }
+
+ @Test
+ public void testSchemeSecure() {
+ assertEquals("http", request.getScheme());
+ assertFalse(request.isSecure());
+
+ request.setScheme("https");
+ assertEquals("https", request.getScheme());
+ assertTrue(request.isSecure());
+ }
+
+ @Test
+ public void testServerNamePort() {
+ assertEquals("localhost", request.getServerName());
+ assertEquals(80, request.getServerPort());
+
+ request.setServerName("myhost");
+ request.setServerPort(12345);
+ assertEquals("myhost", request.getServerName());
+ assertEquals(12345, request.getServerPort());
+ }
+
+ @Test
+ public void testMethod() {
+ assertEquals(HttpConstants.METHOD_GET, request.getMethod());
+
+ request.setMethod(HttpConstants.METHOD_POST);
+ assertEquals(HttpConstants.METHOD_POST, request.getMethod());
+ }
+
+ @Test
+ public void testHeaders() {
+ assertFalse(request.getHeaderNames().hasMoreElements());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(Calendar.MILLISECOND, 0);
+ long dateValue = calendar.getTimeInMillis();
+
+ request.addHeader("header1", "value1");
+ request.addIntHeader("header2", 5);
+ request.addDateHeader("header3", dateValue);
+
+ assertEquals("value1", request.getHeader("header1"));
+ assertEquals(5, request.getIntHeader("header2"));
+ assertEquals(dateValue, request.getDateHeader("header3"));
+
+ request.setHeader("header1", "value2");
+ request.addIntHeader("header2", 10);
+
+ Enumeration<String> header1Values = request.getHeaders("header1");
+ assertEquals("value2", header1Values.nextElement());
+ assertFalse(header1Values.hasMoreElements());
+
+ Enumeration<String> header2Values = request.getHeaders("header2");
+ assertEquals("5", header2Values.nextElement());
+ assertEquals("10", header2Values.nextElement());
+ assertFalse(header2Values.hasMoreElements());
+ }
+
+ @Test
+ public void testCookies() {
+ assertNull(request.getCookies());
+
+ request.addCookie(new Cookie("cookie1", "value1"));
+ request.addCookie(new Cookie("cookie2", "value2"));
+
+ assertEquals("value1", request.getCookie("cookie1").getValue());
+
+ Cookie[] cookies = request.getCookies();
+ assertEquals(2, cookies.length);
+ assertEquals("value1", cookies[0].getValue());
+ assertEquals("value2", cookies[1].getValue());
+ }
+
+ @Test
+ public void testDefaultResourceBundle() {
+ ResourceBundle bundle = request.getResourceBundle(Locale.US);
+ assertNotNull(bundle);
+ assertFalse(bundle.getKeys().hasMoreElements());
+ }
+
+ @Test
+ public void testRequestParameter() throws Exception {
+ request.setQueryString("param1=123¶m2=" + URLEncoder.encode("äöüÃâ¬!:!", CharEncoding.UTF_8)
+ + "¶m3=a¶m3=b");
+
+ assertEquals(3, request.getRequestParameterMap().size());
+ assertEquals(4, request.getRequestParameterList().size());
+ assertEquals("123", request.getRequestParameter("param1").getString());
+ assertEquals("äöüÃâ¬!:!", request.getRequestParameter("param2").getString());
+ assertEquals("a",request.getRequestParameters("param3")[0].getString());
+ assertEquals("b",request.getRequestParameters("param3")[1].getString());
+
+ assertNull(request.getRequestParameter("unknown"));
+ assertNull(request.getRequestParameters("unknown"));
+ }
+
+ @Test
+ public void testContentTypeCharset() throws Exception {
+ assertNull(request.getContentType());
+ assertNull(request.getCharacterEncoding());
+
+ request.setContentType("image/gif");
+ assertEquals("image/gif", request.getContentType());
+ assertNull(request.getCharacterEncoding());
+
+ request.setContentType("text/plain;charset=UTF-8");
+ assertEquals("text/plain;charset=UTF-8", request.getContentType());
+ assertEquals(CharEncoding.UTF_8, request.getCharacterEncoding());
+
+ request.setCharacterEncoding(CharEncoding.ISO_8859_1);
+ assertEquals("text/plain;charset=ISO-8859-1", request.getContentType());
+ assertEquals(CharEncoding.ISO_8859_1, request.getCharacterEncoding());
+ }
+
+ @Test
+ public void testContent() throws Exception {
+ assertEquals(0, request.getContentLength());
+ assertNull(request.getInputStream());
+
+ byte[] data = new byte[] { 0x01,0x02,0x03 };
+ request.setContent(data);
+
+ assertEquals(data.length, request.getContentLength());
+ assertArrayEquals(data, IOUtils.toByteArray(request.getInputStream()));
+ }
+
+ @Test
+ public void testGetRequestDispatcher() {
+ MockRequestDispatcherFactory requestDispatcherFactory = mock(MockRequestDispatcherFactory.class);
+ RequestDispatcher requestDispatcher = mock(RequestDispatcher.class);
+ when(requestDispatcherFactory.getRequestDispatcher(any(Resource.class), any(RequestDispatcherOptions.class))).thenReturn(requestDispatcher);
+ when(requestDispatcherFactory.getRequestDispatcher(any(String.class), any(RequestDispatcherOptions.class))).thenReturn(requestDispatcher);
+
+ request.setRequestDispatcherFactory(requestDispatcherFactory);
+
+ assertSame(requestDispatcher, request.getRequestDispatcher("/path"));
+ assertSame(requestDispatcher, request.getRequestDispatcher("/path", new RequestDispatcherOptions()));
+ assertSame(requestDispatcher, request.getRequestDispatcher(resource));
+ assertSame(requestDispatcher, request.getRequestDispatcher(resource, new RequestDispatcherOptions()));
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testGetRequestDispatcherWithoutFactory() {
+ request.getRequestDispatcher("/path");
+ }
+
+ @Test
+ public void testGetRemoteUser() {
+ assertNull(null, request.getRemoteUser());
+
+ request.setRemoteUser("admin");
+ assertEquals("admin", request.getRemoteUser());
+ }
+
+ @Test
+ public void testGetRemoteAddr() throws Exception {
+ assertNull(null, request.getRemoteAddr());
+
+ request.setRemoteAddr("1.2.3.4");
+ assertEquals("1.2.3.4", request.getRemoteAddr());
+ }
+
+ @Test
+ public void testGetRemoteHost() throws Exception {
+ assertNull(null, request.getRemoteHost());
+
+ request.setRemoteHost("host1");
+ assertEquals("host1", request.getRemoteHost());
+ }
+
+ @Test
+ public void testGetRemotePort() throws Exception {
+ assertEquals(0, request.getRemotePort());
+
+ request.setRemotePort(1234);
+ assertEquals(1234, request.getRemotePort());
+ }
+
+}
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Wed Jan 13 23:07:09 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletResponseTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletResponseTest.java?rev=1724523&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletResponseTest.java (added)
+++ sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletResponseTest.java Wed Jan 13 23:07:09 2016
@@ -0,0 +1,177 @@
+/*
+ * 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.sling.servlethelpers;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.lang3.CharEncoding;
+import org.apache.sling.servlethelpers.MockSlingHttpServletResponse;
+import org.junit.Before;
+import org.junit.Test;
+
+public class MockSlingHttpServletResponseTest {
+
+ private MockSlingHttpServletResponse response;
+
+ @Before
+ public void setUp() throws Exception {
+ this.response = new MockSlingHttpServletResponse();
+ }
+
+ @Test
+ public void testContentTypeCharset() throws Exception {
+ assertNull(response.getContentType());
+ assertNull(response.getCharacterEncoding());
+
+ response.setContentType("image/gif");
+ assertEquals("image/gif", response.getContentType());
+ assertNull(response.getCharacterEncoding());
+
+ response.setContentType("text/plain;charset=UTF-8");
+ assertEquals("text/plain;charset=UTF-8", response.getContentType());
+ assertEquals(CharEncoding.UTF_8, response.getCharacterEncoding());
+
+ response.setCharacterEncoding(CharEncoding.ISO_8859_1);
+ assertEquals("text/plain;charset=ISO-8859-1", response.getContentType());
+ assertEquals(CharEncoding.ISO_8859_1, response.getCharacterEncoding());
+ }
+
+ @Test
+ public void testContentLength() throws Exception {
+ assertEquals(0, response.getContentLength());
+
+ response.setContentLength(55);
+ assertEquals(55, response.getContentLength());
+ }
+
+ @Test
+ public void testHeaders() throws Exception {
+ assertEquals(0, response.getHeaderNames().size());
+
+ response.addHeader("header1", "value1");
+ response.addIntHeader("header2", 5);
+ response.addDateHeader("header3", System.currentTimeMillis());
+
+ assertEquals(3, response.getHeaderNames().size());
+ assertTrue(response.containsHeader("header1"));
+ assertEquals("value1", response.getHeader("header1"));
+ assertEquals("5", response.getHeader("header2"));
+ assertNotNull(response.getHeader("header3"));
+
+ response.setHeader("header1", "value2");
+ response.addIntHeader("header2", 10);
+
+ assertEquals(3, response.getHeaderNames().size());
+
+ Collection<String> header1Values = response.getHeaders("header1");
+ assertEquals(1, header1Values.size());
+ assertEquals("value2", header1Values.iterator().next());
+
+ Collection<String> header2Values = response.getHeaders("header2");
+ assertEquals(2, header2Values.size());
+ Iterator<String> header2Iterator = header2Values.iterator();
+ assertEquals("5", header2Iterator.next());
+ assertEquals("10", header2Iterator.next());
+
+ response.reset();
+ assertEquals(0, response.getHeaderNames().size());
+ }
+
+ @Test
+ public void testRedirect() throws Exception {
+ response.sendRedirect("/location.html");
+ assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus());
+ assertEquals("/location.html", response.getHeader("Location"));
+ }
+
+ @Test
+ public void testSendError() throws Exception {
+ response.sendError(HttpServletResponse.SC_NOT_FOUND);
+ assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus());
+ }
+
+ @Test
+ public void testSetStatus() throws Exception {
+ assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+
+ response.setStatus(HttpServletResponse.SC_BAD_GATEWAY);
+ assertEquals(HttpServletResponse.SC_BAD_GATEWAY, response.getStatus());
+
+ response.reset();
+ assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+ }
+
+ @Test
+ public void testWriteStringContent() throws Exception {
+ final String TEST_CONTENT = "Der Jodelkaiser äöüÃ⬠á áá»";
+ response.setCharacterEncoding(CharEncoding.UTF_8);
+ response.getWriter().write(TEST_CONTENT);
+ assertEquals(TEST_CONTENT, response.getOutputAsString());
+
+ response.resetBuffer();
+ assertEquals(0, response.getOutputAsString().length());
+ }
+
+ @Test
+ public void testWriteBinaryContent() throws Exception {
+ final byte[] TEST_DATA = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 };
+ response.getOutputStream().write(TEST_DATA);
+ assertArrayEquals(TEST_DATA, response.getOutput());
+
+ response.resetBuffer();
+ assertEquals(0, response.getOutput().length);
+ }
+
+ @Test
+ public void testIsCommitted() throws Exception {
+ assertFalse(response.isCommitted());
+ response.flushBuffer();
+ assertTrue(response.isCommitted());
+ }
+
+ @Test
+ public void testCookies() {
+ assertNull(response.getCookies());
+
+ response.addCookie(new Cookie("cookie1", "value1"));
+ response.addCookie(new Cookie("cookie2", "value2"));
+
+ assertEquals("value1", response.getCookie("cookie1").getValue());
+
+ Cookie[] cookies = response.getCookies();
+ assertEquals(2, cookies.length);
+ assertEquals("value1", cookies[0].getValue());
+ assertEquals("value2", cookies[1].getValue());
+
+ response.reset();
+ assertNull(response.getCookies());
+ }
+
+}
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletResponseTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletResponseTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Wed Jan 13 23:07:09 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/bundles/extensions/servlet-helpers/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletResponseTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: sling/trunk/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/pom.xml?rev=1724523&r1=1724522&r2=1724523&view=diff
==============================================================================
--- sling/trunk/pom.xml (original)
+++ sling/trunk/pom.xml Wed Jan 13 23:07:09 2016
@@ -188,6 +188,7 @@
<module>bundles/extensions/i18n</module>
<module>bundles/extensions/xss</module>
<module>bundles/extensions/resourcebuilder</module>
+ <module>bundles/extensions/servlet-helpers</module>
<!-- Testing Support -->
<module>testing</module>
Modified: sling/trunk/testing/mocks/sling-mock/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/pom.xml?rev=1724523&r1=1724522&r2=1724523&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/pom.xml (original)
+++ sling/trunk/testing/mocks/sling-mock/pom.xml Wed Jan 13 23:07:09 2016
@@ -60,6 +60,12 @@
<version>1.1.10</version>
<scope>compile</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.servlet-helpers</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
<dependency>
<groupId>org.apache.sling</groupId>
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockSling.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockSling.java?rev=1724523&r1=1724522&r2=1724523&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockSling.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockSling.java Wed Jan 13 23:07:09 2016
@@ -216,7 +216,7 @@ public final class MockSling {
* @return Sling script helper instance
*/
public static SlingScriptHelper newSlingScriptHelper(BundleContext bundleContext) {
- SlingHttpServletRequest request = new MockSlingHttpServletRequest(newResourceResolver(bundleContext));
+ SlingHttpServletRequest request = new MockSlingHttpServletRequest(newResourceResolver(bundleContext), bundleContext);
SlingHttpServletResponse response = new MockSlingHttpServletResponse();
return newSlingScriptHelper(request, response, bundleContext);
}
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockHttpSession.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockHttpSession.java?rev=1724523&r1=1724522&r2=1724523&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockHttpSession.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockHttpSession.java Wed Jan 13 23:07:09 2016
@@ -18,141 +18,13 @@
*/
package org.apache.sling.testing.mock.sling.servlet;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.UUID;
-
-import javax.servlet.ServletContext;
-import javax.servlet.http.HttpSession;
-
-import org.apache.commons.collections.IteratorUtils;
-
/**
* Mock {@link HttpSession} implementation.
*/
-public final class MockHttpSession implements HttpSession {
-
- private final ServletContext servletContext = new MockServletContext();
- private final Map<String, Object> attributeMap = new HashMap<String, Object>();
- private final String sessionID = UUID.randomUUID().toString();
- private final long creationTime = System.currentTimeMillis();
- private boolean invalidated = false;
- private boolean isNew = true;
- private int maxActiveInterval = 1800;
-
- @Override
- public ServletContext getServletContext() {
- return this.servletContext;
- }
-
- @Override
- public Object getAttribute(final String name) {
- checkInvalidatedState();
- return this.attributeMap.get(name);
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public Enumeration<String> getAttributeNames() {
- checkInvalidatedState();
- return IteratorUtils.asEnumeration(this.attributeMap.keySet().iterator());
- }
-
- @Override
- public String getId() {
- return this.sessionID;
- }
-
- @Override
- public long getCreationTime() {
- checkInvalidatedState();
- return this.creationTime;
- }
-
- @Override
- public Object getValue(final String name) {
- checkInvalidatedState();
- return getAttribute(name);
- }
-
- @Override
- public String[] getValueNames() {
- checkInvalidatedState();
- return this.attributeMap.keySet().toArray(new String[this.attributeMap.keySet().size()]);
- }
-
- @Override
- public void putValue(final String name, final Object value) {
- checkInvalidatedState();
- setAttribute(name, value);
- }
-
- @Override
- public void removeAttribute(final String name) {
- checkInvalidatedState();
- this.attributeMap.remove(name);
- }
-
- @Override
- public void removeValue(final String name) {
- checkInvalidatedState();
- this.attributeMap.remove(name);
- }
-
- @Override
- public void setAttribute(final String name, final Object value) {
- checkInvalidatedState();
- this.attributeMap.put(name, value);
- }
-
- @Override
- public void invalidate() {
- checkInvalidatedState();
- this.invalidated = true;
- }
-
- private void checkInvalidatedState() {
- if (invalidated) {
- throw new IllegalStateException("Session is already invalidated.");
- }
- }
+public final class MockHttpSession extends org.apache.sling.servlethelpers.MockHttpSession {
- public boolean isInvalidated() {
- return invalidated;
- }
-
- @Override
- public boolean isNew() {
- checkInvalidatedState();
- return isNew;
+ protected MockServletContext newMockServletContext() {
+ return new MockServletContext();
}
- public void setNew(boolean isNew) {
- this.isNew = isNew;
- }
-
- @Override
- public long getLastAccessedTime() {
- checkInvalidatedState();
- return creationTime;
- }
-
- @Override
- public int getMaxInactiveInterval() {
- return maxActiveInterval;
- }
-
- @Override
- public void setMaxInactiveInterval(final int interval) {
- this.maxActiveInterval = interval;
- }
-
- // --- unsupported operations ---
- @Override
- @SuppressWarnings("deprecation")
- public javax.servlet.http.HttpSessionContext getSessionContext() {
- throw new UnsupportedOperationException();
- }
-
-}
+}
\ No newline at end of file
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java?rev=1724523&r1=1724522&r2=1724523&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestDispatcherFactory.java Wed Jan 13 23:07:09 2016
@@ -18,31 +18,12 @@
*/
package org.apache.sling.testing.mock.sling.servlet;
-import javax.servlet.RequestDispatcher;
-
-import org.apache.sling.api.request.RequestDispatcherOptions;
-import org.apache.sling.api.resource.Resource;
-
/**
* Interface to create a mock {@link RequestDispatcher} when calling the getRequestDispatcher methods
* on {@link MockSlingHttpServletRequest} instances.
*/
-public interface MockRequestDispatcherFactory {
-
- /**
- * Get request dispatcher for given path.
- * @param path Path
- * @param options Options. Null if no options are provided.
- * @return Request dispatcher
- */
- RequestDispatcher getRequestDispatcher(String path, RequestDispatcherOptions options);
+public interface MockRequestDispatcherFactory extends org.apache.sling.servlethelpers.MockRequestDispatcherFactory {
- /**
- * Get request dispatcher for given resource.
- * @param resource Resource
- * @param options Options. Null if no options are provided.
- * @return Request dispatcher
- */
- RequestDispatcher getRequestDispatcher(Resource resource, RequestDispatcherOptions options);
+ // inherit from superclass
}
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestPathInfo.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestPathInfo.java?rev=1724523&r1=1724522&r2=1724523&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestPathInfo.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockRequestPathInfo.java Wed Jan 13 23:07:09 2016
@@ -18,69 +18,11 @@
*/
package org.apache.sling.testing.mock.sling.servlet;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.sling.api.request.RequestPathInfo;
-import org.apache.sling.api.resource.Resource;
-
/**
* Mock {@link RequestPathInfo} implementation.
*/
-public final class MockRequestPathInfo implements RequestPathInfo {
-
- private String extension;
- private String resourcePath;
- private String selectorString;
- private String suffix;
-
- @Override
- public String getExtension() {
- return this.extension;
- }
-
- @Override
- public String getResourcePath() {
- return this.resourcePath;
- }
-
- @Override
- public String[] getSelectors() {
- if (StringUtils.isEmpty(this.selectorString)) {
- return new String[0];
- } else {
- return StringUtils.split(this.selectorString, ".");
- }
- }
-
- @Override
- public String getSelectorString() {
- return this.selectorString;
- }
-
- @Override
- public String getSuffix() {
- return this.suffix;
- }
-
- public void setExtension(final String extension) {
- this.extension = extension;
- }
-
- public void setResourcePath(final String resourcePath) {
- this.resourcePath = resourcePath;
- }
-
- public void setSelectorString(final String selectorString) {
- this.selectorString = selectorString;
- }
-
- public void setSuffix(final String suffix) {
- this.suffix = suffix;
- }
+public final class MockRequestPathInfo extends org.apache.sling.servlethelpers.MockRequestPathInfo {
- // --- unsupported operations ---
- @Override
- public Resource getSuffixResource() {
- throw new UnsupportedOperationException();
- }
+ // inherit from superclass
}
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockServletContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockServletContext.java?rev=1724523&r1=1724522&r2=1724523&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockServletContext.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/servlet/MockServletContext.java Wed Jan 13 23:07:09 2016
@@ -18,284 +18,11 @@
*/
package org.apache.sling.testing.mock.sling.servlet;
-import java.io.InputStream;
-import java.net.URL;
-import java.util.Enumeration;
-import java.util.EventListener;
-import java.util.Map;
-import java.util.Set;
-
-import javax.servlet.Filter;
-import javax.servlet.FilterRegistration;
-import javax.servlet.RequestDispatcher;
-import javax.servlet.Servlet;
-import javax.servlet.ServletContext;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRegistration;
-import javax.servlet.ServletRegistration.Dynamic;
-import javax.servlet.SessionCookieConfig;
-import javax.servlet.SessionTrackingMode;
-import javax.servlet.descriptor.JspConfigDescriptor;
-
/**
* Mock {@link ServletContext} implementation.
*/
-public final class MockServletContext implements ServletContext {
-
- @Override
- public String getMimeType(final String file) {
- return "application/octet-stream";
- }
-
- // --- unsupported operations ---
- @Override
- public Object getAttribute(final String name) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Enumeration<String> getAttributeNames() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public ServletContext getContext(final String uriPath) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public String getContextPath() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public String getInitParameter(final String name) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Enumeration<String> getInitParameterNames() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public int getMajorVersion() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public int getMinorVersion() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public RequestDispatcher getNamedDispatcher(final String name) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public String getRealPath(final String pPath) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public RequestDispatcher getRequestDispatcher(final String path) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public URL getResource(final String pPath) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public InputStream getResourceAsStream(final String path) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Set<String> getResourcePaths(final String path) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public String getServerInfo() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Servlet getServlet(final String name) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public String getServletContextName() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Enumeration<String> getServletNames() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Enumeration<Servlet> getServlets() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void log(final String msg) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void log(final Exception exception, final String msg) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void log(final String msg, final Throwable throwable) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void removeAttribute(final String name) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void setAttribute(final String name, final Object object) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public int getEffectiveMajorVersion() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public int getEffectiveMinorVersion() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean setInitParameter(final String name, final String value) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Dynamic addServlet(final String servletName, final String className) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Dynamic addServlet(final String servletName, final Servlet servlet) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Dynamic addServlet(final String servletName, final Class<? extends Servlet> servletClass) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public <T extends Servlet> T createServlet(final Class<T> clazz) throws ServletException {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public ServletRegistration getServletRegistration(final String servletName) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Map<String, ? extends ServletRegistration> getServletRegistrations() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public FilterRegistration.Dynamic addFilter(final String filterName, final String className) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public FilterRegistration.Dynamic addFilter(final String filterName, final Filter filter) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public FilterRegistration.Dynamic addFilter(final String filterName, final Class<? extends Filter> filterClass) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public <T extends Filter> T createFilter(final Class<T> clazz) throws ServletException {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public FilterRegistration getFilterRegistration(final String filterName) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public SessionCookieConfig getSessionCookieConfig() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void setSessionTrackingModes(final Set<SessionTrackingMode> sessionTrackingModes) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Set<SessionTrackingMode> getDefaultSessionTrackingModes() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void addListener(final String pClassName) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public <T extends EventListener> void addListener(final T listener) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void addListener(final Class<? extends EventListener> listenerClass) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public <T extends EventListener> T createListener(final Class<T> clazz) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public JspConfigDescriptor getJspConfigDescriptor() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public ClassLoader getClassLoader() {
- throw new UnsupportedOperationException();
- }
+public final class MockServletContext extends org.apache.sling.servlethelpers.MockServletContext {
- @Override
- public void declareRoles(final String... roleNames) {
- throw new UnsupportedOperationException();
- }
+ // inherit from superclass
}