|
What this is
This file is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.tomcat5;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Locale;
import javax.enterprise.deploy.model.DeployableObject;
import javax.enterprise.deploy.shared.DConfigBeanVersionType;
import javax.enterprise.deploy.shared.ModuleType;
import javax.enterprise.deploy.spi.DeploymentConfiguration;
import javax.enterprise.deploy.spi.DeploymentManager;
import javax.enterprise.deploy.spi.Target;
import javax.enterprise.deploy.spi.TargetModuleID;
import javax.enterprise.deploy.spi.exceptions.DConfigBeanVersionUnsupportedException;
import javax.enterprise.deploy.spi.exceptions.InvalidModuleException;
import javax.enterprise.deploy.spi.exceptions.TargetException;
import javax.enterprise.deploy.spi.status.ProgressObject;
import junit.framework.*;
import org.openide.ErrorManager;
/** Tests for IDE & Tomcat communication.
*
* Requires running instance of Tomcat with configured manager application
* available at URL http://localhost:8080/manager/
*
* @author Radim Kubacki
*/
public class DeploymentTest extends TestCase {
public static Test suite () {
TestSuite suite = new TestSuite (DeploymentTest.class);
return suite;
}
private TomcatManager tm;
private java.io.File datadir;
public DeploymentTest (java.lang.String testName) {
super (testName);
}
protected void setUp () throws java.lang.Exception {
super.setUp ();
tm = (TomcatManager)TomcatFactory.create ().getDeploymentManager (
"tomcat:"+TomcatFactoryTest.TOMCAT_URI,
TomcatFactoryTest.TOMCAT_UNAME,
TomcatFactoryTest.TOMCAT_PASSWD
);
datadir = new java.io.File ("/usr/local/home/radim/devel/prj40/nb_all/tomcatint/tomcat5/test/unit/data");
}
/** Test for deployment and undeployment of web module. */
public void testDeploymentOfWar () {
java.io.File webapp = new java.io.File (datadir, "sample.war");
java.io.File context = new java.io.File (datadir, "samplewar.xml"); // no such file
System.out.println("testDistribute (deploy) of "+webapp+" using "+context);
ProgressObject po = tm.distribute (tm.getTargets (), webapp, context);
try {
Thread.sleep (5000);
}
catch (InterruptedException ie) {
// do nothing
}
TargetModuleID [] tmIDs = po.getResultTargetModuleIDs ();
assertTrue ("There should be one result target module", tmIDs != null && tmIDs.length == 1);
try {
checkResponse (new URL(tmIDs[0].getWebURL ()+"/index.jsp"));
} catch (Exception e) {
fail (e.getMessage ());
}
ProgressObject po2 = tm.undeploy (tmIDs);
try {
Thread.sleep (5000);
}
catch (InterruptedException ie) {
// do nothing
}
try {
checkResponse (new URL(tmIDs[0].getWebURL ()+"/index.jsp"));
fail ("deployed application is still accessible");
} catch (Exception e) {
// OK
System.out.println("correctly thrown exception: "+e.getMessage ());
}
}
/** Test for deployment and undeployment of web module. */
public void testDeploymentOfDirectory () {
java.io.File webapp = new java.io.File (datadir, "sampleweb");
java.io.File context = new java.io.File (datadir, "sampleweb.xml");
System.out.println("testDistribute (install) of "+webapp+" using "+context);
ProgressObject po = tm.distribute (tm.getTargets (), webapp, context);
try {
Thread.sleep (5000);
}
catch (InterruptedException ie) {
// do nothing
}
TargetModuleID [] tmIDs = po.getResultTargetModuleIDs ();
assertTrue ("There should be one result target module", tmIDs != null && tmIDs.length == 1);
try {
checkResponse (new URL(tmIDs[0].getWebURL ()+"/index.jsp"));
} catch (Exception e) {
fail (e.getMessage ());
}
ProgressObject po2 = tm.undeploy (tmIDs);
try {
Thread.sleep (5000);
}
catch (InterruptedException ie) {
// do nothing
}
try {
checkResponse (new URL(tmIDs[0].getWebURL ()+"/index.jsp"));
fail ("deployed application is still accessible");
} catch (Exception e) {
// OK
System.out.println("correctly thrown exception: "+e.getMessage ());
}
}
/** Tries to connect to given URL and scans the output whether
* its first line starts with OK.
*/
private boolean checkResponse (URL url) throws Exception {
java.net.URLConnection conn = url.openConnection();
HttpURLConnection hconn = (HttpURLConnection) conn;
// Set up standard connection characteristics
hconn.setAllowUserInteraction(false);
hconn.setUseCaches(false);
hconn.setDoOutput(false);
hconn.setRequestMethod("GET");
// Establish the connection with the server
hconn.connect();
// Process the response message
java.io.Reader reader = new InputStreamReader(hconn.getInputStream());
StringBuffer buff = new StringBuffer();
String error = null;
boolean first = true;
while (true) {
int ch = reader.read();
if (ch < 0) {
break;
} else if ((ch == '\r') || (ch == '\n')) {
String line = buff.toString();
buff.setLength(0);
// PENDING : fireProgressEvent
TomcatFactory.getEM ().log(ErrorManager.INFORMATIONAL, line);
if (first) {
if (!line.startsWith("OK")) {
error = line;
}
first = false;
}
} else {
buff.append((char) ch);
}
}
if (buff.length() > 0) {
System.out.println(buff);;
}
if (buff.length () == 0 && first) {
// actually bug in Tomcat - after remove there is empty page returned.
throw new Exception ("URL stream content is empty");
}
if (error != null) {
throw new Exception ("URL reading failed: "+error);
}
return true;
}
// Add test methods here, they have to start with 'test' name.
// for example:
// public void testHello() {}
}
|