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  
29  package org.objectledge.database.persistence;
30  
31  import java.io.Reader;
32  import java.util.Date;
33  import java.util.List;
34  
35  import javax.sql.DataSource;
36  
37  import junit.framework.TestCase;
38  
39  import org.jcontainer.dna.Logger;
40  import org.jcontainer.dna.impl.DefaultConfiguration;
41  import org.jcontainer.dna.impl.Log4JLogger;
42  import org.objectledge.context.Context;
43  import org.objectledge.database.Database;
44  import org.objectledge.database.DatabaseUtils;
45  import org.objectledge.database.DefaultDatabase;
46  import org.objectledge.database.HsqldbDataSource;
47  import org.objectledge.database.IdGenerator;
48  import org.objectledge.database.JotmTransaction;
49  import org.objectledge.filesystem.FileSystem;
50  
51  /**
52   * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a>
53   *
54   */
55  public class PersistenceTest extends TestCase
56  {
57      private DataSource dataSource;
58  
59      private Persistence persistence;
60  
61      /***
62       * Constructor for PersistenceTest.
63       * @param arg0
64       */
65      public PersistenceTest(String arg0) throws Exception
66      {
67          super(arg0);
68          dataSource = getDataSource();
69          IdGenerator idGenerator = new IdGenerator(dataSource);
70          Logger logger = new Log4JLogger(org.apache.log4j.Logger.getLogger(getClass()));
71          JotmTransaction transaction = new JotmTransaction(0, 120, new Context(), logger, null);
72          Database database = new DefaultDatabase(dataSource, idGenerator, transaction);
73          persistence = new DefaultPersistence(database, logger);
74      }
75  
76      /*
77          public void testPersistence()
78          {
79              
80          }
81      */
82      /*
83       * Test for Persistent load(long, PersistentFactory)
84       */
85      public void testLoadlongPersistentFactory() throws Exception
86      {
87          TestObject object = (TestObject)persistence.load(1, testFactory);
88          assertNull(object);
89          object = (TestObject)persistence.load(0, testFactory);
90          assertNull(object);
91          List list = persistence.load(null, testFactory);
92          assertEquals(list.size(), 0);
93          object = new TestObject("foo", new Date());
94          persistence.save(object);
95          object = (TestObject)persistence.load(object.getId(), testFactory);
96          assertNotNull(object);
97          list = persistence.load(null, testFactory);
98          assertEquals(list.size(), 1);
99          list = persistence.load("id = " + object.getId(), testFactory);
100         assertEquals(list.size(), 1);
101         list = persistence.load("id = -1", testFactory);
102         assertEquals(list.size(), 0);
103         list = persistence.load("value = 'bar'", testFactory);
104         assertEquals(list.size(), 0);
105         list = persistence.load("value = 'foo'", testFactory);
106         assertEquals(list.size(), 1);
107         object.setValue("bar");
108         persistence.save(object);
109         list = persistence.load("value = 'foo'", testFactory);
110         assertEquals(list.size(), 0);
111         list = persistence.load("value = 'bar'", testFactory);
112         assertEquals(list.size(), 1);
113         object.setValue("foo");
114         assertEquals(object.getValue(), "foo");
115         persistence.revert(object);
116         assertEquals(object.getValue(), "bar");
117 
118         TestObject object2 = new TestObject("foo", null);
119         try
120         {
121             persistence.revert(object2);
122             fail("should throw the exception");
123         }
124         catch (IllegalStateException e)
125         {
126             //ok!            
127         }
128         object2.setSaved(10);
129         try
130         {
131             persistence.revert(object2);
132             fail("should throw the exception");
133         }
134         catch (PersistenceException e)
135         {
136             //ok!            
137         }
138         object2.setSaved(-1);
139         
140         assertEquals(persistence.exists("test_object",null),true);
141         assertEquals(persistence.exists("test_object","id = 10"),false);
142         assertEquals(persistence.count("test_object",null),1);
143         
144         assertEquals(list.size(),1);
145         persistence.delete(object);
146         list = persistence.load(null, testFactory);
147         assertEquals(list.size(),0);
148         
149         assertEquals(persistence.exists("test_object",null),false);
150         assertEquals(persistence.exists("test_object","id = 10"),false);
151         assertEquals(persistence.count("test_object",null),0);
152         assertEquals(persistence.count("test_object","id = 10"),0);
153         
154         
155     }
156 
157     /*
158      * Test for List load(String, PersistentFactory)
159      *
160      */
161     /*
162     public void testLoadStringPersistentFactory()
163     {
164     }
165     
166     public void testSave()
167     {
168     }
169     
170     public void testRevert()
171     {
172     }
173     
174     public void testDelete()
175     {
176     }
177     
178     public void testExists()
179     {
180     }
181     
182     public void testCount()
183     {
184     }
185     */
186 
187     /////////////////////////////////////////////////////////////////////////////////////////////
188 
189     private DataSource getDataSource() throws Exception
190     {
191         DefaultConfiguration conf = new DefaultConfiguration("config", "", "/");
192         DefaultConfiguration url = new DefaultConfiguration("url", "", "/config");
193         url.setValue("jdbc:hsqldb:.");
194         conf.addChild(url);
195         DefaultConfiguration user = new DefaultConfiguration("user", "", "/config");
196         user.setValue("sa");
197         conf.addChild(user);
198         DataSource ds = new HsqldbDataSource(conf);
199         FileSystem fs = FileSystem.getStandardFileSystem(".");
200         Reader reader;
201         if(!DatabaseUtils.hasTable(ds, "ledge_id_table"))
202         {
203             reader =  fs.getReader("sql/database/IdGeneratorTables.sql", "UTF-8");
204             DatabaseUtils.runScript(ds, reader);
205         }
206         if(!DatabaseUtils.hasTable(ds, "test_object"))
207         {
208             reader = fs.getReader("sql/database/persistence/TestObject.sql", "UTF-8");
209             DatabaseUtils.runScript(ds, reader);
210         }
211         return ds;
212     }
213 
214     private PersistentFactory testFactory = new PersistentFactory()
215     {
216         public Persistent newInstance()
217         {
218             return new TestObject();
219         }
220     };
221 
222 }