FormattingOutputWriterTest.java :  » Testing » MockCreator » net » sf » mockcreator » codegeneration » Java Open Source

Java Open Source » Testing » MockCreator 
MockCreator » net » sf » mockcreator » codegeneration » FormattingOutputWriterTest.java
package net.sf.mockcreator.codegeneration;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import net.sf.mockcreator.TestCase;
import net.sf.mockcreator.codegeneration.FormattingOutputWriter;


public class FormattingOutputWriterTest extends TestCase {

    public FormattingOutputWriterTest(String name) {
        super(name);
    }

    ByteArrayOutputStream baos;
    FormattingOutputWriter fos;
    
    public void setUp() throws Exception {
        baos = new ByteArrayOutputStream();
        Writer wr = new OutputStreamWriter(baos);
        fos = new FormattingOutputWriter(wr);
    }
    
    public String print(String s) throws IOException {
        fos.write(s.toCharArray(),0,s.length());
        fos.close();
        // System.out.println("for =====\n"+s+"\n ===== we got \n"+baos.toString()+"\n =====\n\n\n");
        return baos.toString();
    }

    public void testNoBrackets() throws IOException {
        check("s1;\ns2;","s1;\ns2;\n");
    }

    public void testBracketsOnly() throws IOException {
        check("a {}","a {\n}");
    }

    public void testNestedBracketsOnly() throws IOException {
        check("{{}}","{\n    {\n    }\n}");
    }
    
    public void testNoSpaces() throws IOException {
        check("s1{s2{s3;s4;}}","s1{\n    s2{\n        s3;\n        s4;\n    }\n}");
    }

    public void testSpaces() throws IOException {
        check("s1{  s2{   s3;    s4;  }  }","s1{\n    s2{\n        s3;\n        s4;\n    }  \n}");
    }
    
    public void testLineBreaks() throws IOException {
        check("s1\n{s2{s3;}}","s1{\n    s2{\n        s3;\n    }\n}");
    }

    public void testTwoMethods() throws IOException {
        check("a(){}\n\nb(){}","a(){\n}\nb(){\n}");
    }

    public void testTwoMethodsAndContent() throws IOException {
        check("a(){c;}\n\nb(){d;}","a(){\n    c;\n}\nb(){\n    d;\n}");
    }

    public void testComment() throws IOException {
        check("// a;{}\nwhat? // h;\n{}","// a;{}\nwhat? // h;\n{\n}");
    }
    
    private void check(String src,String exp) throws IOException {
        String dst = print(src);
        assertEquals(exp,dst);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.