Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content Type Check + Leaner Simulator JAR #49

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions build.savant
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ logbackVersion = "1.4.8"
slf4jVersion = "2.0.7"
testngVersion = "7.8.0"

project(group: "org.primeframework", name: "prime-mvc", version: "4.22.10", licenses: ["ApacheV2_0"]) {
project(group: "org.primeframework", name: "prime-mvc", version: "4.22.11", licenses: ["ApacheV2_0"]) {
workflow {
fetch {
// Dependency resolution order:
Expand Down Expand Up @@ -130,14 +130,34 @@ target(name: "compile", description: "Compiles the project") {
target(name: "jar", description: "JARs the project", dependsOn: ["compile"]) {
java.jar()

// Create a separate test jar that only includes org/primeframework
def simulatorPatterns = [
~/org\/primeframework\/mvc\/test.+/,
~/org\/primeframework\/mvc\/message\/TestMessageObserver/,
~/org\/primeframework\/mvc\/TestPrimeMain/,
~/org\/primeframework\/mvc\/util\/Throwing.*/,
~/org\/primeframework\/mock/,
// dependencies of RequestBuilder
~/org\/primeframework\/mvc\/http\/MultipartBodyHandler.*/,
~/org\/primeframework\/mvc\/http\/MultipartFileUpload.*/
]
def simulatorExcludePatterns = [
// don't want to exclude things that start with Test, like TestPrimeMain or TestMessageObserver
~/.*Test$/
]
// Create a separate test jar that only includes org.primeframework.mvc.test.RequestSimulator
// and its dependencies
file.copy(to: "build/classes/simulator") {
fileSet(dir: "build/classes/test", includePatterns: [~/org\/primeframework.+/])
fileSet(dir: "build/classes/test",
includePatterns: simulatorPatterns,
excludePatterns: simulatorExcludePatterns)
}

// Create a separate test jar that only includes org/primeframework
// Create a separate test src jar that only includes org.primeframework.mvc.test.RequestSimulator
// and its dependencies
file.copy(to: "build/src/simulator") {
fileSet(dir: "src/test/java", includePatterns: [~/org\/primeframework.+/])
fileSet(dir: "src/test/java",
includePatterns: simulatorPatterns,
excludePatterns: simulatorExcludePatterns)
}

file.jar(file: "build/jars/${project.name}-simulator-${project.version}.jar") {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.primeframework</groupId>
<artifactId>prime-mvc</artifactId>
<version>4.22.10</version>
<version>4.22.11</version>
<packaging>jar</packaging>

<name>FusionAuth App</name>
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/org/example/action/api/NoContentTypeJsonAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024, 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.
* 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.example.action.api;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import org.primeframework.mvc.action.annotation.Action;
import org.primeframework.mvc.action.result.annotation.Stream;

@Action
@Stream
public class NoContentTypeJsonAction {
public int length;

public String name;

public InputStream stream;

public String type;

public String get() {
try (StringWriter stringWriter = new StringWriter()) {
stringWriter.write("{\"hello\": 123}");
stream = new ByteArrayInputStream(stringWriter.toString().getBytes());
length = stringWriter.getBuffer().length();
name = "";
type = "text/plain";
} catch (IOException e) {
throw new RuntimeException(e);
}
return "success";
}
}
133 changes: 133 additions & 0 deletions src/test/java/org/primeframework/mvc/test/DOMHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2024, 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.
* 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.mvc.test;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.primeframework.mvc.test.RequestResult.ThrowingConsumer;
import org.primeframework.mvc.util.ThrowingRunnable;

public class DOMHelper {
public String body;

public Document document;

public DOMHelper(String body, Document document) {
this.body = body;
this.document = document;
}

/**
* Perform any custom modifications to the HTML document
*
* @return this.
*/
public DOMHelper custom(ThrowingRunnable runnable) throws Exception {
runnable.run();
return this;
}

/**
* Perform any custom modifications to the HTML document
*
* @param consumer the HTML document consumer
* @return this.
*/
public DOMHelper custom(ThrowingConsumer<Document> consumer) throws Exception {
consumer.accept(document);
return this;
}

/**
* Remove an attribute from a DOM element.
*
* @param selector the DOM selector
* @param name the name of the attribute to remove
* @return this.
*/
public DOMHelper removeAttribute(String selector, String name) {
Element element = document.selectFirst(selector);
if (element == null) {
throw new AssertionError("Expected at least one element to match the selector " + selector + ". Found [0] elements instead. Unable to set element value.\n\nActual body:\n" + body);
}

element.removeAttr(name);

return this;
}

/**
* Set an attribute w/ value on a DOM element.
*
* @param selector the DOM selector
* @param name the name of the attribute
* @param value the value of the attribute
* @return this.
*/
public DOMHelper setAttribute(String selector, String name, String value) {
Element element = document.selectFirst(selector);
if (element == null) {
throw new AssertionError("Expected at least one element to match the selector " + selector + ". Found [0] elements instead. Unable to set element value.\n\nActual body:\n" + body);
}

element.attr(name, value);

return this;
}

public DOMHelper setChecked(String selector, boolean value) {
Element element = document.selectFirst(selector);
if (element == null) {
throw new AssertionError("Expected at least one element to match the selector " + selector + ". Found [0] elements instead. Unable to set element value.\n\nActual body:\n" + body);
}

if (element.is("input[type=radio]") && value) {
Elements elements = document.select(element.tagName().toLowerCase() + "[type=radio][name=" + element.attr("name") + "]");
for (Element e : elements) {
e.attr("checked", false);
}
}

element.attr("checked", value);
return this;
}

public DOMHelper setValue(String selector, Object value) {
if (value != null) {
Element element = document.selectFirst(selector);
if (element == null) {
throw new AssertionError("Expected at least one element to match the selector " + selector + ". Found [0] elements instead. Unable to set element value.\n\nActual body:\n" + body);
}

// Handle a select element
if (element.is("select")) {
// Remove the selected attribute for each option, add it to the one that matches the requested value.
for (Element option : element.getElementsByTag("option")) {
if (option.attr("value").equals(value.toString())) {
option.attr("selected", "selected");
} else {
option.removeAttr("selected");
}
}
} else {
element.val(value.toString());
}
}

return this;
}
}
Loading