Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use assertThrows for checking exceptions #121

Merged
merged 1 commit into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -196,7 +197,7 @@ public void setupPluginsTest2() throws CmdLineException, IOException, URISyntaxE
assertEquals(requestedPluginInfo, cfgPluginInfo);
}

@Test(expected = PluginInputException.class)
@Test
public void setupPluginsBadExtension() throws CmdLineException, IOException, URISyntaxException {
File pluginTxtFile = new File(this.getClass().getResource("/plugins.t").toURI());

Expand All @@ -205,7 +206,7 @@ public void setupPluginsBadExtension() throws CmdLineException, IOException, URI

parser.parseArgument("--plugin-file", pluginFile.toString());

Config cfg = options.setup();
assertThrows(PluginInputException.class, options::setup);
}

@Test
Expand Down Expand Up @@ -309,14 +310,15 @@ public void showVersionTest() throws Exception {
assertEquals(version, aliasVersionOut.toString().trim());
}

@Test(expected = VersionNotFoundException.class)
@Test
public void showVersionErrorTest() throws CmdLineException {
ByteArrayOutputStream nullPropertiesOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(nullPropertiesOut));
CliOptions cliOptionsSpy = spy(options);
parser.parseArgument("--version");
doReturn(null).when(cliOptionsSpy).getPropertiesInputStream(any(String.class));
cliOptionsSpy.showVersion();

assertThrows(VersionNotFoundException.class, cliOptionsSpy::showVersion);
}

@Test
Expand Down Expand Up @@ -361,10 +363,11 @@ public void useNotLatestTest() throws CmdLineException {
assertEquals(false, cfg.isUseLatestAll());
}

