KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import org.netbeans.junit.NbTest;
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.junit.NbTestSuite;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.URLMapper;
31
32 /** Functionality tests for saving and loading TCGroup configuration data
33  *
34  * @author Marek Slama
35  */

36 public class TCGroupParserTest extends NbTestCase {
37
38     public TCGroupParserTest() {
39         super("");
40     }
41     
42     public TCGroupParserTest(java.lang.String JavaDoc testName) {
43         super(testName);
44     }
45     
46     public static void main(java.lang.String JavaDoc[] args) {
47         junit.textui.TestRunner.run(suite());
48     }
49     
50     public static NbTest suite() {
51         NbTestSuite suite = new NbTestSuite(TCGroupParserTest.class);
52         return suite;
53     }
54
55     protected void setUp () throws Exception JavaDoc {
56     }
57     
58     ////////////////////////////////
59
//Testing CORRECT data
60
////////////////////////////////
61
/** Test of loaded data
62      */

63     public void testLoadTCGroup00 () throws Exception JavaDoc {
64         System.out.println("");
65         System.out.println("TCGroupParserTest.testLoadTCGroup00 START");
66         
67         TCGroupParser tcGroupParser = createGroupParser("data/valid/Windows/Groups/group00","tcgroup00");
68         
69         TCGroupConfig tcGroupCfg = tcGroupParser.load();
70         
71         //Check loaded data
72
assertNotNull("Could not load data.", tcGroupCfg);
73         
74         assertTrue("Attribute \"open\". Expected true but is false.", tcGroupCfg.open);
75         assertFalse("Attribute \"close\". Expected false but is true.", tcGroupCfg.close);
76         assertTrue("Attribute \"was-opened\". Expected true but is false.", tcGroupCfg.wasOpened);
77         
78         System.out.println("TCGroupParserTest.testLoadTCGroup00 FINISH");
79     }
80     
81     /** Test of loaded data
82      */

83     public void testLoadTCGroup01 () throws Exception JavaDoc {
84         System.out.println("");
85         System.out.println("TCGroupParserTest.testLoadTCGroup01 START");
86         
87         TCGroupParser tcGroupParser = createGroupParser("data/valid/Windows/Groups/group00","tcgroup01");
88         
89         TCGroupConfig tcGroupCfg = tcGroupParser.load();
90         
91         //Check loaded data
92
assertNotNull("Could not load data.", tcGroupCfg);
93         
94         assertFalse("Attribute \"open\". Expected false but is true.", tcGroupCfg.open);
95         assertTrue("Attribute \"close\". Expected true but is false.", tcGroupCfg.close);
96         assertFalse("Attribute \"was-opened\". Expected false but is true.", tcGroupCfg.wasOpened);
97         
98         System.out.println("TCGroupParserTest.testLoadTCGroup01 FINISH");
99     }
100     
101     /** Test of saving
102      */

103     public void testSaveTCGroup00 () throws Exception JavaDoc {
104         System.out.println("");
105         System.out.println("TCGroupParserTest.testSaveTCGroup00 START");
106         
107         TCGroupParser tcGroupParser = createGroupParser("data/valid/Windows/Groups/group00","tcgroup00");
108         
109         TCGroupConfig tcGroupCfg1 = tcGroupParser.load();
110         
111         tcGroupParser.save(tcGroupCfg1);
112         
113         TCGroupConfig tcGroupCfg2 = tcGroupParser.load();
114         
115         //Compare data
116
assertTrue("Compare configuration data",tcGroupCfg1.equals(tcGroupCfg2));
117                 
118         System.out.println("TCGroupParserTest.testSaveTCGroup00 FINISH");
119     }
120     
121     /** Test of saving
122      */

123     public void testSaveTCGroup01 () throws Exception JavaDoc {
124         System.out.println("");
125         System.out.println("TCGroupParserTest.testSaveTCGroup01 START");
126         
127         TCGroupParser tcGroupParser = createGroupParser("data/valid/Windows/Groups/group00","tcgroup01");
128         
129         TCGroupConfig tcGroupCfg1 = tcGroupParser.load();
130         
131         tcGroupParser.save(tcGroupCfg1);
132         
133         TCGroupConfig tcGroupCfg2 = tcGroupParser.load();
134         
135         //Compare data
136
assertTrue("Compare configuration data",tcGroupCfg1.equals(tcGroupCfg2));
137         
138         System.out.println("TCGroupParserTest.testSaveTCGroup01 FINISH");
139     }
140     
141     ////////////////////////////////////////
142
//Tests of handling of INVALID data
143
////////////////////////////////////////
144
/** Test of missing file
145      */

146     public void testLoadTCGroup00Invalid () throws Exception JavaDoc {
147         System.out.println("");
148         System.out.println("TCGroupParserTest.testLoadTCGroup00Invalid START");
149         
150         TCGroupParser tcGroupParser = createGroupParser("data/invalid/Windows/Groups/group00","tcgroup00");
151         
152         try {
153             tcGroupParser.load();
154         } catch (FileNotFoundException JavaDoc exc) {
155             //Missing file detected
156
//ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
157
System.out.println("TCGroupParserTest.testLoadTCGroup00Invalid FINISH");
158             return;
159         }
160         
161         fail("Missing file was not detected.");
162     }
163     
164     /** Test of empty file
165      */

166     public void testLoadTCGroup01Invalid () throws Exception JavaDoc {
167         System.out.println("");
168         System.out.println("TCGroupParserTest.testLoadTCGroup01Invalid START");
169         
170         TCGroupParser tcGroupParser = createGroupParser("data/invalid/Windows/Groups/group00","tcgroup01");
171         
172         try {
173             tcGroupParser.load();
174         } catch (IOException JavaDoc exc) {
175             //ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
176
System.out.println("TCGroupParserTest.testLoadTCGroup01Invalid FINISH");
177             return;
178         }
179         
180         fail("Empty file was not detected.");
181     }
182     
183     /** Test of missing required attribute "id" of element "properties".
184      */

185     public void testLoadTCGroup02Invalid () throws Exception JavaDoc {
186         System.out.println("");
187         System.out.println("TCGroupParserTest.testLoadTCGroup02Invalid START");
188         
189         TCGroupParser tcGroupParser = createGroupParser("data/invalid/Windows/Groups/group00","tcgroup02");
190         
191         try {
192             tcGroupParser.load();
193         } catch (IOException JavaDoc exc) {
194             //ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
195
System.out.println("TCGroupParserTest.testLoadTCGroup02Invalid FINISH");
196             return;
197         }
198         
199         fail("Missing required attribute \"id\" of element \"properties\" was not detected.");
200     }
201     
202     /** Test of file name and "id" mismatch.
203      */

204     public void testLoadTCGroup03Invalid () throws Exception JavaDoc {
205         System.out.println("");
206         System.out.println("TCGroupParserTest.testLoadTCGroup03Invalid START");
207         
208         TCGroupParser tcGroupParser = createGroupParser("data/invalid/Windows/Groups/group00","tcgroup03");
209                 
210         try {
211             tcGroupParser.load();
212         } catch (IOException JavaDoc exc) {
213             //ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
214
System.out.println("TCGroupParserTest.testLoadTCGroup03Invalid FINISH");
215             return;
216         }
217         
218         fail("Mismatch of file name and value of attribute \"id\" of element \"properties\" was not detected.");
219     }
220     
221     private TCGroupParser createGroupParser (String JavaDoc path, String JavaDoc name) {
222         URL JavaDoc url;
223         url = TCGroupParserTest.class.getResource(path);
224         assertNotNull("url not found.",url);
225         
226         FileObject [] foArray = URLMapper.findFileObjects(url);
227         assertNotNull("Test parent folder not found. Array is null.",foArray);
228         assertTrue("Test parent folder not found. Array is empty.",foArray.length > 0);
229         
230         FileObject parentFolder = foArray[0];
231         assertNotNull("Test parent folder not found. ParentFolder is null.",parentFolder);
232         
233         TCGroupParser tcGroupParser = new TCGroupParser(name);
234         tcGroupParser.setInLocalFolder(true);
235         tcGroupParser.setLocalParentFolder(parentFolder);
236         
237         return tcGroupParser;
238     }
239 }
240
Popular Tags