arquillian using a random port with tomcat7 embeded - jboss-arquillian

I'd like to use a random port for arquillian.
So in arquillian.xml
I do:
<arquillian>
<container qualifier="tomcat7" default="true">
<configuration>
...
<property name="bindHttpPort">0</property>
...
</configuration>
</container>
</arquillian>
In my unit test:
#ArquillianResource
private URL base;
I hope to have the real port (localPort) used by Apache Tomcat (because yes it start with a random port) but this URL is with 0 port the one from configuration not random one.
So how to have access to this?

Are you using Apache Maven to run such tests ?
Here is how I did. On Maven side I'm using the buildhelper plugin and surefire to define the random port and pass it to tests as a system property
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>reserve-network-port</id>
<phase>initialize</phase>
<goals>
<goal>reserve-network-port</goal>
</goals>
<configuration>
<portNames>
<portName>tomcat.http.port</portName>
</portNames>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<!-- Port used for Tomcat HTTP connector -->
<tomcat.http.port>${tomcat.http.port}</tomcat.http.port>
</systemProperties>
</configuration>
</plugin>
</plugins>
And then I configured arquillian with
<arquillian>
<container qualifier="tomcat" default="true">
<configuration>
<property name="bindHttpPort">${tomcat.http.port:9090}</property>
</configuration>
</container
</arquillian>
Note : I'm using a default value for the port for when I'm launching the test from my IDE to avoid to have to manually configure it.
HTH
Cheers,

You can use the arquillian-available-port-extension.
Simply add the dependency in your pom
<dependency>
<groupId>com.github.mryan43</groupId>
<artifactId>arquillian-available-port-extension</artifactId>
<version>${arquillian-available-port-extension.version}</version>
</dependency>
and put in your arquillian.xml :
<property name="bindHttpPort">${available.port}</property>
This has the advantage of working both when running in maven and when running in your IDE.
https://github.com/mryan43/arquillian-available-port-extension

Related

Adding Information To JUnit Test Results via tycho-surefire-plugin

The test results of JUnit tests have a properties tag with a bunch of properties. What is logged seems to be at the discretion of each executor of the tests.
I want to process the XML files further, so it would be really nice to have the same keys each time. For maven-surefire-plugin that's pretty straightforward:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<propertyName>propertyValue1</propertyName>
</systemPropertyVariables>
</configuration>
</plugin>
This adds the line <property name="propertyName" value="propertyValue1"/> to the XML result file.
For the tycho-surefire-plugin, I tried the following:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<systemPropertyVariables>
<propertyName>propertyValue1</propertyName>
</systemPropertyVariables>
<systemProperties>
<property>
<name>propertyName</name>
<value>propertyValue2</value>
</property>
</systemProperties>
<argLine>-DpropertyName=propertyValue3</argLine>
</configuration>
</plugin>
...but neither of these values is printed inside the XML result.
How do I add information to the JUnit test results using tycho-surefire-plugin?
The documentation of the tycho-surefire-plugin states that you should use the <systemProperties> map:
<configuration>
<systemProperties>
<propertyName>propertyValue1</propertyName>
</systemProperties>
</configuration>
This will start the forked test JVM with -DpropertyName=propertyValue1.

How to run equivalent of Mule studio "Export" in a script?

