Skip to content

Commit

Permalink
test(builders): add ant fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
xizhao committed May 14, 2018
1 parent 27ea4ff commit 1ed9769
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/fixtures/ant/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Example of ActiveJDBC using Ant

To execute:

1. Run create.sql against your instance of MySQL - this will create a new table.
2. Execute: "ant run" in this directory
91 changes: 91 additions & 0 deletions test/fixtures/ant/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="Simple ActiveJDBC Example" default="clean" basedir=".">



<property name="dist" value="dist"/>
<property name="classes" value="build/classes"/>
<property name="test_classes" value="build/test_classes"/>

<path id="compile_classpath">
<pathelement location="${classes}"/>
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>

<path id="build_classpath">
<path refid="compile_classpath"/>
<fileset dir="build_time_libs">
<include name="*.jar"/>
</fileset>
</path>

<path id="test_classpath">
<path refid="compile_classpath"/>
<fileset dir="build_time_libs">
<include name="*.jar"/>
</fileset>
<pathelement location="${test_classes}"/>
</path>

<path id="run_classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<fileset dir="dist">
<include name="*.jar"/>
</fileset>
</path>

<target name="clean">
<delete dir="${classes}" failonerror="true"/>
<mkdir dir="${classes}"/>
<delete dir="${test_classes}" failonerror="true"/>
<mkdir dir="${test_classes}"/>
<delete dir="${dist}" failonerror="true"/>
<mkdir dir="${dist}"/>
</target>

<target name="compile" depends="clean">
<javac srcdir="src" destdir="${classes}" debug="on" optimize="off" deprecation="off" includeantruntime="false">
<classpath refid="compile_classpath"/>
</javac>
</target>


<target name="compile_tests" depends="compile">
<javac srcdir="src_test" destdir="${test_classes}" debug="on" optimize="off" deprecation="off" includeantruntime="false">
<classpath refid="build_classpath"/>
</javac>
</target>

<target name="instrument" depends="compile_tests">
<java classname="org.javalite.instrumentation.Main">
<sysproperty key="outputDirectory" value="${classes}"/>
<classpath refid="build_classpath"/>
</java>
</target>

<target name="test" depends="instrument">
<junit printsummary="yes" haltonfailure="true" showoutput="true">
<classpath>
<path refid="test_classpath"/>
</classpath>
<formatter type="plain"/>
<test name="activejdbc.ant.example.EmployeeSpec"/>
</junit>
</target>

<target name="package" depends="test">
<jar destfile="${dist}/ant-example.jar" basedir="${classes}"/>
</target>

<target name="run" depends="package">
<java classname="activejdbc.ant.example.Main" failonerror="true" fork="true">
<jvmarg value="-Dactivejdbc.log"/>
<classpath refid="run_classpath"/>
</java>
</target>

</project>
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions test/fixtures/ant/create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DROP TABLE IF EXISTS employees;
CREATE TABLE employees (
id int(11) NOT NULL auto_increment PRIMARY KEY,
first_name VARCHAR(56),
last_name VARCHAR(56)
);
Binary file added test/fixtures/ant/lib/activejdbc-1.4.13.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added test/fixtures/ant/lib/slf4j-api-1.7.6.jar
Binary file not shown.
Binary file added test/fixtures/ant/lib/slf4j-simple-1.7.6.jar
Binary file not shown.
9 changes: 9 additions & 0 deletions test/fixtures/ant/src/activejdbc/ant/example/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package activejdbc.ant.example;

import org.javalite.activejdbc.Model;

public class Employee extends Model {
static {
validatePresenceOf("first_name", "last_name");
}
}
76 changes: 76 additions & 0 deletions test/fixtures/ant/src/activejdbc/ant/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2009-2010 Igor Polevoy
Licensed 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.
*/

package activejdbc.ant.example;

import org.javalite.activejdbc.Base;

/**
* @author Igor Polevoy
*/
public class Main {
public static void main(String[] args) {
Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/test", "root", "p@ssw0rd");

createEmployee();
System.out.println("=========> Created employee:");
selectEmployee();
updateEmployee();
System.out.println("=========> Updated employee:");
selectAllEmployees();
deleteEmployee();
System.out.println("=========> Deleted employee:");
selectAllEmployees();
createEmployee();
System.out.println("=========> Created employee:");
selectEmployee();
deleteAllEmployees();
System.out.println("=========> Deleted all employees:");
selectAllEmployees();

Base.close();
}

private static void createEmployee() {
Employee e = new Employee();
e.set("first_name", "John");
e.set("last_name", "Doe");
e.saveIt();
}

private static void selectEmployee() {
Employee e = Employee.findFirst("first_name = ?", "John");
System.out.println(e);
}

private static void updateEmployee() {
Employee e = Employee.findFirst("first_name = ?", "John");
e.set("last_name", "Steinbeck").saveIt();
}

private static void deleteEmployee() {
Employee e = Employee.findFirst("first_name = ?", "John");
e.delete();
}

private static void deleteAllEmployees() {
Employee.deleteAll();
}

private static void selectAllEmployees() {
System.out.println("Employees list: " + Employee.findAll());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2009-2010 Igor Polevoy
Licensed 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.
*/

package activejdbc.ant.example;

import org.javalite.activejdbc.Base;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.javalite.test.jspec.JSpec.the;

/**
* @author Igor Polevoy
*/
public class EmployeeSpec{

@Before
public void before(){
Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/test", "root", "p@ssw0rd");
Base.openTransaction();
}

@After
public void after(){
Base.rollbackTransaction();
Base.close();
}

@Test
public void shouldValidateMandatoryFields(){

Employee employee = new Employee();

//check errors
the(employee).shouldNotBe("valid");
the(employee.errors().get("first_name")).shouldBeEqual("value is missing");
the(employee.errors().get("last_name")).shouldBeEqual("value is missing");

//set missing values
employee.set("first_name", "John", "last_name", "Doe");

//all is good:
the(employee).shouldBe("valid");
}
}

0 comments on commit 1ed9769

Please sign in to comment.