-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 added
BIN
+717 KB
test/fixtures/ant/build_time_libs/activejdbc-instrumentation-1.4.11.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
test/fixtures/ant/src_test/activejdbc/ant/example/EmployeeSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
|