I have the need to create a deployable ZIP archive from a script, much as the "Export" function does in Mule Studio. I'm expecting the ZIP to contain everything needed to deploy the app: JAR files, message flows, etc etc etc - again, just as Mule Studio Export does.
Is there a simple way to do this, or an example I can follow?
Maven is your best bet. Using the mule plugin for maven runnning mvn package will produce the deployable archive. More info on Mule and Maven here: http://www.mulesoft.org/documentation/display/current/Using+Maven+with+Mule
There is also an ant plugin but don't use it myself. Some info here: http://blogs.mulesoft.org/building-mule-apps-with-ant/
Alternatively, you can read about the application deployment structure here: http://www.mulesoft.org/documentation/display/current/Application+Format , so in theory you can build this structure yourself. But I wouldn't advise it and would stick to Maven.
Use Maven for building the app in jar/zip and deploy to the Mule standalone server automatically ... Make Sure your MULE_HOME is set to your standalone server in Environment variable ... and insert the following in your Maven Script to build it in Jar/Zip and deploy to app folder of Mule Standalone directly :-
<build>
<resources>
<resource>
<directory>>${project.basedir}/lib</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.mule.tools</groupId>
<artifactId>maven-mule-plugin</artifactId>
<version>1.6</version>
<extensions>true</extensions>
<configuration> <!-- This tag sets true/false to copy jar/zip file to Mule server app folder -->
<copyToAppsDirectory>true</copyToAppsDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>ISO-8859-1</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>YOUR_APPPLICATION_NAME</finalName>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>${eclipsePluginVersion}</version>
<configuration>
<!-- by default download all sources when generating project files -->
<downloadSources>true</downloadSources>
<classpathContainers>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER/${vmtype}/${jdkName}
</classpathContainer>
</classpathContainers>
</configuration>
</plugin>
<!--
make sure that MULE_HOME is set when building (required below when copying the
artifact to Mule's apps directory
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.0-beta-1</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>env.MULE_HOME</property>
<message>You must set MULE_HOME before installing the example.</message>
</requireProperty>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>package-example</id>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!-- This tag automatically deploy jar/zip file to server -->
<copy file="${project.build.directory}/${project.build.finalName}.zip"
todir="${env.MULE_HOME}/apps" overwrite="true"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

How to open a browser with a url from maven build

I'm building a web-application with Maven3 and run it via mvn jetty:run-war. Now I'd like to open the web-app from my maven build in the system browser.
I solved my problem on os windows, which is currently my only build system. After the jetty server is started and hosting my web-app the build makes a start call via antrun:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>Run URL in system browser.</id>
<phase>install</phase>
<configuration>
<target>
<exec executable="start" vmlauncher="false">
<arg line="http://localhost:8080/rap?startup=entrypoint"/>
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I have written a Maven plugin that can open browser on each platform/JDK as part of my bck2brwsr VM work. It can be used to show any static page in your project. Following example opens browser with content of pom.xml file:
<plugin>
<groupId>org.apidesign.bck2brwsr</groupId>
<artifactId>bck2brwsr-maven-plugin</artifactId>
<version>0.22</version>
<executions>
<execution>
<id>show-a-file</id>
<phase>verify</phase>
<goals>
<goal>show</goal>
</goals>
<configuration>
<directory>${basedir}</directory>
<startpage>pom.xml</startpage>
</configuration>
</execution>
</executions>
</plugin>
I know opening a static page isn't perfect, but it can contain appropriate redirect, right? Moreover, if there is an interested, the plugin can easily be improved to open any URL. Pull Requests welcomed.
Use Jetty server. Add Jetty plugin in your pom.xml under build tag like this:
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/shinchan</contextPath>
</webApp>
</configuration>
</plugin>
<!-- more plugin tags if any -->
<plugins>
now build using
mvn clean install jetty:run
this will start Jetty server at port 8080, and you can access using http://localhost:8080/shinchan URL on the machine the mvn command is executed.
PS:
For more details read Jetty wiki here: http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin
you may want to consider jetty:deploy-war, but I think it's an overkill.

Tomcat 7 - Maven Plugin?

I just wanted to double-check, has anyone found or is working on a Tomcat 7 plugin? If not, is anyone interested in helping me get it up and running?
I want another quick alternative to Glassfish, JBoss AS 6.0 is a bit heavy still for quick mockups.
Walter
It work for me as the following.
My setting.xml
<server>
<id>local_tomcat</id>
<username>ray</username>
<password>password</password>
</server>
My plugin configuration
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<server>local_tomcat</server>
<url>http://localhost:8080/manager/text</url>
</configuration>
</plugin>
My tomcat-users.xml
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user password="password" roles="manager-gui, manager-script" username="ray"/>
i use the official Tomcat7 Maven Plugin from Apache as follows:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<path>/${project.artifactId}</path>
<port>8080</port>
</configuration>
</plugin>
and to run it: mvn tomcat7:run
There is t7mp - a Tomcat 7 Maven Plugin - on Google code.
Cargo (and its Cargo Maven2 Plugin) also has support for Tomcat 7 (this was CARGO-790).
Apache Tomcat Maven Plugin 2.0-beta-1 supports Tomcat 7.
Using maven cargo your can coufigure your project that way :
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.0.6</version>
<configuration>
<container>
<containerId>tomcat7x</containerId>
<type>installed</type>
<home>${catalina.home}</home>
</container>
<configuration>
<type>existing</type>
<home>${catalina.home}</home>
</configuration>
<deployer>
<type>installed</type>
<deployables>
<deployable>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<type>war</type>
</deployable>
</deployables>
</deployer>
</configuration>
</plugin>
don't forget to configure your catalina.home property
The you can deploy it using:
mvn cargo:deploy
There is the Tomcat Maven Plugin 7 plugin developed by the Apache Tomcat team.
Currently you have to checkout the sources and install it to your local repository.
After that you can use it in the plugin section of your pom:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0-SNAPSHOT</version>
<executions>
<execution>
<id>start-tomcat</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<path>/</path>
<serverXml>src/main/tomcatconf/server.xml</serverXml>
</configuration>
</execution>
</executions>
</plugin>
After I had this error for three days in a row, here's my solution:
The user you are using to connect needs at least the role manager-script.
In your /conf/tomcat-users.xml
<role rolename="manager-script"/>
<user username="test" password="test" roles="manager-script"/>
In your pom.xml, include the following plugin
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<url>http://server.url:8080/manager/text</url>
<path>/YourApp</path>
<username>test</username>
<password>test</password>
</configuration>
</plugin>
Contrary to what I found in the internet you DON'T need to edit your maven setting.xml. The tomcat7-maven-plugin can be configured directly in the configuration-tag
A word to the url-tag: I tested the suffix
/manager
/manager/html
/manager/text
of which only /manager/text worked
My versions:
Tomcat: 7.0.33
Maven: 3.0.4
tomcat7-maven-plugin: 2.0
Java: 1.7.0_07
I'm using tomcat7-maven-plugin for my embedded tomcat instance. Here is how I have configured it. Since my app requires jaas authentication I can also provide that in the setting itself.
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- http port -->
<port>8080</port>
<path>/gcs-explorers</path>
<contextFile>${basedir}/src/main/webapp/META-INF/context.xml</contextFile>
<addWarDependenciesInClassloader>true</addWarDependenciesInClassloader>
<systemProperties>
<java.security.auth.login.config>${basedir}/conf/jaas.config</java.security.auth.login.config>
</systemProperties>
<url>http://127.0.0.1:8080/manager/html</url>
<username>admin</username>
<password>admin</password>
<addContextWarDependencies>true</addContextWarDependencies>
<addWarDependenciesInClassloader>true</addWarDependenciesInClassloader>
<configurationDir>${basedir}</configurationDir>
</configuration>
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.2.0</version>
<type>jar</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.company.package.jaas</groupId>
<artifactId>gcs-jaas</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.company.gcs</groupId>
<artifactId>package-file-share</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
Actually, the standard plugin works for me . I just had to create the role manager-script in tomcat users and change the url parameter to http://localhost:8080/manager/html in order to make it work :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<url>http://localhost:8080/manager/html</url>
<server>local</server>
<path>/${project.artifactId}</path>
<update>true</update>
</configuration>
</plugin>
For Tomcat 7,
Step 1:
Modules tab of server add
Document base: <PATH>\Apache-Tomcat-7.0.0\webapps\manager
Path: /manager
Step 2:
Update POM to:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<url>http://localhost:8080/manager/text</url>
<update>true</update>
<warFile>target/${project.artifactId}-${project.version}.war</warFile>
<path>/${project.artifactId}</path>
<username>tomcat_user</username>
<password>tomcat_password</password>
</configuration>
</plugin>

How to make jetty-maven-plugin deploy a war that is retrieved from a repository?

I'm setting up an integration test module for a good sized web project. The integration test module is separated from the web project itself, and it has it's own pom.
The idea is to use the maven-soapui-plugin to send requests and verify the response. Setting up the soapui-plugin is no hassle. However, I'm having trouble with figuring out how I can tell the jetty-maven-plugin to deploy a war from a remote repository.
If I have understood correctly, the jetty-maven-plugin has a property called '<webApp>/<webApp>' which lets me specify the war file to deploy. The problem is that the war file is not present in the module itself.
I have heard that I can use the maven assembly plugin to retrieve the war from a repository via the projects artifactId, but I am yet to figure out how I would go about doing so.
Here's a summary of what I want:
Retrieve a specific war from a repository or the like, in example via its artifactId.
Deploy this war to the jetty-maven-plugin (goal deploy-war?)
get maven-soapui-plugin to run tests and report the results back in the integration-test phase.
I am pretty sure I've got step 3 covered, but I am very unsure how to achieve step 1 and 2.
Any help is greatly appreciated
It is maybe possible to use dependency:copy to retrieve the war, unpack it and to get the whole thing working with the maven jetty plugin, but this would be hacky and kinda ugly. A cleaner solution would be to use the Maven Cargo plugin and this is my suggestion. Below, a sample POM showing how to retrieve a WAR artifact using its coordinates and how to deploy it on an embedded Jetty container using Cargo:
<dependencies>
<dependency>
<groupId>war group id</groupId>
<artifactId>war artifact id</artifactId>
<type>war</type>
<version>war version</version>
</dependency>
...
</dependencies>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<!-- Container configuration -->
<container>
<containerId>jetty6x</containerId>
<type>embedded</type>
</container>
<!-- Configuration to use with the container or the deployer -->
<configuration>
<deployables>
<deployable>
<groupId>war group id</groupId>
<artifactId>war artifact id</artifactId>
<type>war</type>
<properties>
<context>war context</context>
</properties>
</deployable>
</deployables>
</configuration>
<!-- Don't wait, execute the tests after the container is started -->
<wait>false</wait>
</configuration>
<executions>
<execution>
<id>start-container</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-container</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
Finally, just bind the soapui plugin on the integration-test phase.
I'm doing the same thing, John, but I took a different approach with the Jetty plugin. I think the end result is the same. I'm developing an integration-test suite to run against several web service WARs. I'm using dependency:copy in the package phase and then a list of <contextHandler/>s configured for maven-jetty-plugin:
<project>
…
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-wars</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/wars-to-be-tested</outputDirectory>
<stripVersion>true</stripVersion>
<artifactItems>
…
<artifactItem>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>version</version>
<type>war</type>
</artifactItem>
…
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.1.3.v20100526</version>
<configuration>
…
<contextHandlers>
…
<contextHandler implementation="org.mortbay.jetty.plugin.JettyWebAppContext">
<war>${project.build.directory}/wars-to-be-tested/artifactId.war</war>
<contextPath>/context</contextPath>
</contextHandler>
</contextHandlers>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>stop</goal>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I would prefer to declare the various wars as dependencies and then use dependency:copy-dependencies to set up the wars-to-be-tested directory; this would allow the Maven reactor to figure out that it needs to build my integration-test module after the wars it'll be testing. The problem I ran into was that the Jetty plugin thought that I wanted to "overlay" all of the wars that were listed as dependencies (a concept that I'd never even heard of until I saw it happen); I don't know if allowing that to happen would have hurt anything, but I didn't like it, so I went with the dependency:copy method.
This is just an alternative to using Cargo. I'll be looking into that myself, but I just wanted to provide another way of doing it.