You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Fraser, James" <Ja...@dsto.defence.gov.au> on 2011/06/15 02:23:30 UTC

Embedded tomcat and unit testing.. Tomcat either stops or my unit tests don't execute? [SEC=UNCLASSIFIED]

UNCLASSIFIED 

Hi, 

I have the following in my unit test. 

    @BeforeClass
    public static void setUpClass() throws ServletException,
LifecycleException, MalformedURLException {
        tomcat = new Tomcat();
        tomcat.setBaseDir(".");
        tomcat.setPort(8084);
        Context ctx = tomcat.addWebapp("/",
System.getProperty("user.dir") + "/build/web");
        tomcat.setHostname("localhost"); 

        File contextFile = new File(System.getProperty("user.dir") +
"/build/web/META-INF/context.xml");
        ctx.setConfigFile(contextFile.toURI().toURL()); 

        tomcat.enableNaming();
        tomcat.start();
        tomcat.getServer().await();       
    } 

Without the line `tomcat.getServer().await();`, Tomcat simply stops;
with the line `tomcat.getServer().await();`, it looks as though the
thread blocks and my unit tests don't execute.. Any idea?

James 

IMPORTANT: This email remains the property of the Department of Defence
and is subject to the jurisdiction of section 70 of the Crimes Act 1914.
If you have received this email in error, you are requested to contact
the sender and delete the email. 



RE: Embedded tomcat and unit testing.. Tomcat either stops or my unit tests don't execute? [SEC=UNCLASSIFIED]

Posted by "Fraser, James" <Ja...@dsto.defence.gov.au>.
UNCLASSIFIED

Thanks Chris! 

-----Original Message-----
From: Christopher Schultz [mailto:chris@christopherschultz.net]
Sent: Thursday, 16 June 2011 5:01 AM
To: Tomcat Users List
Subject: Re: Embedded tomcat and unit testing.. Tomcat either stops or
my unit tests don't execute? [SEC=UNCLASSIFIED]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

James,

On 6/15/2011 12:09 AM, Fraser, James wrote:
> Thanks for your help. I got it working in the end. I suspect the main 
> problem had to do with not terminating the running instance of Tomcat 
> correctly in a tear-down method.

That might do it, depending on what resource was under contention (such
as a port number).

Also, calling Tomcat.getServer().await() is /intended/ to block until
the server shuts down. If you run that in your test setup, the test will
never run because it's waiting for Tomcat to shut down.

Instead, I suspect you want to do everything but the "await" in your
test setup, and as you discovered, make sure you shut it down in your
teardown code.

As Chuck points out, Tomcat's internal unit-testing code uses the Tomcat
class and can serve as a useful set of examples if you need further
inspiration.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk35CHgACgkQ9CaO5/Lv0PABxQCeOeNtdE7PjvoyuSDywzV3WSla
2PkAnRWemhH6FeJJ8PlnXT3rfFuhBgRa
=JYsd
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


IMPORTANT: This email remains the property of the Department of Defence
and is subject to the jurisdiction of section 70 of the Crimes Act 1914.
If you have received this email in error, you are requested to contact
the sender and delete the email.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Embedded tomcat and unit testing.. Tomcat either stops or my unit tests don't execute? [SEC=UNCLASSIFIED]

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

James,

On 6/15/2011 12:09 AM, Fraser, James wrote:
> Thanks for your help. I got it working in the end. I suspect the main
> problem had to do with not terminating the running instance of Tomcat
> correctly in a tear-down method.

That might do it, depending on what resource was under contention (such
as a port number).

Also, calling Tomcat.getServer().await() is /intended/ to block until
the server shuts down. If you run that in your test setup, the test will
never run because it's waiting for Tomcat to shut down.

Instead, I suspect you want to do everything but the "await" in your
test setup, and as you discovered, make sure you shut it down in your
teardown code.

As Chuck points out, Tomcat's internal unit-testing code uses the Tomcat
class and can serve as a useful set of examples if you need further
inspiration.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk35CHgACgkQ9CaO5/Lv0PABxQCeOeNtdE7PjvoyuSDywzV3WSla
2PkAnRWemhH6FeJJ8PlnXT3rfFuhBgRa
=JYsd
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Embedded tomcat and unit testing.. Tomcat either stops or my unit tests don't execute? [SEC=UNCLASSIFIED]

Posted by "Fraser, James" <Ja...@dsto.defence.gov.au>.
UNCLASSIFIED

Hi Chuck,

