Skip to content

Commit

Permalink
Use assertThrows in tests
Browse files Browse the repository at this point in the history
Removed one test that was not actually going into the exception
verification prior to this change.
  • Loading branch information
akurtakov committed Dec 13, 2023
1 parent 86be08f commit 4c7749b
Show file tree
Hide file tree
Showing 20 changed files with 247 additions and 441 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -527,18 +528,15 @@ public void testBreeCompilerTargetCompatibilityIsChecked() throws Exception {
List<MavenProject> projects = getSortedProjects(basedir);

MavenProject project = projects.get(0);
try {
getMojo(projects, project).execute();
fail();
} catch (MojoExecutionException e) {
// assert that the compiler mojo checks the target levels of all BREEs (and not just the first or "minimal" one)
assertThat(e.getMessage(), containsString(
"The effective compiler target level 1.5 is incompatible with the following OSGi execution environments"));
assertThat(e.getMessage(), containsString("J2SE-1.2"));
assertThat(e.getMessage(), containsString("CDC-1.0/Foundation-1.0"));
assertThat(e.getMessage(), containsString("OSGi/Minimum-1.2"));
assertThat(e.getMessage(), not(containsString("JavaSE-1.6")));
}
MojoExecutionException e = assertThrows(MojoExecutionException.class,
() -> getMojo(projects, project).execute());
// assert that the compiler mojo checks the target levels of all BREEs (and not just the first or "minimal" one)
assertThat(e.getMessage(), containsString(
"The effective compiler target level 1.5 is incompatible with the following OSGi execution environments"));
assertThat(e.getMessage(), containsString("J2SE-1.2"));
assertThat(e.getMessage(), containsString("CDC-1.0/Foundation-1.0"));
assertThat(e.getMessage(), containsString("OSGi/Minimum-1.2"));
assertThat(e.getMessage(), not(containsString("JavaSE-1.6")));
}

