Browse Source

Provide extensive error information on GridLayout.OutOfBoundsException and IllegalArgumentException (#11461)

tags/8.8.0.beta1
Martin Vysny 5 years ago
parent
commit
c79dca4391

+ 10
- 2
server/src/main/java/com/vaadin/ui/GridLayout.java View 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;
}


+ 24
- 0
server/src/test/java/com/vaadin/tests/server/component/gridlayout/GridLayoutTest.java View 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;

Loading…
Cancel
Save