KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > persistence > ModeParserTest


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.core.windows.persistence;
21
22 import java.awt.Frame JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import org.netbeans.junit.NbTest;
31 import org.netbeans.junit.NbTestCase;
32 import org.netbeans.junit.NbTestSuite;
33
34 import org.netbeans.core.windows.Constants;
35 import org.netbeans.core.windows.SplitConstraint;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.URLMapper;
38
39 /** Functionality tests for saving and loading Mode configuration data
40  *
41  * @author Marek Slama
42  */

43 public class ModeParserTest extends NbTestCase {
44     
45     public ModeParserTest() {
46         super("");
47     }
48     
49     public ModeParserTest(java.lang.String JavaDoc testName) {
50         super(testName);
51     }
52     
53     public static void main(java.lang.String JavaDoc[] args) {
54         junit.textui.TestRunner.run(suite());
55     }
56     
57     public static NbTest suite() {
58         NbTestSuite suite = new NbTestSuite(ModeParserTest.class);
59         return suite;
60     }
61
62     protected void setUp () throws Exception JavaDoc {
63 // File localRoot;
64
// try {
65
// File tempFile = File.createTempFile("test", null);
66
// File parent = tempFile.getParentFile();
67
// localRoot = new File(parent, tempFile.getName().substring(0, tempFile.getName().length() - 4));
68
// if (!localRoot.exists()) {
69
// localRoot.mkdirs();
70
// }
71
// System.out.println("dir created=" + localRoot);
72
// } catch (IOException exc) {
73
// throw exc;
74
// }
75
// FileObject fo = FileUtil.toFileObject(localRoot);
76
// PersistenceManager.getDefault().setRootLocalFolder(fo);
77
}
78     
79     ////////////////////////////////
80
//Testing VALID data
81
////////////////////////////////
82
/** Test of loaded data
83      */

84     public void testLoadMode01 () throws Exception JavaDoc {
85         System.out.println("");
86         System.out.println("ModeParserTest.testLoadMode01 START");
87         
88         ModeParser modeParser = createModeParser("data/valid/Windows/Modes","mode01");
89         
90         ModeConfig modeCfg = modeParser.load();
91         
92         //Check loaded data
93
assertNotNull("Could not load data.",modeCfg);
94         
95         InternalConfig internalCfg = modeParser.getInternalConfig();
96         
97         assertNotNull("Could not load internal data.",internalCfg);
98         
99         //Check internal data
100
assertEquals("moduleCodeNameBase","org.netbeans.core.ui",internalCfg.moduleCodeNameBase);
101         assertEquals("moduleCodeNameBase","1",internalCfg.moduleCodeNameRelease);
102         assertEquals("moduleCodeNameBase","1.2",internalCfg.moduleSpecificationVersion);
103         
104         //Check data
105
assertEquals("Mode state",Constants.MODE_STATE_JOINED,modeCfg.state);
106         
107         assertNotNull("Mode constraints are null",modeCfg.constraints);
108         assertEquals("Mode constraints are not empty",0,modeCfg.constraints.length);
109         
110         assertEquals("Mode type",Constants.MODE_KIND_EDITOR,modeCfg.kind);
111         
112         assertNull("Mode bounds are not null",modeCfg.bounds);
113         assertNull("Mode relative bounds are not null",modeCfg.relativeBounds);
114         
115         assertNotNull("Active TC is null",modeCfg.selectedTopComponentID);
116         assertEquals("Active TC is not empty","",modeCfg.selectedTopComponentID);
117         assertNotNull("Previous active TC is null",modeCfg.previousSelectedTopComponentID);
118         assertEquals("Previous active TC is not empty","",modeCfg.previousSelectedTopComponentID);
119         assertTrue("Permanent",modeCfg.permanent);
120         
121         System.out.println("ModeParserTest.testLoadMode01 FINISH");
122     }
123     
124     /** Test of loaded data
125      */

126     public void testLoadMode02 () throws Exception JavaDoc {
127         System.out.println("");
128         System.out.println("ModeParserTest.testLoadMode02 START");
129         
130         ModeParser modeParser = createModeParser("data/valid/Windows/Modes","mode02");
131         
132         ModeConfig modeCfg = modeParser.load();
133         
134         //Check loaded data
135
assertNotNull("Could not load data.", modeCfg);
136         
137         //Check data
138
assertEquals("Mode state",Constants.MODE_STATE_JOINED,modeCfg.state);
139         
140         assertNotNull("Mode constraints are null",modeCfg.constraints);
141         assertEquals("Mode constraints array has incorrect size",2,modeCfg.constraints.length);
142         SplitConstraint item;
143         item = modeCfg.constraints[0];
144         assertEquals("Mode constraint 0 - orientation",Constants.VERTICAL,item.orientation);
145         assertEquals("Mode constraint 0 - index",1,item.index);
146         assertEquals("Mode constraint 0 - weight",0.3,item.splitWeight,0.0);
147         
148         item = modeCfg.constraints[1];
149         assertEquals("Mode constraint 1 - orientation",Constants.HORIZONTAL,item.orientation);
150         assertEquals("Mode constraint 1 - index",0,item.index);
151         assertEquals("Mode constraint 1 - weight",0.5,item.splitWeight,0.0);
152         
153         assertNull("Mode bounds are not null",modeCfg.bounds);
154         assertNull("Mode relative bounds are not null",modeCfg.relativeBounds);
155         
156         assertEquals("Mode type",Constants.MODE_KIND_VIEW,modeCfg.kind);
157         assertEquals("Active TC","output",modeCfg.selectedTopComponentID);
158         assertFalse("Permanent",modeCfg.permanent);
159         
160         System.out.println("ModeParserTest.testLoadMode02 FINISH");
161     }
162     
163     /** Test of loaded data
164      */

165     public void testLoadMode03 () throws Exception JavaDoc {
166         System.out.println("");
167         System.out.println("ModeParserTest.testLoadMode03 START");
168         
169         ModeParser modeParser = createModeParser("data/valid/Windows/Modes","mode03");
170         
171         ModeConfig modeCfg = modeParser.load();
172         
173         //Check loaded data
174
assertNotNull("Could not load data.", modeCfg);
175         
176         //Check data
177
assertEquals("Mode state",Constants.MODE_STATE_SEPARATED,modeCfg.state);
178         
179         assertNotNull("Mode constraints are null",modeCfg.constraints);
180         assertEquals("Mode constraints are not empty",0,modeCfg.constraints.length);
181         
182         assertNotNull("Mode bounds are null",modeCfg.bounds);
183         
184         assertEquals("Mode bounds x",0,modeCfg.bounds.x);
185         assertEquals("Mode bounds y",0,modeCfg.bounds.y);
186         assertEquals("Mode bounds width",400,modeCfg.bounds.width);
187         assertEquals("Mode bounds height",200,modeCfg.bounds.height);
188         
189         assertNull("Mode relative bounds are not null",modeCfg.relativeBounds);
190         
191         assertEquals("Mode frame state",Frame.NORMAL,modeCfg.frameState);
192         
193         assertEquals("Active TC","output",modeCfg.selectedTopComponentID);
194         assertTrue("Permanent",modeCfg.permanent);
195         
196         System.out.println("ModeParserTest.testLoadMode03 FINISH");
197     }
198     
199     /** Test of loaded data
200      */

201     public void testLoadMode04 () throws Exception JavaDoc {
202         System.out.println("");
203         System.out.println("ModeParserTest.testLoadMode04 START");
204         
205         ModeParser modeParser = createModeParser("data/valid/Windows/Modes","mode04");
206         
207         ModeConfig modeCfg = modeParser.load();
208         
209         //Check loaded data
210
assertNotNull("Could not load data.", modeCfg);
211         
212         //Check data
213
assertEquals("Mode state",Constants.MODE_STATE_SEPARATED,modeCfg.state);
214         
215         assertNotNull("Mode constraints are null",modeCfg.constraints);
216         assertEquals("Mode constraints are not empty",0,modeCfg.constraints.length);
217         
218         assertNull("Mode bounds are not null",modeCfg.bounds);
219         
220         assertNotNull("Mode relative bounds are null",modeCfg.relativeBounds);
221         
222         assertEquals("Mode relative bounds x",0,modeCfg.relativeBounds.x);
223         assertEquals("Mode relative bounds y",0,modeCfg.relativeBounds.y);
224         assertEquals("Mode relative bounds width",40,modeCfg.relativeBounds.width);
225         assertEquals("Mode relative bounds height",20,modeCfg.relativeBounds.height);
226         
227         assertEquals("Mode frame state",Frame.MAXIMIZED_BOTH,modeCfg.frameState);
228         
229         assertEquals("Active TC","output",modeCfg.selectedTopComponentID);
230         assertTrue("Permanent",modeCfg.permanent);
231         
232         System.out.println("ModeParserTest.testLoadMode04 FINISH");
233     }
234
235         /** Test of loaded data
236      */

237     public void testLoadMode05 () throws Exception JavaDoc {
238         System.out.println("");
239         System.out.println("ModeParserTest.testLoadMode05 START");
240         
241         ModeParser modeParser = createModeParser("data/valid/Windows/Modes","mode05");
242         
243         ModeConfig modeCfg = modeParser.load();
244         
245         //Check loaded data
246
assertNotNull("Could not load data.", modeCfg);
247         
248         //Check data
249
assertEquals("Mode kind",Constants.MODE_KIND_SLIDING,modeCfg.kind);
250         assertEquals("Mode sliding side",Constants.LEFT, modeCfg.side);
251 // assertEquals("Mode state",Constants.MODE_STATE_SEPARATED,modeCfg.state);
252

253 // assertNotNull("Mode constraints are null",modeCfg.constraints);
254
// assertEquals("Mode constraints are not empty",0,modeCfg.constraints.length);
255

256 // assertNull("Mode bounds are not null",modeCfg.bounds);
257

258 // assertNotNull("Mode relative bounds are null",modeCfg.relativeBounds);
259

260 // assertEquals("Mode relative bounds x",0,modeCfg.relativeBounds.x);
261
// assertEquals("Mode relative bounds y",0,modeCfg.relativeBounds.y);
262
// assertEquals("Mode relative bounds width",40,modeCfg.relativeBounds.width);
263
// assertEquals("Mode relative bounds height",20,modeCfg.relativeBounds.height);
264

265 // assertEquals("Mode frame state",Frame.MAXIMIZED_BOTH,modeCfg.frameState);
266

267         assertEquals("Active TC","output",modeCfg.selectedTopComponentID);
268         assertTrue("Permanent",modeCfg.permanent);
269         
270         System.out.println("ModeParserTest.testLoadMode05 FINISH");
271     }
272
273         /** Test of sliding mode with pre-defined slide-in sizes
274      */

275     public void testLoadMode06 () throws Exception JavaDoc {
276         System.out.println("");
277         System.out.println("ModeParserTest.testLoadMode06 START");
278         
279         ModeParser modeParser = createModeParser("data/valid/Windows/Modes","mode06");
280         
281         ModeConfig modeCfg = modeParser.load();
282         
283         //Check loaded data
284
assertNotNull("Could not load data.", modeCfg);
285         
286         //Check data
287
assertEquals("Mode kind",Constants.MODE_KIND_SLIDING,modeCfg.kind);
288         assertEquals("Mode sliding side",Constants.LEFT, modeCfg.side);
289         
290         assertEquals("Active TC","output",modeCfg.selectedTopComponentID);
291         assertTrue("Permanent",modeCfg.permanent);
292         
293         Map JavaDoc<String JavaDoc,Integer JavaDoc> slideInSizes = modeCfg.slideInSizes;
294         assertNotNull( slideInSizes );
295         assertEquals( 2, slideInSizes.size() );
296         assertEquals( Integer.valueOf(123), slideInSizes.get( "output" ) );
297         assertEquals( Integer.valueOf(321), slideInSizes.get( "someOtherTopComponentId" ) );
298         
299         System.out.println("ModeParserTest.testLoadMode06 FINISH");
300     }
301
302     /** Test of a mode with with previously active tc
303      */

304     public void testLoadMode07 () throws Exception JavaDoc {
305         System.out.println("");
306         System.out.println("ModeParserTest.testLoadMode07 START");
307         
308         ModeParser modeParser = createModeParser("data/valid/Windows/Modes","mode07");
309         
310         ModeConfig modeCfg = modeParser.load();
311         
312         //Check loaded data
313
assertNotNull("Could not load data.",modeCfg);
314         
315         InternalConfig internalCfg = modeParser.getInternalConfig();
316         
317         assertNotNull("Could not load internal data.",internalCfg);
318         
319         //Check data
320
assertEquals("Mode state",Constants.MODE_STATE_JOINED,modeCfg.state);
321         
322         assertEquals("Mode type",Constants.MODE_KIND_VIEW,modeCfg.kind);
323         
324         assertEquals("Active TC","filesystems",modeCfg.selectedTopComponentID);
325         assertEquals("Previous active TC","explorer",modeCfg.previousSelectedTopComponentID);
326         assertTrue("Permanent",modeCfg.permanent);
327         
328         System.out.println("ModeParserTest.testLoadMode07 FINISH");
329     }
330     
331     /** Test of saving
332      */

333     public void testSaveMode01 () throws Exception JavaDoc {
334         System.out.println("");
335         System.out.println("ModeParserTest.testSaveMode01 START");
336         
337         ModeParser modeParser = createModeParser("data/valid/Windows/Modes","mode01");
338         
339         ModeConfig modeCfg1 = modeParser.load();
340         
341         modeParser.save(modeCfg1);
342         
343         ModeConfig modeCfg2 = modeParser.load();
344         
345         //Compare data
346
assertTrue("Compare configuration data",modeCfg1.equals(modeCfg2));
347                 
348         System.out.println("ModeParserTest.testSaveMode01 FINISH");
349     }
350     
351     /** Test of saving
352      */

353     public void testSaveMode02 () throws Exception JavaDoc {
354         System.out.println("");
355         System.out.println("ModeParserTest.testSaveMode02 START");
356         
357         ModeParser modeParser = createModeParser("data/valid/Windows/Modes","mode02");
358         
359         ModeConfig modeCfg1 = modeParser.load();
360         
361         modeParser.save(modeCfg1);
362         
363         ModeConfig modeCfg2 = modeParser.load();
364         
365         //Compare data
366
assertTrue("Compare configuration data",modeCfg1.equals(modeCfg2));
367                 
368         System.out.println("ModeParserTest.testSaveMode02 FINISH");
369     }
370     
371     /** Test of saving
372      */

373     public void testSaveMode03 () throws Exception JavaDoc {
374         System.out.println("");
375         System.out.println("ModeParserTest.testSaveMode03 START");
376         
377         ModeParser modeParser = createModeParser("data/valid/Windows/Modes","mode03");
378         
379         ModeConfig modeCfg1 = modeParser.load();
380         
381         modeParser.save(modeCfg1);
382         
383         ModeConfig modeCfg2 = modeParser.load();
384         
385         //Compare data
386
assertTrue("Compare configuration data",modeCfg1.equals(modeCfg2));
387                 
388         System.out.println("ModeParserTest.testSaveMode03 FINISH");
389     }
390     
391     /** Test of saving
392      */

393     public void testSaveMode04 () throws Exception JavaDoc {
394         System.out.println("");
395         System.out.println("ModeParserTest.testSaveMode04 START");
396         
397         ModeParser modeParser = createModeParser("data/valid/Windows/Modes","mode04");
398         
399         ModeConfig modeCfg1 = modeParser.load();
400         
401         modeParser.save(modeCfg1);
402         
403         ModeConfig modeCfg2 = modeParser.load();
404         
405         //Compare data
406
assertTrue("Compare configuration data",modeCfg1.equals(modeCfg2));
407                 
408         System.out.println("ModeParserTest.testSaveMode04 FINISH");
409     }
410     
411     
412     /** Test of saving sliding mode with predefined slide-in sizes.
413      */

414     public void testSaveMode06 () throws Exception JavaDoc {
415         System.out.println("");
416         System.out.println("ModeParserTest.testSaveMode06 START");
417         
418         ModeParser modeParser = createModeParser("data/valid/Windows/Modes","mode06");
419         
420         ModeConfig modeCfg1 = modeParser.load();
421         
422         modeParser.save(modeCfg1);
423         
424         ModeConfig modeCfg2 = modeParser.load();
425         
426         //Compare data
427
assertTrue("Compare configuration data",modeCfg1.equals(modeCfg2));
428                 
429         System.out.println("ModeParserTest.testSaveMode06 FINISH");
430     }
431     
432     /** Test of saving a mode with previously active tc.
433      */

434     public void testSaveMode07 () throws Exception JavaDoc {
435         System.out.println("");
436         System.out.println("ModeParserTest.testSaveMode07 START");
437         
438         ModeParser modeParser = createModeParser("data/valid/Windows/Modes","mode07");
439         
440         ModeConfig modeCfg1 = modeParser.load();
441         
442         modeParser.save(modeCfg1);
443         
444         ModeConfig modeCfg2 = modeParser.load();
445         
446         //Compare data
447
assertTrue("Compare configuration data",modeCfg1.equals(modeCfg2));
448                 
449         System.out.println("ModeParserTest.testSaveMode07 FINISH");
450     }
451     
452     ////////////////////////////////
453
//Testing INVALID data
454
////////////////////////////////
455
/** Test of missing file
456      */

457     public void testLoadMode01Invalid () throws Exception JavaDoc {
458         System.out.println("");
459         System.out.println("ModeParserTest.testLoadMode01Invalid START");
460         
461         ModeParser modeParser = createModeParser("data/invalid/Windows/Modes","mode01");
462         
463         try {
464             modeParser.load();
465         } catch (FileNotFoundException JavaDoc exc) {
466             //Missing file detected
467
//ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
468
System.out.println("ModeParserTest.testLoadMode01Invalid FINISH");
469             return;
470         }
471         
472         fail("Missing file was not detected.");
473     }
474     
475     /** Test of empty file
476      */

477     public void testLoadMode02Invalid () throws Exception JavaDoc {
478         System.out.println("");
479         System.out.println("ModeParserTest.testLoadMode02Invalid START");
480         
481         ModeParser modeParser = createModeParser("data/invalid/Windows/Modes","mode02");
482         
483         try {
484             modeParser.load();
485         } catch (IOException JavaDoc exc) {
486             //ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
487
System.out.println("ModeParserTest.testLoadMode02Invalid FINISH");
488             return;
489         }
490         
491         fail("Empty file was not detected.");
492     }
493     
494     /** Test of missing required attribute "unique" of element "name".
495      */

496     public void testLoadMode03Invalid () throws Exception JavaDoc {
497         System.out.println("");
498         System.out.println("ModeParserTest.testLoadMode03Invalid START");
499         
500         ModeParser modeParser = createModeParser("data/invalid/Windows/Modes","mode03");
501         
502         try {
503             modeParser.load();
504         } catch (IOException JavaDoc exc) {
505             //ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
506
System.out.println("ModeParserTest.testLoadMode03Invalid FINISH");
507             return;
508         }
509         
510         fail("Missing required attribute \"unique\" of element \"name\" was not detected.");
511     }
512     
513     /** Test of file name and value of attribute "unique" mismatch.
514      */

515     public void testLoadMode04Invalid () throws Exception JavaDoc {
516         System.out.println("");
517         System.out.println("ModeParserTest.testLoadMode04Invalid START");
518         
519         ModeParser modeParser = createModeParser("data/invalid/Windows/Modes","mode04");
520         
521         try {
522             modeParser.load();
523         } catch (IOException JavaDoc exc) {
524             //ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
525
System.out.println("ModeParserTest.testLoadMode04Invalid FINISH");
526             return;
527         }
528         
529         fail("Mismatch of file name and value of attribute \"unique\" of element \"name\" was not detected.");
530     }
531     
532     private ModeParser createModeParser (String JavaDoc path, String JavaDoc name) {
533         URL JavaDoc url;
534         url = ModeParserTest.class.getResource(path);
535         assertNotNull("url not found.",url);
536         
537         FileObject parentFolder = URLMapper.findFileObject(url);
538         assertNotNull("Test parent folder not found. ParentFolder is null.",url);
539
540         Set JavaDoc setLocal = new HashSet JavaDoc();
541         ModeParser modeParser = new ModeParser(name,setLocal);
542         modeParser.setInLocalFolder(true);
543         modeParser.setLocalParentFolder(parentFolder);
544         
545         return modeParser;
546     }
547     
548 }
549
Popular Tags