/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
// $Id: HibernateIntgUnitTestCase.java,v 1.2.2.2 2005/12/01 19:50:59 dimitris Exp $
package org.jboss.test.hibernate.test;

import org.jboss.test.JBossTestCase;
import org.jboss.test.hibernate.model.User;
import org.jboss.test.hibernate.model.Name;
import org.jboss.test.hibernate.ejb.interfaces.ProfileService;
import org.jboss.test.hibernate.ejb.interfaces.ProfileServiceHome;
import org.jboss.test.hibernate.ejb.interfaces.ProfileServiceUtil;

import java.util.GregorianCalendar;
import java.util.List;
import java.util.Iterator;

import junit.framework.Test;

/**
 * Implementation of HibernateIntgUnitTestCase.
 *
 * @author Steve Ebersole
 */
public class HibernateIntgUnitTestCase extends JBossTestCase
{
   public HibernateIntgUnitTestCase(String name) throws Exception
   {
      super(name);
   }

   /** Setup the test suite.
    */
   public static Test suite() throws Exception
   {
      return getDeploySetup(HibernateIntgUnitTestCase.class, "hib-test.ear");
   }

   public void testEjbInterception() throws Throwable {
      // A simple test to ensure the session gets bound as part of the
      // interceptor
      getLog().debug("Starting hibernate ejb interceptor test case");

      User user = new User();
      user.setEmail("nobody@nowhere.com");
      user.setName( new Name() );
      user.getName().setFirstName("John");
      user.getName().setInitial( new Character('Q') );
      user.getName().setLastName("Public");
      user.setPassword("password");
      user.setTimeOfCreation( new GregorianCalendar() );
      user.setHandle("myHandle");

      ProfileServiceHome home = ProfileServiceUtil.getHome();
      ProfileService service = home.create();

      Long savedUserId = service.storeUser(user).getId();
      getLog().info("User created with id = " + savedUserId );

      List users = service.listUsers();
      getLog().debug("user list = " + users);
      if (users == null || users.isEmpty())
      {
         getLog().info("No users returned in list");
      }
      else
      {
         Iterator itr = users.iterator();
         while ( itr.hasNext() )
         {
            getLog().info("List returned user w/ id = " + ( (User) itr.next() ).getId() );
         }
      }

      user = service.loadUser(savedUserId);
      assertNotNull(user);
   }

}
