01: package net.sf.mockcreator.codegeneration;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.IOException;
05: import java.io.OutputStreamWriter;
06: import java.io.Writer;
07:
08: import net.sf.mockcreator.TestCase;
09: import net.sf.mockcreator.codegeneration.FormattingOutputWriter;
10:
11: public class FormattingOutputWriterTest extends TestCase {
12:
13: public FormattingOutputWriterTest(String name) {
14: super (name);
15: }
16:
17: ByteArrayOutputStream baos;
18: FormattingOutputWriter fos;
19:
20: public void setUp() throws Exception {
21: baos = new ByteArrayOutputStream();
22: Writer wr = new OutputStreamWriter(baos);
23: fos = new FormattingOutputWriter(wr);
24: }
25:
26: public String print(String s) throws IOException {
27: fos.write(s.toCharArray(), 0, s.length());
28: fos.close();
29: // System.out.println("for =====\n"+s+"\n ===== we got \n"+baos.toString()+"\n =====\n\n\n");
30: return baos.toString();
31: }
32:
33: public void testNoBrackets() throws IOException {
34: check("s1;\ns2;", "s1;\ns2;\n");
35: }
36:
37: public void testBracketsOnly() throws IOException {
38: check("a {}", "a {\n}");
39: }
40:
41: public void testNestedBracketsOnly() throws IOException {
42: check("{{}}", "{\n {\n }\n}");
43: }
44:
45: public void testNoSpaces() throws IOException {
46: check("s1{s2{s3;s4;}}",
47: "s1{\n s2{\n s3;\n s4;\n }\n}");
48: }
49:
50: public void testSpaces() throws IOException {
51: check("s1{ s2{ s3; s4; } }",
52: "s1{\n s2{\n s3;\n s4;\n } \n}");
53: }
54:
55: public void testLineBreaks() throws IOException {
56: check("s1\n{s2{s3;}}", "s1{\n s2{\n s3;\n }\n}");
57: }
58:
59: public void testTwoMethods() throws IOException {
60: check("a(){}\n\nb(){}", "a(){\n}\nb(){\n}");
61: }
62:
63: public void testTwoMethodsAndContent() throws IOException {
64: check("a(){c;}\n\nb(){d;}", "a(){\n c;\n}\nb(){\n d;\n}");
65: }
66:
67: public void testComment() throws IOException {
68: check("// a;{}\nwhat? // h;\n{}", "// a;{}\nwhat? // h;\n{\n}");
69: }
70:
71: private void check(String src, String exp) throws IOException {
72: String dst = print(src);
73: assertEquals(exp, dst);
74: }
75: }
|