1 30 31 package com.jgoodies.forms.layout; 32 33 import junit.framework.TestCase; 34 35 41 public final class CellConstraintsTest extends TestCase { 42 43 46 public void testRejectNonPositiveOriginAndExtent() { 47 assertRejects( 0, 1, 1, 1); 48 assertRejects(-1, 1, 1, 1); 49 assertRejects( 1, 0, 1, 1); 50 assertRejects( 1, -1, 1, 1); 51 assertRejects( 1, 1, 0, 1); 52 assertRejects( 1, 1, -1, 1); 53 assertRejects( 1, 1, 1, 0); 54 assertRejects( 1, 1, 1, -1); 55 } 56 57 60 public void testValidEncodings() { 61 assertEquals(new CellConstraints(), 62 new CellConstraints("1, 1")); 63 64 assertEquals(new CellConstraints(2, 3), 65 new CellConstraints("2, 3")); 66 67 assertEquals(new CellConstraints(3, 4, 2, 5), 68 new CellConstraints("3, 4, 2, 5")); 69 70 assertEquals(new CellConstraints(5, 6, 71 CellConstraints.LEFT, 72 CellConstraints.BOTTOM), 73 new CellConstraints("5, 6, left, bottom")); 74 75 assertEquals(new CellConstraints(7, 8, 3, 2, 76 CellConstraints.FILL, 77 CellConstraints.DEFAULT), 78 new CellConstraints("7, 8, 3, 2, f, d")); 79 } 80 81 84 public void testRejectInvalidCellConstraintsEncodings() { 85 assertRejects("0, 1, 1, 1"); assertRejects("0, 1, 1"); assertRejects("0, 1, 1, 1, 1"); assertRejects("1"); assertRejects("1, 1, fill"); assertRejects("1, 1, 3, 4, f"); assertRejects("1, 1, top, center"); assertRejects("1, 1, fill, left"); assertRejects("1, 1, 2, 3, t, c"); assertRejects("1, 1, 2, 3, f, l"); } 96 97 100 public void testRejectInvalidCellConstraintsAlignments() { 101 try { 102 new CellConstraints(1, 1, CellConstraints.BOTTOM, CellConstraints.CENTER); 103 fail("The CellConstraints constructor should reject invalid orientations."); 104 } catch (IllegalArgumentException e) { 105 } catch (Exception e) { 107 fail("The constructor has thrown an unexpected exception: " + e); 108 } 109 try { 110 new CellConstraints(1, 1, CellConstraints.CENTER, CellConstraints.RIGHT); 111 fail("The CellConstraints constructor should reject invalid orientations."); 112 } catch (IllegalArgumentException e) { 113 } catch (Exception e) { 115 fail("The constructor has thrown an unexpected exception: " + e); 116 } 117 CellConstraints cc = new CellConstraints(); 118 try { 119 cc.xy(1, 1, CellConstraints.BOTTOM, CellConstraints.CENTER); 120 fail("The CellConstraints setter should reject invalid orientations."); 121 } catch (IllegalArgumentException e) { 122 } catch (Exception e) { 124 fail("The setter has thrown an unexpected exception: " + e); 125 } 126 try { 127 cc.xy(1, 1, CellConstraints.BOTTOM, CellConstraints.CENTER); 128 fail("The CellConstraints setter should reject invalid orientations."); 129 } catch (IllegalArgumentException e) { 130 } catch (Exception e) { 132 fail("The setter has thrown an unexpected exception: " + e); 133 } 134 } 135 136 138 142 private void assertRejects(String invalidEncoding) { 143 try { 144 new CellConstraints(invalidEncoding); 145 fail("The parser should reject the invalid encoding: " + invalidEncoding); 146 } catch (IllegalArgumentException e) { 147 } catch (IndexOutOfBoundsException e) { 149 } catch (Exception e) { 151 fail("The parser has thrown an unexpected exception for:" 152 + invalidEncoding 153 + "; exception=" + e); 154 } 155 } 156 157 161 private void assertRejects(int gridX, int gridY, 162 int gridWidth, int gridHeight) { 163 try { 164 new CellConstraints(gridX, gridY, gridWidth, gridHeight); 165 fail("The CellConstraints constructor should reject non-positive bounds values."); 166 } catch (IndexOutOfBoundsException e) { 167 } catch (Exception e) { 169 fail("The CellConstraints constructor has thrown an unexpected exception:" + e); 170 } 171 } 172 173 177 private void assertEquals(CellConstraints cc1, CellConstraints cc2) { 178 if ( cc1.gridX != cc2.gridX 179 || cc1.gridY != cc2.gridY 180 || cc1.gridWidth != cc2.gridWidth 181 || cc1.gridHeight != cc2.gridHeight) { 182 fail("Bounds mismatch: cc1=" + cc1 + "; cc2=" + cc2); 183 } 184 if ( cc1.hAlign != cc2.hAlign 185 || cc1.vAlign != cc2.vAlign) { 186 fail("Alignment mismatch: cc1=" + cc1 + "; cc2=" + cc2); 187 } 188 if (!cc1.insets.equals(cc2.insets)) { 189 fail("Insets mismatch: cc1=" + cc1 + "; cc2=" + cc2); 190 } 191 } 192 193 194 } 195 196 | Popular Tags |