Skip to content

Commit

Permalink
Add Linter to Tests folder (#47)
Browse files Browse the repository at this point in the history
* Add Linter for Tests folder

* Linted TestTusClient.java

* Linted TestTusExecutor.java

* Linted TestTusUploader.java

* Linted TestTusURLMemoryStore.java

* Linted TestTusURLMemoryStore.java

* Commented missing methods
  • Loading branch information
cdr-chakotay authored Nov 17, 2021
1 parent 3f808b1 commit 5c6f6f5
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 28 deletions.
1 change: 0 additions & 1 deletion .github/workflows/lintChanges.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jobs:
uses: github/super-linter@v3
env:
VALIDATE_ALL_CODEBASE: true # lint all files
FILTER_REGEX_EXCLUDE: /src/test/*
VALIDATE_JAVA: true # only lint Java files
DEFAULT_BRANCH: master
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Enables better overview of runs
10 changes: 10 additions & 0 deletions src/test/java/io/tus/java/client/MockServerProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@

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

/**
* This class provides a MockServer.
*/
public class MockServerProvider {
protected MockServerClient mockServer;
protected URL mockServerURL;
protected URL creationUrl;

/**
* Test configuration before running.
* @throws Exception
*/
@Before
public void setUp() throws Exception {
creationUrl = new URL("http://tusd.tusdemo.net");
Expand All @@ -22,6 +29,9 @@ public void setUp() throws Exception {
mockServer = startClientAndServer(port);
}

/**
* Clean up after finishing the test-run.
*/
@After
public void tearDown() {
mockServer.stop();
Expand Down
98 changes: 91 additions & 7 deletions src/test/java/io/tus/java/client/TestTusClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,45 @@
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;


/**
* Class to test the tus-Client.
*/
public class TestTusClient extends MockServerProvider {

/**
* Tests if the client object is set up correctly.
*/
@Test
public void testTusClient() {
TusClient client = new TusClient();
assertEquals(client.getUploadCreationURL(), null);
}

/**
* Checks if upload URLS are set correctly.
* @throws MalformedURLException if the provided URL is malformed.
*/
@Test
public void testTusClientURL() throws MalformedURLException {
TusClient client = new TusClient();
client.setUploadCreationURL(creationUrl);
assertEquals(client.getUploadCreationURL(), creationUrl);
}

/**
* Checks if upload URLS are set correctly.
* @throws MalformedURLException if the provided URL is malformed.
*/
@Test
public void testSetUploadCreationURL() throws MalformedURLException {
TusClient client = new TusClient();
client.setUploadCreationURL(new URL("http://tusd.tusdemo.net"));
assertEquals(client.getUploadCreationURL(), new URL("http://tusd.tusdemo.net"));
}

/**
* Tests if resumable uploads can be turned off and on.
*/
@Test
public void testEnableResuming() {
TusClient client = new TusClient();
Expand All @@ -53,6 +70,11 @@ public void testEnableResuming() {
assertEquals(client.resumingEnabled(), false);
}

/**
* Verifies if uploads can be created with the tus client.
* @throws IOException if upload data cannot be read.
* @throws ProtocolException if the upload cannot be constructed.
*/
@Test
public void testCreateUpload() throws IOException, ProtocolException {
mockServer.when(new HttpRequest()
Expand Down Expand Up @@ -81,8 +103,12 @@ public void testCreateUpload() throws IOException, ProtocolException {
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
}

/**
* Tests if a missing location header causes an exception as expected.
* @throws Exception if unreachable code has been reached.
*/
@Test
public void testCreateUploadWithMissingLocationHeader() throws IOException, Exception {
public void testCreateUploadWithMissingLocationHeader() throws Exception {
mockServer.when(new HttpRequest()
.withMethod("POST")
.withPath("/files")
Expand All @@ -100,11 +126,15 @@ public void testCreateUploadWithMissingLocationHeader() throws IOException, Exce
try {
TusUploader uploader = client.createUpload(upload);
throw new Exception("unreachable code reached");
} catch(ProtocolException e) {
} catch (ProtocolException e) {
assertEquals(e.getMessage(), "missing upload URL in response for creating upload");
}
}

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

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

/**
* Tests if {@link TusClient#resumeUpload(TusUpload)} works.
* @throws ResumingNotEnabledException
* @throws FingerprintNotFoundException
* @throws IOException
* @throws ProtocolException
*/
@Test
public void testResumeUpload() throws ResumingNotEnabledException, FingerprintNotFoundException, IOException, ProtocolException {
public void testResumeUpload() throws ResumingNotEnabledException, FingerprintNotFoundException, IOException,
ProtocolException {
mockServer.when(new HttpRequest()
.withMethod("HEAD")
.withPath("/files/foo")
Expand All @@ -169,6 +207,9 @@ public void testResumeUpload() throws ResumingNotEnabledException, FingerprintNo
assertEquals(uploader.getOffset(), 3);
}

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

try {
return new URL(mockServerURL.toString() + "/foo");
} catch(Exception e) {}
} catch (Exception e) { }
return null;
}

Expand All @@ -188,6 +229,11 @@ public void remove(String fingerprint) {
}
}

/**
* Tests if an upload gets started if {@link TusClient#resumeOrCreateUpload(TusUpload)} gets called.
* @throws IOException
* @throws ProtocolException
*/
@Test
public void testResumeOrCreateUpload() throws IOException, ProtocolException {
mockServer.when(new HttpRequest()
Expand All @@ -210,6 +256,12 @@ public void testResumeOrCreateUpload() throws IOException, ProtocolException {
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
}


/**
* Checks if a new upload attempt is started in case of a serverside 404-error, without having an Exception thrown.
* @throws IOException
* @throws ProtocolException
*/
@Test
public void testResumeOrCreateUploadNotFound() throws IOException, ProtocolException {
mockServer.when(new HttpRequest()
Expand Down Expand Up @@ -245,6 +297,11 @@ public void testResumeOrCreateUploadNotFound() throws IOException, ProtocolExcep
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
}

/**
* Tests if {@link TusClient#beginOrResumeUploadFromURL(TusUpload, URL)} works.
* @throws IOException
* @throws ProtocolException
*/
@Test
public void testBeginOrResumeUploadFromURL() throws IOException, ProtocolException {
mockServer.when(new HttpRequest()
Expand All @@ -269,6 +326,10 @@ public void testBeginOrResumeUploadFromURL() throws IOException, ProtocolExcepti
assertEquals(uploader.getOffset(), 3);
}

/**
* Tests if connections are prepared correctly, which means all header are getting set.
* @throws IOException
*/
@Test
public void testPrepareConnection() throws IOException {
HttpURLConnection connection = (HttpURLConnection) mockServerURL.openConnection();
Expand All @@ -278,6 +339,10 @@ public void testPrepareConnection() throws IOException {
assertEquals(connection.getRequestProperty("Tus-Resumable"), TusClient.TUS_VERSION);
}

/**
* Tests if HTTP - Headers are set correctly.
* @throws IOException
*/
@Test
public void testSetHeaders() throws IOException {
HttpURLConnection connection = (HttpURLConnection) mockServerURL.openConnection();
Expand All @@ -298,6 +363,10 @@ public void testSetHeaders() throws IOException {
assertEquals(connection.getRequestProperty("Important"), "yes");
}

/**
* Tests if connection timeouts are set correctly.
* @throws IOException
*/
@Test
public void testSetConnectionTimeout() throws IOException {
HttpURLConnection connection = (HttpURLConnection) mockServerURL.openConnection();
Expand All @@ -312,6 +381,10 @@ public void testSetConnectionTimeout() throws IOException {
assertEquals(connection.getConnectTimeout(), 3000);
}

/**
* Tests whether the connection follows redirects only after explicitly enabling this feature.
* @throws Exception
*/
@Test
public void testFollowRedirects() throws Exception {
HttpURLConnection connection = (HttpURLConnection) mockServerURL.openConnection();
Expand Down Expand Up @@ -355,6 +428,11 @@ public void testFollowRedirects() throws Exception {
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
}

/**
* Tests if the fingerprint in the {@link TusURLStore} does not get removed after upload success.
* @throws IOException
* @throws ProtocolException
*/
@Test
public void testRemoveFingerprintOnSuccessDisabled() throws IOException, ProtocolException {

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

}

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

Expand Down
42 changes: 36 additions & 6 deletions src/test/java/io/tus/java/client/TestTusExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@
import java.net.MalformedURLException;
import java.net.URL;

import static org.junit.Assert.*;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Test class for a {@link TusExecutor}.
*/
public class TestTusExecutor {

/**
* Tests if the delays for connection attempts are set in the right manner.
*/
@Test
public void testSetDelays() {
CountingExecutor exec = new CountingExecutor();
Expand All @@ -20,6 +30,10 @@ public void testSetDelays() {
assertEquals(exec.getCalls(), 0);
}

/**
* Tests if a running execution is interuptable.
* @throws Exception
*/
@Test
public void testInterrupting() throws Exception {
TusExecutor exec = new TusExecutor() {
Expand All @@ -38,7 +52,7 @@ public void run() {
try {
Thread.sleep(100);
executorThread.interrupt();
} catch(InterruptedException e) {
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Expand All @@ -49,6 +63,10 @@ public void run() {
}


/**
* Tests if {@link TusExecutor#makeAttempts()} actually makes attempts.
* @throws Exception
*/
@Test
public void testMakeAttempts() throws Exception {
CountingExecutor exec = new CountingExecutor();
Expand All @@ -59,6 +77,10 @@ public void testMakeAttempts() throws Exception {
}


/**
* Tests if every attempt can throw an IOException.
* @throws Exception
*/
@Test(expected = IOException.class)
public void testMakeAllAttemptsThrowIOException() throws Exception {
CountingExecutor exec = new CountingExecutor() {
Expand All @@ -77,6 +99,10 @@ protected void makeAttempt() throws ProtocolException, IOException {
}
}

/**
* Tests if every attempt can throw a {@link ProtocolException}.
* @throws Exception
*/
@Test(expected = ProtocolException.class)
public void testMakeAllAttemptsThrowProtocolException() throws Exception {
CountingExecutor exec = new CountingExecutor() {
Expand All @@ -95,6 +121,10 @@ protected void makeAttempt() throws ProtocolException, IOException {
}
}

/**
* Tests if an Exception can be thrown also at single attempts.
* @throws Exception
*/
@Test(expected = ProtocolException.class)
public void testMakeOneAttempt() throws Exception {
CountingExecutor exec = new CountingExecutor() {
Expand All @@ -119,7 +149,7 @@ protected void makeAttempt() throws ProtocolException, IOException {
private class MockHttpURLConnection extends HttpURLConnection {
private int statusCode;

public MockHttpURLConnection(int statusCode) throws MalformedURLException {
MockHttpURLConnection(int statusCode) throws MalformedURLException {
super(new URL("http://localhost/"));
this.statusCode = statusCode;
}
Expand All @@ -135,10 +165,10 @@ public boolean usingProxy() {
}

@Override
public void disconnect() {}
public void disconnect() { }

@Override
public void connect() {}
public void connect() { }
}

/**
Expand Down
Loading

0 comments on commit 5c6f6f5

Please sign in to comment.