Tap 5.0.9: How to unit test a DAO service with tapetsry-hibernate module

2008-02-03 Thread Shing Hing Man
I have created a DAO service which has 
HibernateSessionManager (from the tapestry-hibernate
module)  as a property.
In AppModule :

  public static MyFolderDAO buildMyFolderDAO(
@InjectService(HibernateSessionManager)
HibernateSessionManager sessionManager)
  {
MyFolderDAO dao = new
MyFolderDAOHibernate();
dao.setSessionManager(sessionManager);
return dao;
  }


The MyFolderDAO service works when it is injected into
a Tapestry page.
I have problem testing the MyFolderDAO service in a
Junit test.

public class MyFolderDAOHibernateTest extends TestCase
{

public void testAddFolder() {   

RegistryBuilder builder = new RegistryBuilder();
builder.add(TapestryModule.class);  
builder.add(HibernateModule.class);
builder.add(AppModule.class);

Registry registry = builder.build();

registry.performRegistryStartup();

MyFolderDAO dao = registry.getService(myFolderDAO,
MyFolderDAO.class);

MyFolder folder = new MyFolder();
folder.setFoldername(Folder A);
dao.addFolder(folder);

}

}

I get the following exception when I  run the above
JUnit test. It looks as though I have not  built  the 
Registry correctly.
It would be very much appreciated if someone can show
me a way.

Shing 



[ERROR] Alias Construction of service Alias failed:
Error invoking service builder method
org.apache.tapestry.services.TapestryModule.build(Logger,
String, AliasManager, Collection) (at
TapestryModule.java:130) (for service 'Alias'): Symbol
'tapestry.alias-mode' is not defined.
java.lang.RuntimeException: Error invoking service
builder method
org.apache.tapestry.services.TapestryModule.build(Logger,
String, AliasManager, Collection) (at
TapestryModule.java:130) (for service 'Alias'): Symbol
'tapestry.alias-mode' is not defined.
at
org.apache.tapestry.ioc.internal.ServiceBuilderMethodInvoker.createObject(ServiceBuilderMethodInvoker.java:88)
at
org.apache.tapestry.ioc.internal.SingletonServiceLifecycle.createService(SingletonServiceLifecycle.java:29)
at
org.apache.tapestry.ioc.internal.LifecycleWrappedServiceCreator.createObject(LifecycleWrappedServiceCreator.java:54)
at
org.apache.tapestry.ioc.internal.InterceptorStackBuilder.createObject(InterceptorStackBuilder.java:51)
at
org.apache.tapestry.ioc.internal.RecursiveServiceCreationCheckWrapper.createObject(RecursiveServiceCreationCheckWrapper.java:61)
at
org.apache.tapestry.ioc.internal.services.JustInTimeObjectCreator.createObject(JustInTimeObjectCreator.java:66)
at
$Alias_117e094fd17._delegate($Alias_117e094fd17.java)
at
$Alias_117e094fd17.getObjectProvider($Alias_117e094fd17.java)
at
org.apache.tapestry.services.TapestryModule$1.provide(TapestryModule.java:442)
at
org.apache.tapestry.ioc.internal.services.MasterObjectProviderImpl.provide(MasterObjectProviderImpl.java:38)
at
$MasterObjectProvider_117e094fd14.provide($MasterObjectProvider_117e094fd14.java)
at
org.apache.tapestry.ioc.internal.RegistryImpl.getObject(RegistryImpl.java:621)
at
org.apache.tapestry.ioc.internal.RegistryImpl.getObject(RegistryImpl.java:675)
at
org.apache.tapestry.ioc.internal.ObjectLocatorImpl.getObject(ObjectLocatorImpl.java:50)
at
org.apache.tapestry.ioc.internal.util.InternalUtils.calculateParameterValue(InternalUtils.java:207)
at
org.apache.tapestry.ioc.internal.util.InternalUtils.calculateParameters(InternalUtils.java:237)
at
org.apache.tapestry.ioc.internal.util.InternalUtils.calculateParametersForConstructor(InternalUtils.java:225)
at
org.apache.tapestry.ioc.internal.ConstructorServiceCreator.createObject(ConstructorServiceCreator.java:46)
at
org.apache.tapestry.ioc.internal.ServiceResourcesImpl.autobuild(ServiceResourcesImpl.java:123)
at
org.apache.tapestry.ioc.services.TapestryIOCModule.contributeServiceLifecycleSource(TapestryIOCModule.java:80)
at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
org.apache.tapestry.ioc.internal.ContributionDefImpl.invokeMethod(ContributionDefImpl.java:95)
at
org.apache.tapestry.ioc.internal.ContributionDefImpl.contribute(ContributionDefImpl.java:68)
at
org.apache.tapestry.ioc.internal.RegistryImpl.addToMappedConfiguration(RegistryImpl.java:451)
at
org.apache.tapestry.ioc.internal.RegistryImpl.getMappedConfiguration(RegistryImpl.java:408)
at

Re: Tap 5.0.9: How to unit test a DAO service with tapetsry-hibernate module

