3 Replies Latest reply on Apr 4, 2010 9:13 AM by mmaia

    EntityManagerFactory not found in JNDI when running TestNG

    mmaia

      I have implemented JpaIdentityStore for authentication. It's working perfectly when running JBoss and deploying the seam project.


      I have an Observer to listen to login events:




      package br.com.anototudo.seguranca;
      
      import ...
      
      @Name("autenticacaoObserver")
      public class AutenticacaoObserver {
           @Logger
           private Log log;
           
           @In private Credentials credentials;
           
           @Observer(Credentials.EVENT_INIT_CREDENTIALS)
           public void credentialsInicializadas() {
                log.info("Credenciais foram inicializadas");
           }
           
           @Observer(Identity.EVENT_LOGIN_SUCCESSFUL)
           public void loginSuccessful() {
                log.info("O login foi efetuado com sucesso para u Usuario: #0", credentials.getUsername());
           }
      
           @Observer(Identity.EVENT_LOGIN_FAILED)
           public void loginFailed(Exception e) {
                log.info("Erro na autenticacao do usuario: #0, Excecao: #1, Causa: #2", credentials.getUsername(), e, e.getCause());
           }
           
           /**
            * Coloca o objeto User na sessao ao fazer o login.
            * @param user
            */
           @Observer(JpaIdentityStore.EVENT_USER_AUTHENTICATED)
           public void usuarioAutenticado(User user) {
                log.info("Login do usuario: " + user.getEmail());
                Contexts.getSessionContext().set("user", user);
           }
      }




      I'm trying to write an Integration test based on seam examples to test login. When I run my test class I get the following error reported by loginFailed method from my observer above:




      INFO  [br.com.anototudo.seguranca.AutenticacaoObserver] Erro na autenticacao do usuario: maia.marcos@gmail.com, Excecao: javax.security.auth.login.LoginException: EntityManagerFactory not found in JNDI : java:/anototudoEntityManagerFactory, Causa: java.lang.IllegalArgumentException: EntityManagerFactory not found in JNDI : java:/anototudoEntityManagerFactory




      In my componentx.xml I have declared my entityManager with the following code:


        


      <persistence:managed-persistence-context name="entityManager" auto-create="true"
             entity-manager-factory="#{anototudo}" persistence-unit-jndi-name="java:/anototudoEntityManagerFactory"/>
      
      



      As I mentioned it's all working perfectly when running on JBoss AS. The error is only when I run my test class:








       

        • 1. Re: EntityManagerFactory not found in JNDI when running TestNG
          mmaia

          Marcos Maia wrote on Apr 04, 2010 08:01:


          As I mentioned it's all working perfectly when running on JBoss AS. The error is only when I run my test class:




          Forgot to post my LoginTest.java class(both tests are failing with the same error message mentioned above:




          package br.com.anototudo.test.view;
          
          import ...
          
          public class LoginTest extends SeamTest {
          
               @Test
               public void testLoginComponent() throws Exception {
                    
                    new ComponentTest() {
          
                         @Override
                         protected void testComponents() throws Exception {
                              assert getValue("#{identity.loggedIn}").equals(false);
                              setValue("#{credentials.username}", "maia.marcos@gmail.com");
                              setValue("#{credentials.password}", "12345678");
                              invokeMethod("#{identity.login}");
                              assert getValue("#{user.nome}").equals("Administrador do Anototudo");
                              assert getValue("#{user.username}").equals(
                                        "maia.marcos@gmail.com");
                              assert getValue("#{user.password}").equals("12345678");
                              assert getValue("#{identity.loggedIn}").equals(true);
                              invokeMethod("#{identity.logout}");
                              assert getValue("#{identity.loggedIn}").equals(false);
                              setValue("#{credentials.username}", "maia.marcos@gmail.com");
                              setValue("#{credentials.password}", "12345678");
                              invokeMethod("#{identity.login}");
                              assert getValue("#{identity.loggedIn}").equals(false);
                         }
          
                    }.run();
               }
          
               @Test
               public void testLogin() throws Exception {
                    new FacesRequest() {
          
                         @Override
                         protected void invokeApplication() {
                              assert !isSessionInvalid();
                              assert getValue("#{identity.loggedIn}").equals(false);
                         }
          
                    }.run();
          
                    new FacesRequest() {
          
                         @Override
                         protected void updateModelValues() throws Exception {
                              assert !isSessionInvalid();
                              setValue("#{identity.username}", "maia.marcos@gmail.com");
                              setValue("#{identity.password}", "12345678");
                         }
          
                         @Override
                         protected void invokeApplication() {
                              invokeAction("#{identity.login}");
                         }
          
                         @Override
                         protected void renderResponse() {
                              assert getValue("#{user.nome}").equals("Administrador do Anototudo");
                              assert getValue("#{user.username}").equals(
                                        "maia.marcos@gmail.com");
                              assert getValue("#{user.password}").equals("12345678");
                              assert !Manager.instance().isLongRunningConversation();
                              assert getValue("#{identity.loggedIn}").equals(true);
                         }
          
                    }.run();
          
                    new FacesRequest() {
          
                         @Override
                         protected void invokeApplication() {
                              assert !isSessionInvalid();
                              assert getValue("#{identity.loggedIn}").equals(true);
                         }
          
                    }.run();
          
                    new FacesRequest() {
          
                         @Override
                         protected void invokeApplication() {
                              assert !Manager.instance().isLongRunningConversation();
                              assert !isSessionInvalid();
                              invokeMethod("#{identity.logout}");
                              assert Session.instance().isInvalid();
                         }
          
                         @Override
                         protected void renderResponse() {
                              assert getValue("#{identity.loggedIn}").equals(false);
                              assert Session.instance().isInvalid();
                         }
          
                    }.run();
          
               }
          }




          • 2. Re: EntityManagerFactory not found in JNDI when running TestNG
            mmaia

            I have just set log4j configuration for seam package to DEBUG and got the seam Stack Trace for the problem:



            DEBUG [org.jboss.seam.security.Identity] Login failed for: maia.marcos@gmail.com
            javax.security.auth.login.LoginException: EntityManagerFactory not found in JNDI : java:/anototudoEntityManagerFactory
                 at org.jboss.seam.security.jaas.SeamLoginModule.login(SeamLoginModule.java:141)
                 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:597)
                 at javax.security.auth.login.LoginContext.invoke(LoginContext.java:769)
                 at javax.security.auth.login.LoginContext.access$000(LoginContext.java:186)
                 at javax.security.auth.login.LoginContext$5.run(LoginContext.java:706)
                 at java.security.AccessController.doPrivileged(Native Method)
                 at javax.security.auth.login.LoginContext.invokeCreatorPriv(LoginContext.java:703)
                 at javax.security.auth.login.LoginContext.login(LoginContext.java:575)
                 at org.jboss.seam.security.Identity.authenticate(Identity.java:344)
                 at org.jboss.seam.security.Identity.authenticate(Identity.java:332)
                 at org.jboss.seam.security.Identity.login(Identity.java:259)
                 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:597)
                 at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:335)
                 at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:348)
                 at org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java:58)
                 at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
                 at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
                 at org.jboss.seam.core.Expressions$2.invoke(Expressions.java:175)
                 at org.jboss.seam.mock.AbstractSeamTest$ComponentTest.invokeMethod(AbstractSeamTest.java:136)
                 at br.com.anototudo.test.view.LoginTest$1.testComponents(LoginTest.java:22)
                 at org.jboss.seam.mock.AbstractSeamTest$ComponentTest.run(AbstractSeamTest.java:162)
                 at br.com.anototudo.test.view.LoginTest.testLoginComponent(LoginTest.java:36)
                 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:597)
                 at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:644)
                 at org.testng.internal.Invoker.invokeMethod(Invoker.java:546)
                 at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:700)
                 at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1002)
                 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:137)
                 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:121)
                 at org.testng.TestRunner.runWorkers(TestRunner.java:909)
                 at org.testng.TestRunner.privateRun(TestRunner.java:618)
                 at org.testng.TestRunner.run(TestRunner.java:499)
                 at org.testng.SuiteRunner.runTest(SuiteRunner.java:332)
                 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:327)
                 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:299)
                 at org.testng.SuiteRunner.run(SuiteRunner.java:204)
                 at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:915)
                 at org.testng.TestNG.runSuitesLocally(TestNG.java:879)
                 at org.testng.TestNG.run(TestNG.java:787)
                 at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
                 at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124)
            Caused by: java.lang.IllegalArgumentException: EntityManagerFactory not found in JNDI : java:/anototudoEntityManagerFactory
                 at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManagerFactoryFromJndiOrValueBinding(ManagedPersistenceContext.java:245)
                 at org.jboss.seam.persistence.ManagedPersistenceContext.initEntityManager(ManagedPersistenceContext.java:78)
                 at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManager(ManagedPersistenceContext.java:107)
                 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:597)
                 at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
                 at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144)
                 at org.jboss.seam.Component.callComponentMethod(Component.java:2249)
                 at org.jboss.seam.Component.unwrap(Component.java:2275)
                 at org.jboss.seam.Component.getInstance(Component.java:2041)
                 at org.jboss.seam.Component.getInstance(Component.java:1983)
                 at org.jboss.seam.Component.getInstance(Component.java:1977)
                 at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:55)
                 at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:50)
                 at org.jboss.seam.el.SeamELResolver.resolveBase(SeamELResolver.java:148)
                 at org.jboss.seam.el.SeamELResolver.getValue(SeamELResolver.java:51)
                 at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:143)
                 at org.jboss.el.parser.AstIdentifier.getValue(AstIdentifier.java:44)
                 at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
                 at org.jboss.seam.core.Expressions$1.getValue(Expressions.java:112)
                 at org.jboss.seam.security.management.JpaIdentityStore.lookupEntityManager(JpaIdentityStore.java:1051)
                 at org.jboss.seam.security.management.JpaIdentityStore.lookupUser(JpaIdentityStore.java:860)
                 at org.jboss.seam.security.management.JpaIdentityStore.authenticate(JpaIdentityStore.java:806)
                 at org.jboss.seam.security.management.IdentityManager.authenticate(IdentityManager.java:271)
                 at org.jboss.seam.security.jaas.SeamLoginModule.login(SeamLoginModule.java:126)
                 ... 49 more
            Caused by: javax.naming.NameNotFoundException: anototudoEntityManagerFactory not bound
                 at org.jnp.server.NamingServer.getBinding(NamingServer.java:542)
                 at org.jnp.server.NamingServer.getBinding(NamingServer.java:550)
                 at org.jnp.server.NamingServer.getObject(NamingServer.java:556)
                 at org.jnp.server.NamingServer.lookup(NamingServer.java:296)
                 at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:669)
                 at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:629)
                 at javax.naming.InitialContext.lookup(InitialContext.java:392)
                 at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManagerFactoryFromJndiOrValueBinding(ManagedPersistenceContext.java:241)
                 ... 75 more




            Any ideas? I'm lost here, just starting with Seam.




            []s




            • 3. Re: EntityManagerFactory not found in JNDI when running TestNG
              mmaia

              I got it. I had an error in my persistence-test.xml file. I Just fixed the entry for entityManager to the correct one and now it's working:


              Follows the correct enty for EntityManagerFactory:




              <property name="jboss.entity.manager.factory.jndi.name" value="java:/anototudoEntityManagerFactory"/>