Tuesday, August 30, 2011

Hibernate Configuration for Derby DataBase

Derby is a pure Java relational database engine using standard SQL and JDBC as its APIs

Download latest release of derby database from  http://db.apache.org/derby/releases/

Download the zip version, Extract the zip file and set classpath to environment variables.

Table 1. Commands to run the startNetworkServer command
Operating System Command
set DERBY_HOME=C:\derby
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_24
UNIX (Korn Shell)
export DERBY_HOME=/opt/derby
export JAVA_HOME=/usr/j2se
export PATH="$DERBY_HOME/bin:$PATH"

we can find, more details in the following link

Use startNetworkServer command to run the derby database.

Hibernate Configuration

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <property name="dialect">
        <property name="connection.url">
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">
        <property name="hbm2ddl.auto">update</property>
        <mapping resource="EmpTO.hbm.xml" />

By default, the Derby Network Server only accepts requests from the localhost on port 1527

The following is the database connection URL syntax for Java DB:
  • subsubprotocol specifies where Java DB should search for the database, either in a directory, in memory, in a class path, or in a JAR file. It is typically omitted.
  • databaseName is the name of the database to connect to.
  • attribute=value represents an optional, semicolon-separated list of attributes. These attributes enable you to instruct Java DB to perform various tasks, including the following:
    • Create the database specified in the connection URL.
    • Encrypt the database specified in the connection URL.
    • Specify directories to store logging and trace information.
    • Specify a user name and password to connect to the database.
Java DB: jdbc:derby://localhost:1527/testdb;create=true, where testdb is the name of the database to connect to, and create=true instructs the DBMS to create the database.

Ant script for JUnit

Ant script is build to test the JUnit test framework. Before creating any test case we need Java compiler, Ant and JUnit.
we need junit.jar in your Ant's library folder or have jar file in our project lib folder.

Sample JUnit Test class to fecth records.

import junit.framework.TestCase;
public class EmpTest extends TestCase {
     * fetch records based on Employee ID from Employee table.
    public final void testFetchByID() {
        Integer empId = 2;
        EmpTO empTO = empDAO.findById(empId);

        System.out.println("Fetch Employee ID " + empId
                + ", FirstName : " + empTO.getFirstName());

Build.xml for JUnit

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="test">

    <!-- Source and class folders used in project-->
    <property name="testdir" location="bin" />
    <property name="srcdir" location="source" />
    <property name="srctestdir" location="source-test" />

    <!-- consists of destination class files and jar files from lib folder-->
    <path id="test.classpath">
        <pathelement location="${testdir}" />
        <fileset dir="${basedir}/lib">
            <include name="**/*.jar" />

    <!-- Delete all the class files from project -->
    <target name="clean">
            <fileset dir="${testdir}" includes="**/*.class" />
    <!-- compiled source folder of project -->
    <target name="compile" depends="clean">
        <javac srcdir="${srcdir}" destdir="${testdir}">
            <classpath refid="test.classpath" />
        <javac srcdir="${srctestdir}" destdir="${testdir}">
            <classpath refid="test.classpath" />
    <!-- Testing JUnit code by junit task -->
    <target name="test" depends="compile">   
        <junit printsummary="yes" haltonfailure="yes" showoutput="yes">
            <classpath refid="test.classpath" />
            <!-- The results of the tests can be printed in different formats.
            Output will always be sent to a file, unless you set the usefile attribute to false.
            The formatter named brief will only print detailed information for testcases that failed -->
            <formatter type="brief" usefile="false" />
            <!-- Define a number of tests based on pattern matching.
            fork      Run the tests in a separate VM.
            <batchtest fork="yes">
                <fileset dir="${basedir}/source-test/">
                    <include name="**/*Test.java" />

Output of xml file mostly look like this

Buildfile: E:\workspace\Project\EmployeeRecordProj\build.xml
    [javac] Compiling 6 source files to E:\workspace\Project\PROJECT_NAME\bin
    [junit] Running com.test.EmpTest
    [junit] Testsuite: com.test.EmpTest
    [junit] Fetch Employee ID 2, FirstName : Harsha
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 1.61 sec
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 1.61 sec
    [junit] ------------- Standard Output ---------------
    [junit] Fetch Employee ID 2, FirstName : Harsha
    [junit] ------------- ---------------- ---------------
    [junit] ------------- Standard Error -----------------
    [junit] ------------- ---------------- ---------------
Total time: 3 seconds

Wednesday, August 24, 2011

Sample IP Address & Email ID Patterns