views:

118

answers:

2

main method:

 public static void main(String[] args) throws Exception
{
    if (args.length != EXPECTED_NUMBER_OF_ARGUMENTS)
    {
        System.err.println("Usage - java XFRCompiler ConfigXML PackageXML XFR");
    }

    String configXML = args[0];
    String packageXML = args[1];
    String xfr = args[2];

    AutoConfigCompiler compiler = new AutoConfigCompiler();
    compiler.setConfigDocument(loadDocument(configXML));
    compiler.setPackageInfoDoc(loadDocument(packageXML));
    // compiler.setVisiblityDoc(loadDocument("VisibilityFilter.xml"));
    compiler.compileModel(xfr);     

}

private static Document loadDocument(String fileName) throws Exception
{
    TXDOMParser parser = (TXDOMParser) ParserFactory.makeParser(TXDOMParser.class.getName());
    InputSource source = new InputSource(new FileInputStream(fileName));
    parser.parse(source);
    return parser.getDocument();  

}

testcase:

@Test
public void testCompileModel() throws Exception
{
  // construct parameters
  URL configFile =   Thread.currentThread().getContextClassLoader().getResource("Ford_2008_Mustang_Config.xml");
  URL packageFile = Thread.currentThread().getContextClassLoader().getResource("Ford_2008_Mustang_Package.xml");
  File tmpFile = new File("Ford_2008_Mustang_tmp.xfr");
  if(!tmpFile.exists()) {
     tmpFile.createNewFile();
  }

  String[] args = new     String[]{configFile.getPath(),packageFile.getPath(),tmpFile.getPath()};

  try {
    // test main method
    XFRCompiler.main(args);
  } catch (Exception e) {
    assertTrue(true);
  }
  try {
    // test args length is less than 3
    XFRCompiler.main(new String[]{"",""});
  } catch (Exception e) {  
    //ignore
  }
  tmpFile.delete(); 
}

Coverage outputs displayed as the lines from String configXML = args[0]; in main method are not covered.

+2  A: 

I'm worried about all those assertTrue(true). If there can't be an exception, then the assert is not necessary. If there is an unexpected exception, then this code will swallow it and you will get the behavior you see right now.

Then, if you expect an exception, you should code like this:

try {
    ... code that will throw an exception ...
    fail("No exception was thrown");
} catch (SpecficTypeOfException e) {
    assertEquals("message", e.getMessage());
}

That way, wrong types of exception and the exception message will be checked.

PS: Don't post questions with "urgent". We already help as fast as we can.

Aaron Digulla
Thanks for your answer.Even it can go through, but the the coverage reports still displayed as the main method was not covered.
Mike.Huang
Run the code in a debugger to make 100% sure it actually executes these lines. There might be something which we are missing.
Aaron Digulla
+4  A: 
  • assertTrue(true); is a pointless no-op
  • Remove the try/catch around the call to XFRCompiler.main(args);, since all it does is swallow excpetions and make debugging harder; most likely you will then see an exception that tells you where the problem is.
  • There should be a call to fail() after the call to XFRCompiler.main(new String[]{"",""}); since you expect it to throw an exception
  • Put the two calls in separate test methods.
Michael Borgwardt