1 30 31 package com.jgoodies.forms.layout; 32 33 import junit.framework.TestCase; 34 35 41 public final class ColumnSpecTest extends TestCase { 42 43 46 public void testRejectNegativeResizeWeight() { 47 try { 48 new ColumnSpec(ColumnSpec.DEFAULT, Sizes.DEFAULT, -1); 49 fail("The ColumnSpec constructor should reject negative resize weights."); 50 } catch (IllegalArgumentException e) { 51 } catch (Exception e) { 53 fail("The ColumnSpec constructor has thrown an unexpected exception."); 54 } 55 } 56 57 60 public void testRejectParsedNegativeResizeWeight() { 61 try { 62 new ColumnSpec("right:default:-1"); 63 fail("The ColumnSpec parser constructor should reject negative resize weights."); 64 } catch (IllegalArgumentException e) { 65 } catch (Exception e) { 67 fail("The ColumnSpec constructor has thrown an unexpected exception."); 68 } 69 } 70 71 74 public void testValidColumnSpecEncodings() { 75 ColumnSpec spec; 76 spec = new ColumnSpec(ColumnSpec.LEFT, Sizes.PREFERRED, FormSpec.NO_GROW); 77 assertEquals(spec, new ColumnSpec("l:p")); 78 assertEquals(spec, new ColumnSpec("left:p")); 79 assertEquals(spec, new ColumnSpec("l:pref")); 80 assertEquals(spec, new ColumnSpec("left:pref")); 81 82 spec = new ColumnSpec(ColumnSpec.DEFAULT, Sizes.MINIMUM, FormSpec.NO_GROW); 83 assertEquals(spec, new ColumnSpec("min")); 84 assertEquals(spec, new ColumnSpec("f:min")); 85 assertEquals(spec, new ColumnSpec("fill:min")); 86 assertEquals(spec, new ColumnSpec("f:min:nogrow")); 87 assertEquals(spec, new ColumnSpec("fill:min:grow(0)")); 88 89 spec = new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.NO_GROW); 90 assertEquals(spec, new ColumnSpec("d")); 91 assertEquals(spec, new ColumnSpec("default")); 92 assertEquals(spec, new ColumnSpec("f:default")); 93 assertEquals(spec, new ColumnSpec("fill:default")); 94 assertEquals(spec, new ColumnSpec("f:default:nogrow")); 95 assertEquals(spec, new ColumnSpec("fill:default:grow(0)")); 96 97 spec = new ColumnSpec(ColumnSpec.RIGHT, Sizes.pixel(10), FormSpec.NO_GROW); 98 assertEquals(spec, new ColumnSpec("r:10px")); 99 assertEquals(spec, new ColumnSpec("right:10px")); 100 assertEquals(spec, new ColumnSpec("right:10px:nogrow")); 101 assertEquals(spec, new ColumnSpec("right:10px:grow(0)")); 102 assertEquals(spec, new ColumnSpec("right:10px:g(0)")); 103 104 Size size = Sizes.bounded(Sizes.PREFERRED, Sizes.pixel(10), null); 105 spec = new ColumnSpec(ColumnSpec.RIGHT, size, FormSpec.NO_GROW); 106 assertEquals(spec, new ColumnSpec("right:max(10px;pref)")); 107 assertEquals(spec, new ColumnSpec("right:max(pref;10px)")); 108 109 size = Sizes.bounded(Sizes.PREFERRED, null, Sizes.pixel(10)); 110 spec = new ColumnSpec(ColumnSpec.RIGHT, size, FormSpec.NO_GROW); 111 assertEquals(spec, new ColumnSpec("right:min(10px;pref)")); 112 assertEquals(spec, new ColumnSpec("right:min(pref;10px)")); 113 114 size = Sizes.bounded(Sizes.DEFAULT, null, Sizes.pixel(10)); 115 spec = new ColumnSpec(ColumnSpec.DEFAULT, size, FormSpec.NO_GROW); 116 assertEquals(spec, new ColumnSpec("min(10px;default)")); 117 assertEquals(spec, new ColumnSpec("min(10px;d)")); 118 assertEquals(spec, new ColumnSpec("min(default;10px)")); 119 assertEquals(spec, new ColumnSpec("min(d;10px)")); 120 121 spec = new ColumnSpec(ColumnSpec.DEFAULT, Sizes.DEFAULT, FormSpec.DEFAULT_GROW); 122 assertEquals(spec, new ColumnSpec("d:grow")); 123 assertEquals(spec, new ColumnSpec("default:grow(1)")); 124 assertEquals(spec, new ColumnSpec("f:d:g")); 125 assertEquals(spec, new ColumnSpec("f:d:grow(1.0)")); 126 assertEquals(spec, new ColumnSpec("f:d:g(1.0)")); 127 128 spec = new ColumnSpec(ColumnSpec.DEFAULT, Sizes.DEFAULT, 0.75); 129 assertEquals(spec, new ColumnSpec("d:grow(0.75)")); 130 assertEquals(spec, new ColumnSpec("default:grow(0.75)")); 131 assertEquals(spec, new ColumnSpec("f:d:grow(0.75)")); 132 assertEquals(spec, new ColumnSpec("fill:default:grow(0.75)")); 133 } 134 135 136 139 public void testRejectInvalidColumnSpecEncodings() { 140 assertRejects("karsten"); 141 assertRejects("d:a:b:"); 142 assertRejects("top:default:grow"); 143 assertRejects("bottom:10px"); 144 } 145 146 147 150 public void testUnmodifyable() { 151 ColumnSpec unmodifyableSpec = new ColumnSpec("p").asUnmodifyable(); 152 try { 153 unmodifyableSpec.setDefaultAlignment(ColumnSpec.CENTER); 154 fail("An unmodifyable ColumnSpec should reject alignment changes."); 155 } catch (UnsupportedOperationException e) { 156 } 158 try { 159 unmodifyableSpec.setSize(Sizes.MINIMUM); 160 fail("An unmodifyable ColumnSpec should reject size changes."); 161 } catch (UnsupportedOperationException e) { 162 } 164 try { 165 unmodifyableSpec.setResizeWeight(5.5); 166 fail("An unmodifyable ColumnSpec should reject resize weight changes."); 167 } catch (UnsupportedOperationException e) { 168 } 170 } 171 172 173 175 179 private void assertEquals(ColumnSpec spec1, ColumnSpec spec2) { 180 if (!spec1.getDefaultAlignment().equals(spec2.getDefaultAlignment())) { 181 fail("Alignment mismatch: spec1=" + spec1 + "; spec2=" + spec2); 182 } 183 if (!spec1.getSize().equals(spec2.getSize())) { 184 fail("Size mismatch: spec1=" + spec1 + "; spec2=" + spec2); 185 } 186 if (!(spec1.getResizeWeight() == spec2.getResizeWeight())) { 187 fail("Resize weight mismatch: spec1=" + spec1 + "; spec2=" + spec2); 188 } 189 } 190 191 196 private void assertRejects(String invalidEncoding) { 197 try { 198 new ColumnSpec(invalidEncoding); 199 fail("The parser should reject the invalid encoding:" + invalidEncoding); 200 } catch (Exception e) { 201 } 203 } 204 205 } 206 | Popular Tags |