2008-02-03 Thread Shing Hing Man
Thanks for the suggestion!

After reading


http://wiki.apache.org/tapestry/Tapestry5HowToIocAndHibernate

and tried a few combinations. The following works.

1) In AppModule :
 define  the MyFolderDAO service as before.

2) Create AppModuleTest.java which is only available
in unit test.

public class AppModuleTest
{
public static void bind(ServiceBinder binder)
{
  
 binder.bind(ClassNameLocator.class,
ClassNameLocatorImpl.class);
}


public static void
contributeSymbolSource(OrderedConfigurationSymbolProvider
conf){
//tapestry-hibernate fails without
tapestry.app-name symbol defined
conf.add(AppPackage, new SymbolProvider(){
   public String valueForSymbol(String
symbolName){
  
if(symbolName.equalsIgnoreCase(InternalConstants.TAPESTRY_APP_PACKAGE_PARAM))
   return net.sf.folder;
   return null;
} 
},);
}
}

3) In unit test, create  the registry as follow.
 
RegistryBuilder builder = new RegistryBuilder();

builder.add(HibernateModule.class);
builder.add(AppModule.class);
builder.add(AppModuleTest.class);

Registry registry = builder.build();

registry.performRegistryStartup();




It begs the question on  how come the  service and
configuration  in AppModuleTest.java  are not created
automatically in unit test and 
they are created automatically in web application.

Shing 


--- Sven Homburg [EMAIL PROTECTED] wrote:

 dont add the TapestryModule to RegistryBuilder,
 it make only sense, if you testing your DAOs
 inside a page or component.
 
 
 2008/2/3, Shing Hing Man [EMAIL PROTECTED]:
  I have created a DAO service which has
  HibernateSessionManager (from the
 tapestry-hibernate
  module)  as a property.
  In AppModule :
 
public static MyFolderDAO buildMyFolderDAO(
 
 @InjectService(HibernateSessionManager)
  HibernateSessionManager sessionManager)
{
  MyFolderDAO dao = new
  MyFolderDAOHibernate();
  dao.setSessionManager(sessionManager);
  return dao;
}
 
 
  The MyFolderDAO service works when it is injected
 into
  a Tapestry page.
  I have problem testing the MyFolderDAO service in
 a
  Junit test.
 
  public class MyFolderDAOHibernateTest extends
 TestCase
  {
 
  public void testAddFolder() {
 
  RegistryBuilder builder = new
 RegistryBuilder();
  builder.add(TapestryModule.class);
 
 builder.add(HibernateModule.class);
  builder.add(AppModule.class);
 
  Registry registry =
 builder.build();
 
  registry.performRegistryStartup();
 
  MyFolderDAO dao =
 registry.getService(myFolderDAO,
  MyFolderDAO.class);
 
  MyFolder folder = new MyFolder();
  folder.setFoldername(Folder A);
  dao.addFolder(folder);
 
  }
 
  }
 
  I get the following exception when I  run the
 above
  JUnit test. It looks as though I have not  built 
 the
  Registry correctly.
  It would be very much appreciated if someone can
 show
  me a way.
 
  Shing
 
 
 
  [ERROR] Alias Construction of service Alias
 failed:
  Error invoking service builder method
 

org.apache.tapestry.services.TapestryModule.build(Logger,
  String, AliasManager, Collection) (at
  TapestryModule.java:130) (for service 'Alias'):
 Symbol
  'tapestry.alias-mode' is not defined.
  java.lang.RuntimeException: Error invoking service
  builder method
 

org.apache.tapestry.services.TapestryModule.build(Logger,
  String, AliasManager, Collection) (at
  TapestryModule.java:130) (for service 'Alias'):
 Symbol
  'tapestry.alias-mode' is not defined.
  at
 

org.apache.tapestry.ioc.internal.ServiceBuilderMethodInvoker.createObject(ServiceBuilderMethodInvoker.java:88)
  at
 

org.apache.tapestry.ioc.internal.SingletonServiceLifecycle.createService(SingletonServiceLifecycle.java:29)
  at
 

org.apache.tapestry.ioc.internal.LifecycleWrappedServiceCreator.createObject(LifecycleWrappedServiceCreator.java:54)
  at
 

org.apache.tapestry.ioc.internal.InterceptorStackBuilder.createObject(InterceptorStackBuilder.java:51)
  at
 

org.apache.tapestry.ioc.internal.RecursiveServiceCreationCheckWrapper.createObject(RecursiveServiceCreationCheckWrapper.java:61)
  at
 

org.apache.tapestry.ioc.internal.services.JustInTimeObjectCreator.createObject(JustInTimeObjectCreator.java:66)
  at
 

$Alias_117e094fd17._delegate($Alias_117e094fd17.java)
  at
 

$Alias_117e094fd17.getObjectProvider($Alias_117e094fd17.java)
  at
 

org.apache.tapestry.services.TapestryModule$1.provide(TapestryModule.java:442)
  at
 

org.apache.tapestry.ioc.internal.services.MasterObjectProviderImpl.provide(MasterObjectProviderImpl.java:38)