]> source.dussan.org Git - vaadin-framework.git/commitdiff
Provide extensive error information on GridLayout.OutOfBoundsException and IllegalArg...
authorMartin Vysny <martin@vysny.me>
Thu, 21 Feb 2019 05:36:43 +0000 (07:36 +0200)
committerAnastasia Smirnova <anasmi@utu.fi>
Thu, 21 Feb 2019 05:36:43 +0000 (07:36 +0200)
server/src/main/java/com/vaadin/ui/GridLayout.java
server/src/test/java/com/vaadin/tests/server/component/gridlayout/GridLayoutTest.java

index 0a3e139ea93f706e8454c6ad76dbe05aa5571ccd..46275dfdafd7f0d4c64569f75ad8871043b3ec9c 100644 (file)
@@ -203,8 +203,9 @@ public class GridLayout extends AbstractLayout
 
         // Checks the validity of the coordinates
         if (column2 < column1 || row2 < row1) {
-            throw new IllegalArgumentException(
-                    "Illegal coordinates for the component");
+            throw new IllegalArgumentException(String.format(
+                    "Illegal coordinates for the component: %s!<=%s, %s!<=%s",
+                    column1, column2, row1, row2));
         }
         if (column1 < 0 || row1 < 0 || column2 >= getColumns()
                 || row2 >= getRows()) {
@@ -611,6 +612,11 @@ public class GridLayout extends AbstractLayout
             return childData.row2;
         }
 
+        @Override
+        public String toString() {
+            return String.format("Area{%s,%s - %s,%s}", getColumn1(), getRow1(),
+                    getColumn2(), getRow2());
+        }
     }
 
     private static boolean componentsOverlap(ChildComponentData a,
@@ -694,6 +700,8 @@ public class GridLayout extends AbstractLayout
          * @param areaOutOfBounds
          */
         public OutOfBoundsException(Area areaOutOfBounds) {
+            super(String.format("%s, layout dimension: %sx%s", areaOutOfBounds,
+                    getColumns(), getRows()));
             this.areaOutOfBounds = areaOutOfBounds;
         }
 
index abf2e823d5dde61fef7696eb0efed62da09c8347..88b06d7a2d8a09a201077bb59eca3e428c7fba07 100644 (file)
@@ -99,6 +99,30 @@ public class GridLayoutTest {
         assertEquals(1, gl.getColumnExpandRatio(1), 0);
     }
 
+    @Test
+    public void verifyOutOfBoundsExceptionContainsHelpfulMessage() {
+        GridLayout grid = new GridLayout(1, 1);
+        try {
+            grid.addComponent(new Label(), 3, 3);
+            fail("Should have failed");
+        } catch (GridLayout.OutOfBoundsException ex) {
+            assertEquals("Area{3,3 - 3,3}, layout dimension: 1x1",
+                    ex.getMessage());
+        }
+    }
+
+    @Test
+    public void verifyAddComponentFailsWithHelpfulMessageOnInvalidArgs() {
+        GridLayout grid = new GridLayout(6, 6);
+        try {
+            grid.addComponent(new Label(), 3, 3, 2, 2);
+            fail("Should have failed");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Illegal coordinates for the component: 3!<=2, 3!<=2",
+                    ex.getMessage());
+        }
+    }
+
     private void assertContentPositions(GridLayout grid) {
         assertEquals(grid.getComponentCount(), children.length);
         int c = 0;