Skip to content
This repository has been archived by the owner on Apr 5, 2019. It is now read-only.

Commit

Permalink
replace assertions with assertj
Browse files Browse the repository at this point in the history
  • Loading branch information
klieber committed Mar 23, 2017
1 parent 86b8b25 commit d889133
Show file tree
Hide file tree
Showing 25 changed files with 146 additions and 209 deletions.
Expand Up @@ -28,9 +28,9 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.github.klieber.phantomjs.archive.mapping.ArchiveSpec;
import com.github.klieber.phantomjs.archive.mapping.ArchiveMapping;
import com.github.klieber.phantomjs.archive.mapping.ArchiveMappings;
import com.github.klieber.phantomjs.archive.mapping.ArchiveSpec;
import com.github.klieber.phantomjs.os.OperatingSystem;
import com.github.klieber.phantomjs.util.VersionUtil;
import org.slf4j.Logger;
Expand Down
Expand Up @@ -25,8 +25,6 @@
*/
package com.github.klieber.phantomjs.locate;

import com.github.klieber.phantomjs.os.OperatingSystem;
import com.github.klieber.phantomjs.os.OperatingSystemFactory;
import com.github.klieber.phantomjs.archive.PhantomJSArchive;
import com.github.klieber.phantomjs.archive.PhantomJSArchiveBuilder;
import com.github.klieber.phantomjs.cache.CachedArtifact;
Expand All @@ -39,6 +37,8 @@
import com.github.klieber.phantomjs.extract.PhantomJsExtractor;
import com.github.klieber.phantomjs.install.Installer;
import com.github.klieber.phantomjs.install.PhantomJsInstaller;
import com.github.klieber.phantomjs.os.OperatingSystem;
import com.github.klieber.phantomjs.os.OperatingSystemFactory;
import com.github.klieber.phantomjs.resolve.PhantomJsBinaryResolver;
import com.github.klieber.phantomjs.util.ArtifactBuilder;
import com.github.klieber.phantomjs.util.Predicate;
Expand Down
Expand Up @@ -33,11 +33,7 @@

import static com.googlecode.catchexception.CatchException.catchException;
import static com.googlecode.catchexception.CatchException.caughtException;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -53,23 +49,23 @@ public void testBuildWindowsPhantomJSArchive() {
when(operatingSystem.getName()).thenReturn("win");
builder = new PhantomJSArchiveBuilder(operatingSystem, "1.9.0");
PhantomJSArchive archive = builder.build();
assertEquals("phantomjs-1.9.0-windows.zip", archive.getArchiveName());
assertThat(archive.getArchiveName()).isEqualTo("phantomjs-1.9.0-windows.zip");
}

@Test
public void testBuildMacOSXPhantomJSArchive() {
when(operatingSystem.getName()).thenReturn("mac");
builder = new PhantomJSArchiveBuilder(operatingSystem, "1.9.0");
PhantomJSArchive archive = builder.build();
assertEquals("phantomjs-1.9.0-macosx.zip",archive.getArchiveName());
assertThat(archive.getArchiveName()).isEqualTo("phantomjs-1.9.0-macosx.zip");
}

@Test
public void testBuildMacOSX250PhantomJSArchive() {
when(operatingSystem.getName()).thenReturn("mac");
builder = new PhantomJSArchiveBuilder(operatingSystem, "2.5.0-beta");
PhantomJSArchive archive = builder.build();
assertEquals("phantomjs-2.5.0-beta-macos.zip",archive.getArchiveName());
assertThat(archive.getArchiveName()).isEqualTo("phantomjs-2.5.0-beta-macos.zip");
}

@Test
Expand All @@ -78,7 +74,7 @@ public void testBuildLinuxPhantomJSArchive() {
when(operatingSystem.getArchitecture()).thenReturn("i686");
builder = new PhantomJSArchiveBuilder(operatingSystem, "1.9.0");
PhantomJSArchive archive = builder.build();
assertEquals("phantomjs-1.9.0-linux-i686.tar.bz2", archive.getArchiveName());
assertThat(archive.getArchiveName()).isEqualTo("phantomjs-1.9.0-linux-i686.tar.bz2");
}

@Test
Expand All @@ -87,14 +83,14 @@ public void testBuildLinuxPhantomJSArchiveX64() {
when(operatingSystem.getArchitecture()).thenReturn("x86_64");
builder = new PhantomJSArchiveBuilder(operatingSystem, "1.9.2");
PhantomJSArchive archive = builder.build();
assertEquals("phantomjs-1.9.2-linux-x86_64.tar.bz2", archive.getArchiveName());
assertThat(archive.getArchiveName()).isEqualTo("phantomjs-1.9.2-linux-x86_64.tar.bz2");
}

