Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start Derby from Java so tests can run without Maven #121

Merged
merged 3 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Roller is made up of the following Maven projects:
* _roller-project_: Top level project
* _app_: Roller Weblogger webapp, JSP pages, Velocity templates
* _assembly-release_: Used to create official distributions of Roller
* _docs_: Roller documentation in ODT format
* _docs_: Roller documentation in ASCII Doc format
* _it-selenium_: Integrated browser tests for Roller using Selenium

## Documentation
Expand Down
5 changes: 3 additions & 2 deletions app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ limitations under the License.
<parent>
<groupId>org.apache.roller</groupId>
<artifactId>roller-project</artifactId>
<version>6.1.1</version>
<version>6.1.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -629,7 +629,7 @@ limitations under the License.
</dependencies>
</plugin>

<!-- Activates the Derby database for unit tests and mvn jetty:run -->
<!-- Activates the Derby database for unit tests and mvn jetty:run
<plugin>
<groupId>com.btmatthews.maven.plugins.inmemdb</groupId>
<artifactId>inmemdb-maven-plugin</artifactId>
Expand Down Expand Up @@ -667,6 +667,7 @@ limitations under the License.
</executions>

</plugin>
-->

<plugin>
<artifactId>maven-resources-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void runScript(
if (!con.getAutoCommit()) {
con.commit();
}

// on success, echo command to messages
successMessage(command);

Expand Down
9 changes: 4 additions & 5 deletions app/src/main/resources/sql/droptables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ drop table roller_mediafiletag;
drop table roller_mediafile;
drop table roller_mediafiledir;

-- oauth tables
drop table roller_oauthconsumer;
drop table roller_oauthaccessor;

-- core services tables
drop table roller_hitcounts;
Expand All @@ -52,10 +55,6 @@ drop table custom_template_rendition;

-- core platform tables
drop table roller_permission;
drop table weblog;
drop table userrole;
drop table roller_user;

-- oauth tables
drop table roller_oauthconsumer;
drop table roller_oauthaccessor;
drop table weblog;
120 changes: 120 additions & 0 deletions app/src/test/java/org/apache/roller/util/DerbyManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. The ASF licenses this file to You
* under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. For additional information regarding
* copyright in this work, please see the NOTICE file in the top level
* directory of this distribution.
*/
package org.apache.roller.util;

import java.io.File;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;

import org.apache.derby.drda.NetworkServerControl;
import org.apache.roller.weblogger.business.startup.SQLScriptRunner;


public class DerbyManager {
private final String databaseDir;
private final String databaseScriptsDir;
private final int port;

public DerbyManager(String databaseDir, String databaseScriptsDir, int port) {
this.databaseDir = databaseDir;
this.databaseScriptsDir = databaseScriptsDir;
this.port = port;
}

public void startDerby() throws Exception {
try {
System.out.println("==============");
System.out.println("Starting Derby");
System.out.println("==============");

System.setProperty("derby.system.home", databaseDir);

System.setProperty("derby.drda.portNumber", ""+port);
System.setProperty("derby.drda.host", "localhost");
System.setProperty("derby.drda.maxThreads","10");
//System.setProperty("derby.drda.logConnections","true");
NetworkServerControl server = new NetworkServerControl();
server.start(new PrintWriter(System.out));

try {Thread.sleep(2000);} catch (Exception ignored) {}
System.out.println("Runtime Info: " + server.getRuntimeInfo());

Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection conn = DriverManager.getConnection(
"jdbc:derby://localhost:" + port + "/rollerdb;create=true","APP", "APP");

// create roller tables
SQLScriptRunner runner1 = new SQLScriptRunner(
databaseScriptsDir + File.separator + "droptables.sql");
runner1.runScript(conn, false);
runner1.runScript(conn, false); // not sure why this is necessary

SQLScriptRunner runner = new SQLScriptRunner(
databaseScriptsDir + File.separator + "derby" + File.separator + "createdb.sql");
try {
runner.runScript(conn, true);
} catch (Exception ignored) {
for (String message : runner.getMessages()) {
System.out.println(message);
}
ignored.printStackTrace();
}

} catch (Exception e) {
e.printStackTrace();
throw new Exception("ERROR starting Derby");
}
}

public void stopDerby() throws Exception {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
String driverURL = "jdbc:derby://localhost:" + port + "/rollerdb";
Connection conn = DriverManager.getConnection(driverURL,"APP", "APP");

// drop Roller tables
SQLScriptRunner runner = new SQLScriptRunner(
databaseScriptsDir
+ File.separator + "droptables.sql");
runner.runScript(conn, false);

System.out.println("==============");
System.out.println("Stopping Derby");
System.out.println("==============");

try {
DriverManager.getConnection(driverURL + ";shutdown=true");
} catch (Exception ignored) {}

System.setProperty("derby.system.home", databaseDir);

// Network Derby
System.setProperty("derby.drda.portNumber", ""+port);
System.setProperty("derby.drda.host", "localhost");
System.setProperty("derby.drda.maxThreads","10");
//System.setProperty("derby.drda.logConnections","true");
NetworkServerControl server = new NetworkServerControl();
server.shutdown();

} catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.apache.roller.weblogger.pojos.WeblogEntryComment.ApprovalStatus;
import org.apache.roller.weblogger.pojos.WeblogHitCount;
import org.apache.roller.weblogger.pojos.WeblogPermission;
import org.apache.roller.util.DerbyManager;

/**
* Utility class for unit test classes.
Expand All @@ -66,9 +67,17 @@ public final class TestUtils {
// Username prefix we are using (simplifies local testing)
public static final String JUNIT_PREFIX = "junit_";

private static DerbyManager derbyManager = null;

public static void setupWeblogger() throws Exception {

if (!WebloggerFactory.isBootstrapped()) {
synchronized (TestUtils.class) {
if (derbyManager == null) {
derbyManager = new DerbyManager("./target/testdb", "./target/dbscripts", 4224);
derbyManager.startDerby();
}
}

// do core services preparation
WebloggerStartup.prepare();
Expand Down Expand Up @@ -105,6 +114,8 @@ public static void shutdownWeblogger() throws Exception {

// trigger shutdown
WebloggerFactory.getWeblogger().shutdown();

derbyManager.stopDerby();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/src/test/resources/roller-custom.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

database.configurationType=jdbc
database.jdbc.driverClass=org.apache.derby.jdbc.ClientDriver
database.jdbc.connectionURL=jdbc:derby://localhost:4224/memory:rollerdb
database.jdbc.connectionURL=jdbc:derby://localhost:4224/rollerdb
database.jdbc.username=APP
database.jdbc.password=APP

Expand Down
2 changes: 1 addition & 1 deletion assembly-release/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.roller</groupId>
<artifactId>roller-project</artifactId>
<version>6.1.1</version>
<version>6.1.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions assembly-release/sign-release.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

export rcstring="-rc1"
export vstring="6.1.1"
export rcstring=""
export vstring="6.1.2-SNAPSHOT"

# for rc releases we rename the release files
if [ rcstring != "" ]; then
Expand Down
2 changes: 1 addition & 1 deletion it-selenium/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>org.apache.roller</groupId>
<artifactId>roller-project</artifactId>
<version>6.1.1</version>
<version>6.1.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
13 changes: 2 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ limitations under the License.
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.roller</groupId>
<artifactId>roller-project</artifactId>
<version>6.1.1</version>
<version>6.1.2-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Roller</name>
Expand All @@ -45,7 +45,7 @@ limitations under the License.
<derby.version>10.11.1.1</derby.version>
<jaxb.version>2.3.1</jaxb.version>
<jetty.version>10.0.5</jetty.version>
<roller.version>6.1.1</roller.version>
<roller.version>6.1.2-SNAPSHOT</roller.version>
</properties>

<modules>
Expand Down Expand Up @@ -87,15 +87,6 @@ limitations under the License.
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
</plugin>
<plugin>
<groupId>com.btmatthews.maven.plugins.inmemdb</groupId>
<artifactId>inmemdb-maven-plugin</artifactId>
<version>1.4.3</version>
<configuration>
<monitorKey>inmemdb</monitorKey>
<monitorPort>11527</monitorPort>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
Expand Down