[GLASSFISH-18585] embedded glassfish: jersey-test does not honor @Singleton and @ManagedBean Created: 20/Aug/11  Updated: 01/Apr/12

Status: Open
Project: glassfish
Component/s: embedded
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major
Reporter: jst Assignee: sakshi.jain
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux + Glassfish.


Attachments: Text File embedded_log.txt     File UsingEmbeddedGlassfish.diff     File UsingExternalGlassfish.diff    

 Description   

I am trying to switch my project to run in Glassfish. The application seems to work, but I have problems with tests. They all seem to fail, as my class, annoted as

import javax.annotation.ManagedBean;
import javax.inject.Singleton;

@Path("/")
@Singleton
@ManagedBean
public class Quoridor {
}

is instantiate more than one per each test run. Could the Jersey framework honor the @Singleton + @ManagedBean annotation as glassfish does?



 Comments   
Comment by jst [ 20/Aug/11 ]

Steps to reproduce:

$ hg clone http://source.apidesign.org/hg/quoridor/
$ cd quoridor/
$ hg up -C accdcc2ab312
$ mvn install

The build fails with: Tests run: 13, Failures: 2, Errors: 6, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Quoridor related projects ......................... SUCCESS [0.265s]
[INFO] Quoridor Library .................................. SUCCESS [1.752s]
[INFO] Quoridor Visual Board ............................. SUCCESS [0.794s]
[INFO] wsdor ............................................. SUCCESS [0.410s]
[INFO] webidor server .................................... FAILURE [49.682s]

Now you can open the quoridor/webidor project in NetBeans place a breakpoint into constructor of Quoridor class and debug for example ChatTest. You should see that the instances of Quoridor class are created multiple times, in spite it is a singleton.

Comment by jst [ 20/Aug/11 ]

Update Please "hg up -C 92bd2090dd5f" as I found out that with compile on save the debugging is broken. I had to disable it completely. With 92bd2090dd5f debugging part of my report works.

Comment by Pavel Bucek [ 23/Aug/11 ]

just my $0.02:

@Singleton and @ManagedBean (and others) should work as expected. I haven't seen your testcase, but Mu guess is that you are trying to access your singleton instance from various tests (@Test annotated methods), which should return (by definition) different instances. Why? Each test currently "restarts" Jersey, reloads all resource methods, providers, etc..

What you could do is try to access your @Singleton annotated class twice from one test method, that should work as you expect.

Comment by jst [ 23/Aug/11 ]

Should you check my usecase you'd find out that there is only single test method in
http://source.apidesign.org/hg/quoridor/file/92bd2090dd5f/webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java
Still multiple instances are created.

Comment by jbenoit [ 04/Sep/11 ]

I've reproduced your original reported error. To help confirm if problem using embedded glassfish, can you please try using external Glassfish instead of embedded glassfish? I've done so, but I encounter 403 & 404 errors whilest trying to confirm behavior.

Comment by jst [ 05/Sep/11 ]

Good to know the problem has been reproduced. I'll be happy to change my project testing code anyway you suggest. However I am don't have that much experience with application servers, so I don't know how to eliminate embedded glassfish properly.

Comment by jbenoit [ 05/Sep/11 ]

See http://jersey.java.net/nonav/documentation/latest/test-framework.html#d4e1275

I did this:

  • added this <dependency> to quoridor/webidor/pom.xml, at line 57:

<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-external</artifactId>
<version>$

{jerseyVersion}</version>
<scope>test</scope>
</dependency>

the above <dependency> was added right before:
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-embedded-glassfish</artifactId>
<version>${jerseyVersion}

</version>
<scope>test</scope>
</dependency>

then i built and deployed quoridor\webidor\target\webidor-1.19.war to external Glassfish, i.e.

// build webidor-1.19.war
C:\a\fresh\jersey-757\quoridor\webidor>mvn package -Dmaven.repo.local=C:\a\fresh\jersey-757\quoridor\repo -DargLine="-Dhttp.proxyHost=yourProxyHostNameGoesHere" -Dmaven.test.skip=true