@Test
public void testBuildInvalidPlatform() {
when(operatingSystem.getName()).thenReturn("invalid");
builder = new PhantomJSArchiveBuilder(operatingSystem, "");
catchException(builder).build();
assertThat(caughtException(), is(instanceOf(UnsupportedPlatformException.class)));
assertThat(caughtException()).isInstanceOf(UnsupportedPlatformException.class);
}
}
Expand Up @@ -32,7 +32,7 @@
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -61,33 +61,33 @@ public void before() {
@Test
public void testGetExtension() {
when(archiveFormat.getExtension()).thenReturn(EXTENSION);
assertEquals(EXTENSION, archive.getExtension());
assertThat(archive.getExtension()).isEqualTo(EXTENSION);
}

@Test
public void testGetArchiveName() {
when(archiveFormat.getFileTemplate()).thenReturn(ARCHIVE_FILE_TEMPLATE);
when(archiveFormat.getClassifier()).thenReturn(CLASSIFIER);
when(archiveFormat.getExtension()).thenReturn(EXTENSION);
assertEquals(ARCHIVE_NAME, archive.getArchiveName());
assertThat(archive.getArchiveName()).isEqualTo(ARCHIVE_NAME);
}

@Test
public void testGetPathToExecutable() {
when(archiveFormat.getExecutableTemplate()).thenReturn(EXECUTABLE_TEMPLATE);
when(archiveFormat.getClassifier()).thenReturn(CLASSIFIER);
when(archiveFormat.getExtension()).thenReturn(EXTENSION);
assertEquals(PATH_TO_EXECUTABLE, archive.getPathToExecutable());
assertThat(archive.getPathToExecutable()).isEqualTo(PATH_TO_EXECUTABLE);
}

@Test
public void testGetVersion() {
assertEquals(VERSION,archive.getVersion());
assertThat(archive.getVersion()).isEqualTo(VERSION);
}

@Test
public void testGetClassifier() {
when(archiveFormat.getClassifier()).thenReturn(CLASSIFIER);
assertEquals(CLASSIFIER,archive.getClassifier());
assertThat(archive.getClassifier()).isEqualTo(CLASSIFIER);
}
}
Expand Up @@ -40,7 +40,7 @@

import java.io.File;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
Expand Down Expand Up @@ -87,6 +87,7 @@ public void testGetFile() throws Exception {
when(localRepositoryManager.getPathForLocalArtifact(artifact)).thenReturn(ARTIFACT_PATH);
when(localRepository.getBasedir()).thenReturn(basedir);

assertEquals(new File(REPOSITORY_PATH, ARTIFACT_PATH).getPath(), cachedArtifact.getFile().getAbsolutePath());
assertThat(cachedArtifact.getFile().getAbsolutePath())
.isEqualTo(new File(REPOSITORY_PATH, ARTIFACT_PATH).getPath());
}
}
Expand Up @@ -51,10 +51,7 @@

import static com.googlecode.catchexception.CatchException.catchException;
import static com.googlecode.catchexception.CatchException.caughtException;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.same;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -105,23 +102,27 @@ public void before() {
@Test
public void shouldDownload() throws DownloadException, ArtifactResolutionException {
when(artifactBuilder.createArtifact(phantomJSArchive)).thenReturn(artifact);
when(repositorySystem.resolveArtifact(same(repositorySystemSession), artifactRequestCaptor.capture())).thenReturn(artifactResult);
when(repositorySystem.resolveArtifact(same(repositorySystemSession), artifactRequestCaptor.capture())).thenReturn(
artifactResult);
when(artifactResult.getArtifact()).thenReturn(artifact);
when(artifact.getFile()).thenReturn(archiveFile);

assertSame(archiveFile, repositoryDownloader.download(phantomJSArchive));
assertThat(repositoryDownloader.download(phantomJSArchive)).isSameAs(archiveFile);

ArtifactRequest request = artifactRequestCaptor.getValue();
assertSame(artifact, request.getArtifact());
assertSame(remoteRepositories, request.getRepositories());
assertThat(request.getArtifact()).isSameAs(artifact);
assertThat(request.getRepositories()).isSameAs(remoteRepositories);
}

@Test
public void shouldHandleArtifactResolutionException() throws DownloadException, ArtifactResolutionException {
when(artifactBuilder.createArtifact(phantomJSArchive)).thenReturn(artifact);
when(repositorySystem.resolveArtifact(same(repositorySystemSession), artifactRequestCaptor.capture())).thenThrow(new ArtifactResolutionException(Collections.<ArtifactResult>emptyList()));
when(repositorySystem.resolveArtifact(
same(repositorySystemSession),
artifactRequestCaptor.capture()
)).thenThrow(new ArtifactResolutionException(Collections.<ArtifactResult>emptyList()));

catchException(repositoryDownloader).download(phantomJSArchive);
assertThat(caughtException(), is(instanceOf(DownloadException.class)));
assertThat(caughtException()).isInstanceOf(DownloadException.class);
}
}
Expand Up @@ -34,17 +34,12 @@
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;

