summaryrefslogtreecommitdiffstats
path: root/sdo-java/branches/sdo-1.1-incubating/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java
blob: 6cc0529c5fb85a99e9c0d4d2b6a2d3af7b8249f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
 *
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.apache.tuscany.sdo.codegen;

import java.io.PrintWriter;
import java.io.StringWriter;

import junit.framework.TestCase;

/**
 * @version $Rev$ $Date$
 */
public class JavaInterfaceGeneratorTestCase extends TestCase {
    private static final String SEP = System.getProperty("line.separator");

    private JavaInterfaceGenerator gen;
    private StringWriter writer;
    private MockType foo;

    public void testHeaderNoBaseClass() {
        gen.visitType(foo);
        gen.visitEnd();
        assertEquals(localize("public interface Foo {\n}\n"), writer.toString());
    }

    public void testHeaderWithPackage() {
        gen.visitType(new MockType("org.apache.Foo", null));
        gen.visitEnd();
        assertEquals(localize("package org.apache;\n\npublic interface Foo {\n}\n"), writer.toString());
    }

    public void testHeaderOneBaseClass() throws NoJavaImplementationException {
        foo.addBaseType(new MockType("bar1", Bar1.class));
        gen.visitType(foo);
        gen.visitEnd();
        assertEquals(localize("public interface Foo extends org.apache.tuscany.sdo.codegen.Bar1 {\n}\n"), writer.toString());
    }

    public void testHeaderMultipleBaseClass() throws NoJavaImplementationException {
        foo.addBaseType(new MockType("bar1", Bar1.class));
        foo.addBaseType(new MockType("bar2", Bar2.class));
        gen.visitType(foo);
        gen.visitEnd();
        assertEquals(localize("public interface Foo extends org.apache.tuscany.sdo.codegen.Bar1, org.apache.tuscany.sdo.codegen.Bar2 {\n}\n"), writer.toString());
    }

    public void testBooleanProperty() throws NoJavaImplementationException {
        gen.visitType(foo);
        gen.visitProperty(new MockProperty("true", Boolean.TYPE, false, false));
        gen.visitEnd();
        assertEquals(localize("public interface Foo {\n\tboolean isTrue();\n\tvoid setTrue(boolean value);\n}\n"), writer.toString());
    }

    public void testByteArrayProperty() throws NoJavaImplementationException {
        gen.visitType(foo);
        gen.visitProperty(new MockProperty("bytes", byte[].class, false, false));
        gen.visitEnd();
        assertEquals(localize("public interface Foo {\n\tbyte[] getBytes();\n\tvoid setBytes(byte[] value);\n}\n"), writer.toString());
    }

    public void testObjectProperty() throws NoJavaImplementationException {
        gen.visitType(foo);
        gen.visitProperty(new MockProperty("bar", Bar1.class, false, false));
        gen.visitEnd();
        assertEquals(localize("public interface Foo {\n\torg.apache.tuscany.sdo.codegen.Bar1 getBar();\n\tvoid setBar(org.apache.tuscany.sdo.codegen.Bar1 value);\n}\n"), writer.toString());
    }

    public void testReadOnlyProperty() throws NoJavaImplementationException {
        gen.visitType(foo);
        gen.visitProperty(new MockProperty("int", Integer.TYPE, false, true));
        gen.visitEnd();
        assertEquals(localize("public interface Foo {\n\tint getInt();\n}\n"), writer.toString());
    }

    public void testManyProperty() throws NoJavaImplementationException {
        gen.visitType(foo);
        gen.visitProperty(new MockProperty("list", Integer.TYPE, true, false));
        gen.visitEnd();
        assertEquals(localize("public interface Foo {\n\tjava.util.List getList();\n}\n"), writer.toString());
    }

    public void testTwoProperties() throws NoJavaImplementationException {
        gen.visitType(foo);
        gen.visitProperty(new MockProperty("true", Boolean.TYPE, false, false));
        gen.visitProperty(new MockProperty("int", Integer.TYPE, false, false));
        gen.visitEnd();
        assertEquals(localize("public interface Foo {\n\tboolean isTrue();\n\tvoid setTrue(boolean value);\n\tint getInt();\n\tvoid setInt(int value);\n}\n"), writer.toString());
    }

    protected void setUp() throws Exception {
        super.setUp();
        writer = new StringWriter();
        gen = new JavaInterfaceGenerator(new PrintWriter(writer));
        foo = new MockType("foo", null);
    }

    private String localize(String s) {
        StringBuffer b = new StringBuffer(s.length() + s.length()/10);
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '\t') {
                b.append("    ");
            } else if (ch == '\n') {
                b.append(SEP);
            } else {
                b.append(ch);
            }
        }
        return b.toString();
    }

}