KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > layoutdesign > LayoutModelTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form.layoutdesign;
21
22 import java.io.File JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.FileWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28 import junit.framework.TestCase;
29
30 public class LayoutModelTest extends TestCase {
31
32     public LayoutModelTest(String JavaDoc name) {
33         super(name);
34     }
35
36     /**
37      * Tests the layout model by comparing a dumped layout model (filled with
38      * a layout structure) with a golden file. In case the dump does not match,
39      * it is saved into a file under build/test/unit/results so it can be
40      * compared with the golden file manually.
41      */

42     public void testDump() throws IOException JavaDoc {
43         String JavaDoc golden = getExpectedDump();
44
45         LayoutModel layoutModel = new LayoutModel();
46         LayoutModelTest.fillModelToDump(layoutModel);
47         String JavaDoc dump = layoutModel.dump(null);
48         dump = Pattern.compile("\n").matcher(dump) // NOI18N
49
.replaceAll(System.getProperty("line.separator")); // NOI18N
50

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 JavaDoc getExpectedDump() throws IOException JavaDoc {
60         URL JavaDoc url = getClass().getClassLoader().getResource("");
61         // classloader points to form/build/test/unit/classes/
62
// golden file is in form/test/unit/data/goldenfiles/
63
File JavaDoc file = new File JavaDoc(url.getFile() + "../../../../test/unit/data/goldenfiles/layoutModelDump.pass")
64                     .getCanonicalFile();
65         assert file.length() < 100000;
66         int length = (int) file.length();
67
68         FileReader JavaDoc fr = null;
69         try {
70             fr = new FileReader JavaDoc(file);
71             char[] buf = new char[length];
72             fr.read(buf);
73             return new String JavaDoc(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         // horizontal, first row
100
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         // horizontal, second row
115
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         // vertical, first row
132
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         // vertical, second row
142
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 JavaDoc dump) throws IOException JavaDoc {
158         URL JavaDoc url = getClass().getClassLoader().getResource("");
159         // will go to form/build/test/unit/results
160
File JavaDoc file = new File JavaDoc(url.getFile() + "../results").getCanonicalFile();
161         if (!file.exists()) {
162             file.mkdirs();
163         }
164         file = new File JavaDoc(file, "layoutModelDump.fail");
165         if (file.exists()) {
166             file.delete();
167         }
168         file.createNewFile();
169
170         FileWriter JavaDoc fw = null;
171         try {
172             fw = new FileWriter JavaDoc(file);
173             fw.write(dump);
174         }
175         finally {
176             if (fw != null) {
177                 fw.close();
178             }
179         }
180     }
181 }
182
Popular Tags