Skip to content

Commit 33d00d7

Browse files
committed
Add mirror and refresh tests
1 parent baf4601 commit 33d00d7

File tree

6 files changed

+322
-12
lines changed

6 files changed

+322
-12
lines changed

src/main/java/org/hydev/mcpm/client/database/mirrors/MirrorSelectBoundary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ default List<Pair<Mirror, Integer>> pingMirrors() throws IOException
4444
{
4545
return listAvailableMirrors().stream().filter(Mirror::isWeb)
4646
.parallel()
47-
.map(m -> new Pair<>(m, ping(m.url())))
47+
.map(m -> Pair.of(m, ping(m.url())))
4848
.sorted(comparingInt(Map.Entry::getValue))
4949
.toList();
5050
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package org.hydev.mcpm.client.arguments;
2+
3+
import net.sourceforge.argparse4j.inf.ArgumentParserException;
4+
import org.hydev.mcpm.client.arguments.mock.MockMirrorBoundary;
5+
import org.hydev.mcpm.client.arguments.parsers.MirrorParser;
6+
import org.hydev.mcpm.client.commands.controllers.MirrorController;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
import java.io.IOException;
13+
import java.util.List;
14+
15+
public class MirrorParserTest {
16+
private MockMirrorBoundary mirrors;
17+
18+
private ArgsParser args;
19+
20+
@BeforeEach
21+
public void setup() {
22+
var mirrorList = List.of(
23+
MockMirrorBoundary.mockMirror("mcpm.pizza.com"),
24+
MockMirrorBoundary.mockMirror("mcpm.sales.com"),
25+
MockMirrorBoundary.mockMirror("mcpm.something.com"),
26+
MockMirrorBoundary.mockMirror("mcpm.another.com")
27+
);
28+
29+
mirrors = new MockMirrorBoundary(mirrorList);
30+
var controller = new MirrorController(mirrors);
31+
var parser = new MirrorParser(controller);
32+
args = new ArgsParser(List.of(parser));
33+
}
34+
35+
@Test
36+
void testNoArguments() {
37+
var exception = assertThrows(
38+
ArgumentParserException.class,
39+
() -> args.parse(new String[] { "mirror" }, log -> { })
40+
);
41+
42+
assertEquals(exception.getMessage(), "too few arguments");
43+
}
44+
45+
@Test
46+
void testPing() throws ArgumentParserException {
47+
args.parse(new String[] { "mirror", "ping" }, log -> { });
48+
49+
// Not going to bother to test log output, just expected behaviour for the MirrorBoundary.
50+
// Feel free to contribute something like InfoController's tests if you want.
51+
assertTrue(mirrors.getDidPingMirrors());
52+
}
53+
54+
@Test
55+
void testPingThrowing() throws ArgumentParserException {
56+
mirrors.setThrowsIOException(true);
57+
58+
args.parse(new String[] { "mirror", "ping" }, log -> { });
59+
60+
mirrors.setThrowsIOException(false);
61+
62+
// Not going to bother to test log output, just expected behaviour for the MirrorBoundary.
63+
// Feel free to contribute something like InfoController's tests if you want.
64+
65+
// We're just looking for no extreme behaviour here... It should not reach the point where mirrors are pinged.
66+
assertFalse(mirrors.getDidUpdateMirrors());
67+
assertFalse(mirrors.getDidPingMirrors());
68+
}
69+
70+
@Test
71+
void testPingRefreshing() throws ArgumentParserException {
72+
args.parse(new String[] { "mirror", "ping", "--refresh" }, log -> { });
73+
74+
// We're just looking for no extreme behaviour here... It should not reach the point where mirrors are pinged.
75+
assertTrue(mirrors.getDidUpdateMirrors());
76+
assertTrue(mirrors.getDidPingMirrors());
77+
}
78+
79+
@Test
80+
void testSelectNoArguments() throws ArgumentParserException, IOException {
81+
args.parse(new String[] { "mirror", "select" }, log -> { });
82+
83+
// This is the default value for MockMirrorSelector.
84+
// I guess there's also no guarantee that selected mirror works?
85+
assertEquals(mirrors.getSelectedMirror().host(), "mcpm.pizza.com");
86+
}
87+
88+
@Test
89+
void testSelectHost() throws ArgumentParserException, IOException {
90+
args.parse(new String[] { "mirror", "select", "mcpm.something.com" }, log -> { });
91+
92+
assertEquals(mirrors.getSelectedMirror().host(), "mcpm.something.com");
93+
}
94+
95+
@Test
96+
void testSelectMissing() throws ArgumentParserException, IOException {
97+
args.parse(new String[] { "mirror", "select", "mcpm.some.mirror.com" }, log -> { });
98+
99+
// This is the default value for MockMirrorSelector.
100+
assertEquals(mirrors.getSelectedMirror().host(), "mcpm.pizza.com");
101+
}
102+
103+
@Test
104+
void testSelectThrows() throws ArgumentParserException, IOException {
105+
mirrors.setThrowsIOException(true);
106+
107+
args.parse(new String[] { "mirror", "select", "mcpm.something.com" }, log -> { });
108+
109+
mirrors.setThrowsIOException(false);
110+
111+
// This is the default value for MockMirrorSelector, again no change is expected.
112+
assertEquals(mirrors.getSelectedMirror().host(), "mcpm.pizza.com");
113+
}
114+
115+
@Test
116+
void testInvalidOp() throws ArgumentParserException, IOException {
117+
mirrors.setThrowsIOException(true);
118+
119+
args.parse(new String[] { "mirror", "select", "mcpm.something.com" }, log -> { });
120+
121+
mirrors.setThrowsIOException(false);
122+
123+
// This is the default value for MockMirrorSelector, again no change is expected.
124+
assertEquals(mirrors.getSelectedMirror().host(), "mcpm.pizza.com");
125+
}
126+
}

src/test/java/org/hydev/mcpm/client/arguments/ParserFactoryTest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
package org.hydev.mcpm.client.arguments;
22

33
import org.hydev.mcpm.client.arguments.parsers.CommandConfigurator;
4-
import org.hydev.mcpm.client.arguments.parsers.ExportPluginsParser;
5-
import org.hydev.mcpm.client.arguments.parsers.InfoParser;
6-
import org.hydev.mcpm.client.arguments.parsers.InstallParser;
7-
import org.hydev.mcpm.client.arguments.parsers.ListParser;
8-
import org.hydev.mcpm.client.arguments.parsers.MirrorParser;
9-
import org.hydev.mcpm.client.arguments.parsers.PageParser;
10-
import org.hydev.mcpm.client.arguments.parsers.RefreshParser;
11-
import org.hydev.mcpm.client.arguments.parsers.SearchParser;
12-
import org.hydev.mcpm.client.arguments.parsers.UninstallParser;
13-
import org.hydev.mcpm.client.arguments.parsers.UpdateParser;
144
import org.junit.jupiter.api.Tag;
155
import org.junit.jupiter.api.Test;
166

17-
import javax.naming.ldap.Control;
187
import java.util.Set;
198
import java.util.stream.Collectors;
209

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.hydev.mcpm.client.arguments;
2+
3+
4+
import net.sourceforge.argparse4j.inf.ArgumentParserException;
5+
import org.hydev.mcpm.client.arguments.mock.MockMirrorBoundary;
6+
import org.hydev.mcpm.client.arguments.mock.MockRefreshFetcher;
7+
import org.hydev.mcpm.client.arguments.parsers.RefreshParser;
8+
import org.hydev.mcpm.client.commands.controllers.RefreshController;
9+
import org.hydev.mcpm.client.database.fetcher.QuietFetcherListener;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
import java.io.IOException;
14+
import java.util.List;
15+
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertThrows;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
20+
public class RefreshParserTest {
21+
private MockRefreshFetcher fetcher;
22+
private RefreshController controller;
23+
private ArgsParser args;
24+
25+
@BeforeEach
26+
public void setup() {
27+
fetcher = new MockRefreshFetcher();
28+
var mirrors = new MockMirrorBoundary(List.of(
29+
MockMirrorBoundary.mockMirror("mcpm.pizza.com"),
30+
MockMirrorBoundary.mockMirror("mcpm.another.com")
31+
));
32+
controller = new RefreshController(fetcher, new QuietFetcherListener(), mirrors);
33+
var parser = new RefreshParser(controller);
34+
args = new ArgsParser(List.of(parser));
35+
}
36+
37+
@Test
38+
void testFetchDatabase() throws ArgumentParserException {
39+
args.parse(new String[] { "refresh" }, log -> { });
40+
41+
assertTrue(fetcher.getFetched());
42+
}
43+
44+
@Test
45+
void testFailedToFetch() throws ArgumentParserException {
46+
fetcher.setDefaultResult(null);
47+
48+
args.parse(new String[] { "refresh" }, log -> { });
49+
50+
// We won't test for anything fancy here.
51+
// It should still fetch and pass, but I'm not going to bother uhh...
52+
// I'm not going to bother checking log. This method should really return a result.
53+
assertTrue(fetcher.getFetched());
54+
}
55+
56+
57+
@Test
58+
void testController() throws IOException {
59+
controller.refresh();
60+
61+
assertTrue(fetcher.getFetched());
62+
}
63+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.hydev.mcpm.client.arguments.mock;
2+
3+
import org.hydev.mcpm.client.database.mirrors.Mirror;
4+
import org.hydev.mcpm.client.database.mirrors.MirrorSelectBoundary;
5+
import org.hydev.mcpm.utils.Pair;
6+
import org.jetbrains.annotations.NotNull;
7+
8+
import java.io.IOException;
9+
import java.util.List;
10+
import java.util.Set;
11+
import java.util.stream.IntStream;
12+
13+
public class MockMirrorBoundary implements MirrorSelectBoundary {
14+
private Mirror selected;
15+
private final List<Mirror> mirrors;
16+
17+
private boolean didUpdateMirrors = false;
18+
private boolean didPingMirrors = false;
19+
private boolean throwsIOException = false;
20+
21+
public MockMirrorBoundary(List<Mirror> mirrors) {
22+
this.mirrors = mirrors;
23+
this.selected = mirrors.stream().findFirst().orElse(null);
24+
}
25+
26+
public static Mirror mockMirror(String host) {
27+
return new Mirror(
28+
host,
29+
Set.of("http", "https"),
30+
2,
31+
"Canada",
32+
300,
33+
40,
34+
"https://" + host
35+
);
36+
}
37+
38+
@Override
39+
public @NotNull List<Mirror> listAvailableMirrors() throws IOException {
40+
if (throwsIOException) {
41+
throw new IOException();
42+
}
43+
44+
return mirrors;
45+
}
46+
47+
@Override
48+
public List<Pair<Mirror, Integer>> pingMirrors() throws IOException {
49+
// This method was really handy in the boundary! I'm sorry for asking it to be moved.
50+
51+
didPingMirrors = true;
52+
53+
if (throwsIOException) {
54+
throw new IOException();
55+
}
56+
57+
return IntStream.range(0, mirrors.size())
58+
.mapToObj(index -> Pair.of(mirrors.get(index), (index + 1) * 10))
59+
.toList();
60+
}
61+
62+
@Override
63+
public void updateMirrors() throws IOException {
64+
didUpdateMirrors = true;
65+
66+
if (throwsIOException) {
67+
throw new IOException();
68+
}
69+
/* do nothing... shouldn't this be automatic? */
70+
}
71+
72+
@Override
73+
public Mirror getSelectedMirror() throws IOException {
74+
if (throwsIOException) {
75+
throw new IOException();
76+
}
77+
78+
return selected;
79+
}
80+
81+
@Override
82+
public void setSelectedMirror(Mirror mirror) throws IOException {
83+
if (throwsIOException) {
84+
throw new IOException();
85+
}
86+
87+
selected = mirror;
88+
}
89+
90+
public boolean getDidUpdateMirrors() {
91+
return didUpdateMirrors;
92+
}
93+
94+
public boolean getDidPingMirrors() {
95+
return didPingMirrors;
96+
}
97+
98+
public void setThrowsIOException(boolean value) {
99+
throwsIOException = value;
100+
}
101+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.hydev.mcpm.client.arguments.mock;
2+
3+
import org.hydev.mcpm.client.database.fetcher.DatabaseFetcher;
4+
import org.hydev.mcpm.client.database.fetcher.DatabaseFetcherListener;
5+
import org.hydev.mcpm.client.models.Database;
6+
import org.jetbrains.annotations.Nullable;
7+
8+
import java.util.List;
9+
10+
public class MockRefreshFetcher implements DatabaseFetcher {
11+
private boolean fetched = false;
12+
13+
private @Nullable Database defaultResult = new Database(List.of());
14+
15+
@Override
16+
public @Nullable Database fetchDatabase(boolean cache, DatabaseFetcherListener listener) {
17+
if (!cache) {
18+
fetched = true;
19+
}
20+
21+
return defaultResult;
22+
}
23+
24+
public boolean getFetched() {
25+
return fetched;
26+
}
27+
28+
public void setDefaultResult(@Nullable Database database) {
29+
this.defaultResult = database;
30+
}
31+
}

0 commit comments

Comments
 (0)