Can I get servlet logging from gwt test case - gwt

I have been using GwtTestCase for a while and am trying to test a simple RestyGwt client api. I have added the servlet to my gwt.xml class but am not able to get any logging from the servlet. It appears that the servlet is not being created.
Here is my simple servlet that I have tried to get some kind of information from, including just throwing a runtime exception.
public class JerseyTestServlet extends ServletContainer {
{
System.err.println("RUNNING JERESEY TEST SERVLET");
}
private static final long serialVersionUID = -7518118461257020639L;
public JerseyTestServlet() {
super(new RestApplication());
System.out.println("RUNNING JERESEY TEST SERVLET");
throw new RuntimeException("FOOO");
}
}
The class does match the class name and package of the servlet.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.5.1//EN" "http://google-web-toolkit.googlecode.com/svn/tags/2.5.1/distro-source/core/src/gwt-module.dtd">
<module rename-to='simulator'>
<inherits name="com.testing.NgProductionClient" />
<servlet path='/rest' class='com.testing.JerseyTestServlet'/>
</module>
I want to get some kind of debugging information about the servlet when it starts up so I can troubleshoot path'ing problems as they arrise. Is it possible to get debugging information from the servlet inside a GwtTestCase?

My mistake of course. The .gwt.xml file should contain the glob path for all the rest services so in my case it needed the /rest/* . Once I did this the rest service worked for my unit tests. Also I forgot that the servlet doesn't start by default unless you give it the options to load on default but I don't know how this is possible without the test framework using a web.xml. I am happy with the solution and it makes testing a Mock'd rest client very simple.

Related

How to access the endpoint assembled inside an ear file?

I am building a web app using java ee as backend and angular as frontend. As application server i have chosen Wildfly 13. As a build tool for backend I have chosen Maven. I have created this structure for the project :
-Project
---Project-ear
---Project-model
---Project-service
---Project-service-api
Neither of this modules packages a war file, I package 3 jars and in the end I assemble them in an ear file using
"Project-ear"
I have tried to consume the exposed service inside
"Project-service"
but I cant. It seems I don't set the right url. Can somebody help me please?
I have tried to access the following urls:
http://localhost:8080/Project-ear/Project-service/resources/test/testDtos/,
http://localhost:8080/resources/test/testDtos/
I have created the RestActivator class :
#ApplicationPath("/resources")
public class RestActivator extends Application {
}
And I have created a Resource class :
#Path("/test")
public class TestResource {
#GET
#Path("/testDtos")
#Produces(MediaType.APPLICATION_JSON)
public Response getWorkflowDiagram() {
TestDto testDto = new TestDto();
testDto.setFirstName("Test");
testDto.setLastName("Test");
return Response.ok(testDto).build();
}
}
I don't get any error message, I just can't access the URL endpoint.
When you configure the Application Server (Wildfly) and add the ear artifact, it creates a target folder in your ear-folder. There you can find a file called application.xml. You have to set the context-root like that:
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd"
version="7">
<module>
<web>
<web-uri>test.war</web-uri>
<context-root>/api</context-root>
</web>
</module>
</application>
Your URL depends on what you have set in context-root in application.xml and how have set the application path in your root class (RestActivator).
In this example:
localhost:8080/api/resources
And to get access to your method in TestResource:
localhost:8080/api/resources/test/testDtos

Gettting WELD Exception on server startup of weblogic where as using Google Guice for DI in Jersey based application

I am using Weblogic 12b as App server. My application uses Jersey 2.5.1 with Guice3 in my project. I have a class called Application derived from org.glassfish.jersey.server.ResourceConfig. On server startup I am getting error as below:
Caused By: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type ServiceLocator with qualifiers #Default
at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] #Inject public Application(ServiceLocator)
at Application.<init>(Application.java:22)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
at org.jboss.weld.bootstrap.Validator.validateProducer(Validator.java:417)
at org.jboss.weld.injection.producer.InjectionTargetService.validateProducer(InjectionTargetService.java:36)
at org.jboss.weld.manager.InjectionTargetFactoryImpl.validate(InjectionTargetFactoryImpl.java:135)
It seems it is taking WELD in place of google Guice for DI.
Same issue I am getting in business Tier where EJB classes are composed of Java Classes and they are injected using #Inject.
I have even tried to change he import #Inject to google inject but the exception changed but not resolved.
I tried to use beans.xml in web-inf
#ApplicationPath("/")
public class Application extends ResourceConfig {
#Inject
public Application(final ServiceLocator serviceLocator) {
}
}
You need to effectively disable CDI.
You can do this by adding a WEB-INF/beans.xml file to your application that contains:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="none">
</beans>
Note the bean-discovery-mode="none".
If your project contains additional jars with classes that might look CDI beans then you will also need to add a similar META-INF/beans.xml file to those as well.
However, I suspect that this may lead to other unrelated issues. Normally application servers like to control the lifecycle of your classes and this includes those related to JAX-RS.

Arquillian test with real EAR from build on JBoss 7.1

I want to test an Java EE application with Arquillian. A simple setup is working to put some test classes onto the server managed by Arquillian to test them.
Now, I want to use a EAR file which was build during the build process. With
#Deployment
public static EnterpriseArchive createDeployment() {
File earFile = ...
EnterpriseArchive archive = ShrinkWrap.createFromZipFile(EnterpriseArchive.class, earFile);
return archive;
}
I am able to put the EAR via Arquillian to JBoss. I see the deployment and there are no errors during deployment. Only Arquillian returns with an error that it can not find the test class, which is obviously ok.
Now is the question where to put the test class to. I can put the test class into the test.war put into the EAR by Arquillian, but I get an ArquillianServletRunner not found exception. When I put the test classes into JAR files as module or library the test classes are not found when put as module or the injects do not work when put as libraries due to dependency issues.
Where to I have to put the test classes to???
My arquillian.xml:
<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://jboss.org/schema/arquillian"
xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<defaultProtocol type="Servlet 3.0" />
<container qualifier="jboss7" default="true">
<configuration>
<property name="jbossHome">${cargo.dir}/jboss-as-dist-7.1.1.Final/jboss-as-7.1.1.Final</property>
</configuration>
</container>
<engine>
<property name="deploymentExportPath">target/deployments</property>
</engine>
</arquillian>
You can run your test class on the client side using #Deployment(testable = false), but this has the downside that you cannot use the persistence extention (and maybe others) at the moment.
My example code:
#RunWith(Arquillian.class)
public class PersonWebServiceIT {
private PersonWebService service;
#Deployment(testable = false)
public static Archive<?> createDeployment() {
return ShrinkWrap
.create(ZipImporter.class, "test.war")
.importFrom(
new File("simple-webservice-1.0.0-SNAPSHOT.war"))
.as(WebArchive.class);
}
#Test
public void testFindPersons(#ArquillianResource URL deploymentUrl) {
....
}
}
Putting the test classes to the war inside the ear did work for me
WebArchive war = ear.getAsType(WebArchive.class, "/mywarname.war");
war.addClass(MyTestClass.class);
When I am dealing with existing EAR, I prefer to separate the WAR that runs the tests, from the actual tests that I put in special JAR along with other testing EJBs. I have posted an example how do I do it with the necessary application.xml manipulation under similar question: https://stackoverflow.com/a/17036383/1667977

Deploying GWT App with RPC calls on Apache

I'm developing a GWT app, let's say SoapCon that connects to a web service so I'm using Axis on my server-side to connect to this web service. When I run my app in development mode, it works fine (which returns xml as its response). But when I deployed my app to Apache 2.0 in Linux, RPC failed and returns 404 for the servlet mapping URL.
When I deployed this app to Apache, I recursively copied the /war/soapcon to the /var/www/html/SoapCon directory and the SoapCon.html and .css.
Here's my web.xml:
<!-- Servlets -->
<servlet>
<servlet-name>greetServlet</servlet-name>
<servlet-class>com.sample.google.server.SampleServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>greetServlet</servlet-name>
<url-pattern>/soapcon/greet</url-pattern>
</servlet-mapping>
SoapCon.gwt.xml
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='soapcon'>
....
The client side stub for the RPC service:
#RemoteServiceRelativePath("greet")
public interface SampleService extends RemoteService {
String method( String params, ... );
}
When I run my app
http://localhost/SoapCon/SoapCon.html
the module is loaded but when I clicked a button which will call RPC method from my server, error occurs returning:
com.google.gwt.user.client.rpc.StatusCodeException: 404 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /SoapCon/soapcon/greet was not found on this server.</p>
</body></html>
What should I do? please help. thanks in advance.
I assume you are running tomcat on your apache server. To deploy your gwt project you should build a .war-file, and do not try to copy all parts manually.
The 404 error tells us, that the URL "/SoapCon/soapcon/greet" was not found / in the right place on your server. Deploying the .war instead of manual copying will automatically place everything in the right position.

EJB3 Annotation

I am using JBoss 5 GA, I created a test Session bean, and local interface. I have created a servlet client. I tried to inject the interface in the servlet using #EJB..
But when I call this servlet I got the requested resource is not available!!!! When I comment the //#EJB, the page run successfully, any help please????
Jotnarta
It would have been helpful to add some piece of code in your question, at least the annotations in your EJB, local interface (if you annotated it) and servlet...
Nevertheless, according to the Chapter 11. Introduction to EJB injection in Servlets of the JBoss EJB3 Tutorials, for an EJB module containing an EJB3 SLSB defined like this:
#Stateless(name="calculator")
#Remote(CalculatorRemote.class)
#Local(CalculatorLocal.class)
public class CalculatorBean implements CalculatorRemote, CalculatorLocal
{
...
The local interface can be injected in a Servlet of a web module this way:
private CalculatorLocal calculator;
/**
* Injecting the EJB
*/
#EJB(name = "calculator")
public void setCalculator(CalculatorLocal calculator)
{
this.calculator = calculator;
}
There is an important note in this tutorial that i'm pasting below:
For the injection to take place in a
web module, your web.xml should use
the 2.5 version of the web-app xsd:
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">