1 19 20 package org.netbeans.modules.form.layoutdesign; 21 22 import java.io.File ; 23 import java.io.FileReader ; 24 import java.io.FileWriter ; 25 import java.io.IOException ; 26 import java.net.URL ; 27 import java.util.regex.Pattern ; 28 import junit.framework.TestCase; 29 30 public class LayoutModelTest extends TestCase { 31 32 public LayoutModelTest(String name) { 33 super(name); 34 } 35 36 42 public void testDump() throws IOException { 43 String golden = getExpectedDump(); 44 45 LayoutModel layoutModel = new LayoutModel(); 46 LayoutModelTest.fillModelToDump(layoutModel); 47 String dump = layoutModel.dump(null); 48 dump = Pattern.compile("\n").matcher(dump) .replaceAll(System.getProperty("line.separator")); 51 boolean same = dump.equals(golden); 52 if (!same) { 53 writeWrongDump(dump); 54 } 55 56 assertTrue("Model dump gives different result than expected", same); 57 } 58 59 private String getExpectedDump() throws IOException { 60 URL url = getClass().getClassLoader().getResource(""); 61 File file = new File (url.getFile() + "../../../../test/unit/data/goldenfiles/layoutModelDump.pass") 64 .getCanonicalFile(); 65 assert file.length() < 100000; 66 int length = (int) file.length(); 67 68 FileReader fr = null; 69 try { 70 fr = new FileReader (file); 71 char[] buf = new char[length]; 72 fr.read(buf); 73 return new String (buf); 74 } 75 finally { 76 if (fr != null) { 77 fr.close(); 78 } 79 } 80 } 81 82 static void fillModelToDump(LayoutModel model) { 83 int id = 0; 84 85 LayoutComponent container = new LayoutComponent(Integer.toString(++id), true); 86 model.addComponent(container, null, -1); 87 88 LayoutComponent lc1 = new LayoutComponent(Integer.toString(++id), false); 89 model.addComponent(lc1, container, -1); 90 LayoutComponent lc2 = new LayoutComponent(Integer.toString(++id), false); 91 model.addComponent(lc2, container, -1); 92 LayoutComponent lc3 = new LayoutComponent(Integer.toString(++id), false); 93 model.addComponent(lc3, container, -1); 94 LayoutComponent lc4 = new LayoutComponent(Integer.toString(++id), false); 95 model.addComponent(lc4, container, -1); 96 LayoutComponent lc5 = new LayoutComponent(Integer.toString(++id), false); 97 model.addComponent(lc5, container, -1); 98 99 LayoutInterval li; 101 LayoutInterval subGroup; 102 LayoutInterval group = new LayoutInterval(LayoutInterval.SEQUENTIAL); 103 group.add(new LayoutInterval(LayoutInterval.SINGLE), -1); 104 group.add(lc1.getLayoutInterval(LayoutConstants.HORIZONTAL), -1); 105 group.add(new LayoutInterval(LayoutInterval.SINGLE), -1); 106 li = lc2.getLayoutInterval(LayoutConstants.HORIZONTAL); 107 li.setSizes(LayoutConstants.USE_PREFERRED_SIZE, 80, LayoutConstants.USE_PREFERRED_SIZE); 108 group.add(li, -1); 109 li = new LayoutInterval(LayoutInterval.SINGLE); 110 li.setSizes(LayoutConstants.NOT_EXPLICITLY_DEFINED, 100, Short.MAX_VALUE); 111 group.add(li, -1); 112 container.getLayoutRoot(LayoutConstants.HORIZONTAL).add(group, -1); 113 114 group = new LayoutInterval(LayoutInterval.SEQUENTIAL); 116 subGroup = new LayoutInterval(LayoutInterval.PARALLEL); 117 subGroup.setGroupAlignment(LayoutConstants.TRAILING); 118 li = lc3.getLayoutInterval(LayoutConstants.HORIZONTAL); 119 li.setAlignment(LayoutConstants.LEADING); 120 subGroup.add(li, -1); 121 subGroup.add(lc4.getLayoutInterval(LayoutConstants.HORIZONTAL), -1); 122 group.add(new LayoutInterval(LayoutInterval.SINGLE), -1); 123 group.add(subGroup, -1); 124 group.add(new LayoutInterval(LayoutInterval.SINGLE), -1); 125 li = lc5.getLayoutInterval(LayoutConstants.HORIZONTAL); 126 li.setSizes(0, LayoutConstants.NOT_EXPLICITLY_DEFINED, Short.MAX_VALUE); 127 group.add(li, -1); 128 group.add(new LayoutInterval(LayoutInterval.SINGLE), -1); 129 container.getLayoutRoot(LayoutConstants.HORIZONTAL).add(group, -1); 130 131 group = new LayoutInterval(LayoutInterval.SEQUENTIAL); 133 group.add(new LayoutInterval(LayoutInterval.SINGLE), -1); 134 subGroup = new LayoutInterval(LayoutInterval.PARALLEL); 135 subGroup.setGroupAlignment(LayoutConstants.BASELINE); 136 subGroup.add(lc1.getLayoutInterval(LayoutConstants.VERTICAL), -1); 137 subGroup.add(lc2.getLayoutInterval(LayoutConstants.VERTICAL), -1); 138 group.add(subGroup, -1); 139 group.add(new LayoutInterval(LayoutInterval.SINGLE), -1); 140 141 subGroup = new LayoutInterval(LayoutInterval.PARALLEL); 143 li = new LayoutInterval(LayoutInterval.SEQUENTIAL); 144 li.add(lc3.getLayoutInterval(LayoutConstants.VERTICAL), -1); 145 li.add(new LayoutInterval(LayoutInterval.SINGLE), -1); 146 li.add(lc4.getLayoutInterval(LayoutConstants.VERTICAL), -1); 147 subGroup.add(li, -1); 148 subGroup.add(lc5.getLayoutInterval(LayoutConstants.VERTICAL), -1); 149 group.add(subGroup, -1); 150 li = new LayoutInterval(LayoutInterval.SINGLE); 151 li.setSizes(LayoutConstants.NOT_EXPLICITLY_DEFINED, 50, Short.MAX_VALUE); 152 group.add(li, -1); 153 154 container.getLayoutRoot(LayoutConstants.VERTICAL).add(group, -1); 155 } 156 157 private void writeWrongDump(String dump) throws IOException { 158 URL url = getClass().getClassLoader().getResource(""); 159 File file = new File (url.getFile() + "../results").getCanonicalFile(); 161 if (!file.exists()) { 162 file.mkdirs(); 163 } 164 file = new File (file, "layoutModelDump.fail"); 165 if (file.exists()) { 166 file.delete(); 167 } 168 file.createNewFile(); 169 170 FileWriter fw = null; 171 try { 172 fw = new FileWriter (file); 173 fw.write(dump); 174 } 175 finally { 176 if (fw != null) { 177 fw.close(); 178 } 179 } 180 } 181 } 182 | Popular Tags |