Skip to content

Commit

Permalink
Increased test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikealdo committed Dec 3, 2015
1 parent 1bade7f commit d86971e
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,12 @@
public class SessionFieldResolver {

private static final Object NULL = new NULLClass();
private Long dashletId;
private ActionContext actionContext;

public SessionFieldResolver(ActionContext actionContext) {
this.actionContext = actionContext;
}

public SessionFieldResolver(Long dashletId, ActionContext actionContext) {
this.dashletId = dashletId;
this.actionContext = actionContext;
}

public List<Field> findAllSessionFieldsFor(Class<? extends Component> componentClass) {
List<Field> result = new ArrayList<>();
Class clazz = componentClass;
Expand Down Expand Up @@ -87,23 +81,9 @@ public String createSessionId(String componentId, Field field) {
}
String componentPrefix = "component";
componentId = "_" + componentId;
if (dashletId != null) {
componentPrefix = resolveDashletPrefixForSessionParameter();
if (!componentId.equals("_simplySearchComponent")) {
componentId = "";
}
}
return componentPrefix + componentId + fieldName;
}

private String resolveDashletPrefixForSessionParameter() {
return "dashlets_" + dashletId + "";
}

private String resolveDashletPrefixForOgnlExpression() {
return "dashlets[" + dashletId + "]";
}

private String createSessionId(String componentId) {
return createSessionId(componentId, null);
}
Expand All @@ -117,19 +97,7 @@ public void putSessionFieldValuesFromComponentIntoSession(Component component) {
}

private String createOgnlExpression(Component component, Field field) {
String ognlExpr;
String dashletPrefix = "";
if (dashletId != null) {
dashletPrefix = resolveDashletPrefixForOgnlExpression();
}
if (dashletPrefix.isEmpty()) {
ognlExpr = component.getId() + "." + field.getName();
} else if (component.getId().equals("simplySearchComponent")) {
ognlExpr = dashletPrefix + ".simplySearchComponent." + field.getName();
} else {
ognlExpr = dashletPrefix + "." + field.getName();
}
return ognlExpr;
return component.getId() + "." + field.getName();
}

private void putValueIntoSession(String sessionId, String expr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,71 +23,75 @@

public class StrutsWithComponentsPrepareFilter extends StrutsPrepareFilter {

public static final String CONFIG_PARAM_NAME = "config";
public static final String DEFAULT_CONFIG_FILES = "struts-overridden-default.xml,struts-plugin.xml,struts.xml";

@Override
public void init(FilterConfig filterConfig) throws ServletException {
//copy actual filter configuration and put default configuration files parameter if it's not already placed here
ConfigurableFilterConfig newerFilterConfig = new ConfigurableFilterConfig(filterConfig);
if ( !newerFilterConfig.containsInitParameter(CONFIG_PARAM_NAME) ) {
newerFilterConfig.addInitParameter(CONFIG_PARAM_NAME, DEFAULT_CONFIG_FILES);
}

super.init(newerFilterConfig);
}

@Override
protected void postInit(Dispatcher dispatcher, FilterConfig filterConfig) {
//do the only useful thing which StrutsListener do
filterConfig.getServletContext().setAttribute(StrutsStatics.SERVLET_DISPATCHER, dispatcher);
}

/**
* Object which has customizable init parameters. Everything else delegates to real {@link FilterConfig} instance.
*/
private static class ConfigurableFilterConfig implements FilterConfig {

private final FilterConfig filterConfig;
private final Map<String, String> initParameters = new LinkedHashMap<String, String>();

public ConfigurableFilterConfig(FilterConfig filterConfig) {
super();
this.filterConfig = filterConfig;
for (Enumeration<?> iterator = filterConfig.getInitParameterNames(); iterator.hasMoreElements(); ) {
String paramName = (String)iterator.nextElement();
String paramValue = filterConfig.getInitParameter(paramName);
initParameters.put(paramName, paramValue);
}
}

@Override
public String getFilterName() {
return filterConfig.getFilterName();
}

@Override
public ServletContext getServletContext() {
return filterConfig.getServletContext();
}

public static final String CONFIG_PARAM_NAME = "config";
public static final String DEFAULT_CONFIG_FILES = "struts-overridden-default.xml,struts-plugin.xml,struts.xml";

/***
* copy actual filter configuration and put default configuration files parameter if it's not already placed here
* @param filterConfig
* @throws ServletException
*/
@Override
public String getInitParameter(String name) {
return initParameters.get(name);
public void init(FilterConfig filterConfig) throws ServletException {
//
ConfigurableFilterConfig newerFilterConfig = new ConfigurableFilterConfig(filterConfig);
if (!newerFilterConfig.containsInitParameter(CONFIG_PARAM_NAME)) {
newerFilterConfig.addInitParameter(CONFIG_PARAM_NAME, DEFAULT_CONFIG_FILES);
}

super.init(newerFilterConfig);
}

@Override
public Enumeration<String> getInitParameterNames() {
return new IteratorEnumeration(initParameters.keySet().iterator());
}

public void addInitParameter(String name, String value) {
initParameters.put(name, value);
protected void postInit(Dispatcher dispatcher, FilterConfig filterConfig) {
filterConfig.getServletContext().setAttribute(StrutsStatics.SERVLET_DISPATCHER, dispatcher);
}

public boolean containsInitParameter(String name) {
return initParameters.containsKey(name);

/**
* Object which has customizable init parameters. Everything else delegates to real {@link FilterConfig} instance.
*/
private static class ConfigurableFilterConfig implements FilterConfig {

private final FilterConfig filterConfig;
private final Map<String, String> initParameters = new LinkedHashMap<String, String>();

public ConfigurableFilterConfig(FilterConfig filterConfig) {
super();
this.filterConfig = filterConfig;
for (Enumeration<?> iterator = filterConfig.getInitParameterNames(); iterator.hasMoreElements(); ) {
String paramName = (String) iterator.nextElement();
String paramValue = filterConfig.getInitParameter(paramName);
initParameters.put(paramName, paramValue);
}
}

@Override
public String getFilterName() {
return filterConfig.getFilterName();
}

@Override
public ServletContext getServletContext() {
return filterConfig.getServletContext();
}

@Override
public String getInitParameter(String name) {
return initParameters.get(name);
}

@Override
public Enumeration<String> getInitParameterNames() {
return new IteratorEnumeration(initParameters.keySet().iterator());
}

public void addInitParameter(String name, String value) {
initParameters.put(name, value);
}

public boolean containsInitParameter(String name) {
return initParameters.containsKey(name);
}
}
}


}
62 changes: 38 additions & 24 deletions src/test/java/cz/mikealdo/struts2components/IndexActionTest.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,50 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* 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 cz.mikealdo.struts2components;

import cz.mikealdo.struts2components.actions.IndexAction;
import cz.mikealdo.struts2components.components.FirstSimpleComponent;
import cz.mikealdo.struts2components.components.SecondSimpleComponent;
import cz.mikealdo.struts2components.components.SimpleComponent;
import cz.mikealdo.struts2components.struts2.components.Component;
import junit.framework.TestCase;

import com.opensymphony.xwork2.Action;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
import org.mockito.Matchers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class IndexActionTest {

private final FirstSimpleComponent firstComponent = new FirstSimpleComponent();
private final SecondSimpleComponent secondComponent = new SecondSimpleComponent();
private IndexAction action = new IndexAction(firstComponent, secondComponent);;

@Test
public void shouldReturnRightResultName() throws Exception {
assertEquals(Action.INPUT, action.execute());
}

@Test
public void shouldReturnFirstComponent() throws Exception {
Component component = action.getChild("firstComponent");

assertEquals(component, firstComponent);
}


@Test
public void shouldReturnSecondComponent() throws Exception {
Component component = action.getChild("secondComponent");

assertEquals(component, secondComponent);
}

@Test
public void shouldReturnTwoComponents() throws Exception {
assertEquals(action.getChildren().size(), 2);

/**
*
*/
public class IndexActionTest extends TestCase {

public void testIndexAction() throws Exception {
IndexAction action = new IndexAction(new FirstSimpleComponent(), new SecondSimpleComponent());
String result = action.execute();
assertEquals(Action.INPUT, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cz.mikealdo.struts2components.components;

import org.junit.Test;

import static org.junit.Assert.*;

public class FirstSimpleComponentTest {

@Test
public void shouldReturnRightId() throws Exception {
String id = new FirstSimpleComponent(0).getId();

assertEquals("firstComponent", id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cz.mikealdo.struts2components.components;

import org.junit.Test;

import static org.junit.Assert.*;

public class SecondSimpleComponentTest {

@Test
public void shouldReturnRightId() throws Exception {
String id = new SecondSimpleComponent(0).getId();

assertEquals("secondComponent", id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cz.mikealdo.struts2components.components;

import org.junit.Test;

import static org.junit.Assert.*;

public class SimpleComponentTest {

private SimpleComponent simpleComponent = new SimpleComponent() {
@Override
public String getId() {
return "id";
}
};

@Test
public void shouldReturnPlusOneRightResult() throws Exception {
String result = simpleComponent.plusOne();

assertEquals("success", result);
assertEquals(simpleComponent.getIntegerInsideComponent(), Integer.valueOf(1));
}
}

0 comments on commit d86971e

Please sign in to comment.