Do not use deprecated junit API
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / SubstatementValidatorTest.java
1 /*
2  * Copyright (c) 2015 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.test;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import java.io.ByteArrayOutputStream;
14 import java.io.PrintStream;
15 import java.io.UnsupportedEncodingException;
16 import java.net.URISyntaxException;
17 import java.util.NoSuchElementException;
18 import java.util.Set;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.rules.ExpectedException;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.stmt.retest.TestUtils;
27
28 public class SubstatementValidatorTest {
29
30     private final PrintStream stdout = System.out;
31     private final ByteArrayOutputStream output = new ByteArrayOutputStream();
32     private String testLog;
33
34     @Rule
35     public ExpectedException expectedEx = ExpectedException.none();
36
37     @Before
38     public void setUp() throws UnsupportedEncodingException {
39         System.setOut(new PrintStream(output, true, "UTF-8"));
40     }
41
42     @After
43     public void cleanUp() {
44         System.setOut(stdout);
45     }
46
47     @Test
48     public void noException() throws URISyntaxException, ReactorException {
49         Set<Module> modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment").toURI());
50         assertNotNull(modules);
51     }
52
53     @Test
54     public void undesirableElementException() throws URISyntaxException, ReactorException {
55         Set<Module> modules = TestUtils.loadModules(getClass().getResource
56                 ("/substatement-validator/undesirable-element").toURI());
57         testLog = output.toString();
58         assertTrue(testLog.contains("TYPE is not valid for REVISION"));
59     }
60
61     @Test
62     public void maximalElementCountException() throws URISyntaxException, ReactorException {
63         Set<Module> modules = TestUtils.loadModules(getClass().getResource
64                 ("/substatement-validator/maximal-element").toURI());
65         testLog = output.toString();
66         assertTrue(testLog.contains("Maximal count of DESCRIPTION for AUGMENT is 1"));
67     }
68
69     @Test
70     public void missingElementException() throws URISyntaxException, ReactorException {
71         expectedEx.expect(IllegalStateException.class);
72
73         Set<Module> modules = TestUtils.loadModules(getClass().getResource
74                 ("/substatement-validator/missing-element").toURI());
75     }
76
77     @Test
78     public void emptyElementException() throws URISyntaxException, ReactorException {
79         expectedEx.expect(NoSuchElementException.class);
80
81         Set<Module> modules = TestUtils.loadModules(getClass().getResource
82                 ("/substatement-validator/empty-element").toURI());
83     }
84
85     @Test
86     public void bug4310test() throws URISyntaxException, ReactorException {
87         expectedEx.expect(IllegalArgumentException.class);
88
89         Set<Module> modules = TestUtils.loadModules(getClass().getResource
90                 ("/substatement-validator/bug-4310").toURI());
91     }
92 }