Skip to content

Commit 5c6f6f5

Browse files
authored
Add Linter to Tests folder (#47)
* Add Linter for Tests folder * Linted TestTusClient.java * Linted TestTusExecutor.java * Linted TestTusUploader.java * Linted TestTusURLMemoryStore.java * Linted TestTusURLMemoryStore.java * Commented missing methods
1 parent 3f808b1 commit 5c6f6f5

File tree

8 files changed

+213
-28
lines changed

8 files changed

+213
-28
lines changed

.github/workflows/lintChanges.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ jobs:
2121
uses: github/super-linter@v3
2222
env:
2323
VALIDATE_ALL_CODEBASE: true # lint all files
24-
FILTER_REGEX_EXCLUDE: /src/test/*
2524
VALIDATE_JAVA: true # only lint Java files
2625
DEFAULT_BRANCH: master
2726
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Enables better overview of runs

src/test/java/io/tus/java/client/MockServerProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99

1010
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
1111

12+
/**
13+
* This class provides a MockServer.
14+
*/
1215
public class MockServerProvider {
1316
protected MockServerClient mockServer;
1417
protected URL mockServerURL;
1518
protected URL creationUrl;
1619

20+
/**
21+
* Test configuration before running.
22+
* @throws Exception
23+
*/
1724
@Before
1825
public void setUp() throws Exception {
1926
creationUrl = new URL("http://tusd.tusdemo.net");
@@ -22,6 +29,9 @@ public void setUp() throws Exception {
2229
mockServer = startClientAndServer(port);
2330
}
2431

32+
/**
33+
* Clean up after finishing the test-run.
34+
*/
2535
@After
2636
public void tearDown() {
2737
mockServer.stop();

src/test/java/io/tus/java/client/TestTusClient.java

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,45 @@
1818
import org.mockserver.model.HttpRequest;
1919
import org.mockserver.model.HttpResponse;
2020

21-
21+
/**
22+
* Class to test the tus-Client.
23+
*/
2224
public class TestTusClient extends MockServerProvider {
25+
26+
/**
27+
* Tests if the client object is set up correctly.
28+
*/
2329
@Test
2430
public void testTusClient() {
2531
TusClient client = new TusClient();
2632
assertEquals(client.getUploadCreationURL(), null);
2733
}
2834

35+
/**
36+
* Checks if upload URLS are set correctly.
37+
* @throws MalformedURLException if the provided URL is malformed.
38+
*/
2939
@Test
3040
public void testTusClientURL() throws MalformedURLException {
3141
TusClient client = new TusClient();
3242
client.setUploadCreationURL(creationUrl);
3343
assertEquals(client.getUploadCreationURL(), creationUrl);
3444
}
3545

46+
/**
47+
* Checks if upload URLS are set correctly.
48+
* @throws MalformedURLException if the provided URL is malformed.
49+
*/
3650
@Test
3751
public void testSetUploadCreationURL() throws MalformedURLException {
3852
TusClient client = new TusClient();
3953
client.setUploadCreationURL(new URL("http://tusd.tusdemo.net"));
4054
assertEquals(client.getUploadCreationURL(), new URL("http://tusd.tusdemo.net"));
4155
}
4256

57+
/**
58+
* Tests if resumable uploads can be turned off and on.
59+
*/
4360
@Test
4461
public void testEnableResuming() {
4562
TusClient client = new TusClient();
@@ -53,6 +70,11 @@ public void testEnableResuming() {
5370
assertEquals(client.resumingEnabled(), false);
5471
}
5572

73+
/**
74+
* Verifies if uploads can be created with the tus client.
75+
* @throws IOException if upload data cannot be read.
76+
* @throws ProtocolException if the upload cannot be constructed.
77+
*/
5678
@Test
5779
public void testCreateUpload() throws IOException, ProtocolException {
5880
mockServer.when(new HttpRequest()
@@ -81,8 +103,12 @@ public void testCreateUpload() throws IOException, ProtocolException {
81103
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
82104
}
83105

106+
/**
107+
* Tests if a missing location header causes an exception as expected.
108+
* @throws Exception if unreachable code has been reached.
109+
*/
84110
@Test
85-
public void testCreateUploadWithMissingLocationHeader() throws IOException, Exception {
111+
public void testCreateUploadWithMissingLocationHeader() throws Exception {
86112
mockServer.when(new HttpRequest()
87113
.withMethod("POST")
88114
.withPath("/files")
@@ -100,11 +126,15 @@ public void testCreateUploadWithMissingLocationHeader() throws IOException, Exce
100126
try {
101127
TusUploader uploader = client.createUpload(upload);
102128
throw new Exception("unreachable code reached");
103-
} catch(ProtocolException e) {
129+
} catch (ProtocolException e) {
104130
assertEquals(e.getMessage(), "missing upload URL in response for creating upload");
105131
}
106132
}
107133

134+
/**
135+
* Tests if uploads with relative upload destinations are working.
136+
* @throws Exception
137+
*/
108138
@Test
109139
public void testCreateUploadWithRelativeLocation() throws Exception {
110140
// We need to enable strict following for POST requests first
@@ -137,14 +167,22 @@ public void testCreateUploadWithRelativeLocation() throws Exception {
137167
upload.setInputStream(new ByteArrayInputStream(new byte[10]));
138168
TusUploader uploader = client.createUpload(upload);
139169

140-
// The upload URL must be relative to the URL of the request by which is was returned,
141-
// not the upload creation URL. In most cases, there is no difference between those two
170+
// The upload URL must be relative to the URL of the request by which it was returned,
171+
// not the upload creation URL. In most cases, there is no difference between those two,
142172
// but it's still important to be correct here.
143173
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "Redirected/foo"));
144174
}
145175

176+
/**
177+
* Tests if {@link TusClient#resumeUpload(TusUpload)} works.
178+
* @throws ResumingNotEnabledException
179+
* @throws FingerprintNotFoundException
180+
* @throws IOException
181+
* @throws ProtocolException
182+
*/
146183
@Test
147-
public void testResumeUpload() throws ResumingNotEnabledException, FingerprintNotFoundException, IOException, ProtocolException {
184+
public void testResumeUpload() throws ResumingNotEnabledException, FingerprintNotFoundException, IOException,
185+
ProtocolException {
148186
mockServer.when(new HttpRequest()
149187
.withMethod("HEAD")
150188
.withPath("/files/foo")
@@ -169,6 +207,9 @@ public void testResumeUpload() throws ResumingNotEnabledException, FingerprintNo
169207
assertEquals(uploader.getOffset(), 3);
170208
}
171209

210+
/**
211+
* Test Implementation for a {@link TusURLStore}.
212+
*/
172213
private class TestResumeUploadStore implements TusURLStore {
173214
public void set(String fingerprint, URL url) {
174215
assertTrue("set method must not be called", false);
@@ -179,7 +220,7 @@ public URL get(String fingerprint) {
179220

180221
try {
181222
return new URL(mockServerURL.toString() + "/foo");
182-
} catch(Exception e) {}
223+
} catch (Exception e) { }
183224
return null;
184225
}
185226

@@ -188,6 +229,11 @@ public void remove(String fingerprint) {
188229
}
189230
}
190231

232+
/**
233+
* Tests if an upload gets started if {@link TusClient#resumeOrCreateUpload(TusUpload)} gets called.
234+
* @throws IOException
235+
* @throws ProtocolException
236+
*/
191237
@Test
192238
public void testResumeOrCreateUpload() throws IOException, ProtocolException {
193239
mockServer.when(new HttpRequest()
@@ -210,6 +256,12 @@ public void testResumeOrCreateUpload() throws IOException, ProtocolException {
210256
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
211257
}
212258

259+
260+
/**
261+
* Checks if a new upload attempt is started in case of a serverside 404-error, without having an Exception thrown.
262+
* @throws IOException
263+
* @throws ProtocolException
264+
*/
213265
@Test
214266
public void testResumeOrCreateUploadNotFound() throws IOException, ProtocolException {
215267
mockServer.when(new HttpRequest()
@@ -245,6 +297,11 @@ public void testResumeOrCreateUploadNotFound() throws IOException, ProtocolExcep
245297
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
246298
}
247299

300+
/**
301+
* Tests if {@link TusClient#beginOrResumeUploadFromURL(TusUpload, URL)} works.
302+
* @throws IOException
303+
* @throws ProtocolException
304+
*/
248305
@Test
249306
public void testBeginOrResumeUploadFromURL() throws IOException, ProtocolException {
250307
mockServer.when(new HttpRequest()
@@ -269,6 +326,10 @@ public void testBeginOrResumeUploadFromURL() throws IOException, ProtocolExcepti
269326
assertEquals(uploader.getOffset(), 3);
270327
}
271328

329+
/**
330+
* Tests if connections are prepared correctly, which means all header are getting set.
331+
* @throws IOException
332+
*/
272333
@Test
273334
public void testPrepareConnection() throws IOException {
274335
HttpURLConnection connection = (HttpURLConnection) mockServerURL.openConnection();
@@ -278,6 +339,10 @@ public void testPrepareConnection() throws IOException {
278339
assertEquals(connection.getRequestProperty("Tus-Resumable"), TusClient.TUS_VERSION);
279340
}
280341

342+
/**
343+
* Tests if HTTP - Headers are set correctly.
344+
* @throws IOException
345+
*/
281346
@Test
282347
public void testSetHeaders() throws IOException {
283348
HttpURLConnection connection = (HttpURLConnection) mockServerURL.openConnection();
@@ -298,6 +363,10 @@ public void testSetHeaders() throws IOException {
298363
assertEquals(connection.getRequestProperty("Important"), "yes");
299364
}
300365

366+
/**
367+
* Tests if connection timeouts are set correctly.
368+
* @throws IOException
369+
*/
301370
@Test
302371
public void testSetConnectionTimeout() throws IOException {
303372
HttpURLConnection connection = (HttpURLConnection) mockServerURL.openConnection();
@@ -312,6 +381,10 @@ public void testSetConnectionTimeout() throws IOException {
312381
assertEquals(connection.getConnectTimeout(), 3000);
313382
}
314383

384+
/**
385+
* Tests whether the connection follows redirects only after explicitly enabling this feature.
386+
* @throws Exception
387+
*/
315388
@Test
316389
public void testFollowRedirects() throws Exception {
317390
HttpURLConnection connection = (HttpURLConnection) mockServerURL.openConnection();
@@ -355,6 +428,11 @@ public void testFollowRedirects() throws Exception {
355428
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
356429
}
357430

431+
/**
432+
* Tests if the fingerprint in the {@link TusURLStore} does not get removed after upload success.
433+
* @throws IOException
434+
* @throws ProtocolException
435+
*/
358436
@Test
359437
public void testRemoveFingerprintOnSuccessDisabled() throws IOException, ProtocolException {
360438

@@ -376,6 +454,12 @@ public void testRemoveFingerprintOnSuccessDisabled() throws IOException, Protoco
376454

377455
}
378456

457+
/**
458+
* Tests if the fingerprint in the {@link TusURLStore} does get removed after upload success,
459+
* after this feature has been enabled via the {@link TusClient#enableRemoveFingerprintOnSuccess()} - method.
460+
* @throws IOException
461+
* @throws ProtocolException
462+
*/
379463
@Test
380464
public void testRemoveFingerprintOnSuccessEnabled() throws IOException, ProtocolException {
381465

src/test/java/io/tus/java/client/TestTusExecutor.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@
77
import java.net.MalformedURLException;
88
import java.net.URL;
99

10-
import static org.junit.Assert.*;
11-
10+
import static org.junit.Assert.assertArrayEquals;
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertFalse;
13+
import static org.junit.Assert.assertTrue;
14+
15+
/**
16+
* Test class for a {@link TusExecutor}.
17+
*/
1218
public class TestTusExecutor {
19+
20+
/**
21+
* Tests if the delays for connection attempts are set in the right manner.
22+
*/
1323
@Test
1424
public void testSetDelays() {
1525
CountingExecutor exec = new CountingExecutor();
@@ -20,6 +30,10 @@ public void testSetDelays() {
2030
assertEquals(exec.getCalls(), 0);
2131
}
2232

33+
/**
34+
* Tests if a running execution is interuptable.
35+
* @throws Exception
36+
*/
2337
@Test
2438
public void testInterrupting() throws Exception {
2539
TusExecutor exec = new TusExecutor() {
@@ -38,7 +52,7 @@ public void run() {
3852
try {
3953
Thread.sleep(100);
4054
executorThread.interrupt();
41-
} catch(InterruptedException e) {
55+
} catch (InterruptedException e) {
4256
e.printStackTrace();
4357
}
4458
}
@@ -49,6 +63,10 @@ public void run() {
4963
}
5064

5165

66+
/**
67+
* Tests if {@link TusExecutor#makeAttempts()} actually makes attempts.
68+
* @throws Exception
69+
*/
5270
@Test
5371
public void testMakeAttempts() throws Exception {
5472
CountingExecutor exec = new CountingExecutor();
@@ -59,6 +77,10 @@ public void testMakeAttempts() throws Exception {
5977
}
6078

6179

80+
/**
81+
* Tests if every attempt can throw an IOException.
82+
* @throws Exception
83+
*/
6284
@Test(expected = IOException.class)
6385
public void testMakeAllAttemptsThrowIOException() throws Exception {
6486
CountingExecutor exec = new CountingExecutor() {
@@ -77,6 +99,10 @@ protected void makeAttempt() throws ProtocolException, IOException {
7799
}
78100
}
79101

102+
/**
103+
* Tests if every attempt can throw a {@link ProtocolException}.
104+
* @throws Exception
105+
*/
80106
@Test(expected = ProtocolException.class)
81107
public void testMakeAllAttemptsThrowProtocolException() throws Exception {
82108
CountingExecutor exec = new CountingExecutor() {
@@ -95,6 +121,10 @@ protected void makeAttempt() throws ProtocolException, IOException {
95121
}
96122
}
97123

124+
/**
125+
* Tests if an Exception can be thrown also at single attempts.
126+
* @throws Exception
127+
*/
98128
@Test(expected = ProtocolException.class)
99129
public void testMakeOneAttempt() throws Exception {
100130
CountingExecutor exec = new CountingExecutor() {
@@ -119,7 +149,7 @@ protected void makeAttempt() throws ProtocolException, IOException {
119149
private class MockHttpURLConnection extends HttpURLConnection {
120150
private int statusCode;
121151

122-
public MockHttpURLConnection(int statusCode) throws MalformedURLException {
152+
MockHttpURLConnection(int statusCode) throws MalformedURLException {
123153
super(new URL("http://localhost/"));
124154
this.statusCode = statusCode;
125155
}
@@ -135,10 +165,10 @@ public boolean usingProxy() {
135165
}
136166

137167
@Override
138-
public void disconnect() {}
168+
public void disconnect() { }
139169

140170
@Override
141-
public void connect() {}
171+
public void connect() { }
142172
}
143173

144174
/**

0 commit comments

Comments
 (0)