Skip to content

Commit

Permalink
Merge pull request #5217 from machi1990/fix/5201
Browse files Browse the repository at this point in the history
fix: Dev mode - 404 Page - Live reloaded list of static files
  • Loading branch information
gwenneg authored Nov 9, 2019
2 parents 58aaf25 + c913ff5 commit 60be0d9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -249,7 +251,7 @@ private boolean checkForClassFilesChangesInModule(DevModeContext.ModuleInfo modu
}
}
} catch (IOException e) {
throw new RuntimeException(e);
throw new UncheckedIOException(e);
}

return hasChanges;
Expand Down Expand Up @@ -342,7 +344,6 @@ Set<String> checkForFileChange() {
} catch (IOException e) {
log.error("Failed to copy resources", e);
}

}

for (String path : watchedFilePaths.keySet()) {
Expand All @@ -354,7 +355,7 @@ Set<String> checkForFileChange() {
if (value > existing) {
ret.add(path);
log.infof("File change detected: %s", file);
if (doCopy) {
if (doCopy && !Files.isDirectory(file)) {
Path target = classesDir.resolve(path);
byte[] data = Files.readAllBytes(file);
try (FileOutputStream out = new FileOutputStream(target.toFile())) {
Expand All @@ -364,15 +365,15 @@ Set<String> checkForFileChange() {
watchedFileTimestamps.put(file, value);
}
} catch (IOException e) {
throw new RuntimeException(e);
throw new UncheckedIOException(e);
}
} else {
watchedFileTimestamps.put(file, 0L);
Path target = classesDir.resolve(path);
try {
Files.deleteIfExists(target);
} catch (IOException e) {
throw new RuntimeException(e);
throw new UncheckedIOException(e);
}
}
}
Expand Down Expand Up @@ -406,7 +407,7 @@ private boolean checkIfFileModified(Path path, Map<Path, Long> pathModificationT

return false;
} catch (IOException e) {
throw new RuntimeException(e);
throw new UncheckedIOException(e);
}
}

Expand All @@ -428,9 +429,10 @@ public RuntimeUpdatesProcessor setWatchedFilePaths(Map<String, Boolean> watchedF
Path config = root.resolve(path);
if (config.toFile().exists()) {
try {
watchedFileTimestamps.put(config, Files.getLastModifiedTime(config).toMillis());
FileTime lastModifiedTime = Files.getLastModifiedTime(config);
watchedFileTimestamps.put(config, lastModifiedTime.toMillis());
} catch (IOException e) {
throw new RuntimeException(e);
throw new UncheckedIOException(e);
}
} else {
watchedFileTimestamps.put(config, 0L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
import io.quarkus.deployment.builditem.ShutdownContextBuildItem;
import io.quarkus.resteasy.common.deployment.ResteasyInjectionReadyBuildItem;
import io.quarkus.resteasy.runtime.standalone.ResteasyStandaloneRecorder;
Expand Down Expand Up @@ -57,6 +58,11 @@ public ResteasyStandaloneBuildItem(String deploymentRootPath) {

}

@BuildStep
HotDeploymentWatchedFileBuildItem watchMetaInfResources() {
return new HotDeploymentWatchedFileBuildItem(META_INF_RESOURCES);
}

@BuildStep()
@Record(STATIC_INIT)
public void staticInit(ResteasyStandaloneRecorder recorder,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package io.quarkus.resteasy.test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;

import org.hamcrest.Matchers;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -11,10 +15,13 @@
import io.restassured.http.ContentType;

public class NotFoundExceptionMapperTestCase {
private static final String META_INF_RESOURCES = "META-INF/resources/";

@RegisterExtension
static QuarkusDevModeTest test = new QuarkusDevModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(RootResource.class));
.addClasses(RootResource.class)
.addAsResource(new StringAsset("index content"), META_INF_RESOURCES + "index.html"));

@Test
public void testResourceNotFound() {
Expand All @@ -32,6 +39,7 @@ public void testHtmlResourceNotFound() {
.then()
.statusCode(404)
.contentType(ContentType.HTML)
.body(containsString("index.html")) // check that index.html is displayed
.body(Matchers.containsString("<h1 class=\"container\">404 - Resource Not Found</h1>"));
}

Expand All @@ -45,4 +53,46 @@ public void testJsonResourceNotFound() {
.contentType(ContentType.JSON);
}

@Test
public void shouldDisplayNewAddedFileIn404ErrorPage() {
String CONTENT = "html content";
test.addResourceFile(META_INF_RESOURCES + "index2.html", CONTENT);

RestAssured.get("/index2.html")
.then()
.statusCode(200)
.body(containsString(CONTENT)); // check that index2.html is live reloaded

RestAssured.given()
.accept(ContentType.HTML)
.when()
.get("/api")
.then() // try to load unknown path
.statusCode(404)
.body(containsString("index2.html")); // check that index2.html is displayed
}

@Test
public void shouldNotDisplayDeletedFileIn404ErrorPage() {
String TEST_CONTENT = "test html content";
test.addResourceFile(META_INF_RESOURCES + "test.html", TEST_CONTENT);

RestAssured
.get("/test.html")
.then()
.statusCode(200)
.body(containsString(TEST_CONTENT)); // check that test.html is live reloaded

test.deleteResourceFile(META_INF_RESOURCES + "test.html"); // delete test.html file

RestAssured
.given()
.accept(ContentType.HTML)
.when()
.get("/test.html")
.then() // try to load static file
.statusCode(404)
.body(not(containsString("test.html"))); // check that test.html is not displayed

}
}

0 comments on commit 60be0d9

Please sign in to comment.