From 8d997ba8ed62f49366dfdb42737d8d1f345b0809 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Sun, 6 Dec 2015 22:51:28 +0100 Subject: [PATCH 1/4] NumberComparator tests --- .../main/java/com/orm/SchemaGenerator.java | 48 +++++++++---------- .../com/orm/util/NumberComparatorTest.java | 33 +++++++++++++ 2 files changed, 56 insertions(+), 25 deletions(-) create mode 100644 library/src/test/java/com/orm/util/NumberComparatorTest.java diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index b35e061a..db290d19 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -127,41 +127,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/test/java/com/orm/util/NumberComparatorTest.java b/library/src/test/java/com/orm/util/NumberComparatorTest.java new file mode 100644 index 00000000..fb942264 --- /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 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 From d0776a31414232acbde8aaca182006c0c1cf7f58 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Sun, 6 Dec 2015 23:38:14 +0100 Subject: [PATCH 2/4] #458 Removed not used class, added coverage tests --- .../java/com/orm/util/ReflectionUtil.java | 7 ++-- .../main/java/com/orm/util/SugarConfig.java | 2 +- .../com/orm/util/NumberComparatorTest.java | 2 +- .../java/com/orm/util/SugarConfigTest.java | 34 +++++++++++++++++++ 4 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 library/src/test/java/com/orm/util/SugarConfigTest.java diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index 67a5d8d3..6a18bbb7 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 index fb942264..60cfb1fe 100644 --- a/library/src/test/java/com/orm/util/NumberComparatorTest.java +++ b/library/src/test/java/com/orm/util/NumberComparatorTest.java @@ -6,7 +6,7 @@ import static org.junit.Assert.*; /** - * Created by nursultan on 12/6/15. + * Created by Nursultan Turdaliev on 12/6/15. */ public class NumberComparatorTest { 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..e59b505b --- /dev/null +++ b/library/src/test/java/com/orm/util/SugarConfigTest.java @@ -0,0 +1,34 @@ +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)); + + } + + @Test + public void testGetFields() throws Exception { + + } +} \ No newline at end of file From 6baddc6a8a9378388c8649a8128000304aa21190 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Sun, 6 Dec 2015 23:53:16 +0100 Subject: [PATCH 3/4] Added test to clearCache method. --- library/src/test/java/com/orm/util/SugarConfigTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/src/test/java/com/orm/util/SugarConfigTest.java b/library/src/test/java/com/orm/util/SugarConfigTest.java index e59b505b..5d61f065 100644 --- a/library/src/test/java/com/orm/util/SugarConfigTest.java +++ b/library/src/test/java/com/orm/util/SugarConfigTest.java @@ -23,7 +23,10 @@ public void testSetFields() throws Exception { 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)); + assertFalse("SugarConfig: testing non existent key ", SugarConfig.fields.containsKey(Field.class)); + + SugarConfig.clearCache(); + assertNull("SugarConfig: testing non existent key", SugarConfig.getFields(TestRecord.class)); } From 341ed657d3c2d6a044b9c9f9be207d3cfe4723a1 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Tue, 2 Feb 2016 23:49:12 +0100 Subject: [PATCH 4/4] Refactoring. --- .../src/main/java/com/orm/SugarContext.java | 2 +- .../src/main/java/com/orm/SugarRecord.java | 19 +++++++++---------- .../java/com/orm/SugarTransactionHelper.java | 2 +- 3 files changed, 11 insertions(+), 12 deletions(-) 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 {