Skip to content

Commit

Permalink
another fail() that should be an expected exception;
Browse files Browse the repository at this point in the history
import assertions statically, too
  • Loading branch information
Gintas Grigelionis committed Dec 7, 2017
1 parent e43138b commit 1b84f2e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.ivy.core.settings.IvySettings;
import org.apache.ivy.plugins.version.MavenTimedSnapshotVersionMatcher;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -75,7 +74,7 @@ public void tearDown() {
@Test
public void testSnapshotResolution() throws Exception {
final IvySettings settings = this.ivy.getSettings();
Assert.assertNotNull("Maven timestamped snapshot revision version matcher is absent",
assertNotNull("Maven timestamped snapshot revision version matcher is absent",
settings.getVersionMatcher(new MavenTimedSnapshotVersionMatcher().getName()));
final ResolveOptions resolveOptions = new ResolveOptions();
resolveOptions.setConfs(new String[]{"default"});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
package org.apache.ivy.plugins.version;

import org.apache.ivy.core.module.id.ModuleRevisionId;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
* Tests {@link MavenTimedSnapshotVersionMatcher}
*/
Expand All @@ -34,11 +38,11 @@ public class MavenTimedSnapshotVersionMatcherTest {
public void testIsDynamic() {
final MavenTimedSnapshotVersionMatcher versionMatcher = new MavenTimedSnapshotVersionMatcher();
final ModuleRevisionId regularSnapshot = ModuleRevisionId.newInstance("org.apache", "ant-ivy", "1.0.2-SNAPSHOT");
Assert.assertFalse(regularSnapshot + " wasn't expected to be a dynamic revision", versionMatcher.isDynamic(regularSnapshot));
assertFalse(regularSnapshot + " wasn't expected to be a dynamic revision", versionMatcher.isDynamic(regularSnapshot));
final ModuleRevisionId timestampedSnapshot = ModuleRevisionId.newInstance("org.apache", "ant-ivy", "1.0.2-20100925.223013-19");
Assert.assertTrue(timestampedSnapshot + " was expected to be a dynamic revision", versionMatcher.isDynamic(timestampedSnapshot));
assertTrue(timestampedSnapshot + " was expected to be a dynamic revision", versionMatcher.isDynamic(timestampedSnapshot));
final ModuleRevisionId exactRevision = ModuleRevisionId.newInstance("org.apache", "ant-ivy", "2.4.0");
Assert.assertFalse(exactRevision + " wasn't expected to be a dynamic revision", versionMatcher.isDynamic(exactRevision));
assertFalse(exactRevision + " wasn't expected to be a dynamic revision", versionMatcher.isDynamic(exactRevision));

}

Expand All @@ -47,22 +51,22 @@ public void testIsDynamic() {
*/
@Test
public void testSnapshotParsing() {
Assert.assertNull("Revision wasn't expected to be a snapshot",
assertNull("Revision wasn't expected to be a snapshot",
MavenTimedSnapshotVersionMatcher.computeIfSnapshot("1.9.9"));

final String regularSnapshot = "1.9.9-SNAPSHOT";
final MavenTimedSnapshotVersionMatcher.MavenSnapshotRevision snapshotRevision = MavenTimedSnapshotVersionMatcher.computeIfSnapshot(regularSnapshot);
Assert.assertNotNull(regularSnapshot + " was expected to be a snapshot", snapshotRevision);
Assert.assertFalse(regularSnapshot + " wasn't expected to be a timestamped snapshot",
assertNotNull(regularSnapshot + " was expected to be a snapshot", snapshotRevision);
assertFalse(regularSnapshot + " wasn't expected to be a timestamped snapshot",
snapshotRevision.isTimestampedSnapshot());

final String timestampedRev = "21.03.22-20150925.223013-232";
final MavenTimedSnapshotVersionMatcher.MavenSnapshotRevision timestampedSnapshot = MavenTimedSnapshotVersionMatcher.computeIfSnapshot(timestampedRev);
Assert.assertNotNull(timestampedRev + " was expected to be a snapshot", timestampedSnapshot);
Assert.assertTrue(timestampedRev + " was expected to be a timestamped snapshot", timestampedSnapshot.isTimestampedSnapshot());
assertNotNull(timestampedRev + " was expected to be a snapshot", timestampedSnapshot);
assertTrue(timestampedRev + " was expected to be a timestamped snapshot", timestampedSnapshot.isTimestampedSnapshot());

final String exactRevision = "21.2.2-a20140204.232421-2";
Assert.assertNull(exactRevision + " wasn't expected to be a snapshot",
assertNull(exactRevision + " wasn't expected to be a snapshot",
MavenTimedSnapshotVersionMatcher.computeIfSnapshot(exactRevision));
}
}
46 changes: 27 additions & 19 deletions test/java/org/apache/ivy/util/url/HttpclientURLHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
import org.apache.ivy.util.FileUtil;
import org.apache.ivy.util.url.URLHandler.URLInfo;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.File;
import java.io.IOException;
Expand All @@ -37,9 +38,11 @@
import java.util.Random;

import static org.apache.ivy.plugins.resolver.IBiblioResolver.DEFAULT_M2_ROOT;
import static org.hamcrest.Matchers.endsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* Test {@link HttpClientHandler}
Expand All @@ -57,6 +60,9 @@ public class HttpclientURLHandlerTest {
((NamedTimeoutConstraint) defaultTimeoutConstraint).setConnectionTimeout(5000);
}

@Rule
public ExpectedException expExc = ExpectedException.none();

@Before
public void setUp() {
testDir = new File("build/HttpclientURLHandlerTest");
Expand All @@ -77,8 +83,10 @@ public void tearDown() {

@Test
public void testIsReachable() throws Exception {
assertTrue("URL resource was expected to be reachable", handler.isReachable(new URL("http://www.google.fr/"), defaultTimeoutConstraint));
assertFalse("URL resource was expected to be unreachable", handler.isReachable(new URL("http://www.google.fr/unknownpage.html"), defaultTimeoutConstraint));
assertTrue("URL resource was expected to be reachable",
handler.isReachable(new URL("http://www.google.fr/"), defaultTimeoutConstraint));
assertFalse("URL resource was expected to be unreachable",
handler.isReachable(new URL("http://www.google.fr/unknownpage.html"), defaultTimeoutConstraint));
}

/**
Expand All @@ -91,8 +99,9 @@ public void testIsReachable() throws Exception {
@Test
public void testGetURLInfo() throws Exception {
URLHandler handler = new HttpClientHandler();
assertTrue("Default Maven URL must end with '/'", DEFAULT_M2_ROOT.endsWith("/"));
URLInfo info = handler.getURLInfo(new URL(DEFAULT_M2_ROOT
+ "/commons-lang/commons-lang/[1.0,3.0[/commons-lang-[1.0,3.0[.pom"), defaultTimeoutConstraint);
+ "commons-lang/commons-lang/[1.0,3.0[/commons-lang-[1.0,3.0[.pom"), defaultTimeoutConstraint);

assertEquals(URLHandler.UNAVAILABLE, info);
}
Expand Down Expand Up @@ -126,6 +135,13 @@ public void testContentEncoding() throws Exception {
*/
@Test
public void testCredentials() throws Exception {
// we catch it and check for presence of 401 in the exception message.
// It's not exactly an contract that the IOException will have the 401 message
// but for now that's how it's implemented and it's fine to check for the presence
// of that message at the moment
expExc.expect(IOException.class);
expExc.expectMessage(endsWith("ivysettings.xml' 401 - 'Unauthorized"));

final CredentialsStore credentialsStore = CredentialsStore.INSTANCE;
final String realm = "test-http-client-handler-realm";
final String host = "localhost";
Expand All @@ -138,8 +154,8 @@ public void testCredentials() throws Exception {
final Path repoRoot = new File("test/repositories").toPath();
assertTrue(repoRoot + " is not a directory", Files.isDirectory(repoRoot));
// create a server backed by BASIC auth with the set of "allowed" credentials
try (final AutoCloseable server = TestHelper.createBasicAuthHttpServerBackedRepo(serverBindAddr, contextRoot,
repoRoot, realm, Collections.singletonMap(userName, password))) {
try (final AutoCloseable server = TestHelper.createBasicAuthHttpServerBackedRepo(serverBindAddr,
contextRoot, repoRoot, realm, Collections.singletonMap(userName, password))) {

final File target = new File(testDir, "downloaded.xml");
assertFalse("File " + target + " already exists", target.exists());
Expand All @@ -152,30 +168,22 @@ public void testCredentials() throws Exception {
// now create a server backed by BASIC auth with a set of credentials that do *not* match
// with what the Ivy credentials store will return for a given realm+host combination, i.e.
// Ivy credential store will return back invalid credentials and the server will reject them
try (final AutoCloseable server = TestHelper.createBasicAuthHttpServerBackedRepo(serverBindAddr, contextRoot,
repoRoot, realm, Collections.singletonMap("other-" + userName, "other-" + password))) {
try (final AutoCloseable server = TestHelper.createBasicAuthHttpServerBackedRepo(serverBindAddr,
contextRoot, repoRoot, realm, Collections.singletonMap("other-" + userName, "other-" + password))) {

final File target = new File(testDir, "should-not-have-been-downloaded.xml");
assertFalse("File " + target + " already exists", target.exists());
final URL src = new URL("http://localhost:" + serverBindAddr.getPort() + "/"
+ contextRoot + "/ivysettings.xml");
// download it (expected to fail)
try {
handler.download(src, target, null, defaultTimeoutConstraint);
Assert.fail("Download from " + src + " was expected to fail due to invalid credentials");
} catch (IOException ioe) {
// we catch it and check for presence of 401 in the exception message.
// It's not exactly an contract that the IOException will have the 401 message
// but for now that's how it's implemented and it's fine to check for the presence
// of that message at the moment
assertTrue("Expected to find 401 error message in exception", ioe.getMessage().contains("401"));
}
handler.download(src, target, null, defaultTimeoutConstraint);
}
}

private void assertDownloadOK(final URL url, final File file) throws Exception {
handler.download(url, file, null, defaultTimeoutConstraint);
assertTrue("Content from " + url + " wasn't downloaded to " + file, file.exists());
assertTrue("Unexpected content at " + file + " for resource that was downloaded from " + url, file.isFile() && file.length() > 0);
assertTrue("Unexpected content at " + file + " for resource that was downloaded from "
+ url, file.isFile() && file.length() > 0);
}
}

0 comments on commit 1b84f2e

Please sign in to comment.