Thanks for your help. I got it working in the end. I suspect the main
problem had to do with not terminating the running instance of Tomcat
correctly in a tear-down method. I solved this using the example in the
Tomcat unit tests; i.e.

 @AfterClass
    public static void tearDownClass() throws LifecycleException {
        if (tomcat.getServer() != null
                && tomcat.getServer().getState() !=
LifecycleState.DESTROYED) {
            if (tomcat.getServer().getState() != LifecycleState.STOPPED)
{
                tomcat.stop();
            }
            tomcat.destroy();
        }
    }

- James 

-----Original Message-----
From: Caldarale, Charles R [mailto:Chuck.Caldarale@unisys.com]
Sent: Wednesday, 15 June 2011 11:27 AM
To: Tomcat Users List
Subject: RE: Embedded tomcat and unit testing.. Tomcat either stops or
my unit tests don't execute? [SEC=UNCLASSIFIED]

> From: Fraser, James [mailto:James.Fraser@dsto.defence.gov.au]
> Subject: RE: Embedded tomcat and unit testing.. Tomcat either stops or

> my unit tests don't execute? [SEC=UNCLASSIFIED]

> I read that Apache use embedded Tomcat for unit testing. Are there any

> examples floating around?

Good thought.  I believe all of the tests are in the Tomcat source
download; they run inside junit.

I'm sure Mark T could tell you exactly what's wrong, but I suspect he's
sleeping at the moment (UK).

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


IMPORTANT: This email remains the property of the Department of Defence
and is subject to the jurisdiction of section 70 of the Crimes Act 1914.
If you have received this email in error, you are requested to contact
the sender and delete the email.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Embedded tomcat and unit testing.. Tomcat either stops or my unit tests don't execute? [SEC=UNCLASSIFIED]

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Fraser, James [mailto:James.Fraser@dsto.defence.gov.au] 
> Subject: RE: Embedded tomcat and unit testing.. Tomcat either 
> stops or my unit tests don't execute? [SEC=UNCLASSIFIED]

> I read that Apache use embedded Tomcat for unit testing. Are there any
> examples floating around? 

Good thought.  I believe all of the tests are in the Tomcat source download; they run inside junit.

I'm sure Mark T could tell you exactly what's wrong, but I suspect he's sleeping at the moment (UK).

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Embedded tomcat and unit testing.. Tomcat either stops or my unit tests don't execute? [SEC=UNCLASSIFIED]

Posted by "Fraser, James" <Ja...@dsto.defence.gov.au>.
UNCLASSIFIED

I read that Apache use embedded Tomcat for unit testing. Are there any
examples floating around? 

-----Original Message-----
From: Caldarale, Charles R [mailto:Chuck.Caldarale@unisys.com]
Sent: Wednesday, 15 June 2011 10:01 AM
To: Tomcat Users List
Subject: RE: Embedded tomcat and unit testing.. Tomcat either stops or
my unit tests don't execute? [SEC=UNCLASSIFIED]

> From: Fraser, James [mailto:James.Fraser@dsto.defence.gov.au]
> Subject: Embedded tomcat and unit testing.. Tomcat either stops or my 
> unit tests don't execute? [SEC=UNCLASSIFIED]

>         tomcat.enableNaming();
>         tomcat.start();
>         tomcat.getServer().await();       

> Without the line `tomcat.getServer().await();`, Tomcat simply stops;

As expected, since all of Tomcat's threads are daemons.

> with the line `tomcat.getServer().await();`, it looks as though the 
> thread blocks and my unit tests don't execute..

Not having used an embedded Tomcat, I can't really comment on what
you're missing.  You might first try diagnosing it by connecting to the
program (with the await() in place) via JConsole, and see what threads
are present.  Compare that with Tomcat running normally (not embedded).

 - Chuck 


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


IMPORTANT: This email remains the property of the Department of Defence
and is subject to the jurisdiction of section 70 of the Crimes Act 1914.
If you have received this email in error, you are requested to contact
the sender and delete the email.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Embedded tomcat and unit testing.. Tomcat either stops or my unit tests don't execute? [SEC=UNCLASSIFIED]

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Fraser, James [mailto:James.Fraser@dsto.defence.gov.au] 
> Subject: Embedded tomcat and unit testing.. Tomcat either stops 
> or my unit tests don't execute? [SEC=UNCLASSIFIED]

>         tomcat.enableNaming();
>         tomcat.start();
>         tomcat.getServer().await();       

> Without the line `tomcat.getServer().await();`, Tomcat simply stops;

As expected, since all of Tomcat's threads are daemons.

> with the line `tomcat.getServer().await();`, it looks as though the
> thread blocks and my unit tests don't execute..

Not having used an embedded Tomcat, I can't really comment on what you're missing.  You might first try diagnosing it by connecting to the program (with the await() in place) via JConsole, and see what threads are present.  Compare that with Tomcat running normally (not embedded).

 - Chuck 


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org