diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index f5fd4f2a..63564ca5 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -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); } } } diff --git a/library/src/main/java/com/orm/SugarContext.java b/library/src/main/java/com/orm/SugarContext.java index 2f653f5e..a764aaa1 100644 --- a/library/src/main/java/com/orm/SugarContext.java +++ b/library/src/main/java/com/orm/SugarContext.java @@ -17,7 +17,7 @@ private SugarContext(Context context) { this.entitiesMap = Collections.synchronizedMap(new WeakHashMap()); } - 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."); } diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 4b9e04ef..64456230 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -24,7 +24,6 @@ import java.util.Map; import java.util.NoSuchElementException; -import static com.orm.SugarContext.getSugarContext; public class SugarRecord { @@ -32,7 +31,7 @@ public class SugarRecord { private Long id = null; private static SQLiteDatabase getSugarDataBase() { - return getSugarContext().getSugarDb().getDB(); + return SugarContext.getInstance().getSugarDb().getDB(); } public static int deleteAll(Class type) { @@ -126,7 +125,7 @@ public static int deleteInTx(Collection objects) { public static List listAll(Class type) { return find(type, null, null, null, null, null); } - + public static List listAll(Class type, String orderBy) { return find(type, null, null, null, orderBy, null); } @@ -210,7 +209,7 @@ public static List getEntitiesFromCursor(Cursor cursor, Class 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) { @@ -261,7 +260,7 @@ public static long save(Object object) { } static long save(SQLiteDatabase db, Object object) { - Map entitiesMap = getSugarContext().getEntitiesMap(); + Map entitiesMap = SugarContext.getInstance().getEntitiesMap(); List columns = ReflectionUtil.getTableFields(object.getClass()); ContentValues values = new ContentValues(columns.size()); Field idField = null; @@ -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(); } @@ -305,7 +304,7 @@ public static long update(Object object) { } static long update(SQLiteDatabase db, Object object) { - Map entitiesMap = getSugarContext().getEntitiesMap(); + Map entitiesMap = SugarContext.getInstance().getEntitiesMap(); List columns = ReflectionUtil.getTableFields(object.getClass()); ContentValues values = new ContentValues(columns.size()); @@ -381,7 +380,7 @@ public boolean delete() { return false; } } - + public static boolean delete(Object object) { Class type = object.getClass(); if (type.isAnnotationPresent(Table.class)) { @@ -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() { @@ -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 { diff --git a/library/src/main/java/com/orm/SugarTransactionHelper.java b/library/src/main/java/com/orm/SugarTransactionHelper.java index 7c00812c..54483d76 100644 --- a/library/src/main/java/com/orm/SugarTransactionHelper.java +++ b/library/src/main/java/com/orm/SugarTransactionHelper.java @@ -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 { diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index 1458a308..7f12c98e 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -33,11 +33,11 @@ public static List getTableFields(Class table) { if (fieldList != null) return fieldList; Log.d("Sugar", "Fetching properties"); - List typeFields = new ArrayList(); + List typeFields = new ArrayList<>(); getAllFields(typeFields, table); - List toStore = new ArrayList(); + List toStore = new ArrayList<>(); for (Field field : typeFields) { if (!field.isAnnotationPresent(Ignore.class) && !Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers())) { toStore.add(field); @@ -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)); diff --git a/library/src/main/java/com/orm/util/SugarConfig.java b/library/src/main/java/com/orm/util/SugarConfig.java index c8cf3868..f653aaf8 100644 --- a/library/src/main/java/com/orm/util/SugarConfig.java +++ b/library/src/main/java/com/orm/util/SugarConfig.java @@ -9,7 +9,7 @@ public class SugarConfig { - static Map, List> fields = new HashMap, List>(); + static Map, List> fields = new HashMap<>(); public static void setFields(Class clazz, List fieldz) { fields.put(clazz, fieldz); diff --git a/library/src/test/java/com/orm/util/NumberComparatorTest.java b/library/src/test/java/com/orm/util/NumberComparatorTest.java new file mode 100644 index 00000000..60cfb1fe --- /dev/null +++ b/library/src/test/java/com/orm/util/NumberComparatorTest.java @@ -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)); + } +} \ No newline at end of file diff --git a/library/src/test/java/com/orm/util/SugarConfigTest.java b/library/src/test/java/com/orm/util/SugarConfigTest.java new file mode 100644 index 00000000..5d61f065 --- /dev/null +++ b/library/src/test/java/com/orm/util/SugarConfigTest.java @@ -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 { + + } +} \ No newline at end of file