Skip to content

Commit

Permalink
Switching to JUnit 5 (#98)
Browse files Browse the repository at this point in the history
* Switching to JUnit 5
  • Loading branch information
stefanofornari committed Jul 10, 2023
1 parent c154c46 commit c2016c3
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 118 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gradle.properties

# Ignore Gradle build output directory
build
bin

#MacOS
.DS_Store
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ compileJava {
dependencies {
implementation 'io.reactivex.rxjava3:rxjava:3.1.6'
implementation 'io.reactivex.rxjava3:rxjava:3.1.6'

// Use JUnit test framework.
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
testImplementation 'org.mockito:mockito-core:5.4.0'
testImplementation 'org.mockito:mockito-junit-jupiter:5.4.0'

implementation platform('software.amazon.awssdk:bom:2.20.101')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

package software.amazon.nio.spi.s3;

import org.junit.Test;

import static org.junit.Assert.*;
import org.junit.jupiter.api.Assertions;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class PosixLikePathRepresentationTest {
String root = "/";
Expand All @@ -27,19 +27,19 @@ public class PosixLikePathRepresentationTest {



@Test(expected = IllegalArgumentException.class)
@Test
public void ofNullWithMore() {
PosixLikePathRepresentation.of(null, "foo");
assertThrows(IllegalArgumentException.class, () -> PosixLikePathRepresentation.of(null, "foo"));
}

@Test
public void ofNull(){
assertEquals(PosixLikePathRepresentation.EMPTY_PATH, PosixLikePathRepresentation.of(null));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void ofEmptyWithMore(){
PosixLikePathRepresentation.of("", "foo");
assertThrows(IllegalArgumentException.class, () -> PosixLikePathRepresentation.of("", "foo"));
}

@Test
Expand Down
39 changes: 17 additions & 22 deletions src/test/java/software/amazon/nio/spi/s3/S3ClientStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@

package software.amazon.nio.spi.s3;

import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import software.amazon.awssdk.awscore.exception.AwsErrorDetails;
import software.amazon.awssdk.http.SdkHttpResponse;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetBucketLocationResponse;
Expand All @@ -23,13 +16,21 @@

import java.util.NoSuchElementException;
import java.util.function.Consumer;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InOrder;
import org.mockito.Mock;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
@SuppressWarnings("unchecked")
public class S3ClientStoreTest extends TestCase {

public class S3ClientStoreTest {
S3ClientStore instance;

@Mock
Expand All @@ -38,17 +39,11 @@ public class S3ClientStoreTest extends TestCase {
@Spy
final S3ClientStore spyInstance = S3ClientStore.getInstance();

@Before
@BeforeEach
public void setUp() throws Exception {
super.setUp();
instance = S3ClientStore.getInstance();
}

@Test
public void testGetInstanceReturnsSingleton() {
assertSame(S3ClientStore.getInstance(), instance);
}

@Test
public void testGetClientForNullBucketName() {
assertEquals(S3ClientStore.DEFAULT_CLIENT, instance.getClientForBucketName(null));
Expand Down Expand Up @@ -227,14 +222,14 @@ public void testGenerateAsyncClientWith403Then301ResponsesNoHeader(){

@Test
public void testCaching() {
S3Client client = S3Client.create();
S3Client client = S3Client.builder().region(Region.US_EAST_1).build();
doReturn(client).when(spyInstance).generateClient("test-bucket");

final S3Client client1 = spyInstance.getClientForBucketName("test-bucket");
verify(spyInstance).generateClient("test-bucket");
assertSame(client1, client);

S3Client differentClient = S3Client.create();
S3Client differentClient = S3Client.builder().region(Region.US_EAST_1).build();
assertNotSame(client, differentClient);

lenient().doReturn(differentClient).when(spyInstance).generateClient("test-bucket");
Expand All @@ -247,14 +242,14 @@ public void testCaching() {

@Test
public void testAsyncCaching() {
S3AsyncClient client = S3AsyncClient.create();
S3AsyncClient client = S3AsyncClient.builder().region(Region.US_EAST_1).build();
doReturn(client).when(spyInstance).generateAsyncClient("test-bucket");

final S3AsyncClient client1 = spyInstance.getAsyncClientForBucketName("test-bucket");
verify(spyInstance).generateAsyncClient("test-bucket");
assertSame(client1, client);

S3AsyncClient differentClient = S3AsyncClient.create();
S3AsyncClient differentClient = S3AsyncClient.builder().region(Region.US_EAST_1).build();
assertNotSame(client, differentClient);

lenient().doReturn(differentClient).when(spyInstance).generateAsyncClient("test-bucket");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@

package software.amazon.nio.spi.s3;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.http.SdkHttpResponse;
import software.amazon.awssdk.services.s3.S3AsyncClient;
Expand All @@ -35,12 +31,19 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.mockito.junit.jupiter.MockitoExtension;

@SuppressWarnings("unchecked")
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class S3FileSystemProviderTest {

S3FileSystemProvider provider;
Expand All @@ -49,11 +52,11 @@ public class S3FileSystemProviderTest {
@Mock
S3AsyncClient mockClient;

@Before
@BeforeEach
public void init() {
provider = new S3FileSystemProvider();
fs = new S3FileSystem("s3://mybucket", provider);
when(mockClient.headObject(any(Consumer.class))).thenReturn(
lenient().when(mockClient.headObject(any(Consumer.class))).thenReturn(
CompletableFuture.supplyAsync(() -> HeadObjectResponse.builder().contentLength(100L).build()));
}

Expand Down Expand Up @@ -262,26 +265,26 @@ public void checkAccessWithoutException() throws IOException, ExecutionException
provider.checkAccess(mockClient, foo);
}

@Test(expected = AccessDeniedException.class)
@Test
public void checkAccessWhenAccessDenied() throws IOException, ExecutionException, InterruptedException {
when(mockClient.headObject(any(Consumer.class))).thenReturn(CompletableFuture.supplyAsync(() ->
HeadObjectResponse.builder()
.sdkHttpResponse(SdkHttpResponse.builder().statusCode(403).build())
.build()));

S3Path foo = fs.getPath("/foo");
provider.checkAccess(mockClient, foo);
assertThrows(AccessDeniedException.class, () -> provider.checkAccess(mockClient, foo));
}

@Test(expected = NoSuchFileException.class)
@Test
public void checkAccessWhenNoSuchFile() throws IOException, ExecutionException, InterruptedException {
when(mockClient.headObject(any(Consumer.class))).thenReturn(CompletableFuture.supplyAsync(() ->
HeadObjectResponse.builder()
.sdkHttpResponse(SdkHttpResponse.builder().statusCode(404).build())
.build()));

S3Path foo = fs.getPath("/foo");
provider.checkAccess(mockClient, foo);
assertThrows(NoSuchFileException.class, () -> provider.checkAccess(mockClient, foo));
}

@Test
Expand All @@ -304,10 +307,10 @@ public void getFileAttributeView() {
assertNotNull(fileAttributeView1);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void getFileAttributeViewIllegalArg() {
S3Path foo = fs.getPath("/foo");
final FileAttributeView fileAttributeView = provider.getFileAttributeView(foo, FileAttributeView.class);
assertThrows(IllegalArgumentException.class, () -> provider.getFileAttributeView(foo, FileAttributeView.class));
}

@Test
Expand Down Expand Up @@ -344,9 +347,9 @@ public void testReadAttributes() {
assertEquals(Collections.emptyMap(), provider.readAttributes(mockClient, fooDir, "*"));
}

@Test(expected = UnsupportedOperationException.class)
@Test
public void setAttribute() {
S3Path foo = fs.getPath("/foo");
provider.setAttribute(foo, "x", "y");
assertThrows(UnsupportedOperationException.class, () -> provider.setAttribute(foo, "x", "y"));
}
}
33 changes: 19 additions & 14 deletions src/test/java/software/amazon/nio/spi/s3/S3FileSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@

package software.amazon.nio.spi.s3;

import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Iterator;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import software.amazon.awssdk.services.s3.S3Client;

@ExtendWith(MockitoExtension.class)
public class S3FileSystemTest {
S3FileSystemProvider provider;
URI s3Uri = URI.create("s3://mybucket/some/path/to/object.txt");
S3FileSystem s3FileSystem;

@Before
@Mock
S3Client mockClient; //client used to determine bucket location

@BeforeEach
public void init() {
this.provider = new S3FileSystemProvider();
s3FileSystem = (S3FileSystem) this.provider.newFileSystem(s3Uri, Collections.emptyMap());
Expand All @@ -42,12 +46,12 @@ public void getSeparator() {
public void close() throws IOException {
assertEquals(0, s3FileSystem.getOpenChannels().size());
s3FileSystem.close();
assertFalse("File system should return false from isOpen when closed has been called", s3FileSystem.isOpen());
assertFalse(s3FileSystem.isOpen(), "File system should return false from isOpen when closed has been called");
}

@Test
public void isOpen() {
assertTrue("File system should be open when newly created", s3FileSystem.isOpen());
assertTrue(s3FileSystem.isOpen(), "File system should be open when newly created");
}

@Test
Expand Down Expand Up @@ -94,10 +98,11 @@ public void getPathMatcher() {
s3FileSystem.getPathMatcher("glob:*.*").getClass());
}


@Test(expected = UnsupportedOperationException.class)
//thrown because cannot be modified
@Test
public void testGetOpenChannelsIsNotModifiable() {
s3FileSystem.getOpenChannels().add(null);
//
// thrown because cannot be modified
//
assertThrows(UnsupportedOperationException.class, () -> s3FileSystem.getOpenChannels().add(null));
}
}
Loading

0 comments on commit c2016c3

Please sign in to comment.