Java Code Examples for org.wso2.carbon.utils.dbcreator.DatabaseCreator

The following examples show how to use org.wso2.carbon.utils.dbcreator.DatabaseCreator. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: carbon-apimgt   Source File: LocalDatabaseCreator.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates registry database if the script exists; returns otherwise.
 *
 * @throws Exception
 */
public void createRegistryDatabase() throws Exception{

    String databaseType = DatabaseCreator.getDatabaseType(this.dataSource.getConnection());

    String scripPath = getDbScriptLocation(databaseType);
    File scripFile = new File(scripPath);
    if(scripFile.exists()){
        super.createRegistryDatabase();
    }
}
 
Example 2
@Before
public void setup() throws Exception {
    System.setProperty(CARBON_HOME, "");
    localDatabaseCreator = new LocalDatabaseCreator(Mockito.mock(DataSource.class));
    PowerMockito.mockStatic(DatabaseCreator.class);
    PowerMockito.when(DatabaseCreator.getDatabaseType(Matchers.any(Connection.class))).thenReturn("mysql");
    PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(file);

}
 
Example 3
@Test
public void testCreateRegistryDatabaseWhenFileExists() throws Exception {

    Mockito.when(file.exists()).thenReturn(true);
    Method method = PowerMockito.method(DatabaseCreator.class, "createRegistryDatabase");
    PowerMockito.suppress(method);
    localDatabaseCreator.createRegistryDatabase();
    Mockito.verify(file, Mockito.times(1)).exists();
}
 
Example 4
@Test
public void testCreateRegistryDatabaseWhenFileDoesNotExist() throws Exception {

    Mockito.when(file.exists()).thenReturn(false);
    Method method = PowerMockito.method(DatabaseCreator.class, "createRegistryDatabase");
    PowerMockito.suppress(method);
    localDatabaseCreator.createRegistryDatabase();
    Mockito.verify(file, Mockito.times(1)).exists();

}
 
Example 5
Source Project: product-es   Source File: MigrationDatabaseCreator.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * executes content in SQL script
 *
 * @throws IOException,SQLException
 */
private void executeSQLScript(String dbscriptName) throws IOException, SQLException {

    String delimiter = ";";
    boolean keepFormat = false;
    StringBuffer sql = new StringBuffer();
    try (InputStream is = getClass().getResourceAsStream(dbscriptName);
         InputStreamReader inputStreamReader = new InputStreamReader(is); BufferedReader reader = new BufferedReader(inputStreamReader
    )) {

        String line;
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            if (!keepFormat) {
                if (line.startsWith("//")) {
                    continue;
                }
                if (line.startsWith("--")) {
                    continue;
                }
                StringTokenizer st = new StringTokenizer(line);
                if (st.hasMoreTokens()) {
                    String token = st.nextToken();
                    if ("REM".equalsIgnoreCase(token)) {
                        continue;
                    }
                }
            }
            sql.append(keepFormat ? "\n" : " ").append(line);
            if (!keepFormat && line.indexOf("--") >= 0) {
                sql.append("\n");
            }
            if ((DatabaseCreator.checkStringBufferEndsWith(sql, delimiter))) {
                executeSQL(sql.substring(0, sql.length() - delimiter.length()));
                sql.replace(0, sql.length(), "");
            }
        }
        if (sql.length() > 0) {
            executeSQL(sql.toString());
        }

    }

}