@Test (expected = PluginDependencyStrategyException.class)
@Test
public void useLatestSpecifiedAndLatestAllTest() throws CmdLineException {
parser.parseArgument("--latest", "--latest-specified");
Config cfg = options.setup();

assertThrows(PluginDependencyStrategyException.class, options::setup);
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyMap;
Expand Down Expand Up @@ -131,7 +132,7 @@ public void startTest() throws IOException {
pluginManagerSpy.start();
}

@Test(expected = DirectoryCreationException.class)
@Test
public void startNoDirectoryTest() throws IOException {
File refDir = mock(File.class);

Expand All @@ -151,7 +152,7 @@ public void startNoDirectoryTest() throws IOException {
mockStatic(Files.class);
when(Files.createDirectories(refPath)).thenThrow(IOException.class);

pluginManager.start();
assertThrows(DirectoryCreationException.class, pluginManager::start);
}

@Test
Expand Down Expand Up @@ -581,8 +582,8 @@ public void checkVersionCompatibilityNullTest() {
assertEquals("", output.toString());
}

@Test(expected = VersionCompatibilityException.class)
public void checkVersionCompatibilityFailTest() throws IOException {
@Test
public void checkVersionCompatibilityFailTest() {
pm.setJenkinsVersion(new VersionNumber("1.609.3"));

Plugin plugin1 = new Plugin("plugin1", "1.0", null, null);
Expand All @@ -592,7 +593,10 @@ public void checkVersionCompatibilityFailTest() throws IOException {
plugin2.setJenkinsVersion("1.609.3");

List<Plugin> pluginsToDownload = new ArrayList<>(Arrays.asList(plugin1, plugin2));
pm.checkVersionCompatibility(pluginsToDownload);

assertThrows(
VersionCompatibilityException.class,
() -> pm.checkVersionCompatibility(pluginsToDownload));
}

@Test
Expand Down Expand Up @@ -684,7 +688,7 @@ public void downloadPluginsSuccessfulTest() throws IOException {
assertEquals(true, pluginManagerSpy.getFailedPlugins().isEmpty());
}

@Test(expected = DownloadPluginException.class)
@Test
public void downloadPluginsUnsuccessfulTest() throws IOException {
Config config = Config.builder()
.withJenkinsWar(Settings.DEFAULT_WAR)
Expand All @@ -699,7 +703,9 @@ public void downloadPluginsUnsuccessfulTest() throws IOException {
List<Plugin> plugins = singletonList(
new Plugin("plugin", "1.0", null, null));

pluginManagerSpy.downloadPlugins(plugins);
assertThrows(
DownloadPluginException.class,
() -> pluginManagerSpy.downloadPlugins(plugins));
}

@Test
Expand Down Expand Up @@ -793,19 +799,24 @@ public void outputPluginReplacementInfoTest() throws IOException {
assertEquals(expected, output.toString().trim());
}

@Test(expected = UpdateCenterInfoRetrievalException.class)
@Test
public void getJsonURLExceptionTest() {
pm.getJson("htttp://ftp-chi.osuosl.org/pub/jenkins/updates/current/update-center.json");
assertThrows(
UpdateCenterInfoRetrievalException.class,
() -> pm.getJson("htttp://ftp-chi.osuosl.org/pub/jenkins/updates/current/update-center.json"));
}

@Test(expected = UpdateCenterInfoRetrievalException.class)
@Test
public void getJsonURLIOTest() throws IOException{
mockStatic(IOUtils.class);

when(IOUtils.toString(any(URL.class), any(Charset.class))).thenThrow(IOException.class);

pm.setCm(new MockCacheManager());
pm.getJson(new URL("http://ftp-chi.osuosl.org/pub/jenkins/updates/current/update-center.json"), "update-center");

assertThrows(
UpdateCenterInfoRetrievalException.class,
() -> pm.getJson(new URL("http://ftp-chi.osuosl.org/pub/jenkins/updates/current/update-center.json"), "update-center"));
}

@Test
Expand Down Expand Up @@ -890,10 +901,13 @@ public void getPluginVersionTest() {
assertEquals("1.10", pm.getPluginVersion(testHpi));
}

@Test (expected = PluginNotFoundException.class)
@Test
public void getLatestPluginVersionExceptionTest() {
setTestUcJson();
pm.getLatestPluginVersion("git");

assertThrows(
PluginNotFoundException.class,
() -> pm.getLatestPluginVersion("git"));
}

@Test
Expand Down Expand Up @@ -989,7 +1003,7 @@ public void resolveDependenciesFromManifestExceptionTest() throws IOException {
assertEquals(true, pm.resolveDependenciesFromManifest(testPlugin).isEmpty());
}

@Test(expected = DownloadPluginException.class)
@Test
public void resolveDependenciesFromManifestNoDownload() throws IOException{
Config config = Config.builder()
.withJenkinsWar(Settings.DEFAULT_WAR)
Expand All @@ -1009,7 +1023,9 @@ public void resolveDependenciesFromManifestNoDownload() throws IOException{
when(Files.createTempFile(any(String.class), any(String.class))).thenReturn(tempPath);
when(tempPath.toFile()).thenReturn(tempFile);

assertEquals(true, pluginManager.resolveDependenciesFromManifest(testPlugin).isEmpty());
assertThrows(
DownloadPluginException.class,
() -> pluginManager.resolveDependenciesFromManifest(testPlugin));
}

@Test
Expand Down Expand Up @@ -1423,14 +1439,16 @@ public void getDownloadPluginUrlTestComplexUpdateCenterUrl() throws IOException
assertThat(result, is("https://jenkins-updates.cloudbees.com/download/plugins/the-plugin/1.0/the-plugin.hpi"));
}

@Test(expected = DownloadPluginException.class)
@Test
public void getAttributeFromManifestExceptionTest() throws Exception {
URL jpiURL = this.getClass().getResource("/delivery-pipeline-plugin.jpi");
File testJpi = new File(jpiURL.getFile());

whenNew(JarFile.class).withArguments(testJpi).thenThrow(new IOException());

pm.getAttributeFromManifest(testJpi, "Plugin-Dependencies");
assertThrows(
DownloadPluginException.class,
() -> pm.getAttributeFromManifest(testJpi, "Plugin-Dependencies"));
}

public void getAttributeFromManifestTest() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
Expand Down Expand Up @@ -126,22 +127,28 @@ public void parsePluginYamlFileTest() throws URISyntaxException {
assertEquals(expectedPluginInfo, pluginInfo);
}

@Test(expected = PluginInputException.class)
@Test
public void badFormatYamlNoArtifactIdTest() throws URISyntaxException {
File pluginYmlFile = new File(this.getClass().getResource("PluginListParserTest/badformat1.yaml").toURI());
List<Plugin> pluginsFromYamlFile = pluginList.parsePluginYamlFile(pluginYmlFile);
assertThrows(
PluginInputException.class,
() -> pluginList.parsePluginYamlFile(pluginYmlFile));
}

@Test(expected = PluginInputException.class)
@Test
public void badFormatYamlGroupIdNoVersion() throws URISyntaxException {
File pluginYmlFile = new File(this.getClass().getResource("PluginListParserTest/badformat2.yaml").toURI());
List<Plugin> pluginsFromYamlFile = pluginList.parsePluginYamlFile(pluginYmlFile);
assertThrows(
PluginInputException.class,
() -> pluginList.parsePluginYamlFile(pluginYmlFile));
}

@Test(expected = PluginInputException.class)
@Test
public void badFormatYamlGroupIdNoVersion2() throws URISyntaxException {
File pluginYmlFile = new File(this.getClass().getResource("PluginListParserTest/badformat3.yaml").toURI());
List<Plugin> pluginsFromYamlFile = pluginList.parsePluginYamlFile(pluginYmlFile);
assertThrows(
PluginInputException.class,
() -> pluginList.parsePluginYamlFile(pluginYmlFile));
}

@Test
Expand Down