1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.services.registry;
18  
19  // Junit imports
20  import junit.framework.Test;
21  import junit.framework.TestSuite;
22  
23  // Jetspeed imports
24  import org.apache.jetspeed.test.JetspeedTestCase;
25  import org.apache.jetspeed.om.registry.base.*;
26  
27  import ojb.broker.PersistenceBroker;
28  import ojb.broker.query.Query;
29  import ojb.broker.query.QueryByCriteria;
30  import ojb.broker.query.Criteria;
31  
32  import org.apache.turbine.util.TurbineConfig;
33  import org.apache.turbine.util.StringUtils;
34  
35  import org.apache.jetspeed.services.JetspeedDatabase;
36  
37  /***
38   * TestRegistryPersistence
39   *
40   * @author <a href="taylor@apache.org">David Sean Taylor</a>
41   * @version $Id: TestRegistryPersistence.java,v 1.1 2004/04/07 22:02:42 jford Exp $
42   */
43  
44  public class TestRegistryPersistence extends JetspeedTestCase {    
45  
46      /***
47       * Defines the testcase name for JUnit.
48       *
49       * @param name the testcase's name.
50       */
51      public TestRegistryPersistence( String name ) {
52          super( name );
53      }
54      
55      /***
56       * Start the tests.
57       *
58       * @param args the arguments. Not used
59       */
60      public static void main(String args[]) {
61          junit.awtui.TestRunner.main( new String[] { TestRegistryPersistence.class.getName() } );
62      }
63   
64      public void setup() {
65          System.out.println("Setup: Testing ObjectBridge");
66       }
67      /***
68       * Creates the test suite.
69       *
70       * @return a test suite (<code>TestSuite</code>) that includes all methods
71       *         starting with "test"
72       */
73      public static Test suite() {
74          // All methods starting with "test" will be executed in the test suite.
75          return new TestSuite( TestRegistryPersistence.class );
76      }
77  
78      /***
79       * 
80       *
81       * @throws Exception
82       */
83  
84      public void testInsertUpdateDelete() throws Exception 
85      {
86          PersistenceBroker pm = null;
87          try
88          {
89              pm = (PersistenceBroker) JetspeedDatabase.getPersistenceManager();
90  
91              MediaTypeTest(pm);
92              SkinTest(pm);  
93          }
94          catch (Throwable t)
95          {
96              t.printStackTrace();
97          }
98      }
99  
100     void MediaTypeTest(PersistenceBroker pm) throws Exception
101     {
102         // test insert
103         BaseMediaTypeEntry newEntry = new BaseMediaTypeEntry(0, "XHTML9", 0, "text.xhtml.9", 
104                                                              "Some Title", "Some Description", "Some Image", "SU");
105 
106         pm.beginTransaction();
107         pm.store(newEntry);
108         pm.commitTransaction();
109 
110         // test update
111         Criteria criteria = new Criteria();
112         criteria.addEqualTo("name", "XHTML9");
113         Query query = new QueryByCriteria(BaseMediaTypeEntry.class, criteria);
114         BaseMediaTypeEntry entry  = (BaseMediaTypeEntry)pm.getObjectByQuery(query);
115         assertTrue(entry.getName().equals("XHTML9"));           
116         System.out.println("id = " + entry.getId());
117         assertTrue(entry.getHidden() == false);           
118         assertTrue(entry.getMimeType().equals("text.xhtml.9"));           
119         assertTrue(entry.getMetaInfo().getTitle().equals("Some Title"));           
120         assertTrue(entry.getDescription().equals("Some Description"));           
121         assertTrue(entry.getMetaInfo().getImage().equals("Some Image"));           
122         assertTrue(entry.getSecurity().getRole().equals("SU"));                     
123 
124         entry.setHidden(true);
125         entry.setMimeType("text.xhtml.9.1");
126         entry.getMetaInfo().setTitle("New Title");
127         entry.setDescription("New Description");
128         entry.getSecurity().setRole("Dumb");
129 
130         pm.beginTransaction();
131         pm.store(entry);
132         pm.commitTransaction();
133 
134         // delete
135         pm.beginTransaction();
136         BaseMediaTypeEntry entry2  = (BaseMediaTypeEntry)pm.getObjectByQuery(query);
137         assertTrue(entry2.getHidden() == true);           
138         assertTrue(entry2.getMimeType().equals("text.xhtml.9.1"));           
139         assertTrue(entry2.getMetaInfo().getTitle().equals("New Title"));           
140         assertTrue(entry2.getMetaInfo().getDescription().equals("New Description"));           
141         assertTrue(entry2.getSecurity().getRole().equals("Dumb"));                     
142 
143         pm.delete(entry2);
144         pm.commitTransaction();
145 
146     }
147 
148     void SkinTest(PersistenceBroker pm) throws Exception
149     {
150         // DST: left off here
151     }
152 
153    /*
154       Configuration object to run Turbine outside a servlet container
155       ( uses turbine.properties )
156     */
157     private static TurbineConfig config = null;
158     
159     /*    
160       Sets up TurbineConfig using the system property:
161       <pre>turbine.properties</pre>
162     */
163     static
164     {
165         try
166         {
167            config = new TurbineConfig( "webapp", "/WEB-INF/conf/TurbineResources.properties");
168            config.init();
169         }
170         catch (Exception e)
171         {
172             //fail(StringUtils.stackTrace(e));
173             System.out.println(StringUtils.stackTrace(e));
174         }
175     }
176 }