// start external Glassfish, already installed into C:\a\fresh\1.8testing dir
// downloaded from http://download.java.net/glassfish/3.1.1/release/glassfish-3.1.1.zip
C:\a\fresh\jersey-757\quoridor>set AS_HOME=C:\a\fresh\1.8testing\glassfish3\glassfish

C:\a\fresh\jersey-757\quoridor>echo %AS_HOME%
C:\a\fresh\1.8testing\glassfish3\glassfish

C:\a\fresh\jersey-757\quoridor>set PATH=%AS_HOME%\bin;%PATH%

C:\a\fresh\jersey-757\quoridor>which asadmin
C:/a/fresh/1.8testing/glassfish3/glassfish/bin/asadmin.bat

// start the domain
C:\a\fresh\jersey-757\quoridor> asadmin start-domain domain1

// deploy webidor-1.19.war
C:\a\fresh\jersey-757\quoridor\webidor> asadmin deploy target\webidor-1.19.war

//run, using proxy behind company firewall:
C:\a\fresh\jersey-757\quoridor>mvn install -Dmaven.repo.local=C:\a\fresh\jersey-757\quoridor\repo -DargLine="-Dhttp.proxyHost=yourProxyHostNameGoesHere" -Djersey.test.containerFactory=com.sun.jersey.test.framework.spi.container.external.ExternalTestContainerFactory -Djersey.test.port=8080

[snip]

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running cz.xelfi.quoridor.webidor.AllGamesTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.532 sec <<< FAILURE!
Running cz.xelfi.quoridor.webidor.FinishedGameTest
Tests run: 5, Failures: 0, Errors: 5, Skipped: 0, Time elapsed: 0.969 sec <<< FAILURE!
Running cz.xelfi.quoridor.webidor.GamesTest
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.328 sec
Running cz.xelfi.quoridor.webidor.QuoridorTest
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.641 sec <<< FAILURE!
Running cz.xelfi.quoridor.webidor.resources.ChatTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.515 sec <<< FAILURE!
Running cz.xelfi.quoridor.webidor.UsersTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.547 sec <<< FAILURE!

Results :

Tests in error:
testListGames(cz.xelfi.quoridor.webidor.AllGamesTest): PUT http://localhost:80
80/allgamess/login?name=Jarda&password=heslo returned a response status of 403
testNotLoggedIn(cz.xelfi.quoridor.webidor.FinishedGameTest): GET http://localh
ost:8080/finishedGame/login?id=not-logged-in returned a response status of 404
testCreateAGame(cz.xelfi.quoridor.webidor.FinishedGameTest): PUT http://localh
ost:8080/finishedGame/login?name=Jarda&password=heslo returned a response status
of 403
testResignAGame(cz.xelfi.quoridor.webidor.FinishedGameTest): PUT http://localh
ost:8080/finishedGame/login?name=Jarda&password=heslo returned a response status
of 403
testResignBGame(cz.xelfi.quoridor.webidor.FinishedGameTest): PUT http://localh
ost:8080/finishedGame/login?name=Jarda&password=heslo returned a response status
of 403
testResignForeignGame(cz.xelfi.quoridor.webidor.FinishedGameTest): PUT http://
localhost:8080/finishedGame/login?name=Jarda&password=heslo returned a response
status of 403
testApplicationWadl(cz.xelfi.quoridor.webidor.QuoridorTest): GET http://localh
ost:8080/quoTest/application.wadl returned a response status of 404
testCreateAGame(cz.xelfi.quoridor.webidor.QuoridorTest): PUT http://localhost:
8080/quoTest/login?name=Jarda&password=heslo returned a response status of 403
testCreateAGame(cz.xelfi.quoridor.webidor.resources.ChatTest): PUT http://loca
lhost:8080/context/login?name=Jarda&password=heslo returned a response status of
403
testListUsers(cz.xelfi.quoridor.webidor.UsersTest): PUT http://localhost:8080/
userstest/login?name=Jarda&password=heslo returned a response status of 403

Tests run: 13, Failures: 0, Errors: 10, Skipped: 0

