hivemind-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <Thomas.Vaug...@usitc.gov>
Subject RE: How do JUnit test programmatic access of the Registry?
Date Tue, 12 Sep 2006 16:31:42 GMT
-- Sorry if this is a repeat, but I just got a bounce-back saying my original response had
a virus --


The web application allows users to upload N files to a temporary store on our application
server.  When they like the way everything got uploaded & tagged, they click "submit"
and it dumps a message on a JMS queue for asynchronous processing at some later time.

If the user uploads 5 of 7 files and the goes and gets some coffee, his session will timeout.
 We want to listen for that event and delete all the files he's uploaded.

The ASO I'm interested in is just a java.lang.String that is the name of the temporary directory
he's uploaded the 5 files to.

We store the temp dir in the ASO because it's needed by several pages in the flow of the app
and because it needs to be accessed by the Session Listener, which isn't a "Tapestry page".

I'm trying to write a unit test to verify that when the session listener notices a session
timeout, it can grab that String and delete any files it finds under the directory.

Any suggestions on how to really test that?

Tom

-----Original Message-----
From: James Carman [mailto:james@carmanconsulting.com] 
Sent: Tuesday, September 12, 2006 10:21 AM
To: user@hivemind.apache.org
Subject: RE: How do JUnit test programmatic access of the Registry?

A session event doesn't happen during the processing of a request,
necessarily.  So, that's why you're getting the NPE.  You're right, there's
not much you can do about that, even in your production environment this
will fail.

What are you trying to clean up when the session is destroyed?  The
references to the objects in the session will be cleaned up automatically
for you.  You don't have db connections (or something similar) in there as
ASOs, do you?  ASOs should really just be "state" (data).  Your HiveMind
services would act upon that state by using the ASM to lookup your ASOs.
So, the cleanup of the data should be easily handled by the garbage
collector.  Do you have a special use case with legacy code or something
that requires you to put heavyweight objects into the session that require
special cleanup?

-----Original Message-----
From: Thomas.Vaughan@usitc.gov [mailto:Thomas.Vaughan@usitc.gov] 
Sent: Monday, September 11, 2006 1:50 PM
To: user@hivemind.apache.org
Subject: RE: How do JUnit test programmatic access of the Registry?

Hi Achim,

Thanks for the interest.

First of all, the "getHivemindRegistry()" method walks up the object tree of
an HttpSessionevent object to grab a hivemind Registry object.  This is not
the cause of the NPE, but since you asked. . . .

static final String REGISTRY_KEY_PREFIX = "org.apache.tapestry.Registry";
static final String SERVLET_NAME = "edis3-efile";
static final String ATTRIBUTE = REGISTRY_KEY_PREFIX + ":" + SERVLET_NAME;
Registry getHivemindRegistry(HttpSessionEvent event) {
	
    HttpSession session = event.getSession();

    ServletContext servletContext = session.getServletContext();

    Registry registry = (Registry)servletContext.getAttribute(ATTRIBUTE);

    return registry;
}

I've attached the NPE stack trace below.  But here's my understanding of it,
which is why my initial email was basically a question about "how do I
insert an ASO?"

1) The call to getASO() gets the Registry using the code above

2) The call to manager.exists(asoName) executes this code:
    public boolean exists(String objectName)
    {
        return _stateObjects.containsKey(objectName) ||  
               _registry.get(objectName).exists();
    }

3) The _stateObjects.... check fails (it shouldn't, as far as I'm
concerned), so it calls _registry.get(objectName).exists()

4) The .get(objectName).exists() is:
    public boolean exists()
    {
        return _persistenceManager.exists(_name);
    }

5) The .exists(_name) method is:
    public boolean exists(String objectName)
    {
        WebSession session = _request.getSession(false);

        if (session == null)
            return false;

        return session.getAttribute(buildKey(objectName)) != null;
    }

6) The NPE is on the _request.getSession(false) call. . . .the _request
(class is WebRequest) is null, and I don't know what I can do about that.

