001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.dd.api.web;
043:
044: import org.netbeans.modules.j2ee.dd.api.web.*;
045: import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException;
046: import org.netbeans.modules.j2ee.dd.api.common.NameAlreadyUsedException;
047: import java.io.*;
048: import junit.framework.*;
049: import org.netbeans.junit.*;
050: import org.openide.filesystems.*;
051: import java.beans.*;
052:
053: public class InvalidStatesTest extends NbTestCase {
054: private static final String VERSION = "2.4";
055: private static final String[] expectedEvents = {
056: "PCE:STATUS[1]:dd_status:2:1",
057: "PCE:STATUS[0]:dd_status:1:0",
058: "PCE:STATUS[0]:SessionTimeout:null:30" };
059: private static java.util.List evtList = new java.util.ArrayList();
060:
061: private WebApp webApp;
062:
063: public InvalidStatesTest(java.lang.String testName) {
064: super (testName);
065: }
066:
067: public static void main(java.lang.String[] args) {
068: junit.textui.TestRunner.run(suite());
069: }
070:
071: public static Test suite() {
072: TestSuite suite = new NbTestSuite(InvalidStatesTest.class);
073:
074: return suite;
075: }
076:
077: /** Test of greeting method, of class HelloWorld. */
078: public void test_InvalidDataReading() {
079: System.out.println("TEST:Invalid Data Reading");
080: assertEquals("Incorrect servlet spec.version :", VERSION,
081: webApp.getVersion());
082: assertEquals("Incorrect dd status :",
083: WebApp.STATE_INVALID_UNPARSABLE, webApp.getStatus());
084: assertNotNull("Error mustn't be null :", webApp.getError());
085: System.out.println("Expected Exception :" + webApp.getError());
086: assertNull("Session Config must be null :", webApp
087: .getSingleSessionConfig());
088: }
089:
090: public void test_Correction1() {
091: System.out.println("TEST:Invalid Data Correction - editing");
092: // replacing web.xml with web_parsable.xml
093: File dataDir = new File(getDataDir().getAbsolutePath()
094: + File.separator + "invalid");
095: FileObject dataFolder = FileUtil.toFileObject(dataDir);
096: FileObject fo1 = dataFolder
097: .getFileObject("web_parsable", "xml");
098: assertTrue("FileObject invalid/web_parsable.xml not found",
099: null != fo1);
100:
101: try {
102: FileLock lock = fo.lock();
103: OutputStream os = fo.getOutputStream(lock);
104: InputStream is = fo1.getInputStream();
105: int b;
106: while ((b = is.read()) != -1)
107: os.write(b);
108: is.close();
109: os.close();
110: lock.releaseLock();
111: } catch (IOException ex) {
112: throw new AssertionFailedErrorException(
113: "Writing data Failed ", ex);
114: }
115: assertEquals("Incorrect dd status :",
116: WebApp.STATE_INVALID_PARSABLE, webApp.getStatus());
117: assertNotNull("Error mustn't be null :", webApp.getError());
118: System.out.println("Expected Exception :" + webApp.getError());
119: assertNotNull("Session Config mustn't be null :", webApp
120: .getSingleSessionConfig());
121: assertNull("Session Timeout must be null :", webApp
122: .getSingleSessionConfig().getSessionTimeout());
123: }
124:
125: public void test_Correction2() {
126: System.out
127: .println("TEST:Invalid Data Correction - programmatic correction");
128: WebApp webAppCopy = null;
129: try {
130: webAppCopy = DDProvider.getDefault().getDDRootCopy(fo);
131: } catch (java.io.IOException ex) {
132: ex.printStackTrace();
133: }
134: assertTrue("WebAppCopy not created ", null != webAppCopy);
135: assertNotNull("Session Config mustn't be null :", webAppCopy
136: .getSingleSessionConfig());
137:
138: webAppCopy.getSingleSessionConfig().setSessionTimeout(
139: new java.math.BigInteger("30"));
140: System.out.println("status = " + webAppCopy.getStatus());
141: try {
142: webAppCopy.write(fo);
143: } catch (java.io.IOException ex) {
144: throw new AssertionFailedErrorException(
145: "write method failed", ex);
146: }
147: System.out.println("sessio nConfig = "
148: + webApp.getSingleSessionConfig());
149: assertNotNull("Session Config mustn't be null :", webApp
150: .getSingleSessionConfig());
151: assertEquals("Incorrect dd status :", WebApp.STATE_VALID,
152: webApp.getStatus());
153: assertNull("Error must be null :", webApp.getError());
154: assertNotNull("Session Timeout mustn't be null :", webApp
155: .getSingleSessionConfig().getSessionTimeout());
156: }
157:
158: public void test_CheckEvents() {
159: System.out.println("TEST:Property Change Events");
160: assertEquals("Incorrect number of PCE :",
161: expectedEvents.length, evtList.size());
162: for (int i = 0; i < expectedEvents.length; i++) {
163: assertEquals("Incorrect PCE[" + i + "] :",
164: expectedEvents[i], evtList.get(i));
165: }
166: }
167:
168: private static FileObject fo;
169: private static boolean initialized;
170:
171: protected void setUp() throws Exception {
172: super .setUp();
173: System.out.println("setUp() .......................");
174:
175: File dataDir = new File(getDataDir().getAbsolutePath()
176: + java.io.File.separator + "invalid");
177: FileObject dataFolder = FileUtil.toFileObject(dataDir);
178:
179: if (!initialized) {
180: FileObject old = dataFolder.getFileObject("web", "xml");
181: if (old != null) {
182: old.delete();
183: }
184: initialized = true;
185: }
186:
187: if (fo == null) {
188: fo = FileUtil.copyFile(dataFolder.getFileObject("web_org",
189: "xml"), dataFolder, "web");
190: }
191:
192: try {
193: webApp = DDProvider.getDefault().getDDRoot(fo);
194: } catch (java.io.IOException ex) {
195: ex.printStackTrace();
196: }
197: assertTrue("WebApp object not found", null != webApp);
198: list = new MyListener(webApp);
199: webApp.addPropertyChangeListener(list);
200: }
201:
202: protected void tearDown() {
203: webApp.removePropertyChangeListener(list);
204: }
205:
206: private MyListener list;
207:
208: private static class MyListener implements PropertyChangeListener {
209: WebApp webApp;
210:
211: MyListener(WebApp webApp) {
212: this .webApp = webApp;
213: }
214:
215: public void propertyChange(PropertyChangeEvent evt) {
216: System.out.println("propertyChanged() "
217: + evt.getPropertyName() + ":" + evt.getOldValue()
218: + ":" + evt.getNewValue());
219: evtList.add("PCE:STATUS[" + webApp.getStatus() + "]:"
220: + getDDProperty(evt.getPropertyName()) + ":"
221: + evt.getOldValue() + ":" + evt.getNewValue());
222: }
223: }
224:
225: private static String getDDProperty(String fullName) {
226: int index = fullName.lastIndexOf('/');
227: return (index > 0 ? fullName.substring(index + 1) : fullName);
228: }
229: }
|