Skip to content

Commit

Permalink
1237 | Subhas/Kavitha | Added both integration and unit tests for DAO
Browse files Browse the repository at this point in the history
  • Loading branch information
Subhas Dandapani committed Oct 29, 2012
1 parent d8feacb commit 2d1acf3
Show file tree
Hide file tree
Showing 23 changed files with 386 additions and 194 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ before_install:
- ln -s $ANDROID_HOME/tools/emulator-arm $ANDROID_HOME/tools/emulator

install: echo
script: mvn clean install
script: mvn clean install -P ci

notifications:
email: false
Expand Down
32 changes: 19 additions & 13 deletions RapidFTR-Android/src/main/java/com/rapidftr/dao/ChildDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.database.Cursor;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.rapidftr.database.DatabaseHelper;
import com.rapidftr.database.DatabaseSession;
import com.rapidftr.model.Child;
import lombok.Cleanup;
Expand All @@ -19,24 +18,27 @@

public class ChildDAO implements Closeable {

private final String userName;
private final DatabaseHelper helper;
private final DatabaseSession session;
protected final String userName;
protected final DatabaseSession session;

@Inject
public ChildDAO(@Named("USER_NAME") String userName, DatabaseHelper helper) {
public ChildDAO(@Named("USER_NAME") String userName, DatabaseSession session) {
this.userName = userName;
this.helper = helper;
this.session = helper.openSession();
this.session = session;
}

public void clearAll() {
session.delete("CHILDREN", " = ?", new String[]{userName});
public Child get(String id) throws JSONException {
@Cleanup Cursor cursor = session.rawQuery("SELECT child_json FROM children WHERE id = ? AND child_owner = ?", new String[]{id, userName});
return cursor.moveToNext() ? new Child(cursor.getString(0)) : null;
}

public int size() {
@Cleanup Cursor cursor = session.rawQuery("SELECT COUNT(1) FROM children WHERE child_owner = ?", new String[]{userName});
return cursor.moveToNext() ? cursor.getInt(0) : 0;
}

public List<Child> getAllChildren() throws JSONException {
@Cleanup Cursor cursor = session.rawQuery("SELECT * FROM CHILDREN WHERE OWNER_USERNAME=? ORDER BY CHILD_ID", new String[]{userName});
public List<Child> all() throws JSONException {
@Cleanup Cursor cursor = session.rawQuery("SELECT child_json FROM children WHERE child_owner = ? ORDER BY id", new String[]{userName});

List<Child> children = new ArrayList<Child>();
while (cursor.moveToNext()) {
Expand All @@ -47,18 +49,22 @@ public List<Child> getAllChildren() throws JSONException {
}

public void create(Child child) throws JSONException {
if (!userName.equals(child.getOwner()))
throw new IllegalArgumentException();

ContentValues values = new ContentValues();
values.put(DB_CHILD_OWNER, child.getOwner());
values.put(DB_CHILD_ID, child.getId());
values.put(DB_CHILD_CONTENT, child.toString());

session.insert("CHILDREN", null, values);
long id = session.insert("CHILDREN", null, values);
if (id <= 0)
throw new IllegalArgumentException();
}

@Override
public void close() throws IOException {
session.close();
helper.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public interface DatabaseHelper extends Closeable {
public static final String DB_CHILD_CONTENT = "child_json";
public static final String DB_CHILD_OWNER = "child_owner";

DatabaseSession openSession();
public DatabaseSession getSession();

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

public interface DatabaseSession extends Closeable {

public int delete(String table, String whereClause, String[] whereArgs);
public Cursor rawQuery(String sql, String[] selectionArgs);
public void execSQL(String sql);
public long insert(String table, String nullColumnHack, ContentValues values);
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit);
public int delete(String table, String whereClause, String[] whereArgs);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.rapidftr.database;

import android.content.ContentValues;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor(suppressConstructorProperties = true)
public class FluentInsert {

public interface Interface {
public FluentInsert insert();
}

protected ContentValues values;
protected final DatabaseSession session;

public FluentInsert set(String column, String value) {
values.put(column, value);
return this;
}

public long insertInto(String table) {
return session.insert(table, null, values);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.rapidftr.database;

import android.database.Cursor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor(suppressConstructorProperties = true)
public class FluentSelect {

public interface Interface {
public FluentSelect select();
}

protected String table;
protected List<String> columns = new ArrayList<String>();
protected StringBuffer selection;
protected List<String> selectionArgs = new ArrayList<String>();
protected StringBuffer orderBy;
protected String limit;

protected final DatabaseSession session;
protected @Getter Cursor cursor;

public FluentSelect from(String table) {
this.table = table;
return this;
}

public FluentSelect select(String column) {
this.columns.add(column);
return this;
}

public FluentSelect where(String criteria, String arg) {
this.selection.append("(").append(criteria).append(")");
this.selectionArgs.add(arg);
return this;
}

public FluentSelect and(String criteria, String arg) {
this.selection.append(" AND ");
return this.where(criteria, arg);
}

public FluentSelect or(String criteria, String arg) {
this.selection.append(" OR ");
return this.where(criteria, arg);
}

public FluentSelect orderBy(String orderBy, boolean asc) {
this.orderBy.append(orderBy).append(asc ? " ASC" : " DESC");
return this;
}

public FluentSelect limit(String limit) {
this.limit = limit;
return this;
}

public FluentSelect execute() {
this.cursor = session.query(table, columns.toArray(new String[columns.size()]), selection.toString(), selectionArgs.toArray(new String[selectionArgs.size()]), null, null, orderBy.toString(), limit);
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,34 @@
import android.content.Context;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import lombok.Getter;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;

public class SQLCipherHelper extends SQLiteOpenHelper implements DatabaseHelper {

public static final String DB_NAME = "rapidftr.db";
public static final int DB_VERSION = 1;

public static final String DB_CHILD_TABLE = "children";
public static final String DB_CHILD_ID = "id";
public static final String DB_CHILD_CONTENT = "child_json";
public static final String DB_CHILD_OWNER = "child_owner";

// Database creation sql statement
public static final String DATABASE_CREATE = "create table "
+ DB_CHILD_TABLE + "("
+ DB_CHILD_ID + " text primary key, "
+ DB_CHILD_CONTENT + " text not null,"
+ DB_CHILD_OWNER + " text not null"
+ ");";

private final String dbKey;
protected @Getter final DatabaseSession session;

@Inject
public SQLCipherHelper(@Named("DB_KEY") String dbKey, Context context) {
super(context, DB_NAME, null, DB_VERSION);
net.sqlcipher.database.SQLiteDatabase.loadLibs(context);
public SQLCipherHelper(@Named("DB_NAME") String dbName, @Named("DB_KEY") String dbKey, Context context) {
super(context, dbName, null, DB_VERSION);
SQLiteDatabase.loadLibs(context);

this.dbKey = dbKey;
}

@Override
public DatabaseSession openSession() {
return null;
this.session = new SQLCipherSession(getWritableDatabase(dbKey));
}

@Override
Expand All @@ -48,4 +42,10 @@ public void onCreate(SQLiteDatabase database) {
public void onUpgrade(SQLiteDatabase database, int i, int i1) {
}

@Override
public void close() {
super.close();
SQLiteDatabase.releaseMemory();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import lombok.RequiredArgsConstructor;
import net.sqlcipher.database.SQLiteDatabase;

@RequiredArgsConstructor
@RequiredArgsConstructor(suppressConstructorProperties = true)
public class SQLCipherSession implements DatabaseSession {

@Delegate(types = DatabaseSession.class)
private final SQLiteDatabase database;
protected final SQLiteDatabase database;

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.rapidftr.forms;

import lombok.*;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.EqualsAndHashCode;

import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor(suppressConstructorProperties = true)
public class FormField {

@JsonProperty("name")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.rapidftr.forms;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.*;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;

Expand All @@ -13,6 +11,8 @@
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor(suppressConstructorProperties = true)
public class FormSection implements Comparable<FormSection> {

private String name;
Expand All @@ -32,4 +32,8 @@ public int compareTo(FormSection other) {
return Integer.valueOf(this.order).compareTo(otherOrder);
}

public String toString() {
return name;
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.rapidftr.forms;

import lombok.Getter;
import lombok.Setter;
import lombok.EqualsAndHashCode;
import lombok.*;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor(suppressConstructorProperties = true)
public class HighlightInfo{

private String order;
Expand Down
15 changes: 14 additions & 1 deletion RapidFTR-Android/src/main/java/com/rapidftr/model/Child.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.rapidftr.model;

import com.google.common.base.Strings;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;
Expand All @@ -15,12 +18,13 @@ public class Child extends JSONObject {
public static final String[] INTERNAL_FIELDS = { ID_FIELD, OWNER_FIELD };

public static final SimpleDateFormat UUID_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
public static final ObjectMapper JSON_MAPPER = new ObjectMapper();

public Child() {
}

public Child(String content) throws JSONException {
super(content == null ? "{}" : content);
super(Strings.nullToEmpty(content).trim().length() == 0 ? "{}" : content);
}

public Child(String id, String owner, String content) throws JSONException {
Expand Down Expand Up @@ -98,4 +102,13 @@ public boolean isValid() {

return count > 0;
}

public boolean equals(Object other) {
try {
return (other != null && other instanceof JSONObject) ? JSON_MAPPER.readTree(toString()).equals(JSON_MAPPER.readTree(other.toString())) : false;
} catch (IOException e) {
return false;
}
}

}
Loading

0 comments on commit 2d1acf3

Please sign in to comment.