Source Code Cross Referenced for AbstractCoreTestCase.java in  » Content-Management-System » contineo » org » contineo » core » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » contineo » org.contineo.core 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.