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(SessionScopeManager.java:53)
at
$StateObjectPersistenceManager_10d9deea139.exists($StateObjectPersistenceManager_10d9deea139.java)
at
$StateObjectPersistenceManager_10d9deea138.exists($StateObjectPersistenceManager_10d9deea138.java)
at
org.apache.tapestry.engine.state.StateObjectManagerImpl.exists(StateObjectManagerImpl.java:45)
at
org.apache.tapestry.engine.state.ApplicationStateManagerImpl.exists(ApplicationStateManagerImpl.java:51)
at
$ApplicationStateManager_10d9deea130.exists($ApplicationStateManager_10d9deea130.java)
at
$ApplicationStateManager_10d9deea131.exists($ApplicationStateManager_10d9deea131.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:[EMAIL PROTECTED]
Sent: Saturday, September 09, 2006 1:17 PM
To: [email protected]
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 <[EMAIL PROTECTED]>:
> 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!
>