BUG-7267: catch RuntimeExceptions when processing sources
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / SubstatementValidatorTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import java.io.ByteArrayOutputStream;
17 import java.io.PrintStream;
18 import java.io.UnsupportedEncodingException;
19 import java.net.URISyntaxException;
20 import java.util.Set;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
29
30 public class SubstatementValidatorTest {
31
32     private final PrintStream stdout = System.out;
33     private final ByteArrayOutputStream output = new ByteArrayOutputStream();
34     private String testLog;
35
36     @Rule
37     public ExpectedException expectedEx = ExpectedException.none();
38
39     @Before
40     public void setUp() throws UnsupportedEncodingException {
41         System.setOut(new PrintStream(output, true, "UTF-8"));
42     }
43
44     @After
45     public void cleanUp() {
46         System.setOut(stdout);
47     }
48
49     @Test
50     public void noException() throws URISyntaxException, ReactorException {
51         final Set<Module> modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
52                 .toURI());
53         assertNotNull(modules);
54     }
55
56     @Test
57     public void undesirableElementException() throws URISyntaxException {
58         try {
59             TestUtils.loadModules(getClass().getResource("/substatement-validator/undesirable-element").toURI());
60             fail("Unexpected success");
61         } catch (ReactorException ex) {
62             assertNotNull(ex.getCause());
63             assertTrue(ex.getCause().getMessage().contains("TYPE is not valid for REVISION"));
64         }
65     }
66
67     @Test
68     public void maximalElementCountException() throws URISyntaxException, ReactorException {
69         try {
70             TestUtils.loadModules(getClass().getResource("/substatement-validator/maximal-element").toURI());
71             fail("Unexpected success");
72         } catch (ReactorException ex) {
73             assertNotNull(ex.getCause());
74             assertTrue(ex.getCause().getMessage().contains("Maximal count of DESCRIPTION for AUGMENT is 1"));
75         }
76     }
77
78     @Test
79     public void missingElementException() throws URISyntaxException, ReactorException {
80         expectedEx.expect(SomeModifiersUnresolvedException.class);
81
82         TestUtils.loadModules(getClass().getResource("/substatement-validator/missing-element").toURI());
83     }
84
85     @Test
86     public void bug6173Test() throws ReactorException, URISyntaxException {
87         final Set<Module> loadModules = TestUtils.loadModules(getClass().getResource(
88                 "/substatement-validator/empty-element").toURI());
89         assertEquals(1, loadModules.size());
90     }
91
92     @Test
93     public void bug4310test() throws URISyntaxException, ReactorException {
94         expectedEx.expect(SomeModifiersUnresolvedException.class);
95         TestUtils.loadModules(getClass().getResource("/substatement-validator/bug-4310").toURI());
96     }
97 }