Skip to content

Commit

Permalink
Even more assertThrows usage
Browse files Browse the repository at this point in the history
Even some expected exceptions converted as a prep for JUnit 5.
  • Loading branch information
akurtakov committed Dec 14, 2023
1 parent 6732155 commit 3ec0f07
Show file tree
Hide file tree
Showing 20 changed files with 106 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
Expand Down Expand Up @@ -196,9 +197,10 @@ public void testCompilerTargetCompatibility() throws Exception {
}
}

@Test(expected = UnknownEnvironmentException.class)
@Test
public void testUnknownEnv() throws Throwable {
ExecutionEnvironmentUtils.getExecutionEnvironment("foo", null, null, new SilentLog());
assertThrows(UnknownEnvironmentException.class,
() -> ExecutionEnvironmentUtils.getExecutionEnvironment("foo", null, null, new SilentLog()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -54,9 +54,9 @@ public void testIsLocked() throws IOException {
assertFalse(isLocked(file));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testNegativeTimeout() throws IOException {
subject.lock(newTestFile(), -1L);
assertThrows(IllegalArgumentException.class, () -> subject.lock(newTestFile(), -1L));
}

@Test
Expand Down Expand Up @@ -87,24 +87,20 @@ private void lockAndRelease(File file) throws IOException {
@Test
public void testLockReentranceDifferentLocker() throws IOException {
final File testFile = newTestFile();
try (var locked = subject.lock(testFile)) {
subject.lock(testFile, 0L);
fail("lock already held by same VM but could be acquired a second time");
} catch (LockTimeoutException e) {
// expected
}
assertThrows("lock already held by same VM but could be acquired a second time", LockTimeoutException.class,
() -> {
subject.lock(testFile);
subject.lock(testFile, 0L);
});
}

@Test
public void testLockedByOtherProcess() throws Exception {
File testFile = newTestFile();
LockProcess lockProcess = new LockProcess(testFile, 200L);
lockProcess.lockFileInForkedProcess();
try (var locked = subject.lock(testFile, 0L)) {
fail("lock already held by other VM but could be acquired a second time");
} catch (LockTimeoutException e) {
// expected
}
assertThrows("lock already held by other VM but could be acquired a second time", LockTimeoutException.class,
() -> subject.lock(testFile, 0L));
lockProcess.cleanup();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public void testValidOSGiManifest() throws OsgiManifestParserException, URISynta
assertEquals(13, manifest.getHeaders().size());
}

@Test(expected = OsgiManifestParserException.class)
@Test
public void testInvalidManifest() throws OsgiManifestParserException, URISyntaxException {
parseManifest("invalid.mf");
assertThrows(OsgiManifestParserException.class, () -> parseManifest("invalid.mf"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.net.URI;
Expand Down Expand Up @@ -191,9 +192,9 @@ public void testGetArtifactWithNonRestartableSink() throws Exception {
assertThat(status.getMessage(), containsString("An error occurred while transferring artifact")); // original message from p2 as top-level status
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testGetArtifactToClosedSink() throws Exception {
subject.getArtifact(new NonStartableArtifactSink(), null);
assertThrows(IllegalArgumentException.class, () -> subject.getArtifact(new NonStartableArtifactSink(), null));
}

@Test
Expand Down Expand Up @@ -233,9 +234,10 @@ public void testGetRawArtifactWithNonRestartableSink() throws Exception {
assertTrue(status.isOK());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testGetRawArtifactToClosedSink() throws Exception {
subject.getRawArtifact(new NonStartableArtifactSink(), null);
assertThrows(IllegalArgumentException.class,
() -> subject.getRawArtifact(new NonStartableArtifactSink(), null));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.tycho.p2resolver;

import static org.junit.Assert.assertThrows;

import java.util.Map;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -78,14 +80,14 @@ public void testRepoWithAttachedArtifactsAndConfigurations() throws Exception {
assertMavenProperties(descriptor, "root.win32.win32.x86");
}

@Test(expected = ProvisionException.class)
@Test
public void testRepoWithoutMavenAdvice() throws Exception {
FeatureRootfileArtifactRepository subject = new FeatureRootfileArtifactRepository(createPublisherInfo(false),
tempFolder.newFolder("testrootfiles"));

IArtifactDescriptor artifactDescriptor = createArtifactDescriptor(PublisherHelper.BINARY_ARTIFACT_CLASSIFIER,
"org.eclipse.tycho.test.p2");
subject.getOutputStream(artifactDescriptor).close();
assertThrows(ProvisionException.class, () -> subject.getOutputStream(artifactDescriptor).close());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.eclipse.tycho.test.util.TestRepositoryContent.BUNDLE_A_KEY;
import static org.eclipse.tycho.test.util.TestRepositoryContent.REPO_BUNDLE_AB;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;

import java.io.File;
Expand Down Expand Up @@ -56,12 +57,12 @@ public void testGetArtifactFile() {
assertEquals(artifactInLocalRepo(BUNDLE_A_KEY, TestRepositoryContent.REPO2_BUNDLE_A, ".jar"), result);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testConstructionWithNonArtifactFileRepository() throws Exception {
IArtifactRepository repository = mock(IArtifactRepository.class); // i.e. not an IFileArtifactRepository

subject = new FileRepositoryArtifactProvider(loaderFor(repository), TRANSFER_POLICY);
subject.getArtifactFile(BUNDLE_A_KEY);
assertThrows(IllegalArgumentException.class, () -> subject.getArtifactFile(BUNDLE_A_KEY));
}

private static File artifactInLocalRepo(IArtifactKey key, URI localRepository, String extension) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.tycho.p2resolver;

import static org.junit.Assert.assertThrows;

import java.net.URI;

import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
Expand Down Expand Up @@ -46,9 +48,9 @@ protected LocalRepositoryP2Indices lookupLocalRepoIndices() {
};
}

@Test(expected = ProvisionException.class)
@Test
public void testCreate() throws ProvisionException {
subject.create(null, null, null, null);
assertThrows(ProvisionException.class, () -> subject.create(null, null, null, null));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.io.File;
Expand Down Expand Up @@ -116,9 +117,10 @@ public void testProfilePublishing() throws Exception {
assertTrue(unitsById(seeds).keySet().contains("config.a.jre.virgo"));
}

@Test(expected = FacadeException.class)
@Test
public void testValidateProfileFile() throws Exception {
((PublisherServiceImpl) subject).validateProfile(resourceFile("publishers/inconsistentname-1.0.profile"));
assertThrows(FacadeException.class, () -> ((PublisherServiceImpl) subject)
.validateProfile(resourceFile("publishers/inconsistentname-1.0.profile")));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
package org.eclipse.tycho.p2resolver;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import java.io.IOException;

Expand Down Expand Up @@ -50,15 +50,11 @@ public void testLoadingCompositeRepositoryWithMissingChildFailsByDefault() throw
* In Tycho, we want composite repositories to fail if they have missing children (and don't
* explicitly specify the "p2.atomic.composite.loading" property).
*/
try {
subject.getService(IArtifactRepositoryManager.class).loadRepository(
ResourceUtil.resourceFile("repositories/composite/missingChildAndAtomicUnset").toURI(),
new NullProgressMonitor());
fail("Exception was not thrown!");
} catch (ProvisionException e) {
assertEquals(ProvisionException.REPOSITORY_FAILED_READ, e.getStatus().getCode());
}

ProvisionException e = assertThrows(ProvisionException.class,
() -> subject.getService(IArtifactRepositoryManager.class).loadRepository(
ResourceUtil.resourceFile("repositories/composite/missingChildAndAtomicUnset").toURI(),
new NullProgressMonitor()));
assertEquals(ProvisionException.REPOSITORY_FAILED_READ, e.getStatus().getCode());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.tycho.p2resolver;

import static org.junit.Assert.assertThrows;

import java.io.File;
import java.net.URI;

Expand Down Expand Up @@ -56,10 +58,10 @@ protected void modifySession(MavenSession mavenSession) {
mavenSession.getRequest().setOffline(true);
}

@Test(expected = ProvisionException.class)
@Test
public void testOfflineLoadingWithoutCacheFails() throws Exception {
IProvisioningAgent offlineAgent = newOfflineAgent();
loadHttpRepository(offlineAgent);
assertThrows(ProvisionException.class, () -> loadHttpRepository(offlineAgent));
}

private IProvisioningAgent newOfflineAgent() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,22 @@
*******************************************************************************/
package org.eclipse.tycho.test.limitations;

import static org.junit.Assert.assertThrows;

import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
import org.eclipse.tycho.test.AbstractTychoIntegrationTest;
import org.junit.Assert;
import org.junit.Test;

public class NonUniqueBasedirsTest extends AbstractTychoIntegrationTest {

@Test
public void testNonUniqueBasedirFailure() throws Exception {
Verifier verifier = getVerifier("limitations.uniqueBaseDirs", false);
try {
verifier.executeGoal("clean");
Assert.fail("build failure expected");
} catch (VerificationException e) {
// expected
}
@Test
public void testNonUniqueBasedirFailure() throws Exception {
Verifier verifier = getVerifier("limitations.uniqueBaseDirs", false);
assertThrows(VerificationException.class, () -> verifier.executeGoal("clean"));

// expect a clear error message -> requested in bug 366967
verifier.verifyTextInLog("Multiple modules within the same basedir are not supported");
}
// expect a clear error message -> requested in bug 366967
verifier.verifyTextInLog("Multiple modules within the same basedir are not supported");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*******************************************************************************/
package org.eclipse.tycho.test.p2Repository;

import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -61,12 +62,9 @@ public void testDownloadOnlyOnce() throws Exception {
verifier.executeGoals(List.of("clean", "install"));
verifier.verifyErrorFreeLog();
for (String bundle : bundles) {
try {
verifier.verifyTextInLog("Writing P2 metadata for osgi.bundle," + bundle);
fail(bundle + " is fetched twice!");
} catch (VerificationException e) {
assertTrue(e.getMessage().contains("Text not found"));
}
VerificationException e = assertThrows(bundle + " is fetched twice!", VerificationException.class,
() -> verifier.verifyTextInLog("Writing P2 metadata for osgi.bundle," + bundle));
assertTrue(e.getMessage().contains("Text not found"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
package org.eclipse.tycho.test.p2Repository;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import java.io.File;
import java.util.Arrays;
Expand All @@ -30,15 +30,10 @@ public class P2RepositoryValidateTest extends AbstractTychoIntegrationTest {
public void testValidate() throws Exception {
Verifier verifier = getVerifier("p2Repository.unresolvableIU", false);
verifier.addCliOption("-Dtest-data-repo=" + P2Repositories.ECLIPSE_352.toString());
try {
// validate is not always enough here as only in the prepare-package there is
// the first mojo that requires classpath resolving and we want to delay it
// until there...
verifier.executeGoal("prepare-package");
fail("Expected build failure");
} catch (VerificationException ex) {
// expected
}
// validate is not always enough here as only in the prepare-package there is
// the first mojo that requires classpath resolving and we want to delay it
// until there...
assertThrows(VerificationException.class, () -> verifier.executeGoal("prepare-package"));
verifier.verifyTextInLog("non.existing.iu");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.eclipse.tycho.test.packaging;

import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.io.File;
Expand Down Expand Up @@ -97,11 +98,7 @@ public void testCorruptedBaselineRepo() throws Exception {
File corruptedBaselineRepo = new File("projects/packaging.reproducibleArtifacts/baseline/repository_corrupted")
.getCanonicalFile();
Verifier verifier = getVerifier("baseline/src", corruptedBaselineRepo);
try {
verifier.executeGoals(List.of("clean", "package"));
Assert.fail("should not reach here");
} catch (VerificationException expected) {
}
assertThrows(VerificationException.class, () -> verifier.executeGoals(List.of("clean", "package")));
File locallyBuiltJar = new File(verifier.getBasedir(), "bundle01/target/baseline.bundle01-1.0.0-SNAPSHOT.jar");
assertTrue(locallyBuiltJar.isFile());
// locally built jar must not be replaced with corrupted 0-byte baseline jar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*******************************************************************************/
package org.eclipse.tycho.test.product;

import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
Expand All @@ -25,11 +25,8 @@ public void testInvalidProductFile() throws Exception {
verifier.addCliOption("-Dtest-data-repo=" + P2Repositories.ECLIPSE_342.toString());

// run build and verify we get a proper error message instead of an NPE
try {
verifier.executeGoal("package");
fail("We expect to fail on malformed product definitions");
} catch (VerificationException e) {
verifier.verifyTextInLog("The product file invalid.product does not contain the mandatory attribute");
}
assertThrows("We expect to fail on malformed product definitions", VerificationException.class,
() -> verifier.executeGoal("package"));
verifier.verifyTextInLog("The product file invalid.product does not contain the mandatory attribute");
}
}
Loading

0 comments on commit 3ec0f07

Please sign in to comment.