Skip to content

Commit

Permalink
Enhance test coverage and fix bug when view contains already suffix
Browse files Browse the repository at this point in the history
fixes: #369
  • Loading branch information
erdlet committed Jul 25, 2023
1 parent 32ae46c commit 6fddf9f
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 53 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/eclipse/krazo/KrazoConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2014-2015 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019 Eclipse Krazo committers and contributors
* Copyright (c) 2018, 2023 Eclipse Krazo committers and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
54 changes: 22 additions & 32 deletions core/src/main/java/org/eclipse/krazo/core/ViewResponseFilter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2014-2015 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019 Eclipse Krazo committers and contributors
* Copyright (c) 2018, 2023 Eclipse Krazo committers and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,29 +18,6 @@
*/
package org.eclipse.krazo.core;

import static jakarta.ws.rs.core.Response.Status.FOUND;
import static jakarta.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
import static jakarta.ws.rs.core.Response.Status.MOVED_PERMANENTLY;
import static jakarta.ws.rs.core.Response.Status.SEE_OTHER;
import static jakarta.ws.rs.core.Response.Status.TEMPORARY_REDIRECT;
import static org.eclipse.krazo.cdi.KrazoCdiExtension.isEventObserved;
import static org.eclipse.krazo.util.AnnotationUtils.getAnnotation;
import static org.eclipse.krazo.util.PathUtils.noPrefix;
import static org.eclipse.krazo.util.PathUtils.noStartingSlash;

import java.lang.reflect.Method;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.eclipse.krazo.KrazoConfig;
import org.eclipse.krazo.engine.Viewable;
import org.eclipse.krazo.event.ControllerRedirectEventImpl;
import org.eclipse.krazo.lifecycle.RequestLifecycle;

import jakarta.annotation.Priority;
import jakarta.enterprise.event.Event;
import jakarta.inject.Inject;
Expand All @@ -56,13 +33,25 @@
import jakarta.ws.rs.container.ContainerResponseContext;
import jakarta.ws.rs.container.ContainerResponseFilter;
import jakarta.ws.rs.container.ResourceInfo;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Request;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import jakarta.ws.rs.core.Variant;
import jakarta.ws.rs.core.*;
import org.eclipse.krazo.KrazoConfig;
import org.eclipse.krazo.engine.Viewable;
import org.eclipse.krazo.event.ControllerRedirectEventImpl;
import org.eclipse.krazo.lifecycle.RequestLifecycle;

import java.lang.reflect.Method;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static jakarta.ws.rs.core.Response.Status.*;
import static org.eclipse.krazo.cdi.KrazoCdiExtension.isEventObserved;
import static org.eclipse.krazo.util.AnnotationUtils.getAnnotation;
import static org.eclipse.krazo.util.PathUtils.noPrefix;
import static org.eclipse.krazo.util.PathUtils.noStartingSlash;