import static com.googlecode.catchexception.CatchException.catchException;
import static com.googlecode.catchexception.CatchException.caughtException;
import static com.googlecode.catchexception.apis.CatchExceptionHamcrestMatchers.hasMessage;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand All @@ -71,9 +66,6 @@ public class RuleBasedDownloaderTest {
@Mock
private PhantomJSArchive phantomJsArchive;

@Mock
private File file;

private RuleBasedDownloader ruleBasedDownloader;

@Before
Expand All @@ -86,7 +78,7 @@ public void before() {
}

@Test
public void shouldUseDownloaderA() throws Exception {
public void shouldUseDownloaderA() throws Exception {
when(phantomJsArchive.getVersion()).thenReturn(VERSION);
when(predicateA.apply(VERSION)).thenReturn(true);
when(predicateB.apply(VERSION)).thenReturn(false);
Expand All @@ -98,7 +90,7 @@ public void shouldUseDownloaderA() throws Exception {
}

@Test
public void shouldUseDownloaderB() throws Exception {
public void shouldUseDownloaderB() throws Exception {
when(phantomJsArchive.getVersion()).thenReturn(VERSION);
when(predicateA.apply(VERSION)).thenReturn(false);
when(predicateB.apply(VERSION)).thenReturn(true);
Expand All @@ -110,7 +102,7 @@ public void shouldUseDownloaderB() throws Exception {
}

@Test
public void shouldUseDownloaderBAfterAFails() throws Exception {
public void shouldUseDownloaderBAfterAFails() throws Exception {
when(phantomJsArchive.getVersion()).thenReturn(VERSION);
when(predicateA.apply(VERSION)).thenReturn(true);
when(predicateB.apply(VERSION)).thenReturn(true);
Expand All @@ -124,35 +116,33 @@ public void shouldUseDownloaderBAfterAFails() throws Exception {
}

@Test
public void shouldFailDownloadDueToDownloaderAFailure() throws Exception {
public void shouldFailDownloadDueToDownloaderAFailure() throws Exception {
when(phantomJsArchive.getVersion()).thenReturn(VERSION);
when(predicateA.apply(VERSION)).thenReturn(true);
when(predicateB.apply(VERSION)).thenReturn(false);

doThrow(new DownloadException("DownloaderA Failed")).when(downloaderA).download(phantomJsArchive);

catchException(this.ruleBasedDownloader).download(phantomJsArchive);
assertThat(caughtException(), allOf(
is(instanceOf(DownloadException.class)),
hasMessage("DownloaderA Failed")
));
assertThat(caughtException())
.isInstanceOf(DownloadException.class)
.hasMessage("DownloaderA Failed");

verify(downloaderA).download(phantomJsArchive);
verifyNoMoreInteractions(downloaderB);
}

@Test
public void shouldFailDownloadDueToNoMatches() throws Exception {
public void shouldFailDownloadDueToNoMatches() throws Exception {
when(phantomJsArchive.getVersion()).thenReturn(VERSION);
when(predicateA.apply(VERSION)).thenReturn(false);
when(predicateB.apply(VERSION)).thenReturn(false);

catchException(this.ruleBasedDownloader).download(phantomJsArchive);

assertThat(caughtException(),allOf(
is(instanceOf(DownloadException.class)),
hasMessage("No matching Downloader found.")
));
assertThat(caughtException())
.isInstanceOf(DownloadException.class)
.hasMessage("No matching Downloader found.");

verifyNoMoreInteractions(downloaderA);
verifyNoMoreInteractions(downloaderB);
Expand Down
Expand Up @@ -40,9 +40,7 @@

import static com.googlecode.catchexception.CatchException.catchException;
import static com.googlecode.catchexception.CatchException.caughtException;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -90,7 +88,7 @@ public void shouldFailDueToEmptyFile() throws Exception {
when(file.length()).thenReturn(0L);

catchException(downloader).download(phantomJSArchive);
assertThat(caughtException(), is(instanceOf(DownloadException.class)));
assertThat(caughtException()).isInstanceOf(DownloadException.class);

verifyStatic();
FileUtils.copyURLToFile(any(URL.class), eq(file));
Expand All @@ -104,7 +102,7 @@ public void shouldFailDueToIOException() throws Exception {
FileUtils.copyURLToFile(any(URL.class), eq(file));

catchException(downloader).download(phantomJSArchive);
assertThat(caughtException(),is(instanceOf(DownloadException.class)));
assertThat(caughtException()).isInstanceOf(DownloadException.class);

verifyStatic();
FileUtils.copyURLToFile(any(URL.class), eq(file));
Expand All @@ -116,6 +114,6 @@ public void shouldFailDueToMalformedUrl() throws Exception {

downloader = new WebDownloader("invalid-base-url", file);
catchException(downloader).download(phantomJSArchive);
assertThat(caughtException(),is(instanceOf(DownloadException.class)));
assertThat(caughtException()).isInstanceOf(DownloadException.class);
}
}
Expand Up @@ -35,9 +35,7 @@

import static com.googlecode.catchexception.CatchException.catchException;
import static com.googlecode.catchexception.CatchException.caughtException;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -80,6 +78,6 @@ public void testCanHandleInterruption() throws Exception {
doThrow(InterruptedException.class).when(process).waitFor();

catchException(executor).execute(builder);
assertThat(caughtException(), is(instanceOf(ExecutionException.class)));
assertThat(caughtException()).isInstanceOf(ExecutionException.class);
}
}

0 comments on commit d889133

Please sign in to comment.