Author: olamy
Date: Fri Mar 16 23:35:00 2012
New Revision: 1301823
URL: http://svn.apache.org/viewvc?rev=1301823&view=rev
Log:
add an upload file unit test
Added:
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/java/
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/java/org/
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/java/org/apache/
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/java/org/apache/tomcat/
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/java/org/apache/tomcat/maven/
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/java/org/apache/tomcat/maven/common/
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/java/org/apache/tomcat/maven/common/TomcatManagerTest.java
(with props)
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/resources/
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/resources/test.txt
(with props)
Modified:
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/pom.xml
Modified: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/pom.xml
URL:
http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/pom.xml?rev=1301823&r1=1301822&r2=1301823&view=diff
==============================================================================
--- tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/pom.xml (original)
+++ tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/pom.xml Fri Mar 16
23:35:00 2012
@@ -77,6 +77,27 @@
</dependency>
<dependency>
+ <groupId>org.apache.tomcat.embed</groupId>
+ <artifactId>tomcat-embed-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tomcat</groupId>
+ <artifactId>tomcat-juli</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tomcat.embed</groupId>
+ <artifactId>tomcat-embed-logging-juli</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tomcat</groupId>
+ <artifactId>tomcat-servlet-api</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-annotations</artifactId>
<version>1.5.5</version>
@@ -109,6 +130,15 @@
</execution>
</executions>
</plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <systemPropertyVariables>
+ <basedir>${basedir}</basedir>
+ </systemPropertyVariables>
+ </configuration>
+ </plugin>
</plugins>
</build>
Added:
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/java/org/apache/tomcat/maven/common/TomcatManagerTest.java
URL:
http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/java/org/apache/tomcat/maven/common/TomcatManagerTest.java?rev=1301823&view=auto
==============================================================================
---
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/java/org/apache/tomcat/maven/common/TomcatManagerTest.java
(added)
+++
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/java/org/apache/tomcat/maven/common/TomcatManagerTest.java
Fri Mar 16 23:35:00 2012
@@ -0,0 +1,141 @@
+package org.apache.tomcat.maven.common;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import junit.framework.TestCase;
+import org.apache.catalina.Context;
+import org.apache.catalina.startup.Tomcat;
+import org.apache.commons.io.IOUtils;
+import org.apache.tomcat.maven.common.deployer.TomcatManager;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Olivier Lamy
+ */
+public class TomcatManagerTest
+ extends TestCase
+{
+
+ Tomcat tomcat;
+
+ UploadServlet uploadServlet;
+
+ int port;
+
+ public static String getBasedir()
+ {
+ return System.getProperty( "basedir" );
+ }
+
+ @Override
+ protected void setUp()
+ throws Exception
+ {
+ super.setUp();
+ tomcat = new Tomcat();
+ tomcat.setBaseDir( System.getProperty( "java.io.tmpdir" ) );
+ tomcat.setPort( 0 );
+
+ Context context = tomcat.addContext( "", System.getProperty(
"java.io.tmpdir" ) );
+ uploadServlet = new UploadServlet();
+ tomcat.addServlet( context, "foo", uploadServlet );
+ context.addServletMapping( "/*", "foo" );
+
+ tomcat.start();
+
+ port = tomcat.getConnector().getLocalPort();
+
+ System.out.println( "Tomcat started on port:" + port );
+ }
+
+ @Override
+ protected void tearDown()
+ throws Exception
+ {
+ super.tearDown();
+ tomcat.stop();
+ }
+
+
+ public void testDeployWar()
+ throws Exception
+ {
+ TomcatManager tomcatManager = new TomcatManager( new URL(
"http://localhost:" + this.port + "/foo/bar" ) );
+ tomcatManager.deploy( "foo", new FileInputStream( new File(
getBasedir(), "src/test/resources/test.txt" ) ) );
+ StringWriter sw = new StringWriter();
+ assertEquals( 1, uploadServlet.uploadedResources.size() );
+ FileInputStream fileInputStream = new FileInputStream(
uploadServlet.uploadedResources.get( 0 ).uploadedFile );
+ try
+ {
+ IOUtils.copy( fileInputStream, sw );
+ assertTrue( sw.toString().contains( "Apache Tomcat rocks!!" ) );
+ }
+ finally
+ {
+ fileInputStream.close();
+ }
+ }
+
+ //-----------------------------
+ // internal for tests
+ //-----------------------------
+
+ public class UploadedResource
+ {
+ public String requestUri;
+
+ public File uploadedFile;
+
+ public UploadedResource( String requestUri, File uploadedFile )
+ {
+ this.requestUri = requestUri;
+ this.uploadedFile = uploadedFile;
+ }
+ }
+
+ public class UploadServlet
+ extends HttpServlet
+ {
+
+ public List<UploadedResource> uploadedResources = new
ArrayList<UploadedResource>();
+
+ @Override
+ protected void doPut( HttpServletRequest req, HttpServletResponse resp
)
+ throws ServletException, IOException
+ {
+ System.out.println( "put ok:" + req.getRequestURI() );
+ super.doPut( req, resp );
+ File file = File.createTempFile( "tomcat-unit-test", "tmp" );
+ uploadedResources.add( new UploadedResource( req.getRequestURI(),
file ) );
+ IOUtils.copy( req.getInputStream(), new FileOutputStream( file ) );
+ }
+ }
+}
Propchange:
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/java/org/apache/tomcat/maven/common/TomcatManagerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/java/org/apache/tomcat/maven/common/TomcatManagerTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Added:
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/resources/test.txt
URL:
http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/resources/test.txt?rev=1301823&view=auto
==============================================================================
---
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/resources/test.txt
(added)
+++
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/resources/test.txt
Fri Mar 16 23:35:00 2012
@@ -0,0 +1,2 @@
+Apache Tomcat rocks!!
+hey Apache Maven rocks too :P
\ No newline at end of file
Propchange:
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/resources/test.txt
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/test/resources/test.txt
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]