Skip to content

Commit 550ae90

Browse files
authored
[PR] Merge pull request #66 from CSC207-2022F-UofT/feat-more-tests
[+] More tests
2 parents 6dcb5bc + 45beb51 commit 550ae90

25 files changed

+433
-159
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ checkstyle {
4747
dependencies {
4848
// The Spigot API with no shadowing. Requires the OSS repo.
4949
compileOnly 'org.spigotmc:spigot-api:1.19-R0.1-SNAPSHOT'
50+
testImplementation 'org.spigotmc:spigot-api:1.19-R0.1-SNAPSHOT'
5051

5152
// Argument parser
5253
// https://mvnrepository.com/artifact/net.sourceforge.argparse4j/argparse4j

src/main/java/org/hydev/mcpm/client/Downloader.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99

1010
import java.io.*;
1111
import java.net.HttpURLConnection;
12+
import java.net.URL;
1213
import java.nio.file.Files;
1314
import java.util.ArrayList;
1415
import java.util.HashMap;
15-
import java.util.List;
1616
import java.util.Map;
17-
import java.net.URL;
1817
import java.util.concurrent.ExecutorService;
1918
import java.util.concurrent.Executors;
20-
import java.util.concurrent.TimeUnit;
2119

2220
import static java.lang.String.format;
2321

@@ -140,18 +138,20 @@ public Downloader threads(int threads)
140138
/**
141139
* Displays a demo for downloader.
142140
*
143-
* @param args Arguments are ignored.
141+
* @param args Not used
144142
*/
145-
public static void main(String[] args) throws IOException {
146-
// Remember to chang link to test
147-
String link = "https://sd.blackball.lv/library/Introduction_to_Algorithms_Third_Edition_(2009).pdf";
148-
File out = new File("./Introduction_to_Algorithms_Third_Edition.pdf");
149-
String link1 = "https://www.iusb.edu/students/academic-success-programs/academic-centers-for-excellence/docs/Basic%20Math%20Review%20Card.pdf";
150-
File out1 = new File("./Math.pdf");
151-
Downloader downloader = new Downloader();
143+
public static void main(String[] args)
144+
{
145+
String link = "https://mcpm.hydev.org/db";
146+
File out = new File("./build/db.json");
147+
String link1 = "https://mcpm.hydev.org/db.zst";
148+
File out1 = new File("./build/db.zst");
149+
Downloader downloader = new Downloader().showProgress(true).threads(2);
152150
Map<String, File> urls = new HashMap<>();
153151
urls.put(link, out);
154152
urls.put(link1, out1);
155153
downloader.downloadFiles(urls);
154+
out.delete();
155+
out1.delete();
156156
}
157157
}

src/main/java/org/hydev/mcpm/client/database/fetcher/LocalDatabaseFetcher.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.nio.file.Files;
1919
import java.nio.file.Path;
2020
import java.nio.file.Paths;
21-
import java.security.NoSuchAlgorithmException;
2221

2322
import static org.hydev.mcpm.Constants.JACKSON;
2423

@@ -177,17 +176,13 @@ private Database fetchHostDatabase(DatabaseFetcherListener listener) {
177176
if (database != null) {
178177
localDatabase = database;
179178

180-
try {
181-
var hash = new HashUtils().hash(body);
179+
var hash = new HashUtils().hash(body);
182180

183-
//noinspection ResultOfMethodCallIgnored
184-
cacheDirectory.toFile().mkdirs();
181+
//noinspection ResultOfMethodCallIgnored
182+
cacheDirectory.toFile().mkdirs();
185183

186-
Files.writeString(Paths.get(cacheDirectory.toString(), DATABASE_FILE_NAME), body);
187-
Files.writeString(Paths.get(cacheDirectory.toString(), HASH_FILE_NAME), hash);
188-
} catch (NoSuchAlgorithmException e) {
189-
e.printStackTrace();
190-
}
184+
Files.writeString(Paths.get(cacheDirectory.toString(), DATABASE_FILE_NAME), body);
185+
Files.writeString(Paths.get(cacheDirectory.toString(), HASH_FILE_NAME), hash);
191186
}
192187

193188
return database;

src/main/java/org/hydev/mcpm/client/interaction/Commands.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/main/java/org/hydev/mcpm/client/models/PluginYml.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.core.JsonProcessingException;
55
import com.fasterxml.jackson.databind.node.ObjectNode;
6+
import com.fasterxml.jackson.dataformat.yaml.JacksonYAMLParseException;
67
import org.jetbrains.annotations.NotNull;
78
import org.yaml.snakeyaml.error.MarkedYAMLException;
89

@@ -49,8 +50,7 @@ public static class InvalidPluginMetaStructure extends Exception { }
4950
* @param yml YML string
5051
* @return PluginYml object
5152
*/
52-
public static PluginYml fromYml(String yml)
53-
throws MarkedYAMLException, InvalidPluginMetaStructure, JsonProcessingException {
53+
public static PluginYml fromYml(String yml) throws InvalidPluginMetaStructure, JsonProcessingException {
5454
// The YAML parser doesn't like \t tab characters
5555
yml = yml.replace("\t", " ");
5656

src/main/java/org/hydev/mcpm/server/crawlers/spiget/CreateDatabase.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.io.File;
1515
import java.io.IOException;
1616
import java.nio.file.Files;
17-
import java.security.NoSuchAlgorithmException;
1817
import java.util.Arrays;
1918
import java.util.List;
2019
import java.util.Optional;
@@ -64,7 +63,7 @@ public static void writeDatabase(File crawlerDirectory,
6463
var hash = new HashUtils().hash(databaseFile);
6564

6665
Files.writeString(versionFile.toPath(), hash);
67-
} catch (NoSuchAlgorithmException | IOException e) {
66+
} catch (IOException e) {
6867
System.err.println("Failed to write database hash file.");
6968
}
7069
}
@@ -168,7 +167,7 @@ private static Optional<PluginVersion> createPluginVersion(long pluginId, File v
168167
System.err.printf("Cannot read plugin.yml for %s: %s\n", metaFile.toPath(), e);
169168

170169
return Optional.empty();
171-
} catch (NumberFormatException | NoSuchAlgorithmException | IOException e) {
170+
} catch (NumberFormatException | IOException e) {
172171
e.printStackTrace();
173172

174173
return Optional.empty();

src/main/java/org/hydev/mcpm/utils/HashUtils.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
package org.hydev.mcpm.utils;
22

33
import org.apache.hc.client5.http.utils.Hex;
4-
import org.jetbrains.annotations.Nullable;
54

6-
import java.io.BufferedInputStream;
7-
import java.io.ByteArrayInputStream;
8-
import java.io.File;
9-
import java.io.FileInputStream;
10-
import java.io.FileNotFoundException;
11-
import java.io.IOException;
12-
import java.io.InputStream;
5+
import java.io.*;
136
import java.nio.charset.StandardCharsets;
147
import java.security.MessageDigest;
158
import java.security.NoSuchAlgorithmException;
@@ -24,11 +17,15 @@ public class HashUtils {
2417

2518
/**
2619
* Default constructor uses the SHA-256 algorithm.
27-
*
28-
* @throws NoSuchAlgorithmException Thrown in the case that the algorithm doesn't ship with an implementation.
2920
*/
30-
public HashUtils() throws NoSuchAlgorithmException {
31-
this(defaultAlgorithm);
21+
public HashUtils() {
22+
try {
23+
digest = MessageDigest.getInstance(defaultAlgorithm);
24+
}
25+
catch (NoSuchAlgorithmException e) {
26+
// Would never happen
27+
throw new RuntimeException(e);
28+
}
3229
}
3330

3431
/**

src/main/java/org/hydev/mcpm/utils/PluginJarFile.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import java.io.File;
77
import java.io.IOException;
8-
import java.nio.charset.Charset;
98
import java.nio.charset.StandardCharsets;
109
import java.util.zip.ZipFile;
1110

@@ -22,11 +21,6 @@ public PluginJarFile(@NotNull File file) throws IOException
2221
super(file);
2322
}
2423

25-
public PluginJarFile(@NotNull File file, int mode, @NotNull Charset charset) throws IOException
26-
{
27-
super(file, mode, charset);
28-
}
29-
3024
/**
3125
* Read the content of a file inside a jar/zip file.
3226
*

src/main/java/org/hydev/mcpm/utils/TemporaryDir.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.util.Comparator;
68

79
/**
810
* This class handles temporary directories like Python's tempfile.TemporaryDirectory:
@@ -54,6 +56,14 @@ public TemporaryDir()
5456
@Override
5557
public void close()
5658
{
57-
path.delete();
59+
// Recursively delete files on exit
60+
try (var w = Files.walk(path.toPath()))
61+
{
62+
w.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
63+
}
64+
catch (IOException e)
65+
{
66+
throw new RuntimeException(e);
67+
}
5868
}
5969
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.hydev.mcpm.client.arguments;
2+
3+
import net.sourceforge.argparse4j.inf.ArgumentParserException;
4+
import net.sourceforge.argparse4j.inf.Namespace;
5+
import net.sourceforge.argparse4j.inf.Subparser;
6+
import org.hydev.mcpm.client.arguments.parsers.CommandParser;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.util.List;
10+
import java.util.function.Consumer;
11+
12+
import static org.hydev.mcpm.utils.ColorLogger.printc;
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
/**
16+
* Tests for ArgsParser
17+
*
18+
* @author Azalea (https://github.com/hykilpikonna)
19+
* @since 2022-11-21
20+
*/
21+
class ArgsParserTest
22+
{
23+
static class TestCommand implements CommandParser
24+
{
25+
@Override
26+
public void run(Namespace details, Consumer<String> log)
27+
{
28+
log.accept(details.toString());
29+
assertEquals(details.getString("a"), "meow");
30+
}
31+
32+
@Override
33+
public String name()
34+
{
35+
return "test";
36+
}
37+
38+
@Override
39+
public String description()
40+
{
41+
return "A testing command";
42+
}
43+
44+
@Override
45+
public void configure(Subparser parser)
46+
{
47+
parser.addArgument("a");
48+
}
49+
}
50+
51+
@Test
52+
void parse() throws ArgumentParserException
53+
{
54+
var p = new ArgsParser(List.of(new TestCommand()), System.out::println);
55+
p.parse(new String[]{"test", "meow"});
56+
// Should print help
57+
p.parse(new String[]{});
58+
// Should print test help
59+
p.parse(new String[]{"test", "-h"});
60+
assertThrows(AssertionError.class, () -> p.parse(new String[]{"test", "asdf"}));
61+
printc(p.help());
62+
assertEquals(p.getRawSubparsers().size(), 1);
63+
}
64+
65+
@Test
66+
void fail()
67+
{
68+
var p = new ArgsParser(List.of(new TestCommand()), System.out::println);
69+
try
70+
{
71+
p.parse(new String[]{"a"});
72+
}
73+
catch (ArgumentParserException e)
74+
{
75+
p.fail(e);
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)