KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ddapi > test > unit > src > org > netbeans > api > web > dd > InvalidStatesTest


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 ddapi.test.unit.src.org.netbeans.api.web.dd;
21
22 import org.netbeans.api.web.dd.*;
23 import org.netbeans.api.web.dd.common.VersionNotSupportedException;
24 import org.netbeans.api.web.dd.common.NameAlreadyUsedException;
25 import java.io.*;
26 import junit.framework.*;
27 import org.netbeans.junit.*;
28 import org.openide.filesystems.*;
29 import java.beans.*;
30
31
32 public class InvalidStatesTest extends NbTestCase {
33     private static final String JavaDoc VERSION="2.4";
34     private static final String JavaDoc[] expectedEvents =
35         {"PCE:STATUS[1]:dd_status:2:1",
36          "PCE:STATUS[0]:dd_status:1:0",
37          "PCE:STATUS[0]:SessionTimeout:null:30"
38         };
39     private static java.util.List JavaDoc evtList = new java.util.ArrayList JavaDoc();
40     
41     private WebApp webApp;
42     
43     public InvalidStatesTest(java.lang.String JavaDoc testName) {
44         super(testName);
45     }
46     
47     public static void main(java.lang.String JavaDoc[] args) {
48         junit.textui.TestRunner.run(suite());
49     }
50     
51     public static Test suite() {
52         TestSuite suite = new NbTestSuite(InvalidStatesTest.class);
53         
54         return suite;
55     }
56     
57     /** Test of greeting method, of class HelloWorld. */
58     public void test_InvalidDataReading () {
59         System.out.println("TEST:Invalid Data Reading");
60         assertEquals("Incorrect servlet spec.version :",VERSION,webApp.getVersion());
61         assertEquals("Incorrect dd status :",WebApp.STATE_INVALID_UNPARSABLE,webApp.getStatus());
62         assertNotNull("Error mustn't be null :", webApp.getError());
63         System.out.println("Expected Exception :"+webApp.getError());
64         assertNull("Session Config must be null :", webApp.getSingleSessionConfig());
65     }
66
67     public void test_Correction1 () {
68         System.out.println("TEST:Invalid Data Correction - editing");
69         // replacing web.xml with web_parsable.xml
70
File dataDir = new File(System.getProperty("test.data.dir")+"/invalid");
71         FileObject dataFolder = FileUtil.toFileObject(dataDir);
72         FileObject fo1 = dataFolder.getFileObject("web_parsable","xml");
73         assertTrue("FileObject invalid/web_parsable.xml not found",null != fo1);
74         
75         try {
76             FileLock lock = fo.lock();
77             OutputStream os = fo.getOutputStream(lock);
78             InputStream is = fo1.getInputStream();
79             int b;
80             while ((b = is.read())!=-1)
81                 os.write(b);
82             is.close();
83             os.close();
84             lock.releaseLock();
85         } catch (IOException ex) {
86             throw new AssertionFailedErrorException("Writing data Failed ",ex);
87         }
88         assertEquals("Incorrect dd status :",WebApp.STATE_INVALID_PARSABLE,webApp.getStatus());
89         assertNotNull("Error mustn't be null :", webApp.getError());
90         System.out.println("Expected Exception :"+webApp.getError());
91         assertNotNull("Session Config mustn't be null :", webApp.getSingleSessionConfig());
92         assertNull("Session Timeout must be null :", webApp.getSingleSessionConfig().getSessionTimeout());
93     }
94     
95     public void test_Correction2 () {
96         System.out.println("TEST:Invalid Data Correction - programmatic correction");
97         WebApp webAppCopy=null;
98         try {
99             webAppCopy = DDProvider.getDefault().getDDRootCopy(fo);
100         } catch (java.io.IOException JavaDoc ex) {
101             ex.printStackTrace();
102         }
103         assertTrue("WebAppCopy not created ", null != webAppCopy);
104         assertNotNull("Session Config mustn't be null :", webAppCopy.getSingleSessionConfig());
105         
106         webAppCopy.getSingleSessionConfig().setSessionTimeout(new java.math.BigInteger JavaDoc("30"));
107         System.out.println("status = "+webAppCopy.getStatus());
108         try {
109             webAppCopy.write(fo);
110         } catch (java.io.IOException JavaDoc ex) {
111             throw new AssertionFailedErrorException("write method failed",ex);
112         }
113         System.out.println("sessio nConfig = "+webApp.getSingleSessionConfig());
114         assertNotNull("Session Config mustn't be null :", webApp.getSingleSessionConfig());
115         assertEquals("Incorrect dd status :",WebApp.STATE_VALID,webApp.getStatus());
116         assertNull("Error must be null :", webApp.getError());
117         assertNotNull("Session Timeout mustn't be null :", webApp.getSingleSessionConfig().getSessionTimeout());
118     }
119     
120     public void test_CheckEvents () {
121         System.out.println("TEST:Property Change Events");
122         assertEquals("Incorrect number of PCE :",expectedEvents.length,evtList.size());
123         for (int i=0;i<expectedEvents.length;i++) {
124             assertEquals("Incorrect PCE["+i+"] :",expectedEvents[i],evtList.get(i));
125         }
126     }
127      
128     private static FileObject fo;
129     
130     protected void setUp() throws Exception JavaDoc {
131         super.setUp();
132         System.out.println("setUp() .......................");
133         
134         if (fo==null) {
135             File dataDir = new File(System.getProperty("test.data.dir")+"/invalid");
136             FileObject dataFolder = FileUtil.toFileObject(dataDir);
137             fo = dataFolder.getFileObject("web","xml");
138         };
139         try {
140             webApp = DDProvider.getDefault().getDDRoot(fo);
141         } catch (java.io.IOException JavaDoc ex) {
142             ex.printStackTrace();
143         }
144         assertTrue("WebApp object not found", null != webApp);
145         list = new MyListener(webApp);
146         webApp.addPropertyChangeListener(list);
147     }
148     
149     protected void tearDown() {
150         webApp.removePropertyChangeListener(list);
151     }
152     
153     private MyListener list;
154     private static class MyListener implements PropertyChangeListener {
155         WebApp webApp;
156         MyListener (WebApp webApp) {
157             this.webApp=webApp;
158         }
159         public void propertyChange(PropertyChangeEvent evt) {
160             System.out.println("propertyChanged() "+evt.getPropertyName()+":"+evt.getOldValue()+":"+evt.getNewValue());
161             evtList.add("PCE:STATUS["+webApp.getStatus()+"]:"+getDDProperty(evt.getPropertyName())+":"+evt.getOldValue()+":"+evt.getNewValue());
162         }
163     }
164     
165     private static String JavaDoc getDDProperty(String JavaDoc fullName) {
166     int index = fullName.lastIndexOf('/');
167         return (index>0?fullName.substring(index+1):fullName);
168     }
169 }
170
Popular Tags