Thanks in advance!
Tom



Here's the NPE that JUnit shows me

java.lang.NullPointerException
	at
org.apache.tapestry.engine.state.SessionScopeManager.exists(SessionScopeMana
ger.java:53)
	at
$StateObjectPersistenceManager_10d9deea139.exists($StateObjectPersistenceMan
ager_10d9deea139.java)
	at
$StateObjectPersistenceManager_10d9deea138.exists($StateObjectPersistenceMan
ager_10d9deea138.java)
	at
org.apache.tapestry.engine.state.StateObjectManagerImpl.exists(StateObjectMa
nagerImpl.java:45)
	at
org.apache.tapestry.engine.state.ApplicationStateManagerImpl.exists(Applicat
ionStateManagerImpl.java:51)
	at
$ApplicationStateManager_10d9deea130.exists($ApplicationStateManager_10d9dee
a130.java)
	at
$ApplicationStateManager_10d9deea131.exists($ApplicationStateManager_10d9dee
a131.java)
	at
gov.usitc.edis.utils.SessionListener.getASO(SessionListener.java:142)
	at
gov.usitc.edis.utils.SessionListenerTest.testGetASO(SessionListenerTest.java
:161)
<...snipped the JUnit stuff...>





-----Original Message-----
From: Achim Hügen [mailto:achim.huegen@gmx.de] 
Sent: Saturday, September 09, 2006 1:17 PM
To: user@hivemind.apache.org
Subject: Re: How do JUnit test programmatic access of the Registry?

Where do you get NullPointerExceptions? Could you post a stack trace?
How does getHivemindRegistry work?

Achim

Am Thu, 07 Sep 2006 17:09:41 +0200 schrieb <Thomas.Vaughan@usitc.gov>:

> Hi,
>
> Here's the situation:
>
> I have a Tapestry 4 application with a hivemodule.xml file inside of
> which is a "tmpDir" ASO String.
>
> I've got an HttpSessionListener configured to perform some cleanup
> operations when the session expires.
>
> Inside that listener, I have code like this:
>
> Object getASO(HttpSessionEvent event, String asoName) {
>     ApplicationStateManager manager = null;
>     Registry registry = getHivemindRegistry(event);
> 		
> 		
>     if(registry.containsService(ApplicationStateManager.class)) {
>         manager = (ApplicationStateManager)
>             registry.getService(ApplicationStateManager.class);
>     } else {
> 	  throw new UsitcApplicationRuntimeException("blah");
>     }
> 		
>     Object aso = null;
>
>     if(manager.exists(asoName)) {
>         aso = manager.get(asoName);	
>     }
> 		
>     return aso;
> }
>
> I suspect this code will run fine in production, but I'm having a
> helluva time writing a unit test for this.  Basically, I just want to
> confirm that if I call 'getAso(event, "tempUploadDir")' it'll return the
> String that is the value of the " tempUploadDir " key in my
> hivemodule.xml ASO.  Simple, right?
>
> In my unit test, I've got code like this:
>
> public void testGetASO() throws Exception {
>     //  ehmtc just extends the HiveMindTestCase to provide access to the
>
>     // protected 'buildFrameworkRegistry' methods
>     EdisHiveMindUtil ehmtc = new EdisHiveMindUtil();
>     Registry registry = ehmtc.buildFrameworkRegistry();
> 	
>     HttpSessionEvent event = createMockHttpSessionEvent(registry);
> 		
>     SessionListener sl = new SessionListener();
>     String dir = (String)sl.getASO(event, "tempUploadDir");
> 		
>     System.out.println("dir = " + dir);
> }
>
> The problem is that even though the registry gets loaded, it
> NullPointerExceptions unless the (key, value) pair for tempUploadDir is
> defined.
>
> Unfortunately, I see no way in the API of force-setting that ASO, nor do
> I see a straight forward way of EasyMocking this.
>
> Please help!
>





Mime
View raw message