Exception handling cleanup in tests (findbugs).

This commit is contained in:
Mike Pope 2019-03-13 09:49:50 +10:30
parent 72a95d1c3a
commit af2d263cf8
7 changed files with 40 additions and 44 deletions

View File

@ -24,6 +24,8 @@ import java.io.InputStream;
import java.io.IOException;
import java.nio.file.Files;
import javax.xml.stream.XMLStreamException;
import net.sf.freecol.common.i18n.Number.Category;
import net.sf.freecol.util.test.FreeColTestCase;
@ -63,8 +65,8 @@ public class CLDRTest extends FreeColTestCase {
assertTrue(inputFile.exists());
try (InputStream in = Files.newInputStream(inputFile.toPath())) {
numberRules = new NumberRules(in);
} catch (Exception e) {
fail("Failed to open input stream: " + e.toString());
} catch (IOException|XMLStreamException ex) {
fail(ex.toString());
}
assertNotNull(NumberRules.getNumberForLanguage("az"));

View File

@ -19,6 +19,7 @@
package net.sf.freecol.common.model;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
@ -26,6 +27,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.stream.XMLStreamException;
import net.sf.freecol.common.io.FreeColXMLWriter;
import net.sf.freecol.common.model.Modifier;
import net.sf.freecol.common.model.Modifier.ModifierType;
@ -673,8 +676,8 @@ public class BuildingTest extends FreeColTestCase {
FreeColXMLWriter xw = new FreeColXMLWriter(sw,
FreeColXMLWriter.WriteScope.toSave(), false)) {
building.toXML(xw);
} catch (Exception e) {
fail(e.toString());
} catch (IOException|XMLStreamException ex) {
fail(ex.toString());
}
}
}

View File

@ -19,9 +19,12 @@
package net.sf.freecol.common.model;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import javax.xml.stream.XMLStreamException;
import net.sf.freecol.common.io.FreeColXMLWriter;
import net.sf.freecol.common.model.Colony;
import net.sf.freecol.common.model.Monarch.MonarchAction;
@ -39,8 +42,8 @@ public class MonarchTest extends FreeColTestCase {
try (StringWriter sw = new StringWriter();
FreeColXMLWriter xw = new FreeColXMLWriter(sw)) {
dutch.getMonarch().toXML(xw);
} catch (Exception e) {
fail(e.toString());
} catch (IOException|XMLStreamException ex) {
fail(ex.toString());
}
}

View File

@ -179,21 +179,16 @@ public class SerializationTest extends FreeColTestCase {
public void testDifficulty() throws Exception {
Specification spec1 = null;
Specification spec2 = null;
try {
spec1 = FreeColTcFile.getFreeColTcFile("classic").getSpecification();
spec1.applyDifficultyLevel("model.difficulty.veryEasy");
StringWriter sw = new StringWriter();
try (FreeColXMLWriter xw = new FreeColXMLWriter(sw,
FreeColXMLWriter.WriteScope.toSave())) {
spec1.toXML(xw);
} catch (IOException ioe) {
fail(ioe.getMessage());
}
spec2 = new Specification(new ByteArrayInputStream(sw.toString().getBytes()));
} catch (Exception e) {
fail(e.getMessage());
spec1 = FreeColTcFile.getFreeColTcFile("classic").getSpecification();
spec1.applyDifficultyLevel("model.difficulty.veryEasy");
StringWriter sw = new StringWriter();
try (FreeColXMLWriter xw = new FreeColXMLWriter(sw,
FreeColXMLWriter.WriteScope.toSave())) {
spec1.toXML(xw);
} catch (IOException ioe) {
fail(ioe.getMessage());
}
spec2 = new Specification(new ByteArrayInputStream(sw.toString().getBytes()));
assertNotNull(spec1);
assertNotNull(spec2);
@ -216,21 +211,16 @@ public class SerializationTest extends FreeColTestCase {
public void testGeneratedLists() throws Exception {
Specification spec1 = null;
Specification spec2 = null;
try {
spec1 = FreeColTcFile.getFreeColTcFile("classic").getSpecification();
spec1.applyDifficultyLevel("model.difficulty.veryEasy");
StringWriter sw = new StringWriter();
try (FreeColXMLWriter xw = new FreeColXMLWriter(sw,
FreeColXMLWriter.WriteScope.toSave(), false)) {
spec1.toXML(xw);
} catch (IOException ioe) {
fail(ioe.getMessage());
}
spec2 = new Specification(new ByteArrayInputStream(sw.toString().getBytes()));
} catch (Exception e) {
fail(e.getMessage());
spec1 = FreeColTcFile.getFreeColTcFile("classic").getSpecification();
spec1.applyDifficultyLevel("model.difficulty.veryEasy");
StringWriter sw = new StringWriter();
try (FreeColXMLWriter xw = new FreeColXMLWriter(sw,
FreeColXMLWriter.WriteScope.toSave(), false)) {
spec1.toXML(xw);
} catch (IOException ioe) {
fail(ioe.getMessage());
}
spec2 = new Specification(new ByteArrayInputStream(sw.toString().getBytes()));
List<GoodsType> food1 = spec1.getFoodGoodsTypeList();
List<GoodsType> food2 = spec2.getFoodGoodsTypeList();

View File

@ -21,6 +21,7 @@ package net.sf.freecol.common.model;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -444,8 +445,8 @@ public final class SpecificationTest extends FreeColTestCase {
spec.loadMods(mods);
UnitType milkmaid = spec.getUnitType("model.unit.milkmaid");
assertEquals(numberOfUnitTypes + 1, spec.getUnitTypeList().size());
} catch (Exception e) {
fail(e.getMessage());
} catch (IOException|XMLStreamException ex) {
fail(ex.toString());
}
}
}

View File

@ -41,11 +41,7 @@ public class UtilsTest extends FreeColTestCase {
l.add(2);
l.add(3);
List<List<Integer>> p = new ArrayList<>();
try {
for (List<Integer> li : CollectionUtils.getPermutations(l)) p.add(li);
} catch (Exception e) {
fail();
}
for (List<Integer> li : CollectionUtils.getPermutations(l)) p.add(li);
assertEquals(p.size(), 6);
assertEquals(p.get(0), makeList(1,2,3));
assertEquals(p.get(1), makeList(1,3,2));

View File

@ -619,13 +619,14 @@ public class FreeColTestCase extends TestCase {
* @param value an {@code int} value
*/
public void setProductionBonus(Colony colony, int value) {
Field productionBonus;
try {
Field productionBonus = Colony.class.getDeclaredField("productionBonus");
productionBonus = Colony.class.getDeclaredField("productionBonus");
productionBonus.setAccessible(true);
productionBonus.setInt(colony, value);
colony.invalidateCache();
} catch (Exception e) {
// do nothing
} catch (NoSuchFieldException|IllegalAccessException ex) {
fail(ex.toString());
}
}