Have been experimental with various Java technologies (At this time, JDBC, reading from properties files and log4j) and tying it all together with JRuby – Which I might add runs also in compiled mode as well.
Simple use of properties files in JRuby involves
# Initialize the properties object from java.util
# Refer to http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html
# on how to use.
properties = java.util::Properties.new
# Make sure that propertyfile exists!
properties.load(java.io.FileInputStream.new("propertyfile.properties"))
puts properties.getProperty("propertykey")
Now for some jdbc action! (The below code is well commented.. Enjoy!)
require 'java' # So we can use the sexy java goodness!
# Make sure you download this from the mysql site! (Link: http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-3.1.10.zip/from/pick)
require 'mysql-connector-java-3.1.10-bin.jar'
import 'com.mysql.jdbc.Driver' # Load the JDBC Driver
# Replace 'conn_string' with the JDBC Connection string (e.g. jdbc:mysql://localhost:3306/DBNAME)
# Replace 'uid' with the database username
# Replace 'pwd' with the database password
conn = java.sql::DriverManager.getConnection(conn_string, uid, pwd);
# Prepared queries are teh win
stmt = conn.prepareStatement("select field from table where otherfield = ?")
stmt.setString(1, "Whatwearesearchingfor") # First param
stmt.executeQuery
while rs.next do
puts rs.getString("field")
end
stmt.close # Because we should close what we open
conn.close # Same as above
And finally, some log4j thrown in!
require 'log4j-1.2.15.jar' # Assume you have already done require java
# The below is how you use log4j controlled by XML
import org.apache.log4j.Logger
import org.apache.log4j.xml.DOMConfigurator
logger = Logger.getLogger("LoggerName") # LoggerName would be defined in the XML file
DOMConfigurator.configure("log4j.xml")
logger.info "This would log an INFO entry if it were configured to be shown"
# Additionally you can use debug, info, warn, error, fatal
The results have been to a good degree of success. All I will need now to create a kickass and open source web frontend testing framework is:
- Celerity
- log4j
- JDBC
- properties files
I’ve basically got 90% of the functionality of a commercial tool right here! Schweet!