KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.regex.*;
33 import junit.framework.TestCase;
34 import org.netbeans.modules.form.FormModel;
35 import org.netbeans.modules.form.GandalfPersistenceManager;
36 import org.netbeans.modules.form.PersistenceException;
37 import org.openide.filesystems.FileObject;
38
39 public abstract class LayoutTestCase extends TestCase {
40
41     private String JavaDoc testSwitch;
42
43     protected LayoutModel lm = null;
44     protected LayoutDesigner ld = null;
45     
46     protected URL JavaDoc url = getClass().getClassLoader().getResource("");
47     
48     protected FileObject startingFormFile;
49     protected File JavaDoc expectedLayoutFile;
50     
51     protected HashMap JavaDoc contInterior = new HashMap JavaDoc();
52     protected HashMap JavaDoc baselinePosition = new HashMap JavaDoc();
53     
54     protected HashMap JavaDoc prefPaddingInParent = new HashMap JavaDoc();
55     protected HashMap JavaDoc prefPadding = new HashMap JavaDoc();
56     protected HashMap JavaDoc compBounds = new HashMap JavaDoc();
57     protected HashMap JavaDoc compMinSize = new HashMap JavaDoc();
58     protected HashMap JavaDoc compPrefSize = new HashMap JavaDoc();
59     protected HashMap JavaDoc hasExplicitPrefSize = new HashMap JavaDoc();
60     
61     protected LayoutComponent lc = null;
62     
63     protected String JavaDoc goldenFilesPath = "../../../../test/unit/data/goldenfiles/";
64
65     protected String JavaDoc className;
66     
67     public LayoutTestCase(String JavaDoc name) {
68         super(name);
69     }
70     
71     
72     /**
73      * Tests the layout model by loading a form file, add/change some components there,
74      * and then compare the results with golden files.
75      * In case the dump does not match, it is saved into a file under
76      * build/test/unit/results so it can be compared with the golden file manually.namename
77      */

78     public void testLayout() throws IOException JavaDoc {
79         loadForm(startingFormFile);
80
81         Method JavaDoc[] methods = this.getClass().getMethods();
82         for (int i=0; i < methods.length; i++) {
83             Method JavaDoc m = methods[i];
84             if (m.getName().startsWith("doChanges")) {
85                 try {
86                     String JavaDoc name = getClass().getName();
87                     String JavaDoc simpleName = name.substring(name.lastIndexOf('.')+1);
88                     System.out.println("Invoking " + simpleName + "." + m.getName());
89                     m.invoke(this, null);
90                     
91                     String JavaDoc methodCount = m.getName().substring(9); // "doChanges".length()
92

93                     String JavaDoc currentLayout = getCurrentLayoutDump();
94                     // Equiv. to Tiger's code: currentLayout.replace("\n", System.getProperty("line.separator"));
95
currentLayout = Pattern.compile("\n").matcher(currentLayout) // NOI18N
96
.replaceAll(System.getProperty("line.separator")); // NOI18N
97
String JavaDoc expectedLayout = getExpectedLayoutDump(methodCount);
98
99                     System.out.print("Comparing ... ");
100
101                     boolean same = expectedLayout.equals(currentLayout);
102                     if (!same) {
103                         System.out.println("failed");
104                         System.out.println("EXPECTED: ");
105                         System.out.println(expectedLayout);
106                         System.out.println("");
107                         System.out.println("CURRENT: ");
108                         System.out.println(currentLayout);
109                         writeCurrentWrongLayout(methodCount, currentLayout);
110                     }
111                     else System.out.println("OK");
112                     System.out.println("");
113
114                     assertTrue("Model dump in step " + methodCount + " gives different result than expected", same);
115                     
116                 } catch (IllegalArgumentException JavaDoc ex) {
117                     ex.printStackTrace();
118                     fail("Error while invoking method: " + m);
119                 } catch (IllegalAccessException JavaDoc ex) {
120                     ex.printStackTrace();
121                     fail("Error while invoking method: " + m);
122                 } catch (InvocationTargetException JavaDoc ex) {
123                     ex.printStackTrace();
124                     fail("Error while invoking method: " + m);
125                 }
126             }
127         }
128     }
129
130     protected void setUp() throws Exception JavaDoc {
131         super.setUp();
132         testSwitch = System.getProperty(LayoutDesigner.TEST_SWITCH);
133         System.setProperty(LayoutDesigner.TEST_SWITCH, "true"); // NOI18N
134
}
135
136     protected void tearDown() throws Exception JavaDoc {
137         if (testSwitch != null)
138             System.setProperty(LayoutDesigner.TEST_SWITCH, testSwitch);
139         else
140             System.getProperties().remove(LayoutDesigner.TEST_SWITCH);
141         super.tearDown();
142     }
143
144     private void loadForm(FileObject file) {
145         FormModel fm = null;
146         GandalfPersistenceManager gpm = new GandalfPersistenceManager();
147         List JavaDoc errors = new ArrayList JavaDoc();
148         try {
149             fm = gpm.loadForm(file, file, null, errors);
150         } catch (PersistenceException pe) {
151             fail(pe.toString());
152         }
153         
154         if (errors.size() > 0) {
155             System.out.println("There were errors while loading the form: " + errors);
156         }
157         
158         lm = fm.getLayoutModel();
159         
160         ld = new LayoutDesigner(lm, new FakeLayoutMapper(fm,
161                                                          contInterior,
162                                                          baselinePosition,
163                                                          prefPaddingInParent,
164                                                          compBounds,
165                                                          compMinSize,
166                                                          compPrefSize,
167                                                          hasExplicitPrefSize,
168                                                          prefPadding));
169     }
170     
171     private String JavaDoc getCurrentLayoutDump() {
172         return lm.dump(null);
173     }
174     
175     private String JavaDoc getExpectedLayoutDump(String JavaDoc methodCount) throws IOException JavaDoc {
176         expectedLayoutFile = new File JavaDoc(url.getFile() + goldenFilesPath + getExpectedResultFileName(methodCount) + ".txt").getCanonicalFile();
177         int length = (int) expectedLayoutFile.length();
178         FileReader JavaDoc fr = null;
179         try {
180             fr = new FileReader JavaDoc(expectedLayoutFile);
181             char[] buf = new char[length];
182             fr.read(buf);
183             return new String JavaDoc(buf);
184         } catch (IOException JavaDoc ioe) {
185             fail(ioe.toString());
186         } finally {
187             if (fr != null) {
188                 try {
189                     fr.close();
190                 } catch (IOException JavaDoc io) {
191                     fail(io.toString());
192                 }
193             }
194         }
195         return null;
196     }
197
198     private String JavaDoc getExpectedResultFileName(String JavaDoc methodCount) {
199         return className + "-ExpectedEndModel" + methodCount;
200     }
201
202     private void writeCurrentWrongLayout(String JavaDoc methodCount, String JavaDoc dump) throws IOException JavaDoc {
203         // will go to form/build/test/unit/results
204
File JavaDoc file = new File JavaDoc(url.getFile() + "../results").getCanonicalFile();
205         if (!file.exists()) {
206             file.mkdirs();
207         }
208         file = new File JavaDoc(file, getExpectedResultFileName(methodCount)+".txt");
209         if (file.exists()) {
210             file.delete();
211         }
212         file.createNewFile();
213
214         FileWriter JavaDoc fw = null;
215         try {
216             fw = new FileWriter JavaDoc(file);
217             fw.write(dump);
218         }
219         finally {
220             if (fw != null) {
221                 fw.close();
222             }
223         }
224     }
225     
226 }
227
Popular Tags