Repository: decorators-squad/eo-yaml
Pull request #243 (2020-03-26 20:45:57)

Adds new exceptions classes
PR for #214 
Adds new YamlReadingException and YamlIndentationException exceptions classes.


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java b/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
index c93d23a..e0f9803 100644
--- a/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
+++ b/src/main/java/com/amihaiemil/eoyaml/AllYamlLines.java
@@ -27,6 +27,7 @@
  */
 package com.amihaiemil.eoyaml;
 
+import com.amihaiemil.eoyaml.exceptions.YamlReadingException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -130,7 +131,7 @@ final class AllYamlLines implements YamlLines {
         } else if(this.lines().size() == 1) {
             node = new ReadPlainScalarValue(first);
         } else {
-            throw new IllegalStateException(
+            throw new YamlReadingException(
                 "Could not parse YAML starting at line " + (first.number() + 1)
               + " . It should be a sequence (line should start with '-'), "
               + "a mapping (line should contain ':') or it should be a plain "
diff --git a/src/main/java/com/amihaiemil/eoyaml/WellIndented.java b/src/main/java/com/amihaiemil/eoyaml/WellIndented.java
index b601e23..52f0a50 100644
--- a/src/main/java/com/amihaiemil/eoyaml/WellIndented.java
+++ b/src/main/java/com/amihaiemil/eoyaml/WellIndented.java
@@ -27,6 +27,7 @@
  */
 package com.amihaiemil.eoyaml;
 
+import com.amihaiemil.eoyaml.exceptions.YamlIndentationException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
@@ -103,7 +104,7 @@ final class WellIndented implements YamlLines {
                 int lineIndent = line.indentation();
                 if(previous.requireNestedIndentation()) {
                     if(lineIndent != prevIndent+2) {
-                        throw new IllegalStateException(
+                        throw new YamlIndentationException(
                             "Indentation of line " + (line.number() + 1)
                              + " is not ok. It should be greater than the one"
                              + " of line " + (previous.number() + 1)
@@ -112,7 +113,7 @@ final class WellIndented implements YamlLines {
                     }
                 } else {
                     if(!"---".equals(previous.trimmed()) && lineIndent > prevIndent) {
-                        throw new IllegalStateException(
+                        throw new YamlIndentationException(
                             "Indentation of line " + (line.number() +1) + " is "
                             + "greater than the one of line "
                             + (previous.number() + 1) + ". "
diff --git a/src/main/java/com/amihaiemil/eoyaml/exceptions/YamlIndentationException.java b/src/main/java/com/amihaiemil/eoyaml/exceptions/YamlIndentationException.java
new file mode 100644
index 0000000..4b5b2fc
--- /dev/null
+++ b/src/main/java/com/amihaiemil/eoyaml/exceptions/YamlIndentationException.java
@@ -0,0 +1,52 @@
+/**
+ * Copyright (c) 2016-2020, Mihai Emil Andronache
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ *  list of conditions and the following disclaimer.
+ *  Redistributions in binary form must reproduce the above copyright notice,
+ *  this list of conditions and the following disclaimer in the documentation
+ *  and/or other materials provided with the distribution.
+ * Neither the name of the copyright holder nor the names of its
+ *  contributors may be used to endorse or promote products derived from
+ *  this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+package com.amihaiemil.eoyaml.exceptions;
+
+/**
+ * Represents an indentation exception.
+ * @author Aislan Nadrowski (aislan.nadrowski@gmail.com)
+ * @version $Id$
+ * @since 3.1.5
+ */
+public final class YamlIndentationException extends RuntimeException {
+
+    /**
+     * Constructor without parameters.
+    */
+    public YamlIndentationException() {
+        this("Indentation is not ok.");
+    }
+
+    /**
+     * Constructor with parameters.
+     * @param message Exception message.
+     */
+    public YamlIndentationException(final String message) {
+        super(message); 
+    }
+}
diff --git a/src/main/java/com/amihaiemil/eoyaml/exceptions/YamlReadingException.java b/src/main/java/com/amihaiemil/eoyaml/exceptions/YamlReadingException.java
new file mode 100644
index 0000000..7c82a4f
--- /dev/null
+++ b/src/main/java/com/amihaiemil/eoyaml/exceptions/YamlReadingException.java
@@ -0,0 +1,52 @@
+/**
+ * Copyright (c) 2016-2020, Mihai Emil Andronache
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ *  list of conditions and the following disclaimer.
+ *  Redistributions in binary form must reproduce the above copyright notice,
+ *  this list of conditions and the following disclaimer in the documentation
+ *  and/or other materials provided with the distribution.
+ * Neither the name of the copyright holder nor the names of its
+ *  contributors may be used to endorse or promote products derived from
+ *  this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+package com.amihaiemil.eoyaml.exceptions;
+
+/**
+ * Represents a reading exception.
+ * @author Aislan Nadrowski (aislan.nadrowski@gmail.com)
+ * @version $Id$
+ * @since 3.1.5
+ */
+public final class YamlReadingException extends RuntimeException {
+ 
+    /**
+     * Constructor without parameters.
+    */
+    public YamlReadingException() {
+        this("Something went wrong while reading YAML.");
+    }
+
+    /**
+     * Constructor with parameters.
+     * @param message Exception message.
+    */
+    public YamlReadingException(final String message) {
+        super(message); 
+    }
+}


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/AllYamlLinesTest.java b/src/test/java/com/amihaiemil/eoyaml/AllYamlLinesTest.java
index e26b964..b33f73b 100644
--- a/src/test/java/com/amihaiemil/eoyaml/AllYamlLinesTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/AllYamlLinesTest.java
@@ -27,6 +27,7 @@
  */
 package com.amihaiemil.eoyaml;
 
+import com.amihaiemil.eoyaml.exceptions.YamlReadingException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
@@ -268,7 +269,7 @@ public final class AllYamlLinesTest {
         try {
             yamlLines.toYamlNode(new RtYamlLine("---", -1));
             Assert.fail("Expected IllegalStateException!");
-        } catch (final IllegalStateException ex) {
+        } catch (final YamlReadingException ex) {
             final String message = ex.getMessage();
             MatcherAssert.assertThat(
                 message,
diff --git a/src/test/java/com/amihaiemil/eoyaml/WellIndentedTest.java b/src/test/java/com/amihaiemil/eoyaml/WellIndentedTest.java
index d0e4584..448e1a3 100644
--- a/src/test/java/com/amihaiemil/eoyaml/WellIndentedTest.java
+++ b/src/test/java/com/amihaiemil/eoyaml/WellIndentedTest.java
@@ -27,6 +27,7 @@
  */
 package com.amihaiemil.eoyaml;
 
+import com.amihaiemil.eoyaml.exceptions.YamlIndentationException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -98,7 +99,7 @@ public final class WellIndentedTest {
     /**
      * WellIndented complains on badly indented nested mapping.
      */
-    @Test(expected = IllegalStateException.class)
+    @Test(expected = YamlIndentationException.class)
     public void complainsOnBadlyIndentedNestedMapping() {
         final List<YamlLine> lines = new ArrayList<>();
         lines.add(new RtYamlLine("first: somethingElse", 0));
@@ -111,13 +112,13 @@ public final class WellIndentedTest {
             wiLines,
             Matchers.iterableWithSize(5)
         );
-        Assert.fail("IllegalStateException was expected!");
+        Assert.fail("YamlIndentationException was expected!");
     }
     
     /**
      * WellIndented complains on badly indented nested sequence.
      */
-    @Test(expected = IllegalStateException.class)
+    @Test(expected = YamlIndentationException.class)
     public void complainsOnBadlyIndentedNestedSequence() {
         final List<YamlLine> lines = new ArrayList<>();
         lines.add(new RtYamlLine("first: somethingElse", 0));
@@ -130,13 +131,13 @@ public final class WellIndentedTest {
             wiLines,
             Matchers.iterableWithSize(5)
         );
-        Assert.fail("IllegalStateException was expected!");
+        Assert.fail("YamlIndentationException was expected!");
     }
     
     /**
      * WellIndented complains on badly indented sequence.
      */
-    @Test(expected = IllegalStateException.class)
+    @Test(expected = YamlIndentationException.class)
     public void complainsOnBadlyIndentedSequence() {
         final List<YamlLine> lines = new ArrayList<>();
         lines.add(new RtYamlLine("- first", 0));
@@ -147,13 +148,13 @@ public final class WellIndentedTest {
             wiLines,
             Matchers.iterableWithSize(3)
         );
-        Assert.fail("IllegalStateException was expected!");
+        Assert.fail("YamlIndentationException was expected!");
     }
     
     /**
      * WellIndented complains on badly indented mapping.
      */
-    @Test(expected = IllegalStateException.class)
+    @Test(expected = YamlIndentationException.class)
     public void complainsOnBadlyIndentedMapping() {
         final List<YamlLine> lines = new ArrayList<>();
         lines.add(new RtYamlLine("first: somethingElse", 0));
@@ -164,6 +165,6 @@ public final class WellIndentedTest {
             wiLines,
             Matchers.iterableWithSize(3)
         );
-        Assert.fail("IllegalStateException was expected!");
+        Assert.fail("YamlIndentationException was expected!");
     }
 }