Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit c416245

Browse files
authored
Use JUnit for temporary folders and files. (#101)
* replace core with junit * fix faulty import * Fix missing directory
1 parent 653e008 commit c416245

File tree

6 files changed

+119
-102
lines changed

6 files changed

+119
-102
lines changed

pom.xml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,22 +158,8 @@
158158
</plugins>
159159
</build>
160160

161-
<repositories>
162-
<repository>
163-
<id>jitpack.io</id>
164-
<url>https://jitpack.io</url>
165-
</repository>
166-
</repositories>
167-
168161
<dependencies>
169162
<!-- tests -->
170-
<dependency>
171-
<groupId>com.kttdevelopment.core</groupId>
172-
<artifactId>test-util</artifactId>
173-
<version>main-SNAPSHOT</version>
174-
<scope>test</scope>
175-
</dependency>
176-
177163
<dependency>
178164
<groupId>junit</groupId>
179165
<artifactId>junit</artifactId>

src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddDirTest.java

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package com.kttdevelopment.simplehttpserver.handlers.file;
22

3-
import com.kttdevelopment.core.tests.TestUtil;
43
import com.kttdevelopment.simplehttpserver.SimpleHttpServer;
54
import com.kttdevelopment.simplehttpserver.handler.FileHandler;
6-
import org.junit.Assert;
7-
import org.junit.Test;
5+
import org.junit.*;
6+
import org.junit.rules.TemporaryFolder;
87

98
import java.io.File;
109
import java.io.IOException;
1110
import java.net.URI;
1211
import java.net.http.*;
12+
import java.nio.file.Files;
1313
import java.util.concurrent.ExecutionException;
1414

1515
public final class FileHandlerAddDirTest {
1616

17+
@Rule
18+
public final TemporaryFolder directory = new TemporaryFolder(new File("."));
19+
1720
@SuppressWarnings("SpellCheckingInspection")
1821
@Test
1922
public final void addDirectoryTestsNoWalk() throws IOException, InterruptedException{
@@ -24,30 +27,32 @@ public final void addDirectoryTestsNoWalk() throws IOException, InterruptedExcep
2427
final String contextWName = "altn";
2528
final String dirNewName = "dirName";
2629

27-
final String testRealFile = "test.txt", testFileContent = "readme";
28-
final String noReadDir = "dirnoread";
29-
final String noReadFile = "innerNoRead.txt";
30+
final String fileName = "file";
31+
final String testContent = String.valueOf(System.currentTimeMillis());
3032

31-
final File noWalk = new File("src/test/resources/directory/nowalk");
33+
final File dir = directory.getRoot();
34+
final File subdir = directory.newFolder();
3235

33-
TestUtil.createTestFile(new File(noWalk,testRealFile),testFileContent);
34-
TestUtil.createTestFile(new File(new File(noWalk,noReadDir),noReadFile),testFileContent);
36+
final File file = new File(directory.getRoot(),fileName);
37+
Files.write(file.toPath(),testContent.getBytes());
38+
final File walk = new File(subdir,fileName);
39+
Files.write(walk.toPath(),testContent.getBytes());
3540

3641
final String context = "";
3742

38-
handler.addDirectory(noWalk); // test file & directory read
39-
handler.addDirectory(contextNoName,noWalk);
40-
handler.addDirectory(noWalk,dirNewName);
41-
handler.addDirectory(contextWName,noWalk,dirNewName);
43+
handler.addDirectory(dir); // test file & directory read
44+
handler.addDirectory(contextNoName,dir);
45+
handler.addDirectory(dir,dirNewName);
46+
handler.addDirectory(contextWName,dir,dirNewName);
4247

4348
server.createContext(context,handler);
4449
server.start();
4550

4651
final String[] validPathsToTest = { // valid reads
47-
noWalk.getName() + '/' + testRealFile,
48-
contextNoName + '/' + noWalk.getName() + '/' + testRealFile,
49-
dirNewName + '/' + testRealFile,
50-
contextWName + '/' + dirNewName + '/' + testRealFile
52+
dir.getName() + '/' + "file",
53+
contextNoName + '/' + dir.getName() + '/' + fileName,
54+
dirNewName + '/' + fileName,
55+
contextWName + '/' + dirNewName + '/' + fileName
5156
};
5257

5358
for(final String path : validPathsToTest){
@@ -57,19 +62,21 @@ public final void addDirectoryTestsNoWalk() throws IOException, InterruptedExcep
5762
.build();
5863

5964
try{
60-
final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
61-
.thenApply(HttpResponse::body).get();
65+
final String response = HttpClient.newHttpClient()
66+
.sendAsync(request, HttpResponse.BodyHandlers.ofString())
67+
.thenApply(HttpResponse::body)
68+
.get();
6269

6370
Assert.assertNotNull("Client did not find data for " + path, response);
64-
Assert.assertEquals("Client data did not match server data for " + path,testFileContent,response);
71+
Assert.assertEquals("Client data did not match server data for " + path,testContent,response);
6572
}catch(final ExecutionException ignored){
6673
Assert.fail("Client did not find data for " + path);
6774
}
6875
}
6976

7077
final String[] invalidPathsToTest = {
71-
noWalk.getName() + '/' + noReadDir,
72-
noWalk.getName() + '/' + noReadDir + '/' + noReadFile
78+
dir.getName() + '/' + subdir.getName(),
79+
dir.getName() + '/' + subdir.getName() + '/' + fileName
7380
};
7481

7582
for(final String path : invalidPathsToTest){
@@ -80,8 +87,10 @@ public final void addDirectoryTestsNoWalk() throws IOException, InterruptedExcep
8087

8188
Exception exception = null;
8289
try{
83-
final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
84-
.thenApply(HttpResponse::body).get();
90+
final String response = HttpClient.newHttpClient()
91+
.sendAsync(request, HttpResponse.BodyHandlers.ofString())
92+
.thenApply(HttpResponse::body)
93+
.get();
8594

8695
Assert.assertNull("Client found data for blocked path " + path,response);
8796
}catch(final ExecutionException e){

src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddFilesTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.kttdevelopment.simplehttpserver.handlers.file;
22

3-
import com.kttdevelopment.core.tests.TestUtil;
43
import com.kttdevelopment.simplehttpserver.SimpleHttpServer;
54
import com.kttdevelopment.simplehttpserver.handler.FileHandler;
65
import com.kttdevelopment.simplehttpserver.handler.FileHandlerAdapter;
7-
import org.junit.Assert;
8-
import org.junit.Test;
6+
import org.junit.*;
7+
import org.junit.rules.TemporaryFolder;
98

109
import java.io.File;
1110
import java.io.IOException;
@@ -15,6 +14,10 @@
1514

1615
public final class FileHandlerAddFilesTest {
1716

17+
@Rule
18+
public final TemporaryFolder directory = new TemporaryFolder(new File("."));
19+
20+
1821
@Test
1922
public final void addFilesTests() throws IOException, ExecutionException, InterruptedException{
2023
final int port = 8080;
@@ -35,13 +38,10 @@ public final String getName(final File file){
3538
final FileHandler handler = new FileHandler(adapter);
3639

3740
final File[] files = new File[]{
38-
new File("src/test/resources/files/test.txt"),
39-
new File("src/test/resources/files/test2.txt")
41+
directory.newFile(),
42+
directory.newFile()
4043
};
4144

42-
for(final File file : files)
43-
TestUtil.createTestFile(file);
44-
4545
handler.addFiles(files);
4646
handler.addFiles(altContext,files);
4747

@@ -52,7 +52,7 @@ public final String getName(final File file){
5252
for(final File file : files){
5353
final String url = "http://localhost:" + port + context;
5454
HttpRequest request = HttpRequest.newBuilder()
55-
.uri(URI.create(url + "/" + file.getName().substring(0, file.getName().lastIndexOf('.'))))
55+
.uri(URI.create(url + '/' + file.getName().substring(0, file.getName().lastIndexOf('.'))))
5656
.build();
5757

5858
String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
@@ -64,7 +64,7 @@ public final String getName(final File file){
6464

6565
final String altUrl = "http://localhost:" + port + altContext;
6666
request = HttpRequest.newBuilder()
67-
.uri(URI.create(altUrl + "/" + file.getName().substring(0,file.getName().lastIndexOf('.'))))
67+
.uri(URI.create(altUrl + '/' + file.getName().substring(0,file.getName().lastIndexOf('.'))))
6868
.build();
6969

7070
response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
@@ -74,7 +74,6 @@ public final String getName(final File file){
7474
}
7575

7676
server.stop();
77-
7877
}
7978

8079
}

src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddTest.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.kttdevelopment.simplehttpserver.handlers.file;
22

3-
import com.kttdevelopment.core.tests.TestUtil;
43
import com.kttdevelopment.simplehttpserver.SimpleHttpServer;
54
import com.kttdevelopment.simplehttpserver.handler.ByteLoadingOption;
65
import com.kttdevelopment.simplehttpserver.handler.FileHandler;
7-
import org.junit.Assert;
8-
import org.junit.Test;
6+
import org.junit.*;
7+
import org.junit.rules.TemporaryFolder;
98

109
import java.io.File;
1110
import java.io.IOException;
@@ -18,6 +17,9 @@
1817

1918
public final class FileHandlerAddTest {
2019

20+
@Rule
21+
public final TemporaryFolder directory = new TemporaryFolder(new File("."));
22+
2123
@SuppressWarnings("SpellCheckingInspection")
2224
@Test
2325
public final void addFileTests() throws IOException{
@@ -26,17 +28,19 @@ public final void addFileTests() throws IOException{
2628
final FileHandler handler = new FileHandler();
2729
final String context = "";
2830

29-
final File dir = new File("src/test/resources/file");
30-
3131
final Map<File,ByteLoadingOption> files = new HashMap<>();
3232
for(final ByteLoadingOption blop : ByteLoadingOption.values())
33-
files.put(new File(dir, blop.name() + ".txt"),blop);
33+
files.put(directory.newFile(blop.name()),blop);
3434

3535
// initial write
36-
final String init = String.valueOf(System.currentTimeMillis());
36+
final String testContent = String.valueOf(System.currentTimeMillis());
3737
files.forEach((file, loadingOption) -> {
38-
TestUtil.createTestFile(file,init);
39-
handler.addFile(file, loadingOption);
38+
try{
39+
Files.write(file.toPath(),testContent.getBytes());
40+
handler.addFile(file, loadingOption);
41+
}catch(final IOException e){
42+
e.printStackTrace();
43+
}
4044
});
4145

4246
server.createContext(context,handler);
@@ -51,7 +55,7 @@ public final void addFileTests() throws IOException{
5155
try{
5256
final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
5357
.thenApply(HttpResponse::body).get();
54-
Assert.assertEquals("Client data did not match server data for " + file.getName(),init,response);
58+
Assert.assertEquals("Client data did not match server data for " + file.getName(),testContent,response);
5559
}catch(final InterruptedException | ExecutionException ignored){
5660
Assert.fail("Failed to read context of " + file.getName());
5761
}
@@ -69,7 +73,7 @@ public final void addFileTests() throws IOException{
6973
final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
7074
.thenApply(HttpResponse::body).get();
7175

72-
Assert.assertEquals("Client data did not match server data for " + file.getName(),loadingOption == ByteLoadingOption.PRELOAD ? init : after,response);
76+
Assert.assertEquals("Client data did not match server data for " + file.getName(),loadingOption == ByteLoadingOption.PRELOAD ? testContent : after,response);
7377
}catch(final InterruptedException | ExecutionException ignored){
7478
Assert.fail("Failed to read context " + file.getName());
7579
}

src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerNoWalkTest.java

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package com.kttdevelopment.simplehttpserver.handlers.file;
22

3-
import com.kttdevelopment.core.tests.TestUtil;
43
import com.kttdevelopment.simplehttpserver.SimpleHttpServer;
54
import com.kttdevelopment.simplehttpserver.handler.FileHandler;
65
import com.kttdevelopment.simplehttpserver.handler.FileHandlerAdapter;
7-
import org.junit.Assert;
8-
import org.junit.Test;
6+
import org.junit.*;
7+
import org.junit.rules.TemporaryFolder;
98

109
import java.io.File;
1110
import java.io.IOException;
1211
import java.net.URI;
1312
import java.net.http.*;
13+
import java.nio.file.Files;
1414
import java.util.concurrent.ExecutionException;
1515

1616
public final class FileHandlerNoWalkTest {
1717

18+
@Rule
19+
public final TemporaryFolder directory = new TemporaryFolder(new File("."));
20+
1821
@SuppressWarnings("SpellCheckingInspection")
1922
@Test
2023
public final void addDirectoryTestsNoWalkAdapter() throws IOException, InterruptedException{
@@ -36,30 +39,33 @@ public final String getName(final File file){
3639
final String contextWName = "altn";
3740
final String dirNewName = "dirName";
3841

39-
final String testRealFile = "test", testFileContent = "readme";
40-
final String noReadDir = "dirnoread";
41-
final String noReadFile = "innerNoRead";
42+
final String fileName = "file.txt";
43+
final String expectedName = "file";
44+
final String testContent = String.valueOf(System.currentTimeMillis());
4245

43-
final File noWalk = new File("src/test/resources/directory/nowalk");
46+
final File dir = directory.getRoot();
47+
final File subdir = directory.newFolder();
4448

45-
TestUtil.createTestFile(new File(noWalk, testRealFile + ".txt"), testFileContent);
46-
TestUtil.createTestFile(new File(new File(noWalk,noReadDir),noReadFile + ".txt"),testFileContent);
49+
final File file = new File(directory.getRoot(),fileName);
50+
Files.write(file.toPath(), testContent.getBytes());
51+
final File walk = new File(subdir,fileName);
52+
Files.write(walk.toPath(),testContent.getBytes());
4753

4854
final String context = "";
4955

50-
handler.addDirectory(noWalk); // test file & directory read
51-
handler.addDirectory(contextNoName,noWalk);
52-
handler.addDirectory(noWalk,dirNewName);
53-
handler.addDirectory(contextWName,noWalk,dirNewName);
56+
handler.addDirectory(dir); // test file & directory read
57+
handler.addDirectory(contextNoName,dir);
58+
handler.addDirectory(dir,dirNewName);
59+
handler.addDirectory(contextWName,dir,dirNewName);
5460

5561
server.createContext(context,handler);
5662
server.start();
5763

5864
final String[] validPathsToTest = { // valid reads
59-
noWalk.getName() + '/' + testRealFile,
60-
contextNoName + '/' + noWalk.getName() + '/' + testRealFile,
61-
dirNewName + '/' + testRealFile,
62-
contextWName + '/' + dirNewName + '/' + testRealFile
65+
dir.getName() + '/' + expectedName,
66+
contextNoName + '/' + dir.getName() + '/' + expectedName,
67+
dirNewName + '/' + expectedName,
68+
contextWName + '/' + dirNewName + '/' + expectedName
6369
};
6470

6571
for(final String path : validPathsToTest){
@@ -69,19 +75,21 @@ public final String getName(final File file){
6975
.build();
7076

7177
try{
72-
final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
73-
.thenApply(HttpResponse::body).get();
78+
final String response = HttpClient.newHttpClient()
79+
.sendAsync(request, HttpResponse.BodyHandlers.ofString())
80+
.thenApply(HttpResponse::body)
81+
.get();
7482

7583
Assert.assertNotNull("Client did not find data for " + path, response);
76-
Assert.assertEquals("Client data did not match server data for " + path,testFileContent,response);
84+
Assert.assertEquals("Client data did not match server data for " + path,testContent,response);
7785
}catch(final ExecutionException ignored){
7886
Assert.fail("Client did not find data for " + path);
7987
}
8088
}
8189

8290
final String[] invalidPathsToTest = {
83-
noWalk.getName() + '/' + noReadDir,
84-
noWalk.getName() + '/' + noReadDir + '/' + noReadFile
91+
dir.getName() + '/' + subdir.getName(),
92+
dir.getName() + '/' + subdir.getName() + '/' + fileName
8593
};
8694

8795
for(final String path : invalidPathsToTest){
@@ -92,8 +100,10 @@ public final String getName(final File file){
92100

93101
Exception exception = null;
94102
try{
95-
final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
96-
.thenApply(HttpResponse::body).get();
103+
final String response = HttpClient.newHttpClient()
104+
.sendAsync(request, HttpResponse.BodyHandlers.ofString())
105+
.thenApply(HttpResponse::body)
106+
.get();
97107

98108
Assert.assertNull("Client found data for blocked path " + path,response);
99109
}catch(final ExecutionException e){

0 commit comments

Comments
 (0)