?? opencmstestcase.java
字號:
// log in the Admin user and switch to the setup project
cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserGuest());
cms.loginUser("Admin", "admin");
cms.getRequestContext().setCurrentProject(cms.readProject("_setupProject"));
if (importFolder != null) {
// import the "simpletest" files
importResources(cms, importFolder, targetFolder);
}
// create the default projects by script
script = new File(getTestDataPath("scripts/script_default_projects.txt"));
stream = new FileInputStream(script);
m_shell.start(stream);
if (publish) {
// publish the current project by script
script = new File(getTestDataPath("scripts/script_publish.txt"));
stream = new FileInputStream(script);
m_shell.start(stream);
} else {
cms.unlockProject(cms.readProject("_setupProject").getId());
}
// switch to the "Offline" project
cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
cms.getRequestContext().setSiteRoot("/sites/default/");
// output a message
System.out.println("----- Starting test cases -----");
} catch (Throwable t) {
t.printStackTrace(System.err);
fail("Unable to setup OpenCms\n" + CmsException.getStackTraceAsString(t));
}
// turn on exceptions after error logging
OpenCmsTestLogAppender.setBreakOnError(true);
// return the initialized cms context Object
return cms;
}
/**
* Adds an additional path to the list of test data configuration files.<p>
*
* @param dataPath the path to add
*/
protected static synchronized void addTestDataPath(String dataPath) {
// check if the db data folder is available
File testDataFolder = new File(dataPath);
if (!testDataFolder.exists()) {
fail("DB setup data not available at " + testDataFolder.getAbsolutePath());
}
m_testDataPath.add(CmsFileUtil.normalizePath(testDataFolder.getAbsolutePath() + File.separator));
}
/**
* Check the setup DB for errors that might have occured.<p>
*
* @param setupDb the setup DB object to check
*/
protected static void checkErrors(CmsSetupDb setupDb) {
if (!setupDb.noErrors()) {
Vector errors = setupDb.getErrors();
for (Iterator i = errors.iterator(); i.hasNext();) {
String error = (String)i.next();
System.out.println(error);
}
fail((String)setupDb.getErrors().get(0));
}
}
/**
* Returns an initialized replacer map.<p>
*
* @param connectionData the connection data to derive the replacer information
*
* @return an initialized replacer map
*/
protected static Map getReplacer(ConnectionData connectionData) {
Map replacer = new HashMap();
replacer.put("${database}", connectionData.m_dbName);
replacer.put("${user}", connectionData.m_userName);
replacer.put("${password}", connectionData.m_userPassword);
replacer.put("${defaultTablespace}", m_defaultTablespace);
replacer.put("${indexTablespace}", m_indexTablespace);
replacer.put("${temporaryTablespace}", m_tempTablespace);
return replacer;
}
/**
* Returns the path to the data files used by the setup wizard.<p>
*
* Whenever possible use this path to ensure that the files
* used for testing are actually the same as for the setup.<p>
*
* @return the path to the data files used by the setup wizard
*/
protected static synchronized String getSetupDataPath() {
if (m_setupDataPath == null) {
// check if the db setup files are available
File setupDataFolder = new File(OpenCmsTestProperties.getInstance().getTestWebappPath());
if (!setupDataFolder.exists()) {
fail("DB setup data not available at " + setupDataFolder.getAbsolutePath());
}
m_setupDataPath = setupDataFolder.getAbsolutePath() + File.separator;
}
// return the path name
return m_setupDataPath;
}
/**
* Returns an initialized DB setup object.<p>
*
* @param connection the connection data
*
* @return the initialized setup DB object
*/
protected static CmsSetupDb getSetupDb(ConnectionData connection) {
// create setup DB instance
CmsSetupDb setupDb = new CmsSetupDb(getSetupDataPath());
// connect to the DB
setupDb.setConnection(
connection.m_jdbcDriver,
connection.m_jdbcUrl,
connection.m_jdbcUrlParams,
connection.m_userName,
connection.m_userPassword);
// check for errors
if (!DB_ORACLE.equals(m_dbProduct)) {
checkErrors(setupDb);
}
return setupDb;
}
/**
* Returns the path to a file in the test data configuration,
* or <code>null</code> if the given file can not be found.<p>
*
* This methods searches the given file in all configured test data paths.
* It returns the file found first.<p>
*
* @param filename the file name to look up
* @return the path to a file in the test data configuration
*/
protected static String getTestDataPath(String filename) {
for (int i = 0; i < m_testDataPath.size(); i++) {
String path = (String)m_testDataPath.get(i);
File file = new File(path + filename);
if (file.exists()) {
if (file.isDirectory()) {
return file.getAbsolutePath() + File.separator;
} else {
return file.getAbsolutePath();
}
}
}
return null;
}
/**
* Imports a resource into the Cms.<p>
*
* @param cms an initialized CmsObject
* @param importFile the name (absolute Path) of the import resource (zip or folder)
* @param targetPath the name (absolute Path) of the target folder in the VFS
* @throws CmsException if something goes wrong
*/
protected static void importResources(CmsObject cms, String importFile, String targetPath) throws CmsException {
OpenCms.getImportExportManager().importData(
cms,
getTestDataPath(File.separator + "imports" + File.separator + importFile),
targetPath,
new CmsShellReport(cms.getRequestContext().getLocale()));
}
/**
* Imports a resource from the RFS test directories to the VFS.<p>
*
* The imported resource will be automatically unlocked.<p>
*
* @param cms the current users OpenCms context
* @param rfsPath the RTF path of the resource to import, must be a path accessibly by the current class loader
* @param vfsPath the VFS path for the imported resource
* @param type the type for the imported resource
* @param properties the properties for the imported resource
* @return the imported resource
*
* @throws Exception if the import fails
*/
protected static CmsResource importTestResource(
CmsObject cms,
String rfsPath,
String vfsPath,
int type,
List properties) throws Exception {
byte[] content = CmsFileUtil.readFile(rfsPath);
CmsResource result = cms.createResource(vfsPath, type, content, properties);
cms.unlockResource(vfsPath);
return result;
}
/**
* Removes the OpenCms database test instance.<p>
*/
protected static void removeDatabase() {
if (m_defaultConnection != null) {
removeDatabase(m_setupConnection, m_defaultConnection, false);
}
if (m_additionalConnection != null) {
removeDatabase(m_setupConnection, m_additionalConnection, false);
}
}
/**
* Removes the OpenCms database test instance.<p>
*
* @param setupConnection the setup connection
* @param defaultConnection the default connection
* @param handleErrors flag to indicate if errors should be handled/checked
*/
protected static void removeDatabase(
ConnectionData setupConnection,
ConnectionData defaultConnection,
boolean handleErrors) {
CmsSetupDb setupDb = null;
boolean noErrors = true;
try {
setupDb = getSetupDb(defaultConnection);
setupDb.dropTables(m_dbProduct, getReplacer(defaultConnection), handleErrors);
noErrors = setupDb.noErrors();
} catch (Exception e) {
noErrors = false;
} finally {
if (setupDb != null) {
setupDb.closeConnection();
}
}
if (!handleErrors || noErrors) {
try {
setupDb = getSetupDb(setupConnection);
setupDb.dropDatabase(m_dbProduct, getReplacer(defaultConnection), handleErrors);
setupDb.closeConnection();
} catch (Exception e) {
noErrors = false;
} finally {
if (setupDb != null) {
setupDb.closeConnection();
}
}
}
if (handleErrors) {
checkErrors(setupDb);
}
}
/**
* Creates a new OpenCms test database including the tables.<p>
*
* Any existing instance of the test database is forcefully removed first.<p>
*/
protected static void setupDatabase() {
if (m_defaultConnection != null) {
setupDatabase(m_setupConnection, m_defaultConnection, true);
}
if (m_additionalConnection != null) {
setupDatabase(m_setupConnection, m_additionalConnection, true);
}
}
/**
* Creates a new OpenCms test database including the tables.<p>
*
* @param setupConnection the setup connection
* @param defaultConnection the default connection
* @param handleErrors flag to indicate if errors should be handled/checked
*/
protected static void setupDatabase(
ConnectionData setupConnection,
ConnectionData defaultConnection,
boolean handleErrors) {
CmsSetupDb setupDb = null;
boolean noErrors = true;
try {
setupDb = getSetupDb(setupConnection);
setupDb.createDatabase(m_dbProduct, getReplacer(defaultConnection), handleErrors);
noErrors = setupDb.noErrors();
setupDb.closeConnection();
} catch (Exception e) {
noErrors = false;
} finally {
if (setupDb != null) {
setupDb.closeConnection();
}
}
if (!handleErrors || noErrors) {
try {
setupDb = getSetupDb(defaultConnection);
setupDb.createTables(m_dbProduct, getReplacer(defaultConnection), handleErrors);
noErrors = setupDb.noErrors();
setupDb.closeConnection();
} catch (Exception e) {
noErrors = false;
} finally {
if (setupDb != null) {
setupDb.closeConnection();
}
}
}
if (noErrors) {
return;
} else if (handleErrors) {
removeDatabase(setupConnection, defaultConnection, false);
setupDatabase(setupConnection, defaultConnection, false);
} else {
checkErrors(setupDb);
}
}
/**
* Compares two lists of CmsProperty objects and creates a list of all properties which are
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -