KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > dd > api > web > 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 org.netbeans.modules.j2ee.dd.api.web;
21
22 import org.netbeans.modules.j2ee.dd.api.web.*;
23 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException;
24 import org.netbeans.modules.j2ee.dd.api.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(getDataDir().getAbsolutePath() + File.separator + "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     private static boolean initialized;
130     
131     protected void setUp() throws Exception JavaDoc {
132         super.setUp();
133         System.out.println("setUp() .......................");
134         
135         File dataDir = new File(getDataDir().getAbsolutePath() + java.io.File.separator + "invalid");
136         FileObject dataFolder = FileUtil.toFileObject(dataDir);
137         
138         if (!initialized){
139             FileObject old = dataFolder.getFileObject("web", "xml");
140             if (old != null){
141                 old.delete();
142             }
143             initialized = true;
144         }
145         
146         if (fo==null) {
147             fo = FileUtil.copyFile(dataFolder.getFileObject("web_org","xml"), dataFolder, "web");
148         }
149         
150         try {
151             webApp = DDProvider.getDefault().getDDRoot(fo);
152         } catch (java.io.IOException JavaDoc ex) {
153             ex.printStackTrace();
154         }
155         assertTrue("WebApp object not found", null != webApp);
156         list = new MyListener(webApp);
157         webApp.addPropertyChangeListener(list);
158     }
159     
160     protected void tearDown() {
161         webApp.removePropertyChangeListener(list);
162     }
163     
164     private MyListener list;
165     private static class MyListener implements PropertyChangeListener {
166         WebApp webApp;
167         MyListener (WebApp webApp) {
168             this.webApp=webApp;
169         }
170         public void propertyChange(PropertyChangeEvent evt) {
171             System.out.println("propertyChanged() "+evt.getPropertyName()+":"+evt.getOldValue()+":"+evt.getNewValue());
172             evtList.add("PCE:STATUS["+webApp.getStatus()+"]:"+getDDProperty(evt.getPropertyName())+":"+evt.getOldValue()+":"+evt.getNewValue());
173         }
174     }
175     
176     private static String JavaDoc getDDProperty(String JavaDoc fullName) {
177         int index = fullName.lastIndexOf('/');
178         return (index>0?fullName.substring(index+1):fullName);
179     }
180 }
181
Popular Tags