1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // TestExample.java Copyright (c) 2009 Giovanni Azua Garcia
5   // bravegag@hotmail.com
6   //  
7   // This program is free software; you can redistribute it and/or
8   // modify it under the terms of the GNU General Public License
9   // as published by the Free Software Foundation; either version 3
10  // of the License, or (at your option) any later version.
11  //
12  // This program is distributed in the hope that it will be useful,
13  // but WITHOUT ANY WARRANTY; without even the implied warranty of
14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  // GNU General Public License for more details.
16  //
17  // You should have received a copy of the GNU General Public License
18  // along with this program; if not, see <http://www.gnu.org/licenses/>.
19  //
20  //----------------------------------------------------------------------
21  package org.perfectjpattern.jee.integration.dao.hibernate;
22  
23  import java.text.*;
24  
25  import junit.framework.*;
26  
27  import org.perfectjpattern.example.datamodel.*;
28  import org.perfectjpattern.jee.api.integration.dao.*;
29  import org.perfectjpattern.jee.integration.dao.*;
30  import org.slf4j.*;
31  
32  import static org.easymock.EasyMock.*;
33  
34  /**
35   * Hibernate Generic DAO example test suite 
36   *
37   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
38   * @version $Revision: 1.0 $Date: Apr 20, 2008 1:58:38 PM $
39   */
40  public 
41  class TestExample
42  extends TestCase
43  {
44      //------------------------------------------------------------------------
45      // public
46      //------------------------------------------------------------------------
47      public void
48      testHibernateGenericDao()
49      throws ParseException
50      {
51          Logger myLoggerMock = createNiceMock(Logger.class);
52          Example.setLogger(myLoggerMock);
53  
54          String myLogging = LINE_BREAK +
55          "**************************************************************" + 
56              LINE_BREAK +
57          "| Customer(id='1', name='Ernesto')" + LINE_BREAK +
58          "|--> Order(id='1', date='3/14/07 8:00 AM')" + LINE_BREAK +
59          "|    |-> Product(id='2', name='Nikon D300', list price='2,000')" + 
60              LINE_BREAK +
61          "|    |-> Product(id='3', name='Nikon 80-400mm', list price='1,500')" + 
62              LINE_BREAK +
63          "|--> Order(id='2', date='10/19/07 8:00 AM')" + LINE_BREAK +
64          "|    |-> Product(id='4', name='Nikon Fisheye 10mm', " +
65              "list price='900')" + LINE_BREAK +
66          "**************************************************************";
67          
68          myLoggerMock.debug(myLogging);
69                          
70          replay(myLoggerMock);
71                  
72          Example.main(new String[0]);
73          
74          verify(myLoggerMock);
75      }
76  
77      //------------------------------------------------------------------------
78      // protected
79      //------------------------------------------------------------------------
80      /** 
81       * {@inheritDoc}
82       */
83      @Override
84      protected void 
85      tearDown() 
86      throws Exception
87      {
88          super.tearDown();
89          
90          IGenericDaoFactory myDaoFactory = HibernateDaoFactory.getInstance();
91  
92          try
93          {
94              // renew 
95              HibernateCurrentSessionStrategy mySessionStrategy = 
96                  new HibernateCurrentSessionStrategy();
97              HibernateConfiguredTransactionStrategy myTransactionStrategy = 
98                  new HibernateConfiguredTransactionStrategy(mySessionStrategy);
99              
100             myDaoFactory.setDaoSessionStrategy(mySessionStrategy);
101             myDaoFactory.setDaoTransactionStrategy(myTransactionStrategy);
102             
103             IGenericDao<Long, Customer> myCustomerDao = myDaoFactory.
104                 createDao(Customer.class);
105             myCustomerDao.deleteAll();
106         }
107         finally 
108         {
109             myDaoFactory.shutdown();
110         }
111     }
112 
113     //------------------------------------------------------------------------
114     // members
115     //------------------------------------------------------------------------
116     private static final String LINE_BREAK = System.getProperty(
117         "line.separator");    
118 }