001: package org.contineo.core;
002:
003: import java.io.BufferedInputStream;
004: import java.io.BufferedOutputStream;
005: import java.io.File;
006: import java.io.FileOutputStream;
007: import java.io.IOException;
008: import java.io.InputStream;
009: import java.io.OutputStream;
010: import java.sql.Connection;
011: import java.sql.ResultSet;
012:
013: import javax.sql.DataSource;
014:
015: import junit.framework.TestCase;
016:
017: import org.apache.commons.io.FileUtils;
018: import org.hsqldb.util.SqlFile;
019: import org.hsqldb.util.SqlTool.SqlToolException;
020: import org.springframework.context.ApplicationContext;
021: import org.springframework.context.support.AbstractApplicationContext;
022: import org.springframework.context.support.ClassPathXmlApplicationContext;
023:
024: /**
025: * Abstract test case for the Core module. This class initialises a test
026: * database and prepares the spring test context.
027: * <p>
028: * All Contineo's tests must extend this test case in order to find a ready and
029: * accessible database.
030: *
031: * @author Marco Meschieri
032: * @version $Id:$
033: * @since 3.0
034: */
035: public abstract class AbstractCoreTestCase extends TestCase {
036:
037: protected ApplicationContext context;
038:
039: protected DataSource ds;
040:
041: protected File tempDir = new File("target/tmp");
042:
043: protected File dbSchemaFile;
044:
045: protected File dataFile;
046:
047: private String userHome;
048:
049: static {
050: System.setProperty("CONTINEO_HOME", "target");
051: }
052:
053: /**
054: * @see TestCase#setUp()
055: */
056: protected void setUp() throws Exception {
057: super .setUp();
058:
059: userHome = System.getProperty("user.home");
060: System.setProperty("user.home", tempDir.getPath());
061: context = new ClassPathXmlApplicationContext(
062: new String[] { "/context.xml" });
063: createTestDirs();
064: createTestDatabase();
065: }
066:
067: protected void createTestDirs() throws IOException {
068: // Create test dirs
069: try {
070: if (tempDir.exists() && tempDir.isDirectory())
071: FileUtils.deleteDirectory(tempDir);
072: } catch (Exception e) {
073: }
074: tempDir.mkdirs();
075: assertTrue(tempDir.exists() && tempDir.isDirectory());
076:
077: dbSchemaFile = new File(tempDir, "contineo-core.sql");
078: dataFile = new File(tempDir, "data.sql");
079:
080: // Copy sql files
081: copyResource("/org/contineo/core/contineo-core.sql",
082: dbSchemaFile.getCanonicalPath());
083: copyResource("/data.sql", dataFile.getCanonicalPath());
084: }
085:
086: protected void copyResource(String classpath, String destinationPath)
087: throws IOException {
088: copyResource(classpath, new File(destinationPath));
089: }
090:
091: /**
092: * Copy a resource from the classpath into a file
093: *
094: * @param classpath The classpath specification
095: * @param out The target file
096: * @throws IOException
097: */
098: protected void copyResource(String classpath, File out)
099: throws IOException {
100: InputStream is = new BufferedInputStream(this .getClass()
101: .getResource(classpath).openStream());
102: OutputStream os = new BufferedOutputStream(
103: new FileOutputStream(out));
104: try {
105: for (;;) {
106: int b = is.read();
107: if (b == -1)
108: break;
109: os.write(b);
110: }
111: } finally {
112: is.close();
113: os.close();
114: }
115: }
116:
117: /**
118: * @see TestCase#tearDown()
119: */
120: protected void tearDown() throws Exception {
121: super .tearDown();
122:
123: destroyDatabase();
124: ((AbstractApplicationContext) context).close();
125:
126: // Restore user home system property
127: System.setProperty("user.home", userHome);
128: }
129:
130: /**
131: * Destroys the in-memory database
132: */
133: private void destroyDatabase() {
134: Connection con = null;
135: try {
136: con = ds.getConnection();
137: con.createStatement().execute("SHUTDOWN IMMEDIATELY");
138: } catch (Exception e) {
139: e.printStackTrace();
140: } finally {
141: try {
142: if (con != null)
143: con.close();
144: } catch (Exception ex) {
145: ex.printStackTrace();
146: }
147: }
148: }
149:
150: /**
151: * Constructor for AbstractCoreTest.
152: *
153: * @param name
154: */
155: public AbstractCoreTestCase(String name) {
156: super (name);
157: }
158:
159: /**
160: * Creates an in-memory test database
161: *
162: * @throws SqlToolException
163: *
164: */
165: private void createTestDatabase() throws Exception {
166: ds = (DataSource) context.getBean("DataSource");
167:
168: Connection con = null;
169: try {
170: con = ds.getConnection();
171:
172: // Load schema
173: SqlFile sqlFile = new SqlFile(dbSchemaFile, false, null);
174: sqlFile.execute(con, false);
175:
176: // Load data
177: sqlFile = new SqlFile(dataFile, false, null);
178: sqlFile.execute(con, false);
179:
180: // Test the connection
181: ResultSet rs = con.createStatement().executeQuery(
182: "select * from co_menus where co_menuid=1");
183: rs.next();
184:
185: assertEquals(1, rs.getInt(1));
186: } finally {
187: if (con != null)
188: con.close();
189: }
190: }
191: }
|