View Javadoc

1   package org.xmlhammer.cli;
2   
3   import java.io.BufferedReader;
4   import java.io.File;
5   import java.io.FileReader;
6   import java.io.IOException;
7   import java.io.StringReader;
8   import java.io.StringWriter;
9   
10  import junit.framework.TestCase;
11  
12  import org.apache.log4j.Level;
13  import org.apache.log4j.Logger;
14  import org.apache.log4j.PatternLayout;
15  import org.apache.log4j.WriterAppender;
16  
17  public abstract class CLITestCase extends TestCase {
18      public CLITestCase(String test) {
19          super(test);
20      }
21      
22      protected String run(String project) {
23          XMLHammer hammer = new XMLHammer(true);
24          Logger logger = Logger.getLogger(XMLHammer.class.getName() + "@" + Integer.toHexString(XMLHammer.class.hashCode()));
25          logger.setLevel(Level.INFO);
26          StringWriter writer = new StringWriter();
27          logger.addAppender(new WriterAppender(new PatternLayout("%m%n"), writer));
28  
29          hammer.run(project, logger, true);
30          
31          return writer.toString();
32      }
33      
34      protected void assertOutput(File golden, String output) throws IOException {
35          assertOutput(golden, output, false);
36      }
37  
38      protected void assertOutput(File golden, String output, boolean log) throws IOException {
39          BufferedReader reader = new BufferedReader(new FileReader(golden));
40  
41          if (log) {
42              System.out.println("----------------------------------------");
43              System.out.print(output);
44              System.out.println("----------------------------------------");
45          }
46  
47          System.out.println(output);
48          String line = reader.readLine();
49          while (line != null) {
50              assertFalse("Missing line: \""+line.trim()+"\"", output.indexOf(line.trim()) == -1);
51              line = reader.readLine();
52          }
53          
54          reader = new BufferedReader(new StringReader(output));
55          line = reader.readLine();
56          String previous = null;
57          while (line != null) {
58              assertFalse("Duplicate line: \""+line.trim()+"\"", line.trim().equals(previous));
59  
60              previous = line.trim();
61              line = reader.readLine();
62          }
63      }
64  }