/**
* <p>A JAX-RS response filter that fires a {@link jakarta.mvc.event.AfterControllerEvent}
Expand Down Expand Up @@ -206,7 +195,8 @@ private String appendExtensionIfRequired(String viewName) {
*/
static String appendExtensionIfRequired(String viewName, String defaultExtension) {
if (viewName == null || viewName.startsWith(REDIRECT)
|| defaultExtension == null || defaultExtension.length() == 0) {
|| defaultExtension == null || defaultExtension.length() == 0
|| viewName.contains(".")) {
return viewName;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright (c) 2023 Eclipse Krazo committers and contributors
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.eclipse.krazo.test.defaultview;

import jakarta.enterprise.context.RequestScoped;
Expand All @@ -14,4 +31,10 @@ public class DefaultViewController {
public String index() {
return "index";
}

@GET
@Path("/with-extension")
public String indexWithExtension() {
return "index.jsp";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2023 Eclipse Krazo committers and contributors
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.eclipse.krazo.test.defaultview;

import jakarta.mvc.engine.ViewEngine;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

import java.util.Map;

@ApplicationPath("mvc")
public class DefaultViewWithDotApplication extends Application {

@Override
public Map<String, Object> getProperties() {
return Map.of(ViewEngine.VIEW_EXTENSION, ".jsp");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2023 Eclipse Krazo committers and contributors
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.eclipse.krazo.test.defaultview;

import jakarta.mvc.engine.ViewEngine;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

import java.util.Map;

@ApplicationPath("mvc")
public class DefaultViewWithoutDotApplication extends Application {

@Override
public Map<String, Object> getProperties() {
return Map.of(ViewEngine.VIEW_EXTENSION, "jsp");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
* Copyright (c) 2014-2015 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2023 Eclipse Krazo committers and contributors
* Copyright (c) 2023 Eclipse Krazo committers and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +19,8 @@

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.eclipse.krazo.test.defaultview.DefaultViewController;
import org.eclipse.krazo.test.defaultview.DefaultViewWithDotApplication;
import org.eclipse.krazo.test.util.WebArchiveBuilder;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
Expand All @@ -40,7 +41,7 @@
* @author Tobias Erdle
*/
@RunWith(Arquillian.class)
public class DefaultViewIT {
public class DefaultViewWithDotIT {
private static final String WEB_INF_SRC = "src/main/resources/defaultview/";
@ArquillianResource
private URL baseURL;
Expand All @@ -58,7 +59,7 @@ public void setUp() {
@Deployment(testable = false, name = "default-view")
public static WebArchive createDeployment() {
return new WebArchiveBuilder()
.addPackage("org.eclipse.krazo.test.defaultview")
.addClasses(DefaultViewWithDotApplication.class, DefaultViewController.class)
.addView(Paths.get(WEB_INF_SRC).resolve("views/index.jsp").toFile(), "index.jsp")
.addBeansXml()
.build();
Expand All @@ -72,4 +73,13 @@ public void test() throws Exception {

assertEquals("Default-View Extension Test", page.getElementById("headline").getTextContent());
}

@Test
public void testWithAppliedExtension() throws Exception {
final HtmlPage page = webClient.getPage(baseURL + "mvc/default-view/with-extension");

System.out.println(page.asXml());

assertEquals("Default-View Extension Test", page.getElementById("headline").getTextContent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2023 Eclipse Krazo committers and contributors
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.eclipse.krazo.test;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.eclipse.krazo.test.defaultview.DefaultViewController;
import org.eclipse.krazo.test.defaultview.DefaultViewWithoutDotApplication;
import org.eclipse.krazo.test.util.WebArchiveBuilder;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.net.URL;
import java.nio.file.Paths;

import static org.junit.Assert.assertEquals;

/**
* Tests default view extension.
*
* @author Tobias Erdle
*/
@RunWith(Arquillian.class)
public class DefaultViewWithoutDotIT {
private static final String WEB_INF_SRC = "src/main/resources/defaultview/";
@ArquillianResource
private URL baseURL;
private WebClient webClient;

@Before
public void setUp() {
webClient = new WebClient();
webClient.getOptions()
.setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions()
.setRedirectEnabled(true);
}

@Deployment(testable = false, name = "default-view")
public static WebArchive createDeployment() {
return new WebArchiveBuilder()
.addClasses(DefaultViewWithoutDotApplication.class, DefaultViewController.class)
.addView(Paths.get(WEB_INF_SRC).resolve("views/index.jsp").toFile(), "index.jsp")
.addBeansXml()
.build();
}

@Test
public void test() throws Exception {
final HtmlPage page = webClient.getPage(baseURL + "mvc/default-view");

System.out.println(page.asXml());

assertEquals("Default-View Extension Test", page.getElementById("headline").getTextContent());
}

@Test
public void testWithAppliedExtension() throws Exception {
final HtmlPage page = webClient.getPage(baseURL + "mvc/default-view/with-extension");

System.out.println(page.asXml());

assertEquals("Default-View Extension Test", page.getElementById("headline").getTextContent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class WebArchiveBuilder {
private final WebArchive archive = ShrinkWrap.create(WebArchive.class);
private List<MavenDependency> additionalDependencies = new ArrayList<>();

public WebArchiveBuilder addClasses(Class<?>... classes) {
archive.addClasses(classes);
return this;
}

public WebArchiveBuilder addPackage(String packageName) {
archive.addPackage(packageName);
return this;
Expand Down

0 comments on commit 6fddf9f

Please sign in to comment.