public void test386210_compilerConfigurationCrosstalk() throws Exception {
Expand Down Expand Up @@ -587,13 +585,10 @@ public void testCompilerLogWithCustomComilerArgsAndLog() throws Exception {
File basedir = getBasedir("projects/logs/customCompilerArgsAndLog");
List<MavenProject> projects = getSortedProjects(basedir);
MavenProject project = projects.get(0);
try {
lookupConfiguredMojo(project, "compile").execute();
fail();
} catch (MojoFailureException e) {
assertThat(e.getMessage(), containsString("Compiler logging is configured by the 'log' compiler"
+ " plugin parameter and the custom compiler argument '-log'. Only either of them is allowed."));
}
MojoFailureException e = assertThrows(MojoFailureException.class,
() -> lookupConfiguredMojo(project, "compile").execute());
assertThat(e.getMessage(), containsString("Compiler logging is configured by the 'log' compiler"
+ " plugin parameter and the custom compiler argument '-log'. Only either of them is allowed."));
}

public void testJreCompilationProfile() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.eclipse.tycho.targetplatform.TargetPlatformFilter.CapabilityPattern.patternWithoutVersion;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;

import java.io.File;
import java.io.IOException;
Expand All @@ -35,9 +36,9 @@
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.tycho.core.test.utils.ResourceUtil;
import org.eclipse.tycho.targetplatform.TargetPlatformFilter;
import org.eclipse.tycho.targetplatform.TargetPlatformFilterSyntaxException;
import org.eclipse.tycho.targetplatform.TargetPlatformFilter.CapabilityType;
import org.eclipse.tycho.targetplatform.TargetPlatformFilter.FilterAction;
import org.eclipse.tycho.targetplatform.TargetPlatformFilterSyntaxException;
import org.eclipse.tycho.version.TychoVersion;

public class TargetPlatformFilterConfigurationReaderTest extends AbstractMojoTestCase {
Expand All @@ -51,30 +52,18 @@ protected void setUp() throws Exception {
}

public void testMissingTypeException() throws Exception {
try {
Xpp3Dom filterConfig = getTargetFilters("targetfilter/missing_scope_type/pom.xml");
subject.parseFilterConfiguration(filterConfig);
fail();
} catch (TargetPlatformFilterSyntaxException e) {
}
Xpp3Dom filterConfig = getTargetFilters("targetfilter/missing_scope_type/pom.xml");
assertThrows(TargetPlatformFilterSyntaxException.class, () -> subject.parseFilterConfiguration(filterConfig));
}

public void testMissingIdException() throws Exception {
try {
Xpp3Dom filterConfig = getTargetFilters("targetfilter/missing_scope_id/pom.xml");
subject.parseFilterConfiguration(filterConfig);
fail();
} catch (TargetPlatformFilterSyntaxException e) {
}
Xpp3Dom filterConfig = getTargetFilters("targetfilter/missing_scope_id/pom.xml");
assertThrows(TargetPlatformFilterSyntaxException.class, () -> subject.parseFilterConfiguration(filterConfig));
}

public void testMissingActionException() throws Exception {
try {
Xpp3Dom filterConfig = getTargetFilters("targetfilter/missing_action/pom.xml");
subject.parseFilterConfiguration(filterConfig);
fail();
} catch (TargetPlatformFilterSyntaxException e) {
}
Xpp3Dom filterConfig = getTargetFilters("targetfilter/missing_action/pom.xml");
assertThrows(TargetPlatformFilterSyntaxException.class, () -> subject.parseFilterConfiguration(filterConfig));
}

public void testValidRemoveAllFilters() throws Exception {
Expand All @@ -94,12 +83,8 @@ public void testValidRemoveAllFilters() throws Exception {
}

public void testDuplicateVersionException() throws Exception {
try {
Xpp3Dom filterConfig = getTargetFilters("targetfilter/duplicate_scope_version/pom.xml");
subject.parseFilterConfiguration(filterConfig);
fail();
} catch (TargetPlatformFilterSyntaxException e) {
}
Xpp3Dom filterConfig = getTargetFilters("targetfilter/duplicate_scope_version/pom.xml");
assertThrows(TargetPlatformFilterSyntaxException.class, () -> subject.parseFilterConfiguration(filterConfig));
}

public void testValidRestrictToFilters() throws Exception {
Expand Down Expand Up @@ -134,12 +119,8 @@ public void testValidRestrictToFilters() throws Exception {
}

public void testDuplicateActionException() throws Exception {
try {
Xpp3Dom filterConfig = getTargetFilters("targetfilter/duplicate_action/pom.xml");
subject.parseFilterConfiguration(filterConfig);
fail();
} catch (TargetPlatformFilterSyntaxException e) {
}
Xpp3Dom filterConfig = getTargetFilters("targetfilter/duplicate_action/pom.xml");
assertThrows(TargetPlatformFilterSyntaxException.class, () -> subject.parseFilterConfiguration(filterConfig));
}

private Xpp3Dom getTargetFilters(String pomFile) throws IOException, Exception, ProjectBuildingException {
Expand Down Expand Up @@ -184,8 +165,7 @@ Xpp3Dom getPluginConfiguration(MavenProject project, String artifactId) {
List<Plugin> plugins = project.getBuild().getPlugins();
for (Plugin plugin : plugins) {
if (artifactId.equals(plugin.getArtifactId())) {
Xpp3Dom config = (Xpp3Dom) plugin.getConfiguration();
return config;
return (Xpp3Dom) plugin.getConfiguration();
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import java.io.InputStream;
import java.net.URISyntaxException;
Expand All @@ -29,12 +29,10 @@ public void testInvalidManifest() throws OsgiManifestParserException, URISyntaxE

@Test
public void testMissingSymbolicName() throws Exception {
try {
parseManifest("noBsn.mf");
fail();
} catch (OsgiManifestParserException e) {
assertThat(e.getMessage(), containsString("Bundle-SymbolicName header is required"));
}
OsgiManifestParserException e = assertThrows(OsgiManifestParserException.class,
() -> parseManifest("noBsn.mf"));
assertThat(e.getMessage(), containsString("Bundle-SymbolicName header is required"));

}

@Test
Expand All @@ -44,34 +42,25 @@ public void testMissingVersion() throws Exception {

@Test
public void testInvalidVersion() throws Exception {
try {
parseManifest("invalidVersion.mf");
fail();
} catch (OsgiManifestParserException e) {
assertThat(e.getMessage(),
containsString("Invalid Manifest header \"Bundle-Version\": 1.0.0.%invalidQualifier"));
}
OsgiManifestParserException e = assertThrows(OsgiManifestParserException.class,
() -> parseManifest("invalidVersion.mf"));
assertThat(e.getMessage(),
containsString("Invalid Manifest header \"Bundle-Version\": 1.0.0.%invalidQualifier"));
}

@Test
public void testDuplicateImport() throws Exception {
try {
parseManifest("duplicateImport.mf");
fail();
} catch (OsgiManifestParserException e) {
assertThat(e.getMessage(), containsString(
"Invalid manifest header Import-Package: \"org.w3c.dom\" : Cannot import a package more than once \"org.w3c.dom\""));
}
OsgiManifestParserException e = assertThrows(OsgiManifestParserException.class,
() -> parseManifest("duplicateImport.mf"));
assertThat(e.getMessage(), containsString(
"Invalid manifest header Import-Package: \"org.w3c.dom\" : Cannot import a package more than once \"org.w3c.dom\""));
}

@Test
public void testInvalidVersionQualifier() throws Exception {
try {
parseManifest("invalidVersionQualifier.mf");
fail();
} catch (OsgiManifestParserException e) {
assertThat(e.getMessage(), containsString("Invalid Manifest header \"Bundle-Version\""));
}
OsgiManifestParserException e = assertThrows(OsgiManifestParserException.class,
() -> parseManifest("invalidVersionQualifier.mf"));
assertThat(e.getMessage(), containsString("Invalid Manifest header \"Bundle-Version\""));
}

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

import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -47,97 +48,56 @@ protected void tearDown() throws Exception {
@Test
public void testExtraRequirementMissingVersionRange() throws Exception {
Xpp3Dom dom = createConfigurationDom("type", "id");
try {
configurationReader.readExtraRequirements(new TargetPlatformConfiguration(), dom);
fail();
} catch (BuildFailureException e) {
assertTrue(e.getMessage()
.contains("Element <versionRange> is missing in <extraRequirements><requirement> section."));
}
BuildFailureException e = assertThrows(BuildFailureException.class,
() -> configurationReader.readExtraRequirements(new TargetPlatformConfiguration(), dom));
assertTrue(e.getMessage()
.contains("Element <versionRange> is missing in <extraRequirements><requirement> section."));
}

@Test
public void testExtraRequirementMissingType() throws Exception {
Xpp3Dom dom = createConfigurationDom("id", "versionRange");
try {
configurationReader.readExtraRequirements(new TargetPlatformConfiguration(), dom);
fail();
} catch (BuildFailureException e) {
assertTrue(
e.getMessage().contains("Element <type> is missing in <extraRequirements><requirement> section."));
}
BuildFailureException e = assertThrows(BuildFailureException.class,
() -> configurationReader.readExtraRequirements(new TargetPlatformConfiguration(), dom));
assertTrue(e.getMessage().contains("Element <type> is missing in <extraRequirements><requirement> section."));
}

@Test
public void testExtraRequirementId() throws Exception {
Xpp3Dom dom = createConfigurationDom("type", "versionRange");
try {
configurationReader.readExtraRequirements(new TargetPlatformConfiguration(), dom);
fail();
} catch (BuildFailureException e) {
assertTrue(e.getMessage().contains("Element <id> is missing in <extraRequirements><requirement> section."));
}
}

@Test()
public void testAddTargetWithValidMissingTargetDefinition() {
Xpp3Dom dom = createGavConfiguration("myGroupId", "myArtifactId", "myVersion");
MavenSession session = setupMockSession();
TargetPlatformConfiguration configuration = new TargetPlatformConfiguration();
try {
configurationReader.addTargetArtifact(configuration, session, new MavenProject(), dom);
} catch (TargetPlatformConfigurationException e) {
assertMessageContains(e, "No target definition file(s) found in project");
}
}

private void assertMessageContains(Throwable throwable, String string) {
if (throwable == null) {
fail("Message " + string + " was not found in the exception stack!");
}
if (throwable.getMessage().contains(string)) {
return;
}
assertMessageContains(throwable.getCause(), string);
BuildFailureException e = assertThrows(BuildFailureException.class,
() -> configurationReader.readExtraRequirements(new TargetPlatformConfiguration(), dom));
assertTrue(e.getMessage().contains("Element <id> is missing in <extraRequirements><requirement> section."));
}

@Test
public void testAddTargetWithMissingVersionInTargetDefinition() throws MojoExecutionException {
Xpp3Dom dom = createGavConfiguration("myGroupId", "myArtifactId", null);
MavenSession session = setupMockSession();
TargetPlatformConfiguration configuration = new TargetPlatformConfiguration();
try {
configurationReader.addTargetArtifact(configuration, session, null, dom);
fail();
} catch (TargetPlatformConfigurationException e) {
assertTrue(e.getMessage().contains("The target artifact configuration is invalid"));
}
TargetPlatformConfigurationException e = assertThrows(TargetPlatformConfigurationException.class,
() -> configurationReader.addTargetArtifact(configuration, session, null, dom));
assertTrue(e.getMessage().contains("The target artifact configuration is invalid"));
}

@Test
public void testAddTargetWithMissingGroupInTargetDefinition() throws MojoExecutionException {
Xpp3Dom dom = createGavConfiguration(null, "myArtifactId", "myVersion");
MavenSession session = setupMockSession();
TargetPlatformConfiguration configuration = new TargetPlatformConfiguration();
try {
configurationReader.addTargetArtifact(configuration, session, null, dom);
fail();
} catch (TargetPlatformConfigurationException e) {
assertTrue(e.getMessage().contains("The target artifact configuration is invalid"));
}
TargetPlatformConfigurationException e = assertThrows(TargetPlatformConfigurationException.class,
() -> configurationReader.addTargetArtifact(configuration, session, null, dom));
assertTrue(e.getMessage().contains("The target artifact configuration is invalid"));
}

@Test
public void testAddTargetWithMissingArtifactIdInTargetDefinition() throws MojoExecutionException {
Xpp3Dom dom = createGavConfiguration("myGroupId", null, "myVersion");
MavenSession session = setupMockSession();
TargetPlatformConfiguration configuration = new TargetPlatformConfiguration();
try {
configurationReader.addTargetArtifact(configuration, session, null, dom);
fail();
} catch (TargetPlatformConfigurationException e) {
assertTrue(e.getMessage().contains("The target artifact configuration is invalid"));
}
TargetPlatformConfigurationException e = assertThrows(TargetPlatformConfigurationException.class,
() -> configurationReader.addTargetArtifact(configuration, session, null, dom));
assertTrue(e.getMessage().contains("The target artifact configuration is invalid"));
}

@Test
Expand All @@ -148,11 +108,7 @@ public void testOptionalResolution() throws MojoExecutionException {
opt.setValue("optional");
res.addChild(opt);
dom.addChild(res);
try {
configurationReader.readDependencyResolutionConfiguration(new TargetPlatformConfiguration(), dom, null);
} catch (BuildFailureException e) {
fail(e.getMessage());
}
configurationReader.readDependencyResolutionConfiguration(new TargetPlatformConfiguration(), dom, null);
}

private MavenSession setupMockSession() {
Expand Down
Loading

0 comments on commit 4c7749b

Please sign in to comment.