Skip to content

Commit

Permalink
test fixtures for thymeleaf
Browse files Browse the repository at this point in the history
  • Loading branch information
tschuehly committed Apr 5, 2024
1 parent 4966e63 commit e2dcb1e
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 411 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package de.tschuehly.spring.viewcomponent.core

import org.junit.jupiter.api.Test

abstract class ThymeleafIntegrationTestBase: IntegrationTestBase() {
@Test
override fun testNestedActionComponent() {
val expectedHtml =
//language=html
"""
<html>
<body id="layoutviewcomponent">
<nav>
This is the NavBar
</nav>
<div id="actionviewcomponent">
<html>
<head>
<script src="http://localhost:35729/livereload.js"></script>
<script defer src="/webjars/htmx.org/dist/htmx.min.js"></script>
</head>
<body id="actionviewcomponent"><h2>ViewAction Get CountUp</h2>
<button hx-get="/custompath/countup" hx-target="#actionviewcomponent">Default ViewAction [GET]</button>
<h3>0</h3>
<h2>ViewAction Post AddItem</h2>
<form hx-post="/actionviewcomponent/additem" hx-target="#actionviewcomponent"><input type="text" name="item">
<button type="submit">Save Item</button>
</form>
<table>
<tr>
<th>Item</th>
<th>Action</th>
</tr>
</table>
<h2>ViewAction Put/Patch Person Form</h2>
<form style="display: inline-grid; gap: 0.5rem">
<label>Name<input type="text" id="name" name="name" value="Thomas"></label>
<label>Age: <input type="number" id="age" name="age" value="23"></label>
<label>Location: <input type="text" id="location" name="location" value="Ludwigsburg"></label>
<button type="submit" hx-put="/actionviewcomponent/savepersonput" hx-target="#actionviewcomponent">Save Changes using Put
</button>
<button type="submit" hx-patch="/actionviewcomponent/savepersonpatch" hx-target="#actionviewcomponent">Save Changes using Patch
</button>
</form>
</body>
</html>
</div>
<footer>This is a footer</footer>
</body>
</html>
""".trimIndent()
assertEndpointReturns("/nested-action", expectedHtml)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.tschuehly.kteviewcomponentexample


import de.tschuehly.spring.viewcomponent.core.IntegrationTestBase
import de.tschuehly.spring.viewcomponent.core.JteIntegrationTestBase
import org.springframework.boot.test.context.SpringBootTest

Expand Down
3 changes: 3 additions & 0 deletions examples/thymeleaf-java-example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ dependencies {
implementation("org.webjars.npm:htmx.org:1.9.2")
implementation("org.webjars:webjars-locator-core:0.53")
testImplementation("org.springframework.boot:spring-boot-starter-test")

testImplementation("org.springframework.boot:spring-boot-devtools")
testImplementation(testFixtures("de.tschuehly:spring-view-component-core:0.7.3-SNAPSHOT"))
}

tasks.withType<Test> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<!--/*@thymesVar id="actionView" type="de.tschuehly.example.thymeleafjava.web.action.ActionViewComponent.ActionView"*/-->
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<script src="http://localhost:35729/livereload.js"></script>
<script defer src="/webjars/htmx.org/dist/htmx.min.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<html>
<body>

<nav>
This is the NavBar
This a NavBar
</nav>
<body>
<!--/*@thymesVar id="layoutView" type="de.tschuehly.example.thymeleafjava.web.layout.LayoutViewComponent.LayoutView"*/-->
<div view:component="${layoutView.nestedViewComponent()}"></div>

</body>
<footer>
This is a footer
</footer>
</body>


</html>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.tschuehly.example.thymeleafjava;

import de.tschuehly.spring.viewcomponent.core.ThymeleafIntegrationTestBase;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -9,206 +10,9 @@
import org.springframework.http.HttpStatus;

@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
class ThymeleafJavaIntegrationTest {
class ThymeleafJavaIntegrationTest extends ThymeleafIntegrationTestBase {
@Autowired
private TestRestTemplate testRestTemplate;

@Test
void testIndexComponent() {
var expectedHtml =
//language=html
"""
<html>
<div>
<h1>This is the IndexViewComponent</h1>
</div>
<a href="/">IndexViewComponent</a><br>
<a href="/simple">SimpleViewComponent</a><br>
<a href="/layout">LayoutViewComponent</a><br>
<a href="/action">ActionViewComponent</a><br>
<a href="/nested-action">NestedActionViewComponent</a><br>
</html>
""".stripIndent();
assertEndpointReturns("/", expectedHtml);
}

@Test
void testSimpleComponent() {
//language=html
var expectedHtml = "<div>\n" +
" <h2>This is the SimpleViewComponent</h2>\n" +
" <div>Hello World</div>\n" +
"</div>";
assertEndpointReturns("/simple", expectedHtml);
}

@Test
void testActionComponent() {
var expectedHtml =
//language=HTML
"""
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://localhost:35729/livereload.js"></script>
<script defer src="/webjars/htmx.org/dist/htmx.min.js"></script>
</head>
<body id="actionviewcomponent">
<h2>ViewAction Get CountUp</h2>
<button hx-get="/custompath/countup" hx-target="#actionviewcomponent">Default ViewAction [GET]</button>
<h3>0</h3>
<h2>ViewAction Post AddItem</h2>
<form hx-post="/actionviewcomponent/additem" hx-target="#actionviewcomponent">
<input type="text" name="item">
<button type="submit">Save Item</button>
</form>
<table>
<tr>
<th>Item</th>
<th>Action</th>
</tr>
\s
</table>
<h2>ViewAction Put/Patch Person Form</h2>
<form style="display: inline-grid; gap: 0.5rem">
<label>
Name <input type="text" id="name" name="name" value="Thomas">
</label>
<label>
Age: <input type="number" id="age" name="age" value="23">
</label>
<label>
Location: <input type="text" id="location" name="location" value="Ludwigsburg">
</label>
<button type="submit" hx-put="/actionviewcomponent/savepersonput" hx-target="#actionviewcomponent">Save Changes using Put</button>
<button type="submit" hx-patch="/actionviewcomponent/savepersonpatch" hx-target="#actionviewcomponent">Save Changes using Patch</button>
</form>
</body>
</html>
""";
assertEndpointReturns(
"/action",
expectedHtml
);
}

@Test
void testNestedActionComponent() {
var expectedHtml =
//language=html
"""
<html>
<body id="layoutviewcomponent">
<nav>
This is the NavBar
</nav>
<div id="actionviewcomponent">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://localhost:35729/livereload.js"></script>
<script defer src="/webjars/htmx.org/dist/htmx.min.js"></script>
</head>
<body id="actionviewcomponent">
<h2>ViewAction Get CountUp</h2>
<button hx-get="/custompath/countup" hx-target="#actionviewcomponent">Default ViewAction [GET]</button>
<h3>0</h3>
<h2>ViewAction Post AddItem</h2>
<form hx-post="/actionviewcomponent/additem" hx-target="#actionviewcomponent">
<input type="text" name="item">
<button type="submit">Save Item</button>
</form>
<table>
<tr>
<th>Item</th>
<th>Action</th>
</tr>
\s
</table>
<h2>ViewAction Put/Patch Person Form</h2>
<form style="display: inline-grid; gap: 0.5rem">
<label>
Name <input type="text" id="name" name="name" value="Thomas">
</label>
<label>
Age: <input type="number" id="age" name="age" value="23">
</label>
<label>
Location: <input type="text" id="location" name="location" value="Ludwigsburg">
</label>
<button type="submit" hx-put="/actionviewcomponent/savepersonput" hx-target="#actionviewcomponent">Save Changes using Put</button>
<button type="submit" hx-patch="/actionviewcomponent/savepersonpatch" hx-target="#actionviewcomponent">Save Changes using Patch</button>
</form>
</body>
</html>
</div>
<footer>
This is a footer
</footer>
</body>
</html>
""";
assertEndpointReturns("/nested-action", expectedHtml);
}

@Test
void testLayoutComponent() {
//language=html
var expectedHtml =
"""
<html>
<body id="layoutviewcomponent">
<nav>
This is the NavBar
</nav>
<div id="simpleviewcomponent"><div>
<h2>This is the SimpleViewComponent</h2>
<div>Hello World</div>
</div></div>
<footer>
This is a footer
</footer>
</body>
</html>
""".stripIndent();
assertEndpointReturns("/layout", expectedHtml);
}


@Test
void testResourceTemplate() {
var expectedHtml = "Hello World";
assertEndpointReturns("/resource-template", expectedHtml);
}

private void assertEndpointReturns(String url, String expectedHtml) {
var response = this.testRestTemplate.exchange(
url, HttpMethod.GET, null, String.class);
Assertions.assertEquals(
HttpStatus.OK, response.getStatusCode()
);
Assertions.assertNotNull(response.getBody());
Assertions.assertEquals(
rmWhitespaceBetweenHtmlTags(expectedHtml),
rmWhitespaceBetweenHtmlTags(response.getBody())
);
}

String rmWhitespaceBetweenHtmlTags(String html) {
return html.replaceAll("(?<=>)(\\s*)(?=\\w)", "")
.replaceAll("(?<=\\w)(\\s*)(?=<)", "")
.replaceAll("(?<=>)(\\s*)(?=<)", "")
.replaceAll("\r\n", "")
.trim();
}
}
TestRestTemplate testRestTemplate;
}
3 changes: 3 additions & 0 deletions examples/thymeleaf-kotlin-example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ dependencies {
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

testImplementation("org.springframework.boot:spring-boot-devtools")
testImplementation(testFixtures("de.tschuehly:spring-view-component-core:0.7.3-SNAPSHOT"))
}

tasks.withType<KotlinCompile> {
Expand Down
30 changes: 15 additions & 15 deletions examples/thymeleaf-kotlin-example/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
rootProject.name = "thymeleaf-kotlin-example"

//includeBuild("..\\..\\thymeleaf"){
// dependencySubstitution {
// substitute(module("de.tschuehly:spring-view-component-thymeleaf")).using(project(":"))
// }
//}
//includeBuild("..\\..\\core"){
// dependencySubstitution {
// substitute(module("de.tschuehly:spring-view-component-core")).using(project(":"))
// }
//}
//includeBuild("..\\..\\jte\\jte-compiler"){
// dependencySubstitution {
// substitute(module("de.tschuehly:spring-view-component-jte-compiler")).using(project(":"))
// }
//}
includeBuild("..\\..\\thymeleaf"){
dependencySubstitution {
substitute(module("de.tschuehly:spring-view-component-thymeleaf")).using(project(":"))
}
}
includeBuild("..\\..\\core"){
dependencySubstitution {
substitute(module("de.tschuehly:spring-view-component-core")).using(project(":"))
}
}
includeBuild("..\\..\\jte\\jte-compiler"){
dependencySubstitution {
substitute(module("de.tschuehly:spring-view-component-jte-compiler")).using(project(":"))
}
}
Loading

0 comments on commit e2dcb1e

Please sign in to comment.