From 25c480efd18b83d486cc616340f1dcd921de24c8 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Wed, 16 Apr 2014 17:46:43 +0200 Subject: Do assert falses and reasons rather than throwing exceptions --- src/test/java/LauncherTest.java | 12 ++++++------ src/test/java/MainTest.java | 32 ++++++++++++-------------------- 2 files changed, 18 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/test/java/LauncherTest.java b/src/test/java/LauncherTest.java index 8abba93..a3217b1 100644 --- a/src/test/java/LauncherTest.java +++ b/src/test/java/LauncherTest.java @@ -31,8 +31,8 @@ public class LauncherTest { Logger.msg("Testing valid startup args"); props = AbstractMain.readC2KArgs(args); - assert "MemoryOnlyClusterStorage".equals(props.get("ClusterStorage")); - assert "1553".equals(props.get("ItemServer.Proxy.port")); + assert "MemoryOnlyClusterStorage".equals(props.get("ClusterStorage")) : "Config file properties not loaded"; + assert "1553".equals(props.get("ItemServer.Proxy.port")) : "Connect file properties not loaded"; } public void testWrongConfigFileName() throws Exception { @@ -40,7 +40,7 @@ public class LauncherTest { args[5] = "filenotfound"; try { props = AbstractMain.readC2KArgs(args); - throw new Exception("Invalid connect file not detected"); + assert false: "Invalid connect file not detected"; } catch (BadArgumentsException ex) { } } @@ -49,7 +49,7 @@ public class LauncherTest { args[7] = "alsonotfound"; try { props = AbstractMain.readC2KArgs(args); - throw new Exception("Invalid connect file not detected"); + assert false : "Invalid connect file not detected"; } catch (BadArgumentsException ex) { } } @@ -59,7 +59,7 @@ public class LauncherTest { args[1] = LauncherTest.class.getResource("server.conf").getPath(); try { props = AbstractMain.readC2KArgs(args); - throw new Exception("Missing connect file not detected"); + assert false: "Missing connect file not detected"; } catch (BadArgumentsException ex) { } } @@ -69,7 +69,7 @@ public class LauncherTest { args[1] = LauncherTest.class.getResource("test.clc").getPath(); try { props = AbstractMain.readC2KArgs(args); - throw new Exception("Missing config file not detected"); + assert false: "Missing config file not detected"; } catch (BadArgumentsException ex) { } } } diff --git a/src/test/java/MainTest.java b/src/test/java/MainTest.java index 1ccc7e9..4c70579 100644 --- a/src/test/java/MainTest.java +++ b/src/test/java/MainTest.java @@ -49,12 +49,10 @@ public class MainTest { String itemType = thisItem.substring(0,delim); OutcomeValidator validator = validators.get(itemType); String data = Gateway.getResource().getTextResource(null, "boot/"+thisItem+(itemType.equals("OD")?".xsd":".xml")); - assert data!=null; + assert data!=null : "Boot "+itemType+" data item "+thisItem+" not found"; String errors = validator.validate(data); - if (errors.length() > 0) { - Logger.error("Kernel resource "+thisItem+" has errors :"+errors); - } - assert errors.length()==0; + + assert errors.length()==0 : "Kernel resource "+thisItem+" has errors :"+errors; if (itemType.equals("CA") || itemType.equals("EA") || itemType.equals("SM")) { Logger.msg(1, "Remarshalling "+thisItem); @@ -66,10 +64,7 @@ public class MainTest { Logger.msg("Marshall/remarshall of "+thisItem+" took "+(now-then)+"ms"); castorTime+=(now-then); errors = validator.validate(remarshalled); - if (errors.length() > 0) { - Logger.error("Remarshalled resource "+thisItem+" has errors :"+errors+"\nRemarshalled form:\n"+remarshalled); - } - assert errors.length()==0; + assert errors.length()==0 : "Remarshalled resource "+thisItem+" has errors :"+errors+"\nRemarshalled form:\n"+remarshalled; // Diff xmlDiff = new Diff(data, remarshalled); // if (!xmlDiff.identical()) { @@ -91,22 +86,19 @@ public class MainTest { OutcomeValidator valid = new OutcomeValidator(getSchema("Script", 0, "boot/OD/Script.xsd")); String testScriptString = FileStringUtility.url2String(MainTest.class.getResource("TestScript.xml")); String errors = valid.validate(testScriptString); - if (errors.length()>0) { - Logger.error("Test script not valid to schema: "+errors); - } - assert errors.length()==0; + assert errors.length()==0 : "Test script not valid to schema: "+errors; Script testScript = new Script(testScriptString); - assert testScript.getInputParams().size()==1; - assert testScript.getInputParams().get("test")!=null; + assert testScript.getInputParams().size()==1 : "Script input param count wrong"; + assert testScript.getInputParams().get("test")!=null : "Could not retrieve script input param value"; testScript.setInputParamValue("test", "Test"); - assert testScript.getInputParams().get("test").getInitialised(); + assert testScript.getInputParams().get("test").getInitialised() : "Script is not initialized when it should be"; Object result = testScript.execute(); - assert result!=null; - assert result instanceof String; - assert ((String)result).equals("TestTest"); + assert result!=null : "Script returned null result"; + assert result instanceof String : "Script failed to return a String"; + assert ((String)result).equals("TestTest") : "Script failed to produce correct result: "+result; } public void testStateMachine() throws Exception { @@ -114,6 +106,6 @@ public class MainTest { String smXml = FileStringUtility.url2String(MainTest.class.getResource("TestStateMachine.xml")); StateMachine sm = (StateMachine)Gateway.getMarshaller().unmarshall(smXml); sm.validate(); - assert sm.isCoherent(); + assert sm.isCoherent() : "Test StateMachine is reporting that it is not coherent"; } } -- cgit v1.2.3