Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage improvement. #462

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions library/src/main/java/com/orm/SchemaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,41 +187,39 @@ protected String createTableSQL(Class<?> table) {
String columnName = NamingHelper.toSQLName(column);
String columnType = QueryBuilder.getColumnType(column.getType());

if (columnType != null) {
if (columnName.equalsIgnoreCase("Id")) {
if (columnType == null || columnName.equalsIgnoreCase("Id")) {
continue;
}
}

if (column.isAnnotationPresent(Column.class)) {
Column columnAnnotation = column.getAnnotation(Column.class);
columnName = columnAnnotation.name();
if (column.isAnnotationPresent(Column.class)) {
Column columnAnnotation = column.getAnnotation(Column.class);
columnName = columnAnnotation.name();

sb.append(", ").append(columnName).append(" ").append(columnType);
sb.append(", ").append(columnName).append(" ").append(columnType);

if (columnAnnotation.notNull()) {
if (columnType.endsWith(NULL)) {
sb.delete(sb.length() - 5, sb.length());
}
sb.append(NOT_NULL);
if (columnAnnotation.notNull()) {
if (columnType.endsWith(NULL)) {
sb.delete(sb.length() - 5, sb.length());
}
sb.append(NOT_NULL);
}

if (columnAnnotation.unique()) {
sb.append(UNIQUE);
}
if (columnAnnotation.unique()) {
sb.append(UNIQUE);
}

} else {
sb.append(", ").append(columnName).append(" ").append(columnType);
} else {
sb.append(", ").append(columnName).append(" ").append(columnType);

if (column.isAnnotationPresent(NotNull.class)) {
if (columnType.endsWith(NULL)) {
sb.delete(sb.length() - 5, sb.length());
}
sb.append(NOT_NULL);
if (column.isAnnotationPresent(NotNull.class)) {
if (columnType.endsWith(NULL)) {
sb.delete(sb.length() - 5, sb.length());
}
sb.append(NOT_NULL);
}

if (column.isAnnotationPresent(Unique.class)) {
sb.append(UNIQUE);
}
if (column.isAnnotationPresent(Unique.class)) {
sb.append(UNIQUE);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/orm/SugarContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private SugarContext(Context context) {
this.entitiesMap = Collections.synchronizedMap(new WeakHashMap<Object, Long>());
}

public static SugarContext getSugarContext() {
public static SugarContext getInstance() {
if (instance == null) {
throw new NullPointerException("SugarContext has not been initialized properly. Call SugarContext.init(Context) in your Application.onCreate() method and SugarContext.terminate() in your Application.onTerminate() method.");
}
Expand Down
19 changes: 9 additions & 10 deletions library/src/main/java/com/orm/SugarRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@
import java.util.Map;
import java.util.NoSuchElementException;

import static com.orm.SugarContext.getSugarContext;

public class SugarRecord {

public static final String SUGAR = "Sugar";
private Long id = null;

private static SQLiteDatabase getSugarDataBase() {
return getSugarContext().getSugarDb().getDB();
return SugarContext.getInstance().getSugarDb().getDB();
}

public static <T> int deleteAll(Class<T> type) {
Expand Down Expand Up @@ -126,7 +125,7 @@ public static <T> int deleteInTx(Collection<T> objects) {
public static <T> List<T> listAll(Class<T> type) {
return find(type, null, null, null, null, null);
}

public static <T> List<T> listAll(Class<T> type, String orderBy) {
return find(type, null, null, null, orderBy, null);
}
Expand Down Expand Up @@ -210,7 +209,7 @@ public static <T> List<T> getEntitiesFromCursor(Cursor cursor, Class<T> type){
try {
while (cursor.moveToNext()) {
entity = type.getDeclaredConstructor().newInstance();
inflate(cursor, entity, getSugarContext().getEntitiesMap());
inflate(cursor, entity, SugarContext.getInstance().getEntitiesMap());
result.add(entity);
}
} catch (Exception e) {
Expand Down Expand Up @@ -261,7 +260,7 @@ public static long save(Object object) {
}

static long save(SQLiteDatabase db, Object object) {
Map<Object, Long> entitiesMap = getSugarContext().getEntitiesMap();
Map<Object, Long> entitiesMap = SugarContext.getInstance().getEntitiesMap();
List<Field> columns = ReflectionUtil.getTableFields(object.getClass());
ContentValues values = new ContentValues(columns.size());
Field idField = null;
Expand All @@ -284,7 +283,7 @@ static long save(SQLiteDatabase db, Object object) {
if (idField != null) {
idField.setAccessible(true);
try {
idField.set(object, new Long(id));
idField.set(object, id);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Expand All @@ -305,7 +304,7 @@ public static long update(Object object) {
}

static long update(SQLiteDatabase db, Object object) {
Map<Object, Long> entitiesMap = getSugarContext().getEntitiesMap();
Map<Object, Long> entitiesMap = SugarContext.getInstance().getEntitiesMap();
List<Field> columns = ReflectionUtil.getTableFields(object.getClass());
ContentValues values = new ContentValues(columns.size());

Expand Down Expand Up @@ -381,7 +380,7 @@ public boolean delete() {
return false;
}
}

public static boolean delete(Object object) {
Class<?> type = object.getClass();
if (type.isAnnotationPresent(Table.class)) {
Expand Down Expand Up @@ -422,7 +421,7 @@ public long update() {

@SuppressWarnings("unchecked")
void inflate(Cursor cursor) {
inflate(cursor, this, getSugarContext().getEntitiesMap());
inflate(cursor, this, SugarContext.getInstance().getEntitiesMap());
}

public Long getId() {
Expand Down Expand Up @@ -460,7 +459,7 @@ public E next() {

try {
entity = type.getDeclaredConstructor().newInstance();
inflate(cursor, entity, getSugarContext().getEntitiesMap());
inflate(cursor, entity, SugarContext.getInstance().getEntitiesMap());
} catch (Exception e) {
e.printStackTrace();
} finally {
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/orm/SugarTransactionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class SugarTransactionHelper {

public static void doInTransaction(SugarTransactionHelper.Callback callback) {
SQLiteDatabase database = SugarContext.getSugarContext().getSugarDb().getDB();
SQLiteDatabase database = SugarContext.getInstance().getSugarDb().getDB();
database.beginTransaction();

try {
Expand Down
7 changes: 3 additions & 4 deletions library/src/main/java/com/orm/util/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public static List<Field> getTableFields(Class table) {
if (fieldList != null) return fieldList;

Log.d("Sugar", "Fetching properties");
List<Field> typeFields = new ArrayList<Field>();
List<Field> typeFields = new ArrayList<>();

getAllFields(typeFields, table);

List<Field> toStore = new ArrayList<Field>();
List<Field> toStore = new ArrayList<>();
for (Field field : typeFields) {
if (!field.isAnnotationPresent(Ignore.class) && !Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers())) {
toStore.add(field);
Expand Down Expand Up @@ -72,8 +72,7 @@ public static void addFieldValueToColumn(ContentValues values, Field column, Obj
field = columnType.getDeclaredField("id");
field.setAccessible(true);
values.put(columnName,
(field != null)
? String.valueOf(field.get(columnValue)) : "0");
String.valueOf(field.get(columnValue)));
} catch (NoSuchFieldException e) {
if (entitiesMap.containsKey(columnValue)) {
values.put(columnName, entitiesMap.get(columnValue));
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/orm/util/SugarConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class SugarConfig {

static Map<Class<?>, List<Field>> fields = new HashMap<Class<?>, List<Field>>();
static Map<Class<?>, List<Field>> fields = new HashMap<>();

public static void setFields(Class<?> clazz, List<Field> fieldz) {
fields.put(clazz, fieldz);
Expand Down
33 changes: 33 additions & 0 deletions library/src/test/java/com/orm/util/NumberComparatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.orm.util;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

/**
* Created by Nursultan Turdaliev on 12/6/15.
*/
public class NumberComparatorTest {

private String ten;
private String eleven;
private NumberComparator numberComparator;

@Before
public void setUp() throws Exception {

ten = "10.sql";
eleven = "11.sql";
numberComparator = new NumberComparator();

}

@Test
public void testCompare() throws Exception {

assertEquals("Testing a small number with a big number",-1,numberComparator.compare(ten,eleven));
assertEquals("Testing equal numbers",0,numberComparator.compare(ten, ten));
assertEquals("Testing a big number with a small number ",1,numberComparator.compare(eleven,ten));
}
}
37 changes: 37 additions & 0 deletions library/src/test/java/com/orm/util/SugarConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.orm.util;

import com.orm.query.TestRecord;

import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.List;

import static org.junit.Assert.*;

/**
* Created by Nursultan Turdaliev on 12/6/15.
*/
public class SugarConfigTest {

@Test
public void testSetFields() throws Exception {

assertNull("SugarConfig: testing non existent key", SugarConfig.getFields(TestRecord.class));

ReflectionUtil.getTableFields(TestRecord.class);

assertTrue("SugarConfig: testing existing key on SugarConfig", SugarConfig.fields.containsKey(TestRecord.class));
assertFalse("SugarConfig: testing non existent key ", SugarConfig.fields.containsKey(Field.class));

SugarConfig.clearCache();
assertNull("SugarConfig: testing non existent key", SugarConfig.getFields(TestRecord.class));

}

@Test
public void testGetFields() throws Exception {

}
}