понедельник, 2 марта 2009 г.

 

Testing Embedded Glassfish b10

Embedded Glassfish for Unit Test is much closer to "real world" than Jetty. EJB, JMS and JTA simply don't work on lightweight Web Containers. I tried it in maven project and get success after some troubles. Here is my configuration.

Maven repositories:

<repositories>
<repository>
<id>gfe</id>
<url>http://maven.glassfish.org/content/groups/glassfish/</url>
</repository>
</repositories>




and dependency:

<dependency>
<groupId>org.glassfish.embedded</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.0-Prelude-Embedded-b10</version>
</dependency>



If you use Hibernate you should do some hack with its dependencies because asm's versions conflict:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.6.ga</version>
<exclusions>
<exclusion>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</exclusion>
<exclusion>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.1</version>
</dependency>





Now we ready to write some code:


// Starting web container
EmbeddedInfo info = new EmbeddedInfo();
info.setServerName("GF Embed");
info.setHttpPort(gfPort);
info.setVerbose(true);
info.setLogging(true);
gfServer = new Server(info);
gfServer.start();

Properties prop = new Properties();
//Setting datasource
prop.setProperty("DEFAULT", "src/main/resources/datasource/SSO_DataSources.xml");

//Here we need to copy resource dtd file, because somewhy embedded glassfish hasn't it
File dtdDir = new File("gfe/lib/dtds");
dtdDir.mkdirs();
String dtdFile = "sun-resources_1_3.dtd";
FileUtils.copy(new File(dtdFile), new File(dtdDir, dtdFile));

gfServer.getCommandExecutor().execute("add-resources", prop);
System.out.println("Hello");

//Deploying war-file here
gfServer.getDeployer().deploy(new File("target/card-service-web-1.05.009.war"));

//... DO SOME WORK
gfServer.stop();




Almost all. One bad thing is that we must use certain version of application descriptors. I coudn't figure what sun-web.xml must be (so I kicked it off) but web.xml must contain such header:


<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:j2ee="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">





That's it. Enjoy full functionality of efficient webserver in a single JVM.

Ярлыки:


This page is powered by Blogger. Isn't yours?

Подпишитесь на каналы Сообщения [Atom]