X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fstmt%2Fretest%2FYangParserNegativeTest.java;h=cd6e6f5ff5443b3ff75440146cfc8812da1fc510;hb=04fa25a4fe8957f6492618aa9a1e9a4f9af39df4;hp=aa214a1c99eb8536af75ab5fcf54cdbf9a1c6987;hpb=d175b8bafc3b7dd3b74114b8e4b308e945b63fe0;p=yangtools.git diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserNegativeTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserNegativeTest.java index aa214a1c99..cd6e6f5ff5 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserNegativeTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/retest/YangParserNegativeTest.java @@ -11,27 +11,46 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import com.google.common.base.Throwables; +import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileInputStream; import java.io.InputStream; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser; import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException; import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; +import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream; import org.opendaylight.yangtools.yang.parser.util.YangValidationException; public class YangParserNegativeTest { + private final PrintStream stdout = System.out; + private final ByteArrayOutputStream output = new ByteArrayOutputStream(); + private String testLog; + + @Before + public void setUp() throws UnsupportedEncodingException { + System.setOut(new PrintStream(output, true, "UTF-8")); + } + + @After + public void cleanUp() { + System.setOut(stdout); + } + @Test public void testInvalidImport() throws Exception { File yang = new File(getClass().getResource("/negative-scenario/testfile1.yang").toURI()); try { - try (InputStream stream = new FileInputStream(yang)) { + try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) { TestUtils.loadModule(stream); fail("SomeModifiersUnresolvedException should be thrown"); } @@ -39,7 +58,7 @@ public class YangParserNegativeTest { Throwable rootCause = Throwables.getRootCause(e); assertTrue(rootCause instanceof InferenceException); assertTrue(rootCause.getMessage().startsWith("Imported module")); - assertTrue(rootCause.getMessage().endsWith("was not found.")); + assertTrue(rootCause.getMessage().contains("was not found.")); } } @@ -47,9 +66,9 @@ public class YangParserNegativeTest { public void testTypeNotFound() throws Exception { File yang = new File(getClass().getResource("/negative-scenario/testfile2.yang").toURI()); try { - try (InputStream stream = new FileInputStream(yang)) { + try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) { TestUtils.loadModule(stream); - fail("IllegalArgumentException should be thrown"); + fail("InferenceException should be thrown"); } } catch (SomeModifiersUnresolvedException e) { Throwable rootCause = Throwables.getRootCause(e); @@ -65,9 +84,9 @@ public class YangParserNegativeTest { File yang2 = new File(getClass().getResource("/negative-scenario/testfile3.yang").toURI()); try { final List streams = new ArrayList<>(2); - try (InputStream testFile0 = new FileInputStream(yang1)) { + try (InputStream testFile0 = new NamedFileInputStream(yang1, yang1.getPath())) { streams.add(testFile0); - try (InputStream testFile3 = new FileInputStream(yang2)) { + try (InputStream testFile3 = new NamedFileInputStream(yang2, yang2.getPath())) { streams.add(testFile3); assertEquals("Expected loaded files count is 2", 2, streams.size()); TestUtils.loadModules(streams); @@ -77,9 +96,8 @@ public class YangParserNegativeTest { } catch (SomeModifiersUnresolvedException e) { final Throwable rootCause = Throwables.getRootCause(e); assertTrue(rootCause instanceof InferenceException); - assertEquals( - "Augment target not found: Absolute{path=[(urn:simple.container.demo?revision=1970-01-01)unknown]}", - rootCause.getMessage()); + assertTrue(rootCause.getMessage().startsWith( + "Augment target 'Absolute{path=[(urn:simple.container.demo?revision=1970-01-01)unknown]}' not found")); } } @@ -87,15 +105,14 @@ public class YangParserNegativeTest { public void testInvalidRefine() throws Exception { File yang = new File(getClass().getResource("/negative-scenario/testfile4.yang").toURI()); try { - try (InputStream stream = new FileInputStream(yang)) { + try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) { TestUtils.loadModule(stream); fail("SourceException should be thrown"); } } catch (SourceException e) { - assertTrue(e - .getMessage() - .contains( - "Error in module 'test4' in the refine of uses 'Relative{path=[(urn:simple.container.demo?revision=1970-01-01)node]}': can not perform refine of 'PRESENCE' for the target 'LEAF_LIST'.")); + assertTrue(e.getMessage().contains("Error in module 'test4' in the refine of uses 'Relative{path=[" + + "(urn:simple.container.demo?revision=1970-01-01)node]}': can not perform refine of 'PRESENCE' for" + + " the target 'LEAF_LIST'.")); } } @@ -103,7 +120,7 @@ public class YangParserNegativeTest { public void testInvalidLength() throws Exception { File yang = new File(getClass().getResource("/negative-scenario/testfile5.yang").toURI()); try { - try (InputStream stream = new FileInputStream(yang)) { + try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) { TestUtils.loadModule(stream); fail("YangParseException should be thrown"); } @@ -116,7 +133,7 @@ public class YangParserNegativeTest { public void testInvalidRange() throws Exception { File yang = new File(getClass().getResource("/negative-scenario/testfile6.yang").toURI()); try { - try (InputStream stream = new FileInputStream(yang)) { + try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) { TestUtils.loadModule(stream); fail("Exception should be thrown"); } @@ -129,13 +146,15 @@ public class YangParserNegativeTest { public void testDuplicateContainer() throws Exception { File yang = new File(getClass().getResource("/negative-scenario/duplicity/container.yang").toURI()); try { - try (InputStream stream = new FileInputStream(yang)) { + try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) { TestUtils.loadModule(stream); fail("SourceException should be thrown"); } } catch (SourceException e) { - String expected = "Error in module 'container': can not add '(urn:simple.container.demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container.demo?revision=1970-01-01)foo' already declared."; - assertEquals(expected, e.getMessage()); + String expected = "Error in module 'container': cannot add '(urn:simple.container" + + ".demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container" + + ".demo?revision=1970-01-01)foo' already declared."; + assertTrue(e.getMessage().contains(expected)); } } @@ -143,13 +162,15 @@ public class YangParserNegativeTest { public void testDuplicateContainerList() throws Exception { File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-list.yang").toURI()); try { - try (InputStream stream = new FileInputStream(yang)) { + try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) { TestUtils.loadModule(stream); fail("SourceException should be thrown"); } } catch (SourceException e) { - String expected = "Error in module 'container-list': can not add '(urn:simple.container.demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container.demo?revision=1970-01-01)foo' already declared."; - assertEquals(expected, e.getMessage()); + String expected = "Error in module 'container-list': cannot add '(urn:simple.container" + + ".demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container" + + ".demo?revision=1970-01-01)foo' already declared."; + assertTrue(e.getMessage().contains(expected)); } } @@ -157,13 +178,15 @@ public class YangParserNegativeTest { public void testDuplicateContainerLeaf() throws Exception { File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-leaf.yang").toURI()); try { - try (InputStream stream = new FileInputStream(yang)) { + try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) { TestUtils.loadModule(stream); fail("SourceException should be thrown"); } } catch (SourceException e) { - String expected = "Error in module 'container-leaf': can not add '(urn:simple.container.demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container.demo?revision=1970-01-01)foo' already declared."; - assertEquals(expected, e.getMessage()); + String expected = "Error in module 'container-leaf': cannot add '(urn:simple.container" + + ".demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container" + + ".demo?revision=1970-01-01)foo' already declared."; + assertTrue(e.getMessage().contains(expected)); } } @@ -171,13 +194,15 @@ public class YangParserNegativeTest { public void testDuplicateTypedef() throws Exception { File yang = new File(getClass().getResource("/negative-scenario/duplicity/typedef.yang").toURI()); try { - try (InputStream stream = new FileInputStream(yang)) { + try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) { TestUtils.loadModule(stream); - fail("IllegalArgumentException should be thrown"); + fail("SourceException should be thrown"); } - } catch (IllegalArgumentException e) { - String expected = "Duplicate name for typedef (urn:simple.container.demo?revision=1970-01-01)int-ext"; - assertEquals(expected, e.getMessage()); + } catch (SourceException e) { + String expected = "Error in module 'typedef': cannot add '(urn:simple.container" + + ".demo?revision=1970-01-01)int-ext'. Node name collision: '(urn:simple.container" + + ".demo?revision=1970-01-01)int-ext' already declared."; + assertTrue(e.getMessage().startsWith(expected)); } } @@ -185,14 +210,11 @@ public class YangParserNegativeTest { public void testDuplicityInAugmentTarget1() throws Exception { File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI()); File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment1.yang").toURI()); - try { - try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) { - TestUtils.loadModules(Arrays.asList(stream1, stream2)); - fail("IllegalStateException should be thrown"); - } - } catch (IllegalStateException e) { - assertEquals(e.getMessage(), - "An augment cannot add node named 'id' because this name is already used in target"); + try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath()); + InputStream stream2 = new NamedFileInputStream(yang2, yang2.getPath())) { + TestUtils.loadModules(Arrays.asList(stream1, stream2)); + testLog = output.toString(); + assertTrue(testLog.contains("An augment cannot add node named 'id' because this name is already used in target")); } } @@ -200,14 +222,11 @@ public class YangParserNegativeTest { public void testDuplicityInAugmentTarget2() throws Exception { File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI()); File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment2.yang").toURI()); - try { - try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) { - TestUtils.loadModules(Arrays.asList(stream1, stream2)); - fail("IllegalStateException should be thrown"); - } - } catch (IllegalStateException e) { - assertEquals(e.getMessage(), - "An augment cannot add node named 'delta' because this name is already used in target"); + try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath()); + InputStream stream2 = new NamedFileInputStream(yang2, yang2.getPath())) { + TestUtils.loadModules(Arrays.asList(stream1, stream2)); + testLog = output.toString(); + assertTrue(testLog.contains("An augment cannot add node named 'delta' because this name is already used in target")); } } @@ -215,14 +234,12 @@ public class YangParserNegativeTest { public void testMandatoryInAugment() throws Exception { File yang1 = new File(getClass().getResource("/negative-scenario/testfile8.yang").toURI()); File yang2 = new File(getClass().getResource("/negative-scenario/testfile7.yang").toURI()); - try { - try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) { - TestUtils.loadModules(Arrays.asList(stream1, stream2)); - fail("IllegalArgumentException should be thrown"); - } - } catch (IllegalArgumentException e) { - String expected = "An augment cannot add node 'linkleaf' because it is mandatory and in module different from target"; - assertEquals(expected, e.getMessage()); + try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath()); + InputStream stream2 = new NamedFileInputStream(yang2, yang2.getPath())) { + TestUtils.loadModules(Arrays.asList(stream1, stream2)); + testLog = output.toString(); + assertTrue(testLog.contains( + "An augment cannot add node 'linkleaf' because it is mandatory and in module different from target")); } } @@ -258,12 +275,13 @@ public class YangParserNegativeTest { public void testInvalidListKeyDefinition() throws Exception { File yang1 = new File(getClass().getResource("/negative-scenario/invalid-list-key-def.yang").toURI()); try { - try (InputStream stream1 = new FileInputStream(yang1)) { + try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath())) { TestUtils.loadModule(stream1); - fail("IllegalArgumentException should be thrown"); + fail("InferenceException should be thrown"); } - } catch (IllegalArgumentException e) { - String expected = "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def?revision=1970-01-01)application-map'"; + } catch (InferenceException e) { + String expected = "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def?revision=1970-01-01)" + + "application-map'"; assertTrue(e.getMessage().startsWith(expected)); } }