View Javadoc

1   /***
2    * Copyright 2009 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.io.FileInputStream;
18  import java.io.IOException;
19  import java.util.Properties;
20  
21  import org.apache.log4j.Logger;
22  
23  import atg.adapter.gsa.GSARepository;
24  import atg.dtm.TransactionDemarcation;
25  import atg.dtm.TransactionDemarcationException;
26  import atg.repository.MutableRepositoryItem;
27  import atg.repository.RepositoryException;
28  import atg.repository.RepositoryItem;
29  import atg.test.AtgDustCase;
30  
31  /***
32   * 
33   * Example test case to illustrate the usage of {@link AtgDustCase} built-in
34   * database functionalities using on the fly created db's (bases on hsql
35   * in-memory db) or against external existing databases.
36   * 
37   * <br/><br/>Based on {@link AtgDustCase}
38   * 
39   * 
40   * @author robert
41   * 
42   */
43  public class SongsRepositoryTest extends AtgDustCase {
44  
45  	@SuppressWarnings("unused")
46  	private static Logger log = Logger.getLogger(SongsRepositoryTest.class);
47  
48  	@Override
49  	public void setUp() throws Exception {
50  		super.setUp();
51  
52  		// make sure all needed files are at the configuration location.
53  		// "target/test-classes/config" is then promoted to the configuration
54  		// staging directory (that location is a maven controlled build location
55  		// and
56  		// therefore not part of the checked in source tree).
57  		copyConfigurationFiles(new String[] { "src/test/resources/config"
58  				.replace("/", File.separator) }, "target/test-classes/config"
59  				.replace("/", File.separator), ".svn");
60  
61  		// Eventually set this one to 'true' to get more debug logging in your
62  		// console from your nucleus based components.
63  		setDebug(false);
64  
65  	}
66  
67  	@Override
68  	public void tearDown() throws Exception {
69  		super.tearDown();
70  	}
71  
72  	/***
73  	 * Runs a test against an in-memory HSQL database
74  	 * 
75  	 * @throws Exception
76  	 */
77  	public void testWithInMemoryDb() throws Exception {
78  
79  		// The actual test is quite generic. The only difference is the way the
80  		// repository is prepared by the prepareRepositoryTest method
81  
82  		prepareRepository("/GettingStarted/SongsRepository",
83  				"/GettingStarted/songs.xml");
84  
85  		songsRepositoryTest();
86  	}
87  
88  	/***
89  	 * Example test with existing Database. This test is disabled by default
90  	 * (set to false/or not set in the env.properties) because the MySQL JDBC
91  	 * drivers (and the env.properties is configured to use mysql) are not
92  	 * included in the atg dust package.
93  	 * 
94  	 * To make use of this test, install a mysql-connector-java (mysql jdbc
95  	 * driver) into your .m2/repository, un-comment the mysql dependency in the
96  	 * pom.xml. Test data can be found in
97  	 * src/test/resources/config/GettingStarted/songs-data.xml.
98  	 * 
99  	 * 
100 	 * @throws Exception
101 	 */
102 	public void testWithExistingDb() throws Exception {
103 
104 		Properties properties = new Properties();
105 		properties
106 				.load(new FileInputStream("src/test/resources/env.properties"));
107 
108 		// a mechanism to disable/enable the repository test against an existing
109 		// database
110 		if (properties.getProperty("enabled") == null
111 				|| properties.getProperty("enabled").equalsIgnoreCase("false")) {
112 			return;
113 		}
114 
115 		// The actual test is quite generic. The only difference is the way the
116 		// repository is prepared by the prepareRepositoryTest method
117 
118 		prepareRepository("/GettingStarted/SongsRepository", properties, false,
119 				false, "/GettingStarted/songs.xml");
120 
121 		songsRepositoryTest();
122 	}
123 
124 	private void songsRepositoryTest() throws TransactionDemarcationException,
125 			RepositoryException, IOException {
126 		GSARepository songsRepository = (GSARepository) resolveNucleusComponent("/GettingStarted/SongsRepository");
127 		assertNotNull(songsRepository);
128 
129 		final TransactionDemarcation td = new TransactionDemarcation();
130 		assertNotNull(td);
131 
132 		try {
133 			// Start a new transaction
134 			td.begin(songsRepository.getTransactionManager());
135 			// Create a new artist
136 			MutableRepositoryItem artist = songsRepository.createItem("artist");
137 			artist.setPropertyValue("name", "joe");
138 			// Persist to the repository
139 			songsRepository.addItem(artist);
140 			// Try to get it back from the repository
141 			String id = artist.getRepositoryId();
142 			RepositoryItem retrievedArtist = songsRepository.getItem(id,
143 					"artist");
144 
145 			assertEquals(artist, retrievedArtist);
146 		} finally {
147 			// End the transaction, roll-back to restore original database state
148 			td.end(true);
149 		}
150 	}
151 
152 }