View Javadoc

1   /***
2    * Copyright 2008 ATG DUST Project
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    * 
7    * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software 
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and limitations under the License.
13   */
14  package test;
15  
16  import java.io.File;
17  import java.util.Arrays;
18  import java.util.Properties;
19  
20  import org.apache.log4j.Logger;
21  
22  import atg.adapter.gsa.GSARepository;
23  import atg.adapter.gsa.GSATest;
24  import atg.adapter.gsa.GSATestUtils;
25  import atg.dtm.TransactionDemarcation;
26  import atg.nucleus.Nucleus;
27  import atg.repository.MutableRepository;
28  import atg.repository.MutableRepositoryItem;
29  import atg.repository.RepositoryItem;
30  import atg.test.util.DBUtils;
31  import atg.test.util.FileUtil;
32  
33  /***
34   * This test starts a repository, adds an item to that repository, then shuts
35   * down. The repository is started up against an in-memory Hypersonic Database.
36   * <br/><br/>Based on {@link GSATest}
37   */
38  public class SimpleRepositoryTest extends GSATest {
39  
40    private static Logger log = Logger.getLogger(SimpleRepositoryTest.class);
41  
42    public void testSimple() throws Exception {
43  
44      // setup the repository
45      File configpath = new File("target/test-classes/config".replace("/",
46          File.separator));
47  
48      // Define the path to our repository definition file called
49      // "simpleRepository.xml"
50      final String[] definitionFiles = { "/test/simpleRepository.xml" };
51      log.info(" definitionFile[0]=" + definitionFiles[0]);
52  
53      // Copy all related properties and definition files to the previously
54      // configured configpath
55      FileUtil.copyDirectory("src/test/resources/config", configpath.getPath(), Arrays
56          .asList(new String[] { ".svn" }));
57  
58      // Use the DBUtils utility class to get JDBC properties for an in memory
59      // HSQL DB called "testdb".
60      Properties props = DBUtils.getHSQLDBInMemoryDBConnection("testdb");
61  
62      // Start up our database
63      DBUtils db = initDB(props);
64  
65      boolean rollback = true;
66  
67      // Setup our testing configpath
68      // RH: disabled logging (last argument to false) to get rid of the double
69      // logging statements
70      GSATestUtils.getGSATestUtils().initializeMinimalConfigpath(configpath,
71          "/SimpleRepository", definitionFiles, props, null, null, null, false);
72  
73      // Start Nucleus
74      Nucleus n = startNucleus(configpath);
75  
76      TransactionDemarcation td = new TransactionDemarcation();
77      MutableRepository r = (MutableRepository) n
78          .resolveName("/SimpleRepository");
79  
80      try {
81        // Start a new transaction
82        td.begin(((GSARepository) r).getTransactionManager());
83        // Create the item
84        MutableRepositoryItem item = r.createItem("simpleItem");
85        item.setPropertyValue("name", "simpleName");
86        // Persist to the repository
87        r.addItem(item);
88        // Try to get it back from the repository
89        String id = item.getRepositoryId();
90        RepositoryItem item2 = r.getItem(id, "simpleItem");
91        assertNotNull(
92            " We did not get back the item just created from the repository.",
93            item2);
94        rollback = false;
95      }
96      finally {
97        // End the transaction, rollback on error
98        if (td != null)
99          td.end(rollback);
100       // shut down Nucleus
101       n.stopService();
102       // Shut down HSQLDB
103       db.shutdown();
104     }
105   }
106 }