I added webidor/src/main/webapp/WEB-INF/sun-web.xml containing <context-root>, rebuilt and redeployed .war:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Servlet 2.5//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd">
<sun-web-app error-url="">
<context-root>/webidor-1.19</context-root>
<class-loader delegate="false"/>
<jsp-config>
<property name="classdebuginfo" value="true">
<description>Enable debug info compilation in the generated servlet class</description>
</property>
<property name="mappedfile" value="true">
<description>Maintain a one-to-one correspondence between static content and the generated servlet class' java code</description>
</property>
</jsp-config>
</sun-web-app>

This is where I'd like you to see if you can get your project to work with external Glassfish, such that it avoids these 403/404 errors.

Comment by jst [ 06/Sep/11 ]

I have modified the sources, removed all but one test and I could successfully run the test against externally running glassfish.

Just

$ hg up -C 92bd2090dd5f
$ patch -p1 <UsingExternalGlassfish.diff

and follow the tests described above. The test succeeds.

Comment by jst [ 06/Sep/11 ]

Here is another patch to show that embedded glassfish does not honor @Singleton. You can try:

$ hg up -C 92bd2090dd5f
$ patch -p1 <UsingEmbeddedGlassfish.diff
$ cd webidor
$ mvn install

The test fails with assert in the constructor of the Quoridor class.

Comment by jbenoit [ 26/Sep/11 ]

Regarding your comments:

I have modified the sources, removed all but one test and I could successfully run the test against externally running glassfish.

Just

$ hg up -C 92bd2090dd5f
$ patch -p1 <UsingExternalGlassfish.diff

I don't have "UsingExternalGlassfish.diff" file, here is what i see:

C:\a\fresh\jersey-757>hg up -C 92bd2090dd5f
abort: no repository found in 'C:\a\fresh\jersey-757' (.hg not found)!

C:\a\fresh\jersey-757>cd quoridor

C:\a\fresh\jersey-757\quoridor>hg up -C 92bd2090dd5f
1 files updated, 0 files merged, 0 files removed, 0 files unresolved

C:\a\fresh\jersey-757\quoridor>patch -p1 <UsingExternalGlassfish.diff
The system cannot find the file specified.

C:\a\fresh\jersey-757\quoridor>find . | grep -i UsingExternalGlassfish.diff

Comment by jbenoit [ 30/Sep/11 ]

I see attachments:
UsingEmbeddedGlassfish.diff
UsingExternalGlassfish.diff
but when I execute command it shows error:

C:\a\fresh\jersey-757\2\quoridor>patch -p1 <UsingEmbeddedGlassfish.diff
Hmm... I can't seem to find a patch in there anywhere.

C:\a\fresh\jersey-757\2\quoridor>patch -p1 <UsingExternalGlassfish.diff
Hmm... I can't seem to find a patch in there anywhere.

Comment by jst [ 30/Sep/11 ]

I don't know what can be wrong. Maybe patch does not work well on windows? Try:

$ hg up -C 92bd2090dd5f
$ wget http://java.net/jira/secure/attachment/47163/UsingExternalGlassfish.diff
$ hg import --no-commit UsingExternalGlassfish.diff

These three commands just did the trick for me (on Linux).

Comment by sakshi.jain [ 07/Dec/11 ]

I have been able to run the webidor test using external glassfish.Hence the issue does lie with embedded.
As you said the class annotated as Singleton is instantiated more than once.This is because it is bootstrapping embedded glassfish twice.
Attached herewith is the log which I obtain on running the webidor test.

Comment by sakshi.jain [ 27/Dec/11 ]

Since embedded glassfish was being bootstrapped twice, I declared the Glassfish instance as static so it will bootstrap only once.So after the first test, embedded glassfish would merely stop instead of shutting down, and for the second test it will start again.However, another issue is encountered here which looks like a design issue & we are looking into that.

Comment by Michal Gajdos [ 01/Apr/12 ]

is there any progress on this issue?

Generated at Sat Jul 30 19:52:29 UTC 2016 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.