1   // 
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //	 this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //	 this list of conditions and the following disclaimer in the documentation  
12  //	 and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //	 nor the names of its contributors may be used to endorse or promote products  
15  //	 derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  // 
28  package org.objectledge.database;
29  
30  import java.io.FileInputStream;
31  
32  import javax.sql.DataSource;
33  
34  import org.dbunit.DatabaseTestCase;
35  import org.dbunit.database.DatabaseDataSourceConnection;
36  import org.dbunit.database.IDatabaseConnection;
37  import org.dbunit.dataset.IDataSet;
38  import org.dbunit.dataset.xml.XmlDataSet;
39  import org.jcontainer.dna.impl.DefaultConfiguration;
40  import org.objectledge.filesystem.FileSystem;
41  
42  /***
43   * 
44   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
45   * @version $Id: IdGeneratorTest.java,v 1.8 2005/02/16 20:00:46 rafal Exp $
46   */
47  public class IdGeneratorTest extends DatabaseTestCase
48  {
49      private DataSource dataSource;
50      
51      /***
52       * Constructor for IdGeneratorTest.
53       * @param arg0
54       */
55      public IdGeneratorTest(String arg0)
56          throws Exception
57      {
58          super(arg0);
59          dataSource = getDataSource();
60      }
61      
62      public void testNextId()
63          throws Exception
64      {
65          IdGenerator idGenerator = new IdGenerator(dataSource);
66          assertEquals(1, idGenerator.getNextId("test_table_one"));
67          assertEquals(2, idGenerator.getNextId("test_table_one"));
68          assertEquals(0, idGenerator.getNextId("test_table_two"));
69          assertEquals(1, idGenerator.getNextId("test_table_two"));
70          idGenerator.stop();
71      }
72      
73      /////////////////////////////////////////////////////////////////////////////////////////////
74  
75      protected IDatabaseConnection getConnection() throws Exception
76      {
77          return new DatabaseDataSourceConnection(dataSource);
78      }
79      
80      protected IDataSet getDataSet() throws Exception
81      {
82          return new XmlDataSet(new FileInputStream("src/test/resources/database/IdGenerator.xml"));
83      }        
84      
85      private DataSource getDataSource()
86          throws Exception
87      {
88          DefaultConfiguration conf = new DefaultConfiguration("config","","/");
89          DefaultConfiguration url = new DefaultConfiguration("url","","/config");
90          url.setValue("jdbc:hsqldb:."); 
91          conf.addChild(url);    
92          DefaultConfiguration user = new DefaultConfiguration("user","","/config");
93          user.setValue("sa");
94          conf.addChild(user);
95          DataSource ds = new HsqldbDataSource(conf);    
96          if(!DatabaseUtils.hasTable(ds, "ledge_id_table"))
97          {
98              FileSystem fs = FileSystem.getStandardFileSystem(".");
99              DatabaseUtils.runScript(ds, fs.getReader("sql/database/IdGeneratorTables.sql", 
100                 "UTF-8"));
101         }
102         return ds;
103     }
104 }