summaryrefslogtreecommitdiffstats
path: root/configgen/src/test/java/com/yahoo/config/codegen/DefParserTest.java
blob: 2260690155ccf9d4b61c2c980d54bc83c9525459 (plain) (blame)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.codegen;

import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.Ignore;

import static org.hamcrest.CoreMatchers.is;

import java.io.*;

/**
 * Unit tests for DefParser.
 *
 * @author hmusum
 * @author gjoranv
 */
public class DefParserTest {

    private static final String TEST_DIR = "target/test-classes/";
    private static final String DEF_NAME = TEST_DIR + "allfeatures.def";

    @Test
    public void testTraverseTree() throws IOException {
        File defFile = new File(DEF_NAME);
        CNode root = new DefParser("test", new FileReader(defFile)).getTree();
        assertNotNull(root);
        CNode[] children = root.getChildren();
        assertThat(children.length, is(31));

        int numGrandChildren = 0;
        int numGreatGrandChildren = 0;
        for (CNode child : children) {
            CNode[] childsChildren = child.getChildren();
            numGrandChildren += childsChildren.length;
            for (CNode grandChild : childsChildren) {
                numGreatGrandChildren += grandChild.getChildren().length;
            }
        }
        assertThat(numGrandChildren, is(14));
        assertThat(numGreatGrandChildren, is(6));

        // Verify that each array creates a sub-tree, and that defaults for leafs are handled correctly.
        CNode myArray = root.getChild("myArray");
        assertThat(myArray.getChildren().length, is(5));
        // int within array
        LeafCNode myArrayInt = (LeafCNode) myArray.getChild("intVal");
        assertThat(myArrayInt.getDefaultValue().getValue(), is("14"));
        // enum within array
        LeafCNode myArrayEnum = (LeafCNode) myArray.getChild("enumVal");
        assertThat(myArrayEnum.getDefaultValue().getValue(), is("TYPE"));

        // Verify array within array and a default value for a leaf in the inner array.
        CNode anotherArray = myArray.getChild("anotherArray");
        assertThat(anotherArray.getChildren().length, is(1));
        LeafCNode foo = (LeafCNode) anotherArray.getChild("foo");
        assertThat(foo.getDefaultValue().getValue(), is("-4"));
    }

    @Test
    public void testFileWithNamespaceInFilename() throws IOException {
        File defFile = new File(TEST_DIR + "bar.foo.def");
        CNode root = new DefParser("test", new FileReader(defFile)).getTree();
        assertThat(root.defMd5, is("31a0f9bda0e5ff929762a29569575a7e"));
    }

    @Test
    public void testMd5Sum() throws IOException {
        File defFile = new File(DEF_NAME);
        CNode root = new DefParser("test", new FileReader(defFile)).getTree();
        assertThat(root.defMd5, is("eb2d24dbbcf054b21be729e2cfaafd93"));
    }

    @Test
    public void testMd5Sum2() {
        String def = "version=1\na string\n";
        CNode root = new DefParser("testMd5Sum2", new StringReader(def)).getTree();
        assertThat(root.defMd5, is("a5e5fdbb2b27e56ba7d5e60e335c598b"));
    }

    @Test
    public void testExplicitNamespace() {
        DefParser parser = createParser("version=1\nnamespace=myproject.config\na string\n");
        CNode root = parser.getTree();
        assertThat(root.getNamespace(), is("myproject.config"));

        // with spaces
        parser = createParser("version=1\nnamespace =  myproject.config\na string\n");
        root = parser.getTree();
        assertThat(root.getNamespace(), is("myproject.config"));

        // invalid
        parser = createParser("version=1\nnamespace \na string\n");
        try {
            parser.getTree();
            fail();
        } catch (Exception e) {
            //e.printStackTrace();
            assertExceptionAndMessage(e, CodegenRuntimeException.class,
                    "Error parsing or reading config definition.Error when parsing line 2: namespace \n" +
                            "namespace");
        }

        // invalid
        parser = createParser("version=1\nnamespace=a..b\na string\n");
        try {
            parser.getTree();
            fail();
        } catch (Exception e) {
            //e.printStackTrace();
            assertExceptionAndMessage(e, CodegenRuntimeException.class,
                    "Error parsing or reading config definition.Error when parsing line 2: namespace=a..b\n" +
                            "namespace=a..b");
        }
    }

    @Test
    public void verifyThatExplicitNamespaceAltersDefMd5() {
        DefParser parser = createParser("version=1\na string\n");
        CNode root = parser.getTree();

        parser = createParser("version=1\nnamespace=myproject.config\na string\n");
        CNode namespaceRoot = parser.getTree();

        assertThat(root.defMd5, not(namespaceRoot.defMd5));
    }


    @Test(expected = CodegenRuntimeException.class)
    public void verify_fail_on_illegal_char_in_namespace() {
        createParser("version=1\nnamespace=Foo\na string\n").getTree();
    }

    @Test(expected = CodegenRuntimeException.class)
    public void verify_fail_on_com_yahoo_in_explicit_namespace() {
        createParser("version=1\n" +
                "namespace=com.yahoo.myproject.config\n" +
                "a string\n").getTree();
    }

    @Test
    public void testInvalidType() {
        Class<?> exceptionClass = DefParser.DefParserException.class;
        try {
            createParser("version=1\n" +
                    "# comment\n" +
                    "a sting").getTree();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage((Exception) e.getCause(), exceptionClass,
                    "Error when parsing line 3: a sting", false);
        }
    }

    @Test
    public void testValidVersions() {
        try {
            testExpectedVersion("version=8", "8");
            testExpectedVersion("version=8-1", "8-1");
            testExpectedVersion("version =8", "8");
            testExpectedVersion("version = 8", "8");
            testExpectedVersion("version = 8 ", "8");
            testExpectedVersion("version =\t8", "8");
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }

    private void testExpectedVersion(String versionLine, String expectedVersion) {
        InnerCNode root = createParser(versionLine).getTree();
        assertThat(root.defVersion, is(expectedVersion));
    }

    @Test
    public void testMissingVersion() {
        try {
            createParser("a string\n").parse();
        } catch (Exception e) {
            fail("Should not get an exception here");
        }
    }

    private DefParser createParser(String def) {
        return new DefParser("test", new StringReader(def));
    }

    @Test
    public void testInvalidVersion() {
        Class<?> exceptionClass = DefParser.DefParserException.class;
        testInvalidVersion("version=a\n", exceptionClass,
                "Error when parsing line 1: version=a\nversion=a");
        testInvalidVersion("version = a\n", exceptionClass,
                "Error when parsing line 1: version = a\n a");
    }

    private void testInvalidVersion(String versionLine, Class<?> exceptionClass, String exceptionMessage) {
        try {
            createParser(versionLine).parse();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage(e, exceptionClass, exceptionMessage);
        }
    }

    @Test
    public void verify_fail_on_default_for_file() {
        Class<?> exceptionClass = DefParser.DefParserException.class;
        DefParser parser = createParser("version=1\nf file default=\"file1.txt\"\n");
        try {
            parser.getTree();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage((Exception) e.getCause(), exceptionClass,
                    "Error when parsing line 2: f file default=\"file1.txt\"\n" +
                            "Invalid default value", false);
        }
    }

    // Helper method for checking correct exception class and message
    private void assertExceptionAndMessage(Exception e, Class<?> exceptionClass, String message) {
        assertExceptionAndMessage(e, exceptionClass, message, true);
    }

    // Helper method for checking correct exception class and message
    private void assertExceptionAndMessage(Exception e, Class<?> exceptionClass, String message, boolean exact) {
        if (exact) {
            assertEquals(message, e.getMessage());
        } else {
            assertTrue(e.getMessage().startsWith(message));
        }
        assertEquals(exceptionClass.getName(), e.getClass().getName());
    }

    @Test(expected = CodegenRuntimeException.class)
    @Ignore("Not implemented yet")
    public void testInvalidEnum() throws DefParser.DefParserException {
        DefParser parser = createParser("version=1\nanEnum enum {A, B, A}\n");
        //parser.validateDef(def);
    }

    @Test
    public void testEnum() {
        StringBuilder sb = createDefTemplate();
        sb.append("enum1 enum {A,B} default=A\n");
        sb.append("enum2 enum {A, B} default=A\n");
        sb.append("enum3 enum { A, B} default=A\n");
        sb.append("enum4 enum { A, B } default=A\n");
        sb.append("enum5 enum { A , B } default=A\n");
        sb.append("enum6 enum {A , B } default=A\n");
        sb.append("enumVal enum { FOO, BAR, FOOBAR }\n");

        DefParser parser = createParser(sb.toString());
        try {
            parser.getTree();
        } catch (Exception e) {
            assertNotNull(null);
        }
        CNode root = parser.getTree();
        LeafCNode node = (LeafCNode) root.getChild("enum1");
        assertNotNull(node);
        assertThat(node.getDefaultValue().getStringRepresentation(), is("A"));
    }

    @Test(expected = DefParser.DefParserException.class)
    public void testInvalidCommaInEnum() throws DefParser.DefParserException, IOException {
        String invalidEnum = "anEnum enum {A, B, } default=A\n";
        String validEnum = "anotherEnum enum {A, B} default=A\n";
        StringBuilder sb = createDefTemplate();
        sb.append(invalidEnum);
        sb.append(validEnum);
        DefParser parser = createParser(sb.toString());
        parser.parse();
    }

    @Ignore //TODO: finish this! The numeric leaf nodes must contain their range.
    @Test
    public void testRanges() {
        StringBuilder sb = new StringBuilder("version=1\n");
        sb.append("i int range=[0,10]");
        sb.append("l long range=[-1e20,0]");
        sb.append("d double range=[0,1]");

        DefParser parser = createParser(sb.toString());
        CNode root = parser.getTree();
        LeafCNode.IntegerLeaf intNode = (LeafCNode.IntegerLeaf) root.getChild("i");
    }

    @Test
    public void testInvalidLine() {
        Class<?> exceptionClass = DefParser.DefParserException.class;
        StringBuilder sb = createDefTemplate();
        String invalidLine = "a inta\n";
        sb.append(invalidLine);
        try {
            createParser(sb.toString()).parse();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage(e, exceptionClass,
                    "Error when parsing line 3: " + invalidLine + "Could not create inta a");
        }
    }

    @Test
    public void testDuplicateDefinition() {
        Class<?> exceptionClass = DefParser.DefParserException.class;
        StringBuilder sb = createDefTemplate();
        String invalidLine = "b int\n";
        sb.append(invalidLine);
        // Add a duplicate line, which should be illegal
        sb.append(invalidLine);
        try {
            createParser(sb.toString()).parse();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage(e, exceptionClass,
                    "Error when parsing line 4: " + invalidLine + "b is already defined");
        }
    }

    @Test
    public void testIllegalCharacterInName() {
        Class<?> exceptionClass = DefParser.DefParserException.class;
        StringBuilder sb = createDefTemplate();
        String invalidLine = "a-b int\n";
        sb.append(invalidLine);
        try {
            createParser(sb.toString()).parse();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage(e, exceptionClass,
                    "Error when parsing line 3: " + invalidLine + "a-b contains unexpected character");
        }
    }

    @Test
    public void parameter_name_starting_with_digit_is_illegal() {
        Class<?> exceptionClass = DefParser.DefParserException.class;
        StringBuilder sb = createDefTemplate();
        String invalidLine = "1a int\n";
        sb.append(invalidLine);
        try {
            createParser(sb.toString()).parse();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage(e, exceptionClass,
                    "Error when parsing line 3: " + invalidLine + "1a must start with a non-digit character");
        }
    }

    @Test
    public void parameter_name_starting_with_uppercase_is_illegal() {
        Class<?> exceptionClass = DefParser.DefParserException.class;
        StringBuilder sb = createDefTemplate();
        String invalidLine = "SomeInt int\n";
        sb.append(invalidLine);
        try {
            createParser(sb.toString()).parse();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage(e, exceptionClass,
                    "Error when parsing line 3: " + invalidLine + "'SomeInt' cannot start with an uppercase letter");
        }
    }

    @Test
    public void parameter_name_starting_with_the_internal_prefix_is_illegal() {
        String internalPrefix = ReservedWords.INTERNAL_PREFIX;
        Class<?> exceptionClass = DefParser.DefParserException.class;
        StringBuilder sb = createDefTemplate();
        String invalidLine = internalPrefix + "i int\n";
        sb.append(invalidLine);
        try {
            createParser(sb.toString()).parse();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage(e, exceptionClass,
                    "Error when parsing line 3: " + invalidLine +
                            "'" + internalPrefix + "i' cannot start with '" + internalPrefix + "'");
        }
    }

    @Test
    public void testIllegalArray() {
        Class<?> exceptionClass = DefParser.DefParserException.class;
        StringBuilder sb = createDefTemplate();
        String invalidLine = "intArr[ int\n";
        sb.append(invalidLine);
        try {
            createParser(sb.toString()).parse();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage(e, exceptionClass,
                    "Error when parsing line 3: " + invalidLine + "intArr[ Expected ] to terminate array definition");
        }
    }

    @Test
    public void testIllegalDefault() {
        Class<?> exceptionClass = DefParser.DefParserException.class;
        StringBuilder sb = createDefTemplate();
        String invalidLine = "a int deflt 10\n";
        sb.append(invalidLine);
        try {
            createParser(sb.toString()).parse();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage(e, exceptionClass,
                    "Error when parsing line 3: " + invalidLine + " deflt 10");
        }
    }

    @Test
    public void testReservedWordInC() {
        Class<?> exceptionClass = DefParser.DefParserException.class;
        StringBuilder sb = createDefTemplate();
        String invalidLine = "auto int\n";
        sb.append(invalidLine);
        try {
            createParser(sb.toString()).parse();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage(e, exceptionClass,
                    "Error when parsing line 3: " + invalidLine + "auto is a reserved word in C");
        }
    }

    @Test
    public void testReservedWordInJava() {
        Class<?> exceptionClass = DefParser.DefParserException.class;
        StringBuilder sb = createDefTemplate();
        String invalidLine = "abstract int\n";
        sb.append(invalidLine);
        try {
            createParser(sb.toString()).parse();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage(e, exceptionClass,
                    "Error when parsing line 3: " + invalidLine + "abstract is a reserved word in Java");
        }
    }

    @Test
    public void testReservedWordInCAndJava() {
        Class<?> exceptionClass = DefParser.DefParserException.class;
        StringBuilder sb = createDefTemplate();
        String invalidLine = "continue int\n";
        sb.append(invalidLine);
        try {
            createParser(sb.toString()).parse();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage(e, exceptionClass,
                    "Error when parsing line 3: " + invalidLine + "continue is a reserved word in C and Java");
        }
    }

    @Test
    public void number_is_allowed_as_non_leading_char_in_namespace() throws IOException, DefParser.DefParserException {
        StringBuilder sb = createDefTemplate();
        String line = "namespace=a.b.c2\nfoo int\n";
        sb.append(line);
        createParser(sb.toString()).parse();
    }

    @Test
    public void number_is_not_allowed_as_namespace_start_char() throws IOException, DefParser.DefParserException {
        StringBuilder sb = createDefTemplate();
        String line = "namespace=2.a.b";
        sb.append(line).append("\n");
        Class<?>  exceptionClass = DefParser.DefParserException.class;
        try {
            createParser(sb.toString()).parse();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage(e, exceptionClass,
                                      "Error when parsing line 3: " + line + "\n" + line);
        }
    }

    @Test
    public void number_is_not_allowed_as_leading_char_in_namespace_token() throws IOException, DefParser.DefParserException {
        StringBuilder sb = createDefTemplate();
        String line = "namespace=a.b.2c";
        sb.append(line).append("\n");
        Class<?> exceptionClass = DefParser.DefParserException.class;
        try {
            createParser(sb.toString()).parse();
            fail("Didn't find expected exception of type " + exceptionClass);
        } catch (Exception e) {
            assertExceptionAndMessage(e, exceptionClass,
                                      "Error when parsing line 3: " + line + "\n" + line);
        }

    }

    @Test
    public void testUnderscoreInNamespace() throws IOException, DefParser.DefParserException {
        StringBuilder sb = createDefTemplate();
        String line = "namespace=a_b.c\nfoo int\n";
        sb.append(line);
        createParser(sb.toString()).parse();

        sb = createDefTemplate();
        line = "namespace=a_b.c_d\nfoo int\n";
        sb.append(line);
        createParser(sb.toString()).parse();
    }

    private StringBuilder createDefTemplate() {
        StringBuilder sb = new StringBuilder();
        sb.append("version=8\n");
        // Add a comment line to check that we get correct line number with comments
        sb.append("# comment\n");

        return sb;
    }
}