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