aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/android/xsdc/cpp/TestCppCodeGenerator.java
blob: 61066968af3e941b635241a331b221d1accbf483 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (C) 2022, The Android Open Source Project
 *
 * Licensed 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 com.android.xsdc.cpp;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.android.xsdc.FileSystem;
import com.android.xsdc.XmlSchema;
import com.android.xsdc.XsdHandler;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class TestCppCodeGenerator {
    public static final String SCHEMA = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
            + "  <xs:element name=\"class\" type=\"xs:string\" />\n"
            + "</xs:schema>";

    @Test
    public void testParseSchema() throws Exception {
        XmlSchema schema = parseSchema(SCHEMA);
        assertEquals(schema.getElementMap().keySet(), Set.of("class"));
    }

    @Test
    public void testSimpleTypeRootParser() throws Exception {
        // We need two root elements to generate reader with element name.
        String schema = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
                + "  <xs:element name=\"simple-type-element\" type=\"xs:string\" />\n"
                + "  <xs:element name=\"another-root\" type=\"xs:string\" />\n"
                + "</xs:schema>";
        Map<String, StringBuffer> files = new TreeMap<>();
        CppCodeGenerator gen =
                new CppCodeGenerator(
                        parseSchema(schema),
                        "com.abc",
                        /*writer=*/ true,
                        CppCodeGenerator.GENERATE_PARSER,
                        false,
                        false,
                        null);

        FileSystem fs = new FileSystem(files);
        gen.print(fs);
        assertTrue(files.get("com_abc.cpp").toString().contains("readSimpleTypeElement"));
        assertTrue(files.get("com_abc.cpp").toString().contains("writeSimpleTypeElement"));
    }

    @Test
    public void testPrintWithoutEnumOutput() throws Exception {
        Map<String, StringBuffer> files = new TreeMap<>();
        CppCodeGenerator gen =
                new CppCodeGenerator(
                        parseSchema(SCHEMA),
                        "com.abc",
                        false,
                        CppCodeGenerator.GENERATE_PARSER,
                        false,
                        false,
                        null);

        FileSystem fs = new FileSystem(files);
        gen.print(fs);

        assertEquals(files.keySet(), Set.of("com_abc.cpp", "include/com_abc.h"));
    }

    @Test
    public void printWithEnumOutput() throws Exception {
        Map<String, StringBuffer> files = new TreeMap<>();

        CppCodeGenerator gen =
                new CppCodeGenerator(
                        parseSchema(SCHEMA),
                        "com.abc",
                        false,
                        CppCodeGenerator.GENERATE_PARSER | CppCodeGenerator.GENERATE_ENUMS,
                        false,
                        false,
                        null);

        FileSystem fs = new FileSystem(files);
        gen.print(fs);

        assertEquals(
                files.keySet(),
                Set.of(
                        "com_abc.cpp",
                        "include/com_abc.h",
                        "com_abc_enums.cpp",
                        "include/com_abc_enums.h"));
    }

    private XmlSchema parseSchema(String contents) throws Exception {
        byte[] bytes = contents.getBytes(StandardCharsets.UTF_8);
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        SAXParser parser = factory.newSAXParser();
        XsdHandler xsdHandler = new XsdHandler();
        parser.parse(new ByteArrayInputStream(bytes), xsdHandler);
        return xsdHandler.getSchema();
    }

    @Test
    public void generateParsersForSpecifiedRoot() throws Exception {
        Map<String, StringBuffer> files = new TreeMap<>();
        String schema = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
                + "  <xs:element name=\"root1\" type=\"xs:string\" />\n"
                + "  <xs:element name=\"root2\" type=\"xs:string\" />\n"
                + "</xs:schema>";
        CppCodeGenerator gen =
                new CppCodeGenerator(
                        parseSchema(schema),
                        "com.abc",
                        false,
                        CppCodeGenerator.GENERATE_PARSER | CppCodeGenerator.GENERATE_ENUMS,
                        false,
                        false,
                        new String[]{"root1"});

        FileSystem fs = new FileSystem(files);
        gen.print(fs);
        assertTrue(files.get("com_abc.cpp").toString().contains("readRoot1"));
        assertFalse(files.get("com_abc.cpp").toString().contains("readRoot2"));
    }
}