Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
additional coverage on the embedded web server
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed May 2, 2015
1 parent fe0f763 commit f304c8e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/main/java/ch/digitalfondue/stampo/WatchDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;

import java.io.IOException;
import java.nio.file.ClosedWatchServiceException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -123,11 +124,11 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
void processEvent(DelayQueue<Delayed> delayQueue) {


// wait for key to be signalled
// wait for key to be signaled
WatchKey key;
try {
key = watcher.poll(250, TimeUnit.MILLISECONDS);
} catch (InterruptedException x) {
} catch (InterruptedException | ClosedWatchServiceException x) {
return;
}

Expand Down
93 changes: 88 additions & 5 deletions src/test/java/ch/digitalfondue/stampo/ServeAndWatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,112 @@
*/
package ch.digitalfondue.stampo;

import static ch.digitalfondue.stampo.TestUtils.fromTestResource;
import static ch.digitalfondue.stampo.TestUtils.fromTestResourceAsString;
import static ch.digitalfondue.stampo.TestUtils.get;
import static java.nio.file.Files.write;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.concurrent.CountDownLatch;

import org.junit.Assert;
import org.junit.Test;

import ch.digitalfondue.stampo.TestUtils.InputOutputDirs;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;

public class ServeAndWatchTest {

@Test
public void checkOutput() throws IOException, InterruptedException {
try (InputOutputDirs iod = get()) {

//FIXME: complete!
try (InputOutputDirs iod = get(); WebClient webClient = new WebClient()) {

//
createContentPagination(iod);
//


Stampo stampo = new Stampo(iod.inputDir, iod.outputDir);
ServeAndWatch sw = new ServeAndWatch("localhost", 8080, true, true, stampo.getConfiguration(), stampo::build);

CountDownLatch cdl = new CountDownLatch(1);

ServeAndWatch sw =
new ServeAndWatch("localhost", 8080, true, true, stampo.getConfiguration(), () -> {
new Stampo(iod.inputDir, iod.outputDir).build();
cdl.countDown();
});
sw.start();

//FIXME: complete!
// check 404
try {
webClient.getPage("http://localhost:8080");
} catch (FailingHttpStatusCodeException e) {
Assert.assertEquals(404, e.getResponse().getStatusCode());
}

stampo.build();

// check index
Assert.assertEquals(fromTestResourceAsString("pagination/result/recursive/index.html"),
webClient.getPage("http://localhost:8080").getWebResponse().getContentAsString());


// check directory listing
Assert.assertTrue(webClient.getPage("http://localhost:8080/post/").getWebResponse()
.getContentAsString()
.contains(fromTestResourceAsString("serveandwatch/directory-listing.html")));


// static content
Page page = webClient.getPage("http://localhost:8080/texts/1.txt");
Assert.assertEquals("text/plain", page.getWebResponse().getContentType());
Assert.assertEquals("hello world", page.getWebResponse().getContentAsString("UTF-8"));


try {
webClient.getPage("http://localhost:8080/texts/2.txt");
} catch (FailingHttpStatusCodeException e) {
Assert.assertEquals(404, e.getResponse().getStatusCode());
}

// add new file
write(iod.inputDir.resolve("static/texts/2.txt"), "hello world 2".getBytes(StandardCharsets.UTF_8));
cdl.await();
// newly created static content
Page page2 = webClient.getPage("http://localhost:8080/texts/2.txt");
Assert.assertEquals("text/plain", page2.getWebResponse().getContentType());
Assert.assertEquals("hello world 2", page2.getWebResponse().getContentAsString("UTF-8"));
//


sw.stop();
}
}

private void createContentPagination(InputOutputDirs iod) throws IOException {
write(iod.inputDir.resolve("content/index.html.peb"),
fromTestResource("pagination/index-recursive.html.peb"));
Files.createDirectories(iod.inputDir.resolve("content/post"));
Files.createDirectories(iod.inputDir.resolve("content/post/durpdurp"));

Files.createDirectories(iod.inputDir.resolve("static/texts"));

write(iod.inputDir.resolve("static/texts/1.txt"), "hello world".getBytes(StandardCharsets.UTF_8));


for (int i = 1; i <= 20; i++) {
write(iod.inputDir.resolve("content/post/post" + i + ".md"),
fromTestResource("pagination/post/post" + i + ".md"));
}

for (int i = 21; i <= 31; i++) {
write(iod.inputDir.resolve("content/post/durpdurp/post" + i + ".md"),
fromTestResource("pagination/post/durpdurp/post" + i + ".md"));
}
}
}
1 change: 1 addition & 0 deletions src/test/resources/serveandwatch/directory-listing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<body><li><a href="..">go up</a><li><a href="durpdurp">durpdurp</a><li><a href="post1">post1</a><li><a href="post10">post10</a><li><a href="post11">post11</a><li><a href="post12">post12</a><li><a href="post13">post13</a><li><a href="post14">post14</a><li><a href="post15">post15</a><li><a href="post16">post16</a><li><a href="post17">post17</a><li><a href="post18">post18</a><li><a href="post19">post19</a><li><a href="post2">post2</a><li><a href="post20">post20</a><li><a href="post3">post3</a><li><a href="post4">post4</a><li><a href="post5">post5</a><li><a href="post6">post6</a><li><a href="post7">post7</a><li><a href="post8">post8</a><li><a href="post9">post9</a></body>

0 comments on commit f304c8e

Please sign in to comment.