Skip to content

Commit

Permalink
New MockContainer, manage session and context so they can be more eas…
Browse files Browse the repository at this point in the history
…ily passed into the prime-mvc request simulator.
  • Loading branch information
robotdan committed Oct 19, 2017
1 parent cef0a24 commit 8b2afb8
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 133 deletions.
2 changes: 1 addition & 1 deletion build.savant
Expand Up @@ -15,7 +15,7 @@
*/
savantVersion = "1.0.0"

project(group: "org.primeframework", name: "prime-mock", version: "0.5.9", licenses: ["ApacheV2_0"]) {
project(group: "org.primeframework", name: "prime-mock", version: "0.6.0", licenses: ["ApacheV2_0"]) {
workflow {
standard()
}
Expand Down
108 changes: 108 additions & 0 deletions src/main/java/org/primeframework/mock/servlet/MockContainer.java
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2001-2017, Inversoft, All Rights Reserved
*
* Licensed 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.primeframework.mock.servlet;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
* Mock container to manage the session and context references.
*
* @author Daniel DeGroff
*/
public class MockContainer {
private MockServletContext context;

private MockHttpSession session;

public MockContainer() {
this.context = new MockServletContext();
}

public MockServletContext getContext() {
return context;
}

/**
* @see HttpServletRequest#getSession(boolean)
*/
public MockHttpSession getSession(boolean create) {
if (session == null && create) {
session = new MockHttpSession(this);
}

return session;
}

/**
* @see HttpServletRequest#getSession()
*/
public MockHttpSession getSession() {
return getSession(true);
}

public MockServletContext newServletContext(File webDir) {
context = new MockServletContext(webDir);
return context;
}

public MockHttpServletRequest newServletRequest(Map<String, List<String>> parameters, String uri, String encoding,
Locale locale, boolean post) {
return new MockHttpServletRequest(parameters, uri, encoding, locale, post, this);
}

public MockHttpServletRequest newServletRequest() {
return new MockHttpServletRequest(this);
}

public MockHttpServletRequest newServletRequest(String uri) {
return new MockHttpServletRequest(uri, this);
}

public MockHttpServletRequest newServletRequest(String uri, Locale locale, boolean post, String encoding) {
return new MockHttpServletRequest(uri, locale, post, encoding, this);
}

public MockHttpServletResponse newServletResponse() {
return new MockHttpServletResponse();
}

/**
* Reset the servlet context to the initial state. This means the servlet context will be rebuilt using the same web
* directory used when it was first initialized using {@link #newServletContext(File)}.
*/
public void resetContext() {
context = context.webDir == null ? new MockServletContext() : new MockServletContext(context.webDir);
}

/**
* Clear the <code>ServletContext</code> attributes.
*/
public void resetContextAttributes() {
context.attributes.clear();
}

/**
* Reset the session the default state. This means the session will be null until requested by calling {@link
* HttpServletRequest#getSession()} or {@link HttpServletRequest#getSession(boolean)}.
*/
public void resetSession() {
session = null;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001-2007, Inversoft Inc., All Rights Reserved
* Copyright (c) 2001-2017, Inversoft Inc., All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,7 +34,6 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.Principal;
import java.util.ArrayList;
Expand All @@ -59,6 +58,8 @@
public class MockHttpServletRequest implements HttpServletRequest {
protected final Map<String, Object> attributes = new HashMap<>();

protected final MockContainer container;

protected final MockServletContext context;

protected final Map<String, FileInfo> files = new LinkedHashMap<>();
Expand Down Expand Up @@ -115,76 +116,47 @@ public class MockHttpServletRequest implements HttpServletRequest {

protected String uri;

public MockHttpServletRequest(String uri, MockServletContext context) {
this.uri = uri;
this.context = context;
protected MockHttpServletRequest(MockContainer container) {
this.container = container;
this.context = container.getContext();
}

public MockHttpServletRequest(String uri, MockHttpSession session) {
protected MockHttpServletRequest(String uri, MockContainer container) {
this.uri = uri;
this.session = session;
this.context = session.context;
this.container = container;
this.context = container.getContext();
}

public MockHttpServletRequest(String uri, Locale locale, boolean post, String encoding,
MockServletContext context) {
protected MockHttpServletRequest(String uri, Locale locale, boolean post, String encoding,
MockContainer container) {
this.uri = uri;
this.locales.add(locale);
this.method = post ? Method.POST : Method.GET;
this.encoding = encoding;
this.context = context;
this.session = new MockHttpSession(context);
this.container = container;
this.context = container.getContext();

if (post) {
contentType = "application/x-www-form-urlencoded";
}
}

public MockHttpServletRequest(String uri, Locale locale, boolean post, String encoding,
MockHttpSession session) {
this.uri = uri;
this.locales.add(locale);
this.method = post ? Method.POST : Method.GET;
this.encoding = encoding;
this.session = session;
this.context = session.context;

if (post) {
contentType = "application/x-www-form-urlencoded";
}
}

public MockHttpServletRequest(Map<String, List<String>> parameters, String uri, String encoding,
Locale locale, boolean post, MockHttpSession session) {
protected MockHttpServletRequest(Map<String, List<String>> parameters, String uri, String encoding,
Locale locale, boolean post, MockContainer container) {
this.parameters.putAll(parameters);
this.uri = uri;
this.encoding = encoding;
this.locales.add(locale);
this.method = post ? Method.POST : Method.GET;
this.session = session;
this.context = session.context;
this.container = container;
this.context = container.getContext();

if (post) {
contentType = "application/x-www-form-urlencoded";
}
}

public MockHttpServletRequest(Map<String, List<String>> parameters, String uri, String encoding,
Locale locale, boolean post, MockServletContext context) {
this.parameters.putAll(parameters);
this.uri = uri;
this.encoding = encoding;
this.locales.add(locale);
this.method = post ? Method.POST : Method.GET;
this.context = context;
this.session = new MockHttpSession(context);

if (post) {
contentType = "application/x-www-form-urlencoded";
}
}


//-------------------------------------------------------------------------
// javax.servlet.ServletRequest methods
//-------------------------------------------------------------------------
Expand Down Expand Up @@ -940,10 +912,7 @@ public void setServletPath(String servletPath) {
* @return The session.
*/
public HttpSession getSession() {
if (session == null) {
session = new MockHttpSession(context);
}
return session;
return container.getSession();
}

/**
Expand All @@ -959,11 +928,7 @@ public void setSession(MockHttpSession session) {
* @return The session.
*/
public HttpSession getSession(boolean create) {
if (session == null && create) {
session = new MockHttpSession(context);
}

return session;
return container.getSession(create);
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001-2015, Inversoft Inc., All Rights Reserved
* Copyright (c) 2001-2017, Inversoft Inc., All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,7 +40,7 @@ public class MockHttpServletResponse implements HttpServletResponse {

protected String contentType;

protected List<Cookie> cookies = new ArrayList<Cookie>();
protected List<Cookie> cookies = new ArrayList<>();

protected String encoding;

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001-2007, Inversoft Inc., All Rights Reserved
* Copyright (c) 2001-2017, Inversoft Inc., All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,10 +31,11 @@
*/
public class MockHttpSession implements HttpSession {
protected final Map<String, Object> attributes = new HashMap<>();
protected final MockServletContext context;

public MockHttpSession(MockServletContext context) {
this.context = context;
private MockContainer container;

protected MockHttpSession(MockContainer container) {
this.container = container;
}

public long getCreationTime() {
Expand All @@ -50,7 +51,7 @@ public long getLastAccessedTime() {
}

public ServletContext getServletContext() {
return context;
return container.getContext();
}

public void setMaxInactiveInterval(int i) {
Expand Down

0 comments on commit 8b2afb8

Please sign in to comment.