From 05051a2d9a3b4d3ed68e3795be4c29c867219dda Mon Sep 17 00:00:00 2001 From: Alex Mitchell Date: Mon, 27 Jul 2015 17:40:33 +0100 Subject: [PATCH 001/139] Catching Throwable instead of ClassNotFoundException because Class.forName can also throw LinkageError and ExceptionInInitializerError. If we silently ignore a ClassNotFoundException, I don't see why we wouldn't ignore the others. A NoClassDefFoundError coming out of this method was causing my application to crash, but it was actually fine that the class couldn't be loaded. --- library/src/main/java/com/orm/util/ReflectionUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index a740b42d..255a1a4c 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -268,7 +268,7 @@ private static Class getDomainClass(String className, Context context) { Class discoveredClass = null; try { discoveredClass = Class.forName(className, true, context.getClass().getClassLoader()); - } catch (ClassNotFoundException e) { + } catch (Throwable e) { Log.e("Sugar", e.getMessage()); } From 8eeaebe2e59f597cc7229b3a54b4796c93b1d9b2 Mon Sep 17 00:00:00 2001 From: RoyMontoya Date: Fri, 16 Oct 2015 14:15:20 -0700 Subject: [PATCH 002/139] cleaned Select from hardcoded Strings --- .../src/main/java/com/orm/query/Select.java | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/library/src/main/java/com/orm/query/Select.java b/library/src/main/java/com/orm/query/Select.java index 9c52a9b4..1ec92bd3 100644 --- a/library/src/main/java/com/orm/query/Select.java +++ b/library/src/main/java/com/orm/query/Select.java @@ -17,6 +17,15 @@ public class Select implements Iterable { private String limit; private String offset; private List args = new ArrayList(); + private static final String SPACE =" "; + private static final String SINGLE_QUOTE ="'"; + private static final String LEFT_PARENTHESIS="("; + private static final String RIGHT_PARENTHESIS=")"; + private static final String SELECT_FROM="SELECT * FROM "; + private static final String WHERE="WHERE "; + private static final String ORDER_BY ="ORDER BY "; + private static final String LIMIT ="LIMIT "; + private static final String OFFSET ="OFFSET "; public Select(Class record) { this.record = record; @@ -54,10 +63,10 @@ public Select where(Condition... condition) { } private void mergeConditions(Condition[] conditions, Condition.Type type) { - StringBuilder toAppend = new StringBuilder(""); + StringBuilder toAppend = new StringBuilder(); for (Condition condition : conditions) { if (toAppend.length() != 0) { - toAppend.append(" ").append(type.name()).append(" "); + toAppend.append(SPACE).append(type.name()).append(SPACE); } if (Condition.Check.LIKE.equals(condition.getCheck()) || @@ -65,9 +74,9 @@ private void mergeConditions(Condition[] conditions, Condition.Type type) { toAppend .append(condition.getProperty()) .append(condition.getCheckSymbol()) - .append("'") + .append(SINGLE_QUOTE) .append(condition.getValue().toString()) - .append("'"); + .append(SINGLE_QUOTE); } else if (Condition.Check.IS_NULL.equals(condition.getCheck()) || Condition.Check.IS_NOT_NULL.equals(condition.getCheck())) { toAppend @@ -82,11 +91,11 @@ private void mergeConditions(Condition[] conditions, Condition.Type type) { } } - if (!"".equals(whereClause)) { - whereClause += " " + type.name() + " "; + if (!whereClause.isEmpty()) { + whereClause += SPACE + type.name() + SPACE; } - whereClause += "(" + toAppend + ")"; + whereClause += LEFT_PARENTHESIS + toAppend + RIGHT_PARENTHESIS; } public Select whereOr(Condition... args) { @@ -137,28 +146,28 @@ public T first() { String toSql() { StringBuilder sql = new StringBuilder(); - sql.append("SELECT * FROM ").append(NamingHelper.toSQLName(this.record)).append(" "); + sql.append(SELECT_FROM).append(NamingHelper.toSQLName(this.record)).append(SPACE); - if (whereClause != null) { - sql.append("WHERE ").append(whereClause).append(" "); + if (!whereClause.isEmpty()) { + sql.append(WHERE).append(whereClause).append(SPACE); } - if (orderBy != null) { - sql.append("ORDER BY ").append(orderBy).append(" "); + if (!orderBy.isEmpty()) { + sql.append(ORDER_BY).append(orderBy).append(SPACE); } - if (limit != null) { - sql.append("LIMIT ").append(limit).append(" "); + if (!limit.isEmpty()) { + sql.append(LIMIT).append(limit).append(SPACE); } - if (offset != null) { - sql.append("OFFSET ").append(offset).append(" "); + if (!offset.isEmpty()) { + sql.append(OFFSET).append(offset).append(SPACE); } return sql.toString(); } - String getWhereCond() { + String getWhereClause() { return whereClause; } From d038f6c79f9c8ce736e6856bf5b90b8641ab5029 Mon Sep 17 00:00:00 2001 From: RoyMontoya Date: Fri, 16 Oct 2015 14:26:40 -0700 Subject: [PATCH 003/139] revert change on whereCond name --- library/src/main/java/com/orm/query/Select.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/query/Select.java b/library/src/main/java/com/orm/query/Select.java index 1ec92bd3..e13410fe 100644 --- a/library/src/main/java/com/orm/query/Select.java +++ b/library/src/main/java/com/orm/query/Select.java @@ -167,7 +167,7 @@ String toSql() { return sql.toString(); } - String getWhereClause() { + String getWhereCond() { return whereClause; } From da0a7a62a940a270b0bee2c70ba1c118d23bd001 Mon Sep 17 00:00:00 2001 From: RoyMontoya Date: Mon, 19 Oct 2015 17:58:59 -0700 Subject: [PATCH 004/139] added test --- .../src/main/java/com/orm/query/Select.java | 59 +++++++++++-------- .../test/java/com/orm/query/SelectTest.java | 30 +++++++++- 2 files changed, 64 insertions(+), 25 deletions(-) diff --git a/library/src/main/java/com/orm/query/Select.java b/library/src/main/java/com/orm/query/Select.java index 9c52a9b4..3275c7bb 100644 --- a/library/src/main/java/com/orm/query/Select.java +++ b/library/src/main/java/com/orm/query/Select.java @@ -1,5 +1,7 @@ package com.orm.query; +import android.database.sqlite.SQLiteQueryBuilder; + import com.orm.SugarRecord; import com.orm.util.NamingHelper; @@ -12,10 +14,10 @@ public class Select implements Iterable { private Class record; private String[] arguments; private String whereClause = ""; - private String orderBy; - private String groupBy; - private String limit; - private String offset; + private String orderBy = ""; + private String groupBy = ""; + private String limit = ""; + private String offset = ""; private List args = new ArrayList(); public Select(Class record) { @@ -41,6 +43,11 @@ public Select limit(String limit) { return this; } + public Select offset(String offset) { + this.offset = offset; + return this; + } + public Select where(String whereClause) { this.whereClause = whereClause; return this; @@ -63,25 +70,25 @@ private void mergeConditions(Condition[] conditions, Condition.Type type) { if (Condition.Check.LIKE.equals(condition.getCheck()) || Condition.Check.NOT_LIKE.equals(condition.getCheck())) { toAppend - .append(condition.getProperty()) - .append(condition.getCheckSymbol()) - .append("'") - .append(condition.getValue().toString()) - .append("'"); + .append(condition.getProperty()) + .append(condition.getCheckSymbol()) + .append("'") + .append(condition.getValue().toString()) + .append("'"); } else if (Condition.Check.IS_NULL.equals(condition.getCheck()) || Condition.Check.IS_NOT_NULL.equals(condition.getCheck())) { toAppend - .append(condition.getProperty()) - .append(condition.getCheckSymbol()); + .append(condition.getProperty()) + .append(condition.getCheckSymbol()); } else { toAppend - .append(condition.getProperty()) - .append(condition.getCheckSymbol()) - .append("? "); + .append(condition.getProperty()) + .append(condition.getCheckSymbol()) + .append("? "); args.add(condition.getValue()); } } - + if (!"".equals(whereClause)) { whereClause += " " + type.name() + " "; } @@ -117,12 +124,12 @@ public List list() { return SugarRecord.find(record, whereClause, arguments, groupBy, orderBy, limit); } - + public long count() { if (arguments == null) { arguments = convertArgs(args); } - + return SugarRecord.count(record, whereClause, arguments, groupBy, orderBy, limit); } @@ -134,24 +141,28 @@ public T first() { List list = SugarRecord.find(record, whereClause, arguments, groupBy, orderBy, "1"); return list.size() > 0 ? list.get(0) : null; } - + String toSql() { StringBuilder sql = new StringBuilder(); sql.append("SELECT * FROM ").append(NamingHelper.toSQLName(this.record)).append(" "); - if (whereClause != null) { + if (!whereClause.isEmpty()) { sql.append("WHERE ").append(whereClause).append(" "); } - if (orderBy != null) { + if (!orderBy.isEmpty()) { sql.append("ORDER BY ").append(orderBy).append(" "); } - if (limit != null) { + if (!groupBy.isEmpty()) { + sql.append("GROUP BY ").append(groupBy).append(" "); + } + + if (!limit.isEmpty()) { sql.append("LIMIT ").append(limit).append(" "); } - if (offset != null) { + if (!offset.isEmpty()) { sql.append("OFFSET ").append(offset).append(" "); } @@ -170,7 +181,7 @@ private String[] convertArgs(List argsList) { String[] argsArray = new String[argsList.size()]; for (int i = 0; i < argsList.size(); i++) { - argsArray[i] = argsList.get(i).toString(); + argsArray[i] = argsList.get(i).toString(); } return argsArray; @@ -185,4 +196,4 @@ public Iterator iterator() { return SugarRecord.findAsIterator(record, whereClause, arguments, groupBy, orderBy, limit); } -} +} \ No newline at end of file diff --git a/library/src/test/java/com/orm/query/SelectTest.java b/library/src/test/java/com/orm/query/SelectTest.java index 3ab345ae..372f0a5d 100644 --- a/library/src/test/java/com/orm/query/SelectTest.java +++ b/library/src/test/java/com/orm/query/SelectTest.java @@ -35,6 +35,34 @@ public void testWhere(){ assertEquals("2", where.getArgs()[1]); } + @Test + public void toSqlAllClauses(){ + String toSql = Select.from(TestRecord.class) + .where("test") + .groupBy("test") + .orderBy("test") + .limit("test") + .offset("test") + .toSql(); + assertEquals("SELECT * FROM TEST_RECORD WHERE test ORDER BY test GROUP BY test LIMIT test OFFSET test ", toSql); + } + + @Test + public void toSqlNoClauses(){ + String toSql = Select.from(TestRecord.class) + .toSql(); + assertEquals("SELECT * FROM TEST_RECORD ", toSql); + } + + @Test + public void toSqlWhereLimitClauses(){ + String toSql = Select.from(TestRecord.class) + .where("test") + .limit("test") + .toSql(); + assertEquals("SELECT * FROM TEST_RECORD WHERE test LIMIT test ", toSql); + } + @Test public void testWhereOr(){ @@ -101,4 +129,4 @@ public void testIsNotNull() { assertEquals("(test IS NOT NULL )", where.getWhereCond()); assertEquals(0, where.getArgs().length); } -} +} \ No newline at end of file From 3cc5cf1b240b3befdea9531495080fd7b0187ce2 Mon Sep 17 00:00:00 2001 From: RoyMontoya Date: Mon, 19 Oct 2015 18:10:41 -0700 Subject: [PATCH 005/139] format fix --- .../src/main/java/com/orm/query/Select.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/library/src/main/java/com/orm/query/Select.java b/library/src/main/java/com/orm/query/Select.java index 3275c7bb..40f1107b 100644 --- a/library/src/main/java/com/orm/query/Select.java +++ b/library/src/main/java/com/orm/query/Select.java @@ -1,7 +1,5 @@ package com.orm.query; -import android.database.sqlite.SQLiteQueryBuilder; - import com.orm.SugarRecord; import com.orm.util.NamingHelper; @@ -70,21 +68,21 @@ private void mergeConditions(Condition[] conditions, Condition.Type type) { if (Condition.Check.LIKE.equals(condition.getCheck()) || Condition.Check.NOT_LIKE.equals(condition.getCheck())) { toAppend - .append(condition.getProperty()) - .append(condition.getCheckSymbol()) - .append("'") - .append(condition.getValue().toString()) - .append("'"); + .append(condition.getProperty()) + .append(condition.getCheckSymbol()) + .append("'") + .append(condition.getValue().toString()) + .append("'"); } else if (Condition.Check.IS_NULL.equals(condition.getCheck()) || Condition.Check.IS_NOT_NULL.equals(condition.getCheck())) { toAppend - .append(condition.getProperty()) - .append(condition.getCheckSymbol()); + .append(condition.getProperty()) + .append(condition.getCheckSymbol()); } else { toAppend - .append(condition.getProperty()) - .append(condition.getCheckSymbol()) - .append("? "); + .append(condition.getProperty()) + .append(condition.getCheckSymbol()) + .append("? "); args.add(condition.getValue()); } } @@ -181,7 +179,7 @@ private String[] convertArgs(List argsList) { String[] argsArray = new String[argsList.size()]; for (int i = 0; i < argsList.size(); i++) { - argsArray[i] = argsList.get(i).toString(); + argsArray[i] = argsList.get(i).toString(); } return argsArray; @@ -196,4 +194,4 @@ public Iterator iterator() { return SugarRecord.findAsIterator(record, whereClause, arguments, groupBy, orderBy, limit); } -} \ No newline at end of file +} From 547c816a2ed6ab0011ef33f07a5755e83ab0c9fb Mon Sep 17 00:00:00 2001 From: RoyMontoya Date: Mon, 19 Oct 2015 18:28:46 -0700 Subject: [PATCH 006/139] added group_by to toSQL --- .../src/main/java/com/orm/query/Select.java | 5 +++++ .../test/java/com/orm/query/SelectTest.java | 18 +++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/library/src/main/java/com/orm/query/Select.java b/library/src/main/java/com/orm/query/Select.java index 8e536170..11652c1c 100644 --- a/library/src/main/java/com/orm/query/Select.java +++ b/library/src/main/java/com/orm/query/Select.java @@ -24,6 +24,7 @@ public class Select implements Iterable { private static final String SELECT_FROM="SELECT * FROM "; private static final String WHERE="WHERE "; private static final String ORDER_BY ="ORDER BY "; + private static final String GROUP_BY ="GROUP BY "; private static final String LIMIT ="LIMIT "; private static final String OFFSET ="OFFSET "; @@ -161,6 +162,10 @@ String toSql() { sql.append(ORDER_BY).append(orderBy).append(SPACE); } + if (!groupBy.isEmpty()) { + sql.append(GROUP_BY).append(groupBy).append(SPACE); + } + if (!limit.isEmpty()) { sql.append(LIMIT).append(limit).append(SPACE); } diff --git a/library/src/test/java/com/orm/query/SelectTest.java b/library/src/test/java/com/orm/query/SelectTest.java index 372f0a5d..39508b77 100644 --- a/library/src/test/java/com/orm/query/SelectTest.java +++ b/library/src/test/java/com/orm/query/SelectTest.java @@ -38,13 +38,13 @@ public void testWhere(){ @Test public void toSqlAllClauses(){ String toSql = Select.from(TestRecord.class) - .where("test") - .groupBy("test") - .orderBy("test") - .limit("test") - .offset("test") + .where("foo") + .orderBy("doe") + .groupBy("john") + .limit("5") + .offset("10") .toSql(); - assertEquals("SELECT * FROM TEST_RECORD WHERE test ORDER BY test GROUP BY test LIMIT test OFFSET test ", toSql); + assertEquals("SELECT * FROM TEST_RECORD WHERE foo ORDER BY doe GROUP BY john LIMIT 5 OFFSET 10 ", toSql); } @Test @@ -57,10 +57,10 @@ public void toSqlNoClauses(){ @Test public void toSqlWhereLimitClauses(){ String toSql = Select.from(TestRecord.class) - .where("test") - .limit("test") + .where("foo") + .limit("10") .toSql(); - assertEquals("SELECT * FROM TEST_RECORD WHERE test LIMIT test ", toSql); + assertEquals("SELECT * FROM TEST_RECORD WHERE foo LIMIT 10 ", toSql); } From f5ca5e3a7dca87ffdbc2ae09cb178880d1bf141e Mon Sep 17 00:00:00 2001 From: Alfredo Matos Date: Fri, 25 Sep 2015 16:30:39 +0100 Subject: [PATCH 007/139] Add MultiColumn Unique Table constraint support --- .../main/java/com/orm/SchemaGenerator.java | 19 +++++++++++++++++++ library/src/main/java/com/orm/dsl/Unique.java | 1 + 2 files changed, 20 insertions(+) diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index c0510478..4d897d58 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.Arrays; import java.util.Collections; @@ -153,6 +154,24 @@ private void createTable(Class table, SQLiteDatabase sqLiteDatabase) { } } + if (table.isAnnotationPresent(Unique.class)) { + String constraint = table.getAnnotation(Unique.class).value(); + + sb.append(", UNIQUE("); + + String[] constraintFields = constraint.split(","); + for(int i = 0; i < constraintFields.length; i++) { + String columnName = NamingHelper.toSQLNameDefault(constraintFields[i]); + sb.append(columnName); + + if(i < (constraintFields.length -1)) { + sb.append(","); + } + } + + sb.append(") ON CONFLICT REPLACE"); + } + sb.append(" ) "); Log.i("Sugar", "Creating table " + tableName); diff --git a/library/src/main/java/com/orm/dsl/Unique.java b/library/src/main/java/com/orm/dsl/Unique.java index 7384b749..76d82559 100644 --- a/library/src/main/java/com/orm/dsl/Unique.java +++ b/library/src/main/java/com/orm/dsl/Unique.java @@ -5,4 +5,5 @@ @Retention(RetentionPolicy.RUNTIME) public @interface Unique { + String value(); } From 98f8df508cb23917b661ce9046695ce2d8f0c159 Mon Sep 17 00:00:00 2001 From: Ovadia Babayev Date: Wed, 28 Oct 2015 12:11:00 +0200 Subject: [PATCH 008/139] Filtered classes by DOMAIN_PACKAGE_NAME --- .../main/java/com/orm/util/ReflectionUtil.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index 2c71d631..1f4c6404 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -306,11 +306,12 @@ private static SharedPreferences getMultiDexPreferences(Context context) { } private static List getAllClasses(Context context) throws PackageManager.NameNotFoundException, IOException { + String packageName = ManifestHelper.getDomainPackageName(context); List paths = getSourcePaths(context); List classNames = new ArrayList<>(); DexFile dexfile = null; try { - for (int i = 0; i getAllClasses(Context context) throws PackageManager Enumeration dexEntries = dexfile.entries(); while (dexEntries.hasMoreElements()) { - classNames.add(dexEntries.nextElement()); + String className = dexEntries.nextElement(); + if (className.startsWith(packageName)) { + classNames.add(className); + } } } } catch (NullPointerException e) { @@ -335,12 +339,18 @@ private static List getAllClasses(Context context) throws PackageManager for (File filePath : classDirectory.listFiles()) { populateFiles(filePath, fileNames, ""); } - classNames.addAll(fileNames); + + for (String className : fileNames) { + if (className.startsWith(packageName)) { + classNames.add(className); + } + } } } } finally { if (null != dexfile) dexfile.close(); } + return classNames; } @@ -372,6 +382,7 @@ public static List getSourcePaths(Context context) throws PackageManager return sourcePaths; } + private static void populateFiles(File path, List fileNames, String parent) { if (path.isDirectory()) { for (File newPath : path.listFiles()) { From 3a507b2091dd266113e57e0779bdc3e50fa24cde Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Tue, 1 Dec 2015 18:26:30 -0200 Subject: [PATCH 009/139] save record when update fails --- library/src/main/java/com/orm/SugarRecord.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index a51b71c4..36979284 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -334,9 +334,13 @@ static long update(SQLiteDatabase db, Object object) { String[] whereArgsArray = whereArgs.toArray(new String[whereArgs.size()]); // Get SugarRecord based on Unique values - long id = db.update(NamingHelper.toSQLName(object.getClass()), values, whereClause.toString(), whereArgsArray); + long rowsEffected = db.update(NamingHelper.toSQLName(object.getClass()), values, whereClause.toString(), whereArgsArray); - return id; + if (rowsEffected == 0) { + return save(db, object); + } else { + return rowsEffected; + } } From 3d7744932e07758e642dca0973baa7f3e8f59a04 Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Wed, 2 Dec 2015 15:32:03 -0200 Subject: [PATCH 010/139] refactor SchemaGenerator and adding some tests --- .../main/java/com/orm/SchemaGenerator.java | 12 +++- .../java/com/orm/SchemaGeneratorTest.java | 64 +++++++++++++++++++ .../test/java/com/orm/models/EmptyModel.java | 12 ++++ .../java/com/orm/models/IntUniqueModel.java | 20 ++++++ .../orm/models/StringFieldAnnotatedModel.java | 21 ++++++ .../orm/models/StringFieldExtendedModel.java | 18 ++++++ ...ringFieldExtendedModelAnnotatedColumn.java | 20 ++++++ 7 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 library/src/test/java/com/orm/SchemaGeneratorTest.java create mode 100644 library/src/test/java/com/orm/models/EmptyModel.java create mode 100644 library/src/test/java/com/orm/models/IntUniqueModel.java create mode 100644 library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java create mode 100644 library/src/test/java/com/orm/models/StringFieldExtendedModel.java create mode 100644 library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index 91f80e1b..bce45da0 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -108,7 +108,7 @@ private void executeScript(SQLiteDatabase db, String file) { Log.i(SUGAR, "Script executed"); } - private void createTable(Class table, SQLiteDatabase sqLiteDatabase) { + protected String createTableSQL(Class table) { Log.i(SUGAR, "Create table if not exists"); List fields = ReflectionUtil.getTableFields(table); String tableName = NamingHelper.toSQLName(table); @@ -161,9 +161,15 @@ private void createTable(Class table, SQLiteDatabase sqLiteDatabase) { sb.append(" ) "); Log.i(SUGAR, "Creating table " + tableName); - if (!sb.toString().isEmpty()) { + return sb.toString(); + } + + private void createTable(Class table, SQLiteDatabase sqLiteDatabase) { + String createSQL = createTableSQL(table); + + if (!createSQL.isEmpty()) { try { - sqLiteDatabase.execSQL(sb.toString()); + sqLiteDatabase.execSQL(createSQL); } catch (SQLException e) { e.printStackTrace(); } diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java new file mode 100644 index 00000000..1caab6f6 --- /dev/null +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -0,0 +1,64 @@ +package com.orm; + +import com.orm.dsl.Table; +import com.orm.models.EmptyModel; +import com.orm.models.IntUniqueModel; +import com.orm.models.StringFieldAnnotatedModel; +import com.orm.models.StringFieldExtendedModel; +import com.orm.models.StringFieldExtendedModelAnnotatedColumn; +import com.orm.query.DummyContext; +import com.orm.util.NamingHelper; + +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; + +public class SchemaGeneratorTest { + @Test + public void testEmptyTableCreation() throws Exception { + SchemaGenerator schemaGenerator = new SchemaGenerator(new DummyContext()); + String createSQL = schemaGenerator.createTableSQL(EmptyModel.class); + assertEquals( + "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(EmptyModel.class) + + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT ) ", + createSQL); + } + + @Test + public void testSimpleColumnTableCreation() throws Exception { + SchemaGenerator schemaGenerator = new SchemaGenerator(new DummyContext()); + String createSQL = schemaGenerator.createTableSQL(StringFieldExtendedModel.class); + assertEquals( + "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(StringFieldExtendedModel.class) + + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + + "NAME TEXT ) ", + createSQL); + + String createSQL2 = schemaGenerator.createTableSQL(StringFieldAnnotatedModel.class); + + assertEquals( + "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(StringFieldAnnotatedModel.class) + + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + + "NAME TEXT ) ", + createSQL2); + + String createSQL3 = schemaGenerator.createTableSQL(StringFieldExtendedModelAnnotatedColumn.class); + + assertEquals( + "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(StringFieldExtendedModelAnnotatedColumn.class) + + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + + "anyName TEXT ) ", + createSQL3); + } + + @Test + public void testUniqueTableCreation() { + SchemaGenerator schemaGenerator = new SchemaGenerator(new DummyContext()); + String createSQL = schemaGenerator.createTableSQL(IntUniqueModel.class); + assertEquals( + "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(IntUniqueModel.class) + + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + + "VALUE INTEGER UNIQUE ) ", + createSQL); + } +} diff --git a/library/src/test/java/com/orm/models/EmptyModel.java b/library/src/test/java/com/orm/models/EmptyModel.java new file mode 100644 index 00000000..8efb9dd4 --- /dev/null +++ b/library/src/test/java/com/orm/models/EmptyModel.java @@ -0,0 +1,12 @@ +package com.orm.models; + +import com.orm.SugarRecord; + +/** + * Created by sibelius on 02/12/15. + */ +public class EmptyModel extends SugarRecord { + public EmptyModel() { + + } +} diff --git a/library/src/test/java/com/orm/models/IntUniqueModel.java b/library/src/test/java/com/orm/models/IntUniqueModel.java new file mode 100644 index 00000000..e73e8968 --- /dev/null +++ b/library/src/test/java/com/orm/models/IntUniqueModel.java @@ -0,0 +1,20 @@ +package com.orm.models; + +import com.orm.SugarRecord; +import com.orm.dsl.Unique; + +/** + * Created by sibelius on 02/12/15. + */ +public class IntUniqueModel extends SugarRecord { + @Unique + private int value; + + public IntUniqueModel() { + + } + + public IntUniqueModel(int value) { + this.value = value; + } +} diff --git a/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java b/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java new file mode 100644 index 00000000..1203db01 --- /dev/null +++ b/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java @@ -0,0 +1,21 @@ +package com.orm.models; + +import com.orm.SugarRecord; +import com.orm.dsl.Column; +import com.orm.dsl.Table; + +/** + * Created by sibelius on 02/12/15. + */ +@Table +public class StringFieldAnnotatedModel extends SugarRecord { + public String name; + + public StringFieldAnnotatedModel() { + + } + + public StringFieldAnnotatedModel(String name) { + this.name = name; + } +} diff --git a/library/src/test/java/com/orm/models/StringFieldExtendedModel.java b/library/src/test/java/com/orm/models/StringFieldExtendedModel.java new file mode 100644 index 00000000..ee781632 --- /dev/null +++ b/library/src/test/java/com/orm/models/StringFieldExtendedModel.java @@ -0,0 +1,18 @@ +package com.orm.models; + +import com.orm.SugarRecord; + +/** + * Created by sibelius on 02/12/15. + */ +public class StringFieldExtendedModel extends SugarRecord { + public String name; + + public StringFieldExtendedModel() { + + } + + public StringFieldExtendedModel(String name) { + this.name = name; + } +} diff --git a/library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java b/library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java new file mode 100644 index 00000000..9f2b43cd --- /dev/null +++ b/library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java @@ -0,0 +1,20 @@ +package com.orm.models; + +import com.orm.SugarRecord; +import com.orm.dsl.Column; + +/** + * Created by sibelius on 02/12/15. + */ +public class StringFieldExtendedModelAnnotatedColumn extends SugarRecord { + @Column(name="anyName") + public String name; + + public StringFieldExtendedModelAnnotatedColumn() { + + } + + public StringFieldExtendedModelAnnotatedColumn(String name) { + this.name = name; + } +} From b8ad30907671ce17d781a10128938746dd790ef2 Mon Sep 17 00:00:00 2001 From: The Gitter Badger Date: Wed, 2 Dec 2015 20:38:35 +0000 Subject: [PATCH 011/139] Add Gitter badge --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f4d129a5..024f77f5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Sugar ORM [![Build Status](https://travis-ci.org/satyan/sugar.svg?branch=master)](https://travis-ci.org/satyan/sugar) [![Coverage Status](https://coveralls.io/repos/satyan/sugar/badge.svg?branch=master)](https://coveralls.io/r/satyan/sugar?branch=master) +[![Join the chat at https://gitter.im/satyan/sugar](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/satyan/sugar?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + Insanely easy way to work with Android databases. Official documentation can be found [here](http://satyan.github.io/sugar). The example application is provided in the **example** folder in the source. From 74016793ae960dbccf2f3070ee504db6ab3cb368 Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Thu, 3 Dec 2015 10:57:37 -0200 Subject: [PATCH 012/139] update README.md --- README.md | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f4d129a5..4cc090e2 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Insanely easy way to work with Android databases. -Official documentation can be found [here](http://satyan.github.io/sugar). The example application is provided in the **example** folder in the source. +Official documentation can be found [here](http://satyan.github.io/sugar) **Outdated** - Check some examples below. The example application is provided in the **example** folder in the source. ## Features @@ -46,9 +46,43 @@ Download the source code and import it as a library project in Eclipse. The proj Visit the [releases](https://github.com/satyan/sugar/releases) page to download jars directly. You can drop them into your `libs` folder and configure the Java build path to include the library. See this [tutorial](http://www.vogella.com/tutorials/AndroidLibraryProjects/article.html) for an excellent guide on how to do this. + +### How to use master version +First, download sugar repository +``` +git clone git@github.com:satyan/sugar.git +``` + +include this in your **settings.gradle** +``` +include ':app' // your module app +include ':sugar' + +def getLocalProperty(prop) { + Properties properties = new Properties() + properties.load(new File(rootDir.absolutePath + '/local.properties').newDataInputStream()) + return properties.getProperty(prop, '') +} + +project(':sugar').projectDir = new File(getLocalProperty('sugar.dir')) + +``` + +include this in your **local.properties** +``` +sugar.dir=/path/to/sugar/library +``` + +add sugar project to the dependencies of your main project (build.gradle) +``` +dependencies { + compile project(':sugar') +} +``` + =================== -After installing, check out how to set up your first database and models [here](http://satyan.github.io/sugar/getting-started.html). +After installing, check out how to set up your first database and models [here](http://satyan.github.io/sugar/getting-started.html) **Outdated**. Check examples of 1.4 and master below: ## Examples ### SugarRecord @@ -123,6 +157,8 @@ books.add(new Book("isbn789", "Title here 3", "4nd edition")) SugarRecord.saveInTx(books); ``` +## [CHANGELOG](https://github.com/satyan/sugar/blob/master/CHANGELOG.md) + ## Contributing Please fork this repository and contribute back using [pull requests](https://github.com/satyan/sugar/pulls). Features can be requested using [issues](https://github.com/satyan/sugar/issues). All code, comments, and critiques are greatly appreciated. From 93c216118488ce9fdc6e7fcf47072096eb80e32d Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Thu, 3 Dec 2015 11:58:54 -0200 Subject: [PATCH 013/139] warn developer to upgrade database on missing colName --- library/src/main/java/com/orm/util/ReflectionUtil.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index 87b42f63..a5d9a58f 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -154,6 +154,12 @@ public static void setFieldValueFromCursor(Cursor cursor, Field field, Object ob int columnIndex = cursor.getColumnIndex(colName); + //TODO auto upgrade to add new columns + if (columnIndex < 0) { + Log.e("SUGAR", "Invalid colName, you should upgrade database"); + return; + } + if (cursor.isNull(columnIndex)) { return; } From 243ee4f20c4cd497eaf146d10175a0efc624f7a4 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 19:53:29 +0100 Subject: [PATCH 014/139] Travis --- .travis.yml | 1 + example/build.gradle | 1 + 2 files changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index dc4c9691..72ccfc19 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ android: components: - sys-img-armeabi-v7a-android-23 - tools + - platform-tools - build-tools-23.0.2 - android-23 diff --git a/example/build.gradle b/example/build.gradle index 129eb11e..7e3052aa 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -20,6 +20,7 @@ android { dependencies { compile project (':library') compile 'com.android.support:appcompat-v7:23.1.1' + compile 'com.android.support:support-v4:23.1.1' testCompile 'org.robolectric:robolectric:3.0' testCompile 'junit:junit:4.12' } From 80baa64f76c4d14644f7edfd85f0142dde28c600 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 20:06:20 +0100 Subject: [PATCH 015/139] travis error --- .travis.yml | 7 +++++-- example/build.gradle | 1 - 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 72ccfc19..8f971250 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,15 @@ language: android +jdk: oracle android: components: - sys-img-armeabi-v7a-android-23 - - tools - - platform-tools + - tool - build-tools-23.0.2 - android-23 + - platform-tool + - extra-android-m2repository + - extra-google-m2repository script: - gradle clean build connectedCheck coveralls diff --git a/example/build.gradle b/example/build.gradle index 7e3052aa..129eb11e 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -20,7 +20,6 @@ android { dependencies { compile project (':library') compile 'com.android.support:appcompat-v7:23.1.1' - compile 'com.android.support:support-v4:23.1.1' testCompile 'org.robolectric:robolectric:3.0' testCompile 'junit:junit:4.12' } From 9e5d001e4ffedd55b0eef9894bfa94c3f60d345f Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 20:29:26 +0100 Subject: [PATCH 016/139] Jacoco --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index c6970775..6e6c2136 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:1.3.0' + classpath 'com.android.tools.build:gradle:1.5.0' classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.3.1' } } From a6f9bfc3122e5b41fd9044c4ad28ceef7b856767 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 20:34:34 +0100 Subject: [PATCH 017/139] Jacoco --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 6e6c2136..6b089a64 100644 --- a/build.gradle +++ b/build.gradle @@ -33,7 +33,7 @@ subprojects { proj -> version "0.7.1.201405082137" } - task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") { + task jacocoTestReport(type: JacocoReport) { group = "Reporting" description = "Generate Jacoco coverage reports after running tests." reports { From 2a014a58582c5c095ecca352379c1b199ce145e7 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 21:12:08 +0100 Subject: [PATCH 018/139] Travis Build fix --- example/build.gradle | 4 ++++ library/build.gradle | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/example/build.gradle b/example/build.gradle index 129eb11e..d79dd899 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -15,6 +15,10 @@ android { minifyEnabled false } } + + testOptions{ + unitTests.returnDefaultValues = true + } } dependencies { diff --git a/library/build.gradle b/library/build.gradle index d9a42c18..3652b458 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -15,6 +15,10 @@ android { minifyEnabled false } } + + testOptions{ + unitTests.returnDefaultValues = true + } } dependencies { From 1624af3cd0205b2ceaf5e543ee0ed13ab252d392 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 21:49:56 +0100 Subject: [PATCH 019/139] Travis Build fix --- .travis.yml | 3 +-- build.gradle | 8 +++++++- example/build.gradle | 1 - 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8f971250..8c73cf42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,10 +4,9 @@ jdk: oracle android: components: - sys-img-armeabi-v7a-android-23 - - tool + - tools - build-tools-23.0.2 - android-23 - - platform-tool - extra-android-m2repository - extra-google-m2repository diff --git a/build.gradle b/build.gradle index 6b089a64..9b9e75df 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,6 @@ apply plugin: 'jacoco' apply plugin: 'com.github.kt3k.coveralls' +apply plugin: 'java' buildscript { repositories { @@ -14,7 +15,11 @@ buildscript { def isReleaseBuild() { return version.contains("SNAPSHOT") == false } - +test{ + testLogging{ + events "passed", "skipped", "standardOut" + } +} allprojects { version = VERSION_NAME group = GROUP @@ -92,3 +97,4 @@ tasks.coveralls { dependsOn jacocoRootReport onlyIf { System.env.'CI' } } +apply plugin: 'java' \ No newline at end of file diff --git a/example/build.gradle b/example/build.gradle index d79dd899..fe05a8f0 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -1,5 +1,4 @@ apply plugin: 'com.android.application' - android { compileSdkVersion 23 buildToolsVersion "23.0.2" From 27539474bb27007dd950e5b240cf48315ca3ebc5 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 21:58:10 +0100 Subject: [PATCH 020/139] Travis Build fix --- build.gradle | 2 +- example/build.gradle | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 9b9e75df..439861fd 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ def isReleaseBuild() { } test{ testLogging{ - events "passed", "skipped", "standardOut" + exceptionFormat = 'full' } } allprojects { diff --git a/example/build.gradle b/example/build.gradle index fe05a8f0..084b9d2d 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -18,6 +18,9 @@ android { testOptions{ unitTests.returnDefaultValues = true } + lintOptions{ + abortOnError false + } } dependencies { From f64b71b921e607444f40f5c4178a162a728fe36b Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 22:14:30 +0100 Subject: [PATCH 021/139] Travis Build fix --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8c73cf42..c9280d04 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ android: - extra-google-m2repository script: - - gradle clean build connectedCheck coveralls + - gradle clean build connectedCheck coveralls --info cache: directories: From 32613715b540dfe4cb867b8957f740d745367a00 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 22:24:48 +0100 Subject: [PATCH 022/139] Travis Build fix --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index c9280d04..c3730259 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,10 +8,9 @@ android: - build-tools-23.0.2 - android-23 - extra-android-m2repository - - extra-google-m2repository script: - - gradle clean build connectedCheck coveralls --info + - gradle clean build connectedCheck coveralls --stacktrace cache: directories: From 75db4fb024be114a57a437c9635a154321d6364d Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 22:32:41 +0100 Subject: [PATCH 023/139] Travis Build fix --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c3730259..8d4c6efb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ android: - extra-android-m2repository script: - - gradle clean build connectedCheck coveralls --stacktrace + - gradle clean build connectedCheck coveralls --stacktrace --debug cache: directories: From f476390107ee538200132f8119ad43135693dbae Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 22:40:01 +0100 Subject: [PATCH 024/139] Travis Build fix --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8d4c6efb..d401372e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,6 @@ android: - tools - build-tools-23.0.2 - android-23 - - extra-android-m2repository script: - gradle clean build connectedCheck coveralls --stacktrace --debug From e7983f546244579ed1270300620f4b56fba3b36a Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 22:45:59 +0100 Subject: [PATCH 025/139] Travis Build fix --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d401372e..892a3f13 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ android: - android-23 script: - - gradle clean build connectedCheck coveralls --stacktrace --debug + - gradle clean build connectedCheck coveralls --stacktrace --info cache: directories: From 1c9ca0d09091c2b8aeb8944bdd8f4f5617d8872e Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 22:50:06 +0100 Subject: [PATCH 026/139] Travis Build fix --- .travis.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 892a3f13..21a24c49 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,15 +1,19 @@ language: android jdk: oracle +env: + global: + - TERM=dumb android: components: - sys-img-armeabi-v7a-android-23 - tools - build-tools-23.0.2 - android-23 + - extra-android-m2repository script: - - gradle clean build connectedCheck coveralls --stacktrace --info + - gradle clean build connectedCheck coveralls --debug cache: directories: From 5761057f16507841414253dccea57b6d03148388 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 23:15:13 +0100 Subject: [PATCH 027/139] Travis Build fix --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 21a24c49..c7e8af28 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ android: - extra-android-m2repository script: - - gradle clean build connectedCheck coveralls --debug + - ./gradlew clean build connectedCheck coveralls --debug cache: directories: From d007a2f3fef9c50e7241954f1e1a2e44d9b438f3 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 23:25:10 +0100 Subject: [PATCH 028/139] Travis Build fix --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c7e8af28..08c2ee82 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ android: - extra-android-m2repository script: - - ./gradlew clean build connectedCheck coveralls --debug + - ./gradlew clean build connectedCheck coveralls cache: directories: From fdae8e7839d5dd524f3c7fb3a83f279ce20c0092 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 23:35:27 +0100 Subject: [PATCH 029/139] Travis Build fix --- .travis.yml | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 08c2ee82..80ca1ced 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: android -jdk: oracle +jdk: oraclejdk7 env: global: @@ -11,9 +11,20 @@ android: - build-tools-23.0.2 - android-23 - extra-android-m2repository - +before_script: + - sudo service postgresql stop || true + - sudo service mysql stop || true + - sudo service memcached stop || true + - sudo service bootlogd stop || true + - sudo service elasticsearch stop || true + - sudo service mongodb stop || true + - sudo service neo4j stop || true + - sudo service cassandra stop || true + - sudo service riak stop || true + - sudo service rsync stop || true + - sudo service x11-common stop || true script: - - ./gradlew clean build connectedCheck coveralls + - gradle clean build connectedCheck coveralls cache: directories: From b087e0cb0721aacd29130152f0936bff236ef062 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Thu, 3 Dec 2015 23:42:17 +0100 Subject: [PATCH 030/139] Travis Build fix --- library/build.gradle | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/build.gradle b/library/build.gradle index 3652b458..ac7ffb66 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -19,6 +19,10 @@ android { testOptions{ unitTests.returnDefaultValues = true } + + lintOptions{ + abortOnError false + } } dependencies { From 9489846e25cdc37034052f22a10dc2fbe6bc1c9c Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Fri, 4 Dec 2015 19:19:51 +0100 Subject: [PATCH 031/139] JacocoReportTest --- build.gradle | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 439861fd..952f49e1 100644 --- a/build.gradle +++ b/build.gradle @@ -50,7 +50,7 @@ subprojects { proj -> excludes: ['**/R*.class', '**/BuildConfig*']) sourceDirectories = files('src/main/java') - executionData = files('build/jacoco/testDebug.exec') + executionData = files('build/jacoco/testDebugUnitTest.exec') doFirst { files('build/intermediates/classes/debug').getFiles().each { file -> if (file.name.contains('$$')) { @@ -96,5 +96,4 @@ tasks.coveralls { dependsOn jacocoRootReport onlyIf { System.env.'CI' } -} -apply plugin: 'java' \ No newline at end of file +} \ No newline at end of file From 4d2df142993c7822421bba81a334e9e31010c428 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Sat, 5 Dec 2015 00:16:13 +0100 Subject: [PATCH 032/139] Multiline and single line comments on sql migration files. --- .../main/java/com/orm/SchemaGenerator.java | 10 +++- .../src/main/java/com/orm/SugarContext.java | 2 - .../com/orm/util/MigrationFileParser.java | 21 ++++++++ .../com/orm/util/MigrationFileParserTest.java | 48 +++++++++++++++++++ 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 library/src/main/java/com/orm/util/MigrationFileParser.java create mode 100644 library/src/test/java/com/orm/util/MigrationFileParserTest.java diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index 449126db..87643cbf 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -10,6 +10,7 @@ import com.orm.dsl.MultiUnique; import com.orm.dsl.NotNull; import com.orm.dsl.Unique; +import com.orm.util.MigrationFileParser; import com.orm.util.NamingHelper; import com.orm.util.NumberComparator; import com.orm.util.QueryBuilder; @@ -98,11 +99,16 @@ private void executeScript(SQLiteDatabase db, String file) { try { InputStream is = this.context.getAssets().open("sugar_upgrades/" + file); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { - Log.i("Sugar script", line); - db.execSQL(line); + sb.append(line); } + MigrationFileParser migrationFileParser = new MigrationFileParser(sb.toString()); + for(String statement: migrationFileParser.getStatements()){ + db.execSQL(statement); + } + } catch (IOException e) { Log.e(SUGAR, e.getMessage()); } diff --git a/library/src/main/java/com/orm/SugarContext.java b/library/src/main/java/com/orm/SugarContext.java index 6190b1aa..2f653f5e 100644 --- a/library/src/main/java/com/orm/SugarContext.java +++ b/library/src/main/java/com/orm/SugarContext.java @@ -10,11 +10,9 @@ public class SugarContext { private static SugarContext instance = null; private SugarDb sugarDb; - private Context context; private Map entitiesMap; private SugarContext(Context context) { - this.context = context; this.sugarDb = new SugarDb(context); this.entitiesMap = Collections.synchronizedMap(new WeakHashMap()); } diff --git a/library/src/main/java/com/orm/util/MigrationFileParser.java b/library/src/main/java/com/orm/util/MigrationFileParser.java new file mode 100644 index 00000000..1691a1eb --- /dev/null +++ b/library/src/main/java/com/orm/util/MigrationFileParser.java @@ -0,0 +1,21 @@ +package com.orm.util; + +/** + * Created by Nursultan Turdaliev on 12/4/15. + */ +public class MigrationFileParser { + + private String content; + + /** + * @param content + */ + public MigrationFileParser(String content){ + this.content = content.replaceAll("(\\/\\*([\\s\\S]*?)\\*\\/)|(--(.)*)|(\n)",""); + } + + public String[] getStatements(){ + return this.content.split(";"); + } + +} diff --git a/library/src/test/java/com/orm/util/MigrationFileParserTest.java b/library/src/test/java/com/orm/util/MigrationFileParserTest.java new file mode 100644 index 00000000..536af4cb --- /dev/null +++ b/library/src/test/java/com/orm/util/MigrationFileParserTest.java @@ -0,0 +1,48 @@ +package com.orm.util; + +import com.orm.util.MigrationFileParser; + +import org.junit.Test; +import org.junit.Before; + +import java.lang.String; + +import static junit.framework.Assert.assertEquals; + +public class MigrationFileParserTest{ + MigrationFileParser emptyFile; + + @Test + public void testSingleLineStatement() + { + MigrationFileParser singleLineComment = new MigrationFileParser("insert into table--comment"); + + String statements[] = singleLineComment.getStatements(); + assertEquals("Testing single line statement size",1,statements.length); + assertEquals("Testing single line statement content","insert into table",statements[0]); + + singleLineComment = new MigrationFileParser("insert into table--comment\n"); + + singleLineComment.getStatements(); + assertEquals("Testing single line statement size",1,statements.length); + assertEquals("Testing single line statement content","insert into table",statements[0]); + } + @Test + public void testMultiLineComment(){ + MigrationFileParser multiLineComment = new MigrationFileParser("insert into table /**comment \n new line 2 \n new line 3 */hello"); + + String statements[] = multiLineComment.getStatements(); + assertEquals("Testing multiline statement size",1,statements.length); + assertEquals("Testing multiline comment","insert into table hello",statements[0]); + } + + @Test + public void testMixedComment(){ + MigrationFileParser mixedComment = new MigrationFileParser("insert into/*multiline\n **comment*/--comment"); + + String statements[] = mixedComment.getStatements(); + + assertEquals("Testing mixed comment statement size",1,statements.length); + assertEquals("Testing mixed comment statments", "insert into", statements[0]); + } +} From 79bd0406df589356bd265013f3305aafd8a5fa58 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Sat, 5 Dec 2015 00:32:34 +0100 Subject: [PATCH 033/139] Logging --- library/src/main/java/com/orm/SchemaGenerator.java | 1 + 1 file changed, 1 insertion(+) diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index 87643cbf..f95e6ef1 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -106,6 +106,7 @@ private void executeScript(SQLiteDatabase db, String file) { } MigrationFileParser migrationFileParser = new MigrationFileParser(sb.toString()); for(String statement: migrationFileParser.getStatements()){ + Log.i("Sugar script", statement); db.execSQL(statement); } From d06aafce60db2989f136be674e620533730e3468 Mon Sep 17 00:00:00 2001 From: Nursultan Turdaliev Date: Sat, 5 Dec 2015 22:18:40 +0100 Subject: [PATCH 034/139] Removed unused imports and redundant variable value assignments. --- .../src/main/java/com/example/activities/SugarActivity.java | 1 - library/src/main/java/com/orm/SchemaGenerator.java | 1 - library/src/main/java/com/orm/SugarRecord.java | 1 - library/src/main/java/com/orm/util/MultiDexHelper.java | 2 +- library/src/main/java/com/orm/util/NamingHelper.java | 5 ----- library/src/main/java/com/orm/util/NumberComparator.java | 4 ++-- library/src/main/java/com/orm/util/ReflectionUtil.java | 5 +---- library/src/main/java/com/orm/util/SugarConfig.java | 5 ----- 8 files changed, 4 insertions(+), 20 deletions(-) diff --git a/example/src/main/java/com/example/activities/SugarActivity.java b/example/src/main/java/com/example/activities/SugarActivity.java index 197314a0..81044546 100644 --- a/example/src/main/java/com/example/activities/SugarActivity.java +++ b/example/src/main/java/com/example/activities/SugarActivity.java @@ -1,7 +1,6 @@ package com.example.activities; import android.app.Activity; -import android.content.Intent; import android.os.Bundle; import com.example.R; diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index f95e6ef1..b35e061a 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -20,7 +20,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.Arrays; import java.util.Collections; diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 36979284..4b9e04ef 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -308,7 +308,6 @@ static long update(SQLiteDatabase db, Object object) { Map entitiesMap = getSugarContext().getEntitiesMap(); List columns = ReflectionUtil.getTableFields(object.getClass()); ContentValues values = new ContentValues(columns.size()); - Field idField = null; StringBuilder whereClause = new StringBuilder(); List whereArgs = new ArrayList<>(); diff --git a/library/src/main/java/com/orm/util/MultiDexHelper.java b/library/src/main/java/com/orm/util/MultiDexHelper.java index 37b57894..970cd3b7 100644 --- a/library/src/main/java/com/orm/util/MultiDexHelper.java +++ b/library/src/main/java/com/orm/util/MultiDexHelper.java @@ -85,7 +85,7 @@ public static List getAllClasses(Context context) throws PackageManager. List classNames = new ArrayList(); for (String path : getSourcePaths(context)) { try { - DexFile dexfile = null; + DexFile dexfile; if (path.endsWith(EXTRACTED_SUFFIX)) { //NOT use new DexFile(path), because it will throw "permission error in /data/dalvik-cache" dexfile = DexFile.loadDex(path, path + ".tmp", 0); diff --git a/library/src/main/java/com/orm/util/NamingHelper.java b/library/src/main/java/com/orm/util/NamingHelper.java index 734b4a56..b7237c09 100644 --- a/library/src/main/java/com/orm/util/NamingHelper.java +++ b/library/src/main/java/com/orm/util/NamingHelper.java @@ -1,14 +1,9 @@ package com.orm.util; -import android.text.TextUtils; - import com.orm.dsl.Column; import com.orm.dsl.Table; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; public class NamingHelper { diff --git a/library/src/main/java/com/orm/util/NumberComparator.java b/library/src/main/java/com/orm/util/NumberComparator.java index b39ca9c9..08a9f0b4 100644 --- a/library/src/main/java/com/orm/util/NumberComparator.java +++ b/library/src/main/java/com/orm/util/NumberComparator.java @@ -49,8 +49,8 @@ public int compare(Object o1, Object o2) { int ia = 0; int ib = 0; - int nza = 0; - int nzb = 0; + int nza; + int nzb; while (true) { nza = nzb = 0; diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index a5d9a58f..67a5d8d3 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -17,7 +17,6 @@ import java.lang.reflect.Modifier; import java.math.BigDecimal; import java.net.URL; -import java.sql.Ref; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Calendar; @@ -27,8 +26,6 @@ import java.util.List; import java.util.Map; -import dalvik.system.DexFile; - public class ReflectionUtil { public static List getTableFields(Class table) { @@ -70,7 +67,7 @@ public static void addFieldValueToColumn(ContentValues values, Field column, Obj Object columnValue = column.get(object); if (columnType.isAnnotationPresent(Table.class)) { - Field field = null; + Field field; try { field = columnType.getDeclaredField("id"); field.setAccessible(true); diff --git a/library/src/main/java/com/orm/util/SugarConfig.java b/library/src/main/java/com/orm/util/SugarConfig.java index b93da25e..c8cf3868 100644 --- a/library/src/main/java/com/orm/util/SugarConfig.java +++ b/library/src/main/java/com/orm/util/SugarConfig.java @@ -1,10 +1,5 @@ package com.orm.util; -import android.content.Context; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; -import android.util.Log; - import java.lang.reflect.Field; import java.util.Collections; import java.util.HashMap; From 297a8381e8a7c4bef9a1875ea6c1a2d6786053b7 Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Fri, 11 Dec 2015 15:09:25 -0200 Subject: [PATCH 035/139] removing static from callback interface --- library/src/main/java/com/orm/SugarTransactionHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/SugarTransactionHelper.java b/library/src/main/java/com/orm/SugarTransactionHelper.java index 73440635..7c00812c 100644 --- a/library/src/main/java/com/orm/SugarTransactionHelper.java +++ b/library/src/main/java/com/orm/SugarTransactionHelper.java @@ -24,7 +24,7 @@ public static void doInTransaction(SugarTransactionHelper.Callback callback) { } } - public static interface Callback { + public interface Callback { void manipulateInTransaction(); } } From def9904938893b65a3a96cea9c8c22bbbf337eae Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Fri, 11 Dec 2015 16:21:19 -0200 Subject: [PATCH 036/139] removing unique for auto generated add columns migrations --- CHANGELOG.md | 3 + .../main/java/com/orm/SchemaGenerator.java | 64 ++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f216161f..5c3f700b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [Unreleased] ### Added +* [#328](https://github.com/satyan/sugar/pull/328) @jedid auto add new columns during database upgrade, fix [#299](https://github.com/satyan/sugar/issues/299) and [#151](https://github.com/satyan/sugar/issues/151) * [#389](https://github.com/satyan/sugar/pull/389) @alfmatos MultiUnique DSL to handle MultiColumn Unique Table constraint * @sibeliusseraphini update, updateInTx methods based on Unique values of SugarRecord * [#155](https://github.com/satyan/sugar/issues/155) @benohalloran adding Cursors for Cursor Adapters [Pull 312](https://github.com/satyan/sugar/pull/312) @@ -12,6 +13,8 @@ * [#423](https://github.com/satyan/sugar/pull/423) @sibeliusseraphini moving changelog of README.md to CHANGELOG.md ### Fixed +* [#362](https://github.com/satyan/sugar/pull/362) @mitchyboy9 fixed NoClassDefFoundError +* [#455](https://github.com/satyan/sugar/pull/455) @nurolopher fixed travis and coveralls config * [#434](https://github.com/satyan/sugar/pull/434) @bendaniel10 fix multi-dex * [#410](https://github.com/satyan/sugar/pull/410) [#408](https://github.com/satyan/sugar/pull/408) @RoyMontoya simplify code * [#327](https://github.com/satyan/sugar/pull/327) @tracytheron support multi-dex diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index b35e061a..9e6601c9 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -21,6 +21,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Field; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -50,15 +51,32 @@ public void createDatabase(SQLiteDatabase sqLiteDatabase) { public void doUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { List domainClasses = getDomainClasses(context); String sql = "select count(*) from sqlite_master where type='table' and name='%s';"; + for (Class domain : domainClasses) { - Cursor cursor = sqLiteDatabase.rawQuery(String.format(sql, NamingHelper.toSQLName(domain)), null); - if (cursor.moveToFirst() && cursor.getInt(0) == 0) { - createTable(domain, sqLiteDatabase); + String tableName = NamingHelper.toSQLName(domain); + Cursor c = sqLiteDatabase.rawQuery(String.format(sql, tableName), null); + if (c.moveToFirst() && c.getInt(0) == 0) { + createTable(domain, sqLiteDatabase); + } else { + addColumns(domain, sqLiteDatabase); } } executeSugarUpgrade(sqLiteDatabase, oldVersion, newVersion); } + private ArrayList getColumnNames(SQLiteDatabase sqLiteDatabase, String tableName) { + Cursor resultsQuery = sqLiteDatabase.query(tableName, null, null, null, null, null, null); + //Check if columns match vs the one on the domain class + ArrayList columnNames = new ArrayList<>(); + for (int i = 0; i < resultsQuery.getColumnCount(); i++) { + String columnName = resultsQuery.getColumnName(i); + columnNames.add(columnName); + } + resultsQuery.close(); + return columnNames; + } + + public void deleteTables(SQLiteDatabase sqLiteDatabase) { List tables = getDomainClasses(context); for (Class table : tables) { @@ -116,6 +134,46 @@ private void executeScript(SQLiteDatabase db, String file) { Log.i(SUGAR, "Script executed"); } + private void addColumns(Class table, SQLiteDatabase sqLiteDatabase) { + + List fields = ReflectionUtil.getTableFields(table); + String tableName = NamingHelper.toSQLName(table); + ArrayList presentColumns = getColumnNames(sqLiteDatabase, tableName); + ArrayList alterCommands = new ArrayList<>(); + + for (Field column : fields) { + String columnName = NamingHelper.toSQLName(column); + String columnType = QueryBuilder.getColumnType(column.getType()); + + if (column.isAnnotationPresent(Column.class)) { + Column columnAnnotation = column.getAnnotation(Column.class); + columnName = columnAnnotation.name(); + } + + if (!presentColumns.contains(columnName)) { + StringBuilder sb = new StringBuilder("ALTER TABLE "); + sb.append(tableName).append(" ADD COLUMN ").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"); + } + + // Unique is not working on ALTER TABLE +// if (column.isAnnotationPresent(Unique.class)) { +// sb.append(" UNIQUE"); +// } + alterCommands.add(sb.toString()); + } + } + + for (String command : alterCommands) { + Log.i("Sugar", command); + sqLiteDatabase.execSQL(command); + } + } + protected String createTableSQL(Class table) { Log.i(SUGAR, "Create table if not exists"); List fields = ReflectionUtil.getTableFields(table); From 8156895bce9b56364b28baa34d2c1689779423d1 Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Mon, 14 Dec 2015 11:22:49 -0200 Subject: [PATCH 037/139] fix satyan/sugar#467 Sometimes Throwable doesn't have a message, so e.getMessage is NULL and generate a NullPointerException --- library/src/main/java/com/orm/util/ReflectionUtil.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index 9b27abdd..dd249817 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -273,7 +273,8 @@ private static Class getDomainClass(String className, Context context) { try { discoveredClass = Class.forName(className, true, context.getClass().getClassLoader()); } catch (Throwable e) { - Log.e("Sugar", e.getMessage()); + String error = (e.getMessage() == null) ? "getDomainClass " + className + " error" : e.getMessage(); + Log.e("Sugar", error); } if ((discoveredClass != null) && From fff7ae0fb7e130926b0550657afb8ad9894bf80f Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Mon, 14 Dec 2015 11:25:42 -0200 Subject: [PATCH 038/139] fix executeScript on SchemaGenerator When a migration script is empty, it will try to execute an empty SQL statement that will generate a RuntimeException Now, it only executes SQL statements that is not empty --- .../src/main/java/com/orm/SchemaGenerator.java | 4 +++- library/src/main/java/com/orm/SugarDb.java | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index 9e6601c9..f5fd4f2a 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -124,7 +124,9 @@ private void executeScript(SQLiteDatabase db, String file) { MigrationFileParser migrationFileParser = new MigrationFileParser(sb.toString()); for(String statement: migrationFileParser.getStatements()){ Log.i("Sugar script", statement); - db.execSQL(statement); + if (!statement.isEmpty()) { + db.execSQL(statement); + } } } catch (IOException e) { diff --git a/library/src/main/java/com/orm/SugarDb.java b/library/src/main/java/com/orm/SugarDb.java index a15110a3..56528f96 100644 --- a/library/src/main/java/com/orm/SugarDb.java +++ b/library/src/main/java/com/orm/SugarDb.java @@ -3,6 +3,7 @@ import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; import com.orm.util.ManifestHelper; import com.orm.util.SugarCursorFactory; @@ -14,6 +15,7 @@ public class SugarDb extends SQLiteOpenHelper { private final SchemaGenerator schemaGenerator; private SQLiteDatabase sqLiteDatabase; + private int openedConnections = 0; public SugarDb(Context context) { super(context, ManifestHelper.getDatabaseName(context), @@ -39,4 +41,20 @@ public synchronized SQLiteDatabase getDB() { return this.sqLiteDatabase; } + @Override + public synchronized SQLiteDatabase getReadableDatabase() { + Log.d("SUGAR", "getReadableDatabase"); + openedConnections++; + return super.getReadableDatabase(); + } + + @Override + public synchronized void close() { + Log.d("SUGAR", "getReadableDatabase"); + openedConnections--; + if(openedConnections == 0) { + Log.d("SUGAR", "closing"); + super.close(); + } + } } From 9f371b378975367a0e59b6c92a00a2de6fc44dbe Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Mon, 14 Dec 2015 12:45:16 -0200 Subject: [PATCH 039/139] update README, documentation is updated --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 62d5e8e0..aa4b2a99 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Insanely easy way to work with Android databases. -Official documentation can be found [here](http://satyan.github.io/sugar) **Outdated** - Check some examples below. The example application is provided in the **example** folder in the source. +Official documentation can be found [here](http://satyan.github.io/sugar) - Check some examples below. The example application is provided in the **example** folder in the source. ## Features From 7b210e1a98c242dc6c65ce415faca004c02bb5d9 Mon Sep 17 00:00:00 2001 From: Filip Lindqvist Date: Fri, 1 Jan 2016 21:04:30 +0100 Subject: [PATCH 040/139] Added retrolambda in classloader class lookup in ReflectionUtil.java --- library/src/main/java/com/orm/util/ReflectionUtil.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index dd249817..1458a308 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -306,7 +306,8 @@ private static List getAllClasses(Context context) throws PackageManager while (urls.hasMoreElements()) { List fileNames = new ArrayList(); String classDirectoryName = urls.nextElement().getFile(); - if (classDirectoryName.contains("bin") || classDirectoryName.contains("classes")) { + if (classDirectoryName.contains("bin") || classDirectoryName.contains("classes") + || classDirectoryName.contains("retrolambda")) { File classDirectory = new File(classDirectoryName); for (File filePath : classDirectory.listFiles()) { populateFiles(filePath, fileNames, ""); From 416df047dc8af185fc0b859a77e3ee506be7b0a0 Mon Sep 17 00:00:00 2001 From: Douglas Alves Date: Thu, 7 Jan 2016 11:03:44 -0200 Subject: [PATCH 041/139] Fixing migration to single dex after multidex. --- library/src/main/java/com/orm/util/MultiDexHelper.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/library/src/main/java/com/orm/util/MultiDexHelper.java b/library/src/main/java/com/orm/util/MultiDexHelper.java index 970cd3b7..87f7fb3a 100644 --- a/library/src/main/java/com/orm/util/MultiDexHelper.java +++ b/library/src/main/java/com/orm/util/MultiDexHelper.java @@ -64,9 +64,6 @@ public static List getSourcePaths(Context context) throws PackageManager if (extractedFile.isFile()) { sourcePaths.add(extractedFile.getAbsolutePath()); //we ignore the verify zip part - } else { - throw new IOException("Missing extracted secondary dex file '" + - extractedFile.getPath() + "'"); } } From 5690f92320980674389cac4669fec553f321dec5 Mon Sep 17 00:00:00 2001 From: Tevin Jeffrey Date: Tue, 12 Jan 2016 11:06:43 -0500 Subject: [PATCH 042/139] Added the @Target annotation for compile-time error checking. - The compiler will generate an error when used on the improper element type. --- library/src/main/java/com/orm/dsl/Column.java | 3 +++ library/src/main/java/com/orm/dsl/Ignore.java | 3 +++ library/src/main/java/com/orm/dsl/MultiUnique.java | 3 +++ library/src/main/java/com/orm/dsl/NotNull.java | 3 +++ library/src/main/java/com/orm/dsl/Table.java | 3 +++ library/src/main/java/com/orm/dsl/Unique.java | 3 +++ 6 files changed, 18 insertions(+) diff --git a/library/src/main/java/com/orm/dsl/Column.java b/library/src/main/java/com/orm/dsl/Column.java index 2c47e3c0..58e66529 100644 --- a/library/src/main/java/com/orm/dsl/Column.java +++ b/library/src/main/java/com/orm/dsl/Column.java @@ -1,9 +1,12 @@ package com.orm.dsl; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) public @interface Column { String name(); boolean unique() default false; diff --git a/library/src/main/java/com/orm/dsl/Ignore.java b/library/src/main/java/com/orm/dsl/Ignore.java index 9678d33a..e4d748f1 100644 --- a/library/src/main/java/com/orm/dsl/Ignore.java +++ b/library/src/main/java/com/orm/dsl/Ignore.java @@ -1,8 +1,11 @@ package com.orm.dsl; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) public @interface Ignore { } diff --git a/library/src/main/java/com/orm/dsl/MultiUnique.java b/library/src/main/java/com/orm/dsl/MultiUnique.java index 55f754a5..86c6e323 100644 --- a/library/src/main/java/com/orm/dsl/MultiUnique.java +++ b/library/src/main/java/com/orm/dsl/MultiUnique.java @@ -1,9 +1,12 @@ package com.orm.dsl; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) public @interface MultiUnique { String value(); } diff --git a/library/src/main/java/com/orm/dsl/NotNull.java b/library/src/main/java/com/orm/dsl/NotNull.java index b2361851..f71ebf54 100644 --- a/library/src/main/java/com/orm/dsl/NotNull.java +++ b/library/src/main/java/com/orm/dsl/NotNull.java @@ -1,8 +1,11 @@ package com.orm.dsl; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) public @interface NotNull { } diff --git a/library/src/main/java/com/orm/dsl/Table.java b/library/src/main/java/com/orm/dsl/Table.java index 0dfaa18b..9c64fa74 100644 --- a/library/src/main/java/com/orm/dsl/Table.java +++ b/library/src/main/java/com/orm/dsl/Table.java @@ -1,9 +1,12 @@ package com.orm.dsl; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) public @interface Table { String name() default ""; } diff --git a/library/src/main/java/com/orm/dsl/Unique.java b/library/src/main/java/com/orm/dsl/Unique.java index 7384b749..c05960ed 100644 --- a/library/src/main/java/com/orm/dsl/Unique.java +++ b/library/src/main/java/com/orm/dsl/Unique.java @@ -1,8 +1,11 @@ package com.orm.dsl; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) public @interface Unique { } From 5924594552d286dc550446c1c0fade4a63b85232 Mon Sep 17 00:00:00 2001 From: hellerve Date: Thu, 21 Jan 2016 17:14:44 +0100 Subject: [PATCH 043/139] added syntax highlighting in readme --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index aa4b2a99..9d7082cc 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ git clone git@github.com:satyan/sugar.git ``` include this in your **settings.gradle** -``` +```gradle include ':app' // your module app include ':sugar' @@ -76,7 +76,7 @@ sugar.dir=/path/to/sugar/library ``` add sugar project to the dependencies of your main project (build.gradle) -``` +```gradle dependencies { compile project(':sugar') } @@ -88,7 +88,7 @@ After installing, check out how to set up your first database and models [here]( ## Examples ### SugarRecord -``` +```java public class Book extends SugarRecord { @Unique String isbn; @@ -108,24 +108,24 @@ public class Book extends SugarRecord { } ``` or -``` +```java @Table public class Book { ... } ``` ### Save Entity -``` +```java Book book = new Book("isbn123", "Title here", "2nd edition") book.save(); ``` ### Load Entity -``` +```java Book book = Book.findById(Book.class, 1); ``` ### Update Entity -``` +```java Book book = Book.findById(Book.class, 1); book.title = "updated title here"; // modify the values book.edition = "3rd edition"; @@ -133,13 +133,13 @@ book.save(); // updates the previous entry with new values. ``` ### Delete Entity -``` +```java Book book = Book.findById(Book.class, 1); book.delete(); ``` ### Update Entity based on Unique values -``` +```java Book book = new Book("isbn123", "Title here", "2nd edition") book.save(); @@ -151,7 +151,7 @@ book.getId() == sameBook.getId(); // true ``` ### Bulk Insert -``` +```java List books = new ArrayList<>(); books.add(new Book("isbn123", "Title here", "2nd edition")) books.add(new Book("isbn456", "Title here 2", "3nd edition")) From fafe384bcf4cb47236fd561ba3a810e3c32a7327 Mon Sep 17 00:00:00 2001 From: enyciaa Date: Fri, 12 Feb 2016 14:53:15 +0000 Subject: [PATCH 044/139] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 9d7082cc..d6e56242 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,12 @@ books.add(new Book("isbn789", "Title here 3", "4nd edition")) SugarRecord.saveInTx(books); ``` +### When using ProGuard +```java +# Ensures entities remain un-obfuscated so table and columns are named correctly +-keep class com.yourpackage.yourapp.domainclasspackage.** { *; } +``` + ## [CHANGELOG](https://github.com/satyan/sugar/blob/master/CHANGELOG.md) ## Contributing From 473820ea5558c283062ef11038bbb59f20b2c031 Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Mon, 7 Mar 2016 16:33:31 -0300 Subject: [PATCH 045/139] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c3f700b..7015053d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Sugar Releases ## [Unreleased] + + +## v1.5 ### Added * [#328](https://github.com/satyan/sugar/pull/328) @jedid auto add new columns during database upgrade, fix [#299](https://github.com/satyan/sugar/issues/299) and [#151](https://github.com/satyan/sugar/issues/151) * [#389](https://github.com/satyan/sugar/pull/389) @alfmatos MultiUnique DSL to handle MultiColumn Unique Table constraint From 1ca7267e17887b60f9e8218dfaa2609eaf6f4d1e Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Mon, 7 Mar 2016 16:35:34 -0300 Subject: [PATCH 046/139] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d6e56242..f9ae8521 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ There are four ways to install Sugar: This is the preferred way. Simply add: ```groovy -compile 'com.github.satyan:sugar:1.4' +compile 'com.github.satyan:sugar:1.5' ``` to your project dependencies and run `gradle build` or `gradle assemble`. @@ -36,7 +36,7 @@ Declare the dependency in Maven: com.github.satyan sugar - 1.4 + 1.5 ``` From 784b3da6b7bc7f2a8692780e58c9166f2ac0c865 Mon Sep 17 00:00:00 2001 From: Reginald Tan Date: Fri, 18 Mar 2016 19:31:36 -0400 Subject: [PATCH 047/139] add save/update/delete usage for @Table annotations --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index f9ae8521..867fc7f8 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,11 @@ Book book = new Book("isbn123", "Title here", "2nd edition") book.save(); ``` +or +```java +SugarRecord.save(book); // if using the @Table annotation +``` + ### Load Entity ```java Book book = Book.findById(Book.class, 1); @@ -132,12 +137,18 @@ book.edition = "3rd edition"; book.save(); // updates the previous entry with new values. ``` + ### Delete Entity ```java Book book = Book.findById(Book.class, 1); book.delete(); ``` +or +```java +SugarRecord.delete(book); // if using the @Table annotation +``` + ### Update Entity based on Unique values ```java Book book = new Book("isbn123", "Title here", "2nd edition") @@ -150,6 +161,11 @@ sameBook.update(); book.getId() == sameBook.getId(); // true ``` +or +```java +SugarRecord.update(sameBook); // if using the @Table annotation +``` + ### Bulk Insert ```java List books = new ArrayList<>(); From fc285aca1e9bac4d7bc137daaa9bbd5ef5d5f6e6 Mon Sep 17 00:00:00 2001 From: frankweb Date: Wed, 23 Mar 2016 19:28:50 -0700 Subject: [PATCH 048/139] change the type of the args list to String --- library/src/main/java/com/orm/query/Select.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/library/src/main/java/com/orm/query/Select.java b/library/src/main/java/com/orm/query/Select.java index 69d9fc80..6b2b962c 100644 --- a/library/src/main/java/com/orm/query/Select.java +++ b/library/src/main/java/com/orm/query/Select.java @@ -6,6 +6,7 @@ import com.orm.util.NamingHelper; import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; import java.util.List; @@ -18,7 +19,7 @@ public class Select implements Iterable { private String groupBy = ""; private String limit = ""; private String offset = ""; - private List args = new ArrayList(); + private List args = new ArrayList(); private static final String SPACE =" "; private static final String SINGLE_QUOTE ="'"; private static final String LEFT_PARENTHESIS="("; @@ -95,7 +96,7 @@ private void mergeConditions(Condition[] conditions, Condition.Type type) { .append(condition.getProperty()) .append(condition.getCheckSymbol()) .append("? "); - args.add(condition.getValue()); + args.add(condition.getValue().toString()); } } @@ -190,14 +191,8 @@ String[] getArgs() { return convertArgs(args); } - private String[] convertArgs(List argsList) { - String[] argsArray = new String[argsList.size()]; - - for (int i = 0; i < argsList.size(); i++) { - argsArray[i] = argsList.get(i).toString(); - } - - return argsArray; + private String[] convertArgs(List argsList) { + return argsList.toArray(new String[argsList.size()]); } @Override From 619b5dbcfc3dfdf91bfdb140636477d137cd4bf1 Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Mon, 28 Mar 2016 18:11:23 -0300 Subject: [PATCH 049/139] adding codetriage to sugar --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 867fc7f8..fa8a2c14 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Sugar ORM [![Build Status](https://travis-ci.org/satyan/sugar.svg?branch=master)](https://travis-ci.org/satyan/sugar) [![Coverage Status](https://coveralls.io/repos/satyan/sugar/badge.svg?branch=master)](https://coveralls.io/r/satyan/sugar?branch=master) +# Sugar ORM [![Build Status](https://travis-ci.org/satyan/sugar.svg?branch=master)](https://travis-ci.org/satyan/sugar) [![Coverage Status](https://coveralls.io/repos/satyan/sugar/badge.svg?branch=master)](https://coveralls.io/r/satyan/sugar?branch=master) [![Code Triagers Badge](http://www.codetriage.com/satyan/sugar/badges/users.svg)](http://www.codetriage.com/satyan/sugar) [![Join the chat at https://gitter.im/satyan/sugar](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/satyan/sugar?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) From 941bd36666ae677ee1dbb5c5a39b407d83f12197 Mon Sep 17 00:00:00 2001 From: DALYKA Date: Tue, 29 Mar 2016 08:35:52 -0700 Subject: [PATCH 050/139] find works with true/false --- .../src/main/java/com/orm/SugarRecord.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 4b9e04ef..957e15fa 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -198,7 +198,11 @@ public static void executeQuery(String query, String... arguments) { } public static List find(Class type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) { - Cursor cursor = getSugarDataBase().query(NamingHelper.toSQLName(type), null, whereClause, whereArgs, + + String args[]; + args = (whereArgs == null) ? null : replaceArgs(whereArgs); + + Cursor cursor = getSugarDataBase().query(NamingHelper.toSQLName(type), null, whereClause, args, groupBy, null, orderBy, limit); return getEntitiesFromCursor(cursor, type); @@ -479,4 +483,17 @@ public void remove() { } } + public static String[] replaceArgs(String[] args){ + + String [] replace = new String[args.length]; + for (int i=0; i Date: Thu, 31 Mar 2016 11:11:30 -0300 Subject: [PATCH 051/139] removing iml from repo --- sugar.iml | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 sugar.iml diff --git a/sugar.iml b/sugar.iml deleted file mode 100644 index e19b5594..00000000 --- a/sugar.iml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file From ed4c8173301e61da6ab93a944892e319e6046f7e Mon Sep 17 00:00:00 2001 From: geracervantes Date: Thu, 31 Mar 2016 16:20:42 -0700 Subject: [PATCH 052/139] add sqlite keywords support --- library/src/main/java/com/orm/KeyWords.java | 142 ++++++++++++++++++ .../main/java/com/orm/SchemaGenerator.java | 6 + .../java/com/orm/SchemaGeneratorTest.java | 8 + .../test/java/com/orm/models/TRANSACTION.java | 12 ++ 4 files changed, 168 insertions(+) create mode 100644 library/src/main/java/com/orm/KeyWords.java create mode 100644 library/src/test/java/com/orm/models/TRANSACTION.java diff --git a/library/src/main/java/com/orm/KeyWords.java b/library/src/main/java/com/orm/KeyWords.java new file mode 100644 index 00000000..14ea5fbc --- /dev/null +++ b/library/src/main/java/com/orm/KeyWords.java @@ -0,0 +1,142 @@ +package com.orm; + +import java.util.ArrayList; + +/** + * Created by root on 29/03/16. + */ +public class KeyWords { + public ArrayList rWords = new ArrayList(); + public boolean ReserverWords(String word){ + rWords.add(""); + rWords.add("ABORT"); + rWords.add("ACTION"); + rWords.add("ADD"); + rWords.add("AFTER"); + rWords.add("ALTER"); + rWords.add("ANALYZE"); + rWords.add("AND"); + rWords.add("AS"); + rWords.add("ASC"); + rWords.add("ATTACH"); + rWords.add("AUTOINCREMENT"); + rWords.add("BEFORE"); + rWords.add("BEGIN"); + rWords.add("BETWEEN"); + rWords.add("BY"); + rWords.add("CASCADE"); + rWords.add("CASE"); + rWords.add("CAST"); + rWords.add("CHECK"); + rWords.add("COLLATE"); + rWords.add("COLUMN"); + rWords.add("COMMIT"); + rWords.add("CONFLICT"); + rWords.add("CONSTRAINT"); + rWords.add("CREATE"); + rWords.add("CROSS"); + rWords.add("CURRENT_DATE"); + rWords.add("CURRENT_TIME"); + rWords.add("CURRENT_TIMESTAMP"); + rWords.add("DATABASE"); + rWords.add("DEFAULT"); + rWords.add("DEFERRABLE"); + rWords.add("DEFERRED"); + rWords.add("DELETE"); + rWords.add("DESC"); + rWords.add("DETACH"); + rWords.add("DISTINCT"); + rWords.add("DROP"); + rWords.add("EACH"); + rWords.add("ELSE"); + rWords.add("END"); + rWords.add("ESCAPE"); + rWords.add("EXCEPT"); + rWords.add("EXCLUSIVE"); + rWords.add("EXISTS"); + rWords.add("EXPLAIN"); + rWords.add("FAIL"); + rWords.add("FOR"); + rWords.add("FOREIGN"); + rWords.add("FROM"); + rWords.add("FULL"); + rWords.add("GLOB"); + rWords.add("GROUP"); + rWords.add("HAVING"); + rWords.add("IF"); + rWords.add("IGNORE"); + rWords.add("IMMEDIATE"); + rWords.add("IN"); + rWords.add("INDEX"); + rWords.add("INDEXED"); + rWords.add("INITIALLY"); + rWords.add("INNER"); + rWords.add("INSERT"); + rWords.add("INSTEAD"); + rWords.add("INTERSECT"); + rWords.add("INTO"); + rWords.add("IS"); + rWords.add("ISNULL"); + rWords.add("JOIN"); + rWords.add("KEY"); + rWords.add("LEFT"); + rWords.add("LIKE"); + rWords.add("LIMIT"); + rWords.add("MATCH"); + rWords.add("NATURAL"); + rWords.add("NO"); + rWords.add("NOT"); + rWords.add("NOTNULL"); + rWords.add("NULL"); + rWords.add("OF"); + rWords.add("OFFSET"); + rWords.add("ON"); + rWords.add("OR"); + rWords.add("ORDER"); + rWords.add("OUTER"); + rWords.add("PLAN"); + rWords.add("PRAGMA"); + rWords.add("PRIMARY"); + rWords.add("QUERY"); + rWords.add("RAISE"); + rWords.add("RECURSIVE"); + rWords.add("REFERENCES"); + rWords.add("REGEXP"); + rWords.add("REINDEX"); + rWords.add("RELEASE"); + rWords.add("RENAME"); + rWords.add("REPLACE"); + rWords.add("RESTRICT"); + rWords.add("RIGHT"); + rWords.add("ROLLBACK"); + rWords.add("ROW"); + rWords.add("SAVEPOINT"); + rWords.add("SELECT"); + rWords.add("SET"); + rWords.add("TABLE"); + rWords.add("TEMP"); + rWords.add("TEMPORARY"); + rWords.add("THEN"); + rWords.add("TO"); + rWords.add("TRANSACTION"); + rWords.add("TRIGGER"); + rWords.add("UNION"); + rWords.add("UNIQUE"); + rWords.add("UPDATE"); + rWords.add("USING"); + rWords.add("VACUUM"); + rWords.add("VALUES"); + rWords.add("VIEW"); + rWords.add("VIRTUAL"); + rWords.add("WHEN"); + rWords.add("WHERE"); + rWords.add("WITH"); + rWords.add("WITHOUT"); + if(rWords.contains(word)) + return true; + else + return false; + } +} + + diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index f5fd4f2a..2e4bfdc6 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -177,9 +177,15 @@ private void addColumns(Class table, SQLiteDatabase sqLiteDatabase) { } protected String createTableSQL(Class table) { + KeyWord link = new KeyWord; Log.i(SUGAR, "Create table if not exists"); List fields = ReflectionUtil.getTableFields(table); String tableName = NamingHelper.toSQLName(table); + if(link.ReservedWords(tableName)) + { + return "ERROR, SQLITE KEYWORD USED IN " +tableName; + + } StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS "); sb.append(tableName).append(" ( ID INTEGER PRIMARY KEY AUTOINCREMENT "); diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index ddef0e3f..80a8c413 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -2,6 +2,7 @@ import com.orm.dsl.Table; import com.orm.models.EmptyModel; +import com.orm.models.TRANSACTION; import com.orm.models.IntUniqueModel; import com.orm.models.MultiColumnUniqueModel; import com.orm.models.StringFieldAnnotatedModel; @@ -74,4 +75,11 @@ public void testMultiColumnUniqueTableCreation() { "UNIQUE(A, B) ON CONFLICT REPLACE ) ", createSQL); } + @Test + public void testKeyWordModel(){ + SchemaGenerator schemaGenerator = new SchemaGenerator(new DummyContext() ); + String createSQL = schemaGenerator.createTableSQL(TRANSACTION.class); + assertEquals("ERROR, SQLITE KEYWORD USED IN "+NamingHelper.toSQLName(TRANSACTION.class),createSQL); + + } } diff --git a/library/src/test/java/com/orm/models/TRANSACTION.java b/library/src/test/java/com/orm/models/TRANSACTION.java new file mode 100644 index 00000000..1fc87eec --- /dev/null +++ b/library/src/test/java/com/orm/models/TRANSACTION.java @@ -0,0 +1,12 @@ +package com.orm.models; + +import com.orm.SugarRecord; + +/** + * Created by sibelius on 02/12/15. + */ +public class TRANSACTION extends SugarRecord { + public TRANSACTION() { + + } +} From 8b8005718a43bd242802f954649fe87cc5f3ebf1 Mon Sep 17 00:00:00 2001 From: geracervantes Date: Thu, 31 Mar 2016 17:37:50 -0700 Subject: [PATCH 053/139] fix error --- library/src/main/java/com/orm/SchemaGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index 2e4bfdc6..06a901a4 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -177,7 +177,7 @@ private void addColumns(Class table, SQLiteDatabase sqLiteDatabase) { } protected String createTableSQL(Class table) { - KeyWord link = new KeyWord; + KeyWord link = new KeyWord(); Log.i(SUGAR, "Create table if not exists"); List fields = ReflectionUtil.getTableFields(table); String tableName = NamingHelper.toSQLName(table); From 37bef8ff46b87d4e74bc1bbe1742f84b8dcef24c Mon Sep 17 00:00:00 2001 From: geracervantes Date: Thu, 31 Mar 2016 18:05:56 -0700 Subject: [PATCH 054/139] fix error --- library/src/main/java/com/orm/SchemaGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index 06a901a4..730b24bc 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -177,7 +177,7 @@ private void addColumns(Class table, SQLiteDatabase sqLiteDatabase) { } protected String createTableSQL(Class table) { - KeyWord link = new KeyWord(); + KeyWords link = new KeyWords(); Log.i(SUGAR, "Create table if not exists"); List fields = ReflectionUtil.getTableFields(table); String tableName = NamingHelper.toSQLName(table); From a0dc1cc1d6a92555b14f2beecf7565735f1368b8 Mon Sep 17 00:00:00 2001 From: geracervantes Date: Sat, 2 Apr 2016 12:16:06 -0700 Subject: [PATCH 055/139] change list and output --- library/src/main/java/com/orm/KeyWords.java | 265 +++++++++--------- .../main/java/com/orm/SchemaGenerator.java | 5 +- .../java/com/orm/SchemaGeneratorTest.java | 9 - .../test/java/com/orm/models/TRANSACTION.java | 12 - 4 files changed, 135 insertions(+), 156 deletions(-) delete mode 100644 library/src/test/java/com/orm/models/TRANSACTION.java diff --git a/library/src/main/java/com/orm/KeyWords.java b/library/src/main/java/com/orm/KeyWords.java index 14ea5fbc..14ab2398 100644 --- a/library/src/main/java/com/orm/KeyWords.java +++ b/library/src/main/java/com/orm/KeyWords.java @@ -1,142 +1,143 @@ package com.orm; -import java.util.ArrayList; + +import java.util.HashSet; +import java.util.Set; /** * Created by root on 29/03/16. */ public class KeyWords { - public ArrayList rWords = new ArrayList(); - public boolean ReserverWords(String word){ - rWords.add(""); - rWords.add("ABORT"); - rWords.add("ACTION"); - rWords.add("ADD"); - rWords.add("AFTER"); - rWords.add("ALTER"); - rWords.add("ANALYZE"); - rWords.add("AND"); - rWords.add("AS"); - rWords.add("ASC"); - rWords.add("ATTACH"); - rWords.add("AUTOINCREMENT"); - rWords.add("BEFORE"); - rWords.add("BEGIN"); - rWords.add("BETWEEN"); - rWords.add("BY"); - rWords.add("CASCADE"); - rWords.add("CASE"); - rWords.add("CAST"); - rWords.add("CHECK"); - rWords.add("COLLATE"); - rWords.add("COLUMN"); - rWords.add("COMMIT"); - rWords.add("CONFLICT"); - rWords.add("CONSTRAINT"); - rWords.add("CREATE"); - rWords.add("CROSS"); - rWords.add("CURRENT_DATE"); - rWords.add("CURRENT_TIME"); - rWords.add("CURRENT_TIMESTAMP"); - rWords.add("DATABASE"); - rWords.add("DEFAULT"); - rWords.add("DEFERRABLE"); - rWords.add("DEFERRED"); - rWords.add("DELETE"); - rWords.add("DESC"); - rWords.add("DETACH"); - rWords.add("DISTINCT"); - rWords.add("DROP"); - rWords.add("EACH"); - rWords.add("ELSE"); - rWords.add("END"); - rWords.add("ESCAPE"); - rWords.add("EXCEPT"); - rWords.add("EXCLUSIVE"); - rWords.add("EXISTS"); - rWords.add("EXPLAIN"); - rWords.add("FAIL"); - rWords.add("FOR"); - rWords.add("FOREIGN"); - rWords.add("FROM"); - rWords.add("FULL"); - rWords.add("GLOB"); - rWords.add("GROUP"); - rWords.add("HAVING"); - rWords.add("IF"); - rWords.add("IGNORE"); - rWords.add("IMMEDIATE"); - rWords.add("IN"); - rWords.add("INDEX"); - rWords.add("INDEXED"); - rWords.add("INITIALLY"); - rWords.add("INNER"); - rWords.add("INSERT"); - rWords.add("INSTEAD"); - rWords.add("INTERSECT"); - rWords.add("INTO"); - rWords.add("IS"); - rWords.add("ISNULL"); - rWords.add("JOIN"); - rWords.add("KEY"); - rWords.add("LEFT"); - rWords.add("LIKE"); - rWords.add("LIMIT"); - rWords.add("MATCH"); - rWords.add("NATURAL"); - rWords.add("NO"); - rWords.add("NOT"); - rWords.add("NOTNULL"); - rWords.add("NULL"); - rWords.add("OF"); - rWords.add("OFFSET"); - rWords.add("ON"); - rWords.add("OR"); - rWords.add("ORDER"); - rWords.add("OUTER"); - rWords.add("PLAN"); - rWords.add("PRAGMA"); - rWords.add("PRIMARY"); - rWords.add("QUERY"); - rWords.add("RAISE"); - rWords.add("RECURSIVE"); - rWords.add("REFERENCES"); - rWords.add("REGEXP"); - rWords.add("REINDEX"); - rWords.add("RELEASE"); - rWords.add("RENAME"); - rWords.add("REPLACE"); - rWords.add("RESTRICT"); - rWords.add("RIGHT"); - rWords.add("ROLLBACK"); - rWords.add("ROW"); - rWords.add("SAVEPOINT"); - rWords.add("SELECT"); - rWords.add("SET"); - rWords.add("TABLE"); - rWords.add("TEMP"); - rWords.add("TEMPORARY"); - rWords.add("THEN"); - rWords.add("TO"); - rWords.add("TRANSACTION"); - rWords.add("TRIGGER"); - rWords.add("UNION"); - rWords.add("UNIQUE"); - rWords.add("UPDATE"); - rWords.add("USING"); - rWords.add("VACUUM"); - rWords.add("VALUES"); - rWords.add("VIEW"); - rWords.add("VIRTUAL"); - rWords.add("WHEN"); - rWords.add("WHERE"); - rWords.add("WITH"); - rWords.add("WITHOUT"); - if(rWords.contains(word)) - return true; - else - return false; + private static Set reservedWords; + static{ + reservedWords = new HashSet<>(); + reservedWords.add(""); + reservedWords.add("ABORT"); + reservedWords.add("ACTION"); + reservedWords.add("ADD"); + reservedWords.add("AFTER"); + reservedWords.add("ALTER"); + reservedWords.add("ANALYZE"); + reservedWords.add("AND"); + reservedWords.add("AS"); + reservedWords.add("ASC"); + reservedWords.add("ATTACH"); + reservedWords.add("AUTOINCREMENT"); + reservedWords.add("BEFORE"); + reservedWords.add("BEGIN"); + reservedWords.add("BETWEEN"); + reservedWords.add("BY"); + reservedWords.add("CASCADE"); + reservedWords.add("CASE"); + reservedWords.add("CAST"); + reservedWords.add("CHECK"); + reservedWords.add("COLLATE"); + reservedWords.add("COLUMN"); + reservedWords.add("COMMIT"); + reservedWords.add("CONFLICT"); + reservedWords.add("CONSTRAINT"); + reservedWords.add("CREATE"); + reservedWords.add("CROSS"); + reservedWords.add("CURRENT_DATE"); + reservedWords.add("CURRENT_TIME"); + reservedWords.add("CURRENT_TIMESTAMP"); + reservedWords.add("DATABASE"); + reservedWords.add("DEFAULT"); + reservedWords.add("DEFERRABLE"); + reservedWords.add("DEFERRED"); + reservedWords.add("DELETE"); + reservedWords.add("DESC"); + reservedWords.add("DETACH"); + reservedWords.add("DISTINCT"); + reservedWords.add("DROP"); + reservedWords.add("EACH"); + reservedWords.add("ELSE"); + reservedWords.add("END"); + reservedWords.add("ESCAPE"); + reservedWords.add("EXCEPT"); + reservedWords.add("EXCLUSIVE"); + reservedWords.add("EXISTS"); + reservedWords.add("EXPLAIN"); + reservedWords.add("FAIL"); + reservedWords.add("FOR"); + reservedWords.add("FOREIGN"); + reservedWords.add("FROM"); + reservedWords.add("FULL"); + reservedWords.add("GLOB"); + reservedWords.add("GROUP"); + reservedWords.add("HAVING"); + reservedWords.add("IF"); + reservedWords.add("IGNORE"); + reservedWords.add("IMMEDIATE"); + reservedWords.add("IN"); + reservedWords.add("INDEX"); + reservedWords.add("INDEXED"); + reservedWords.add("INITIALLY"); + reservedWords.add("INNER"); + reservedWords.add("INSERT"); + reservedWords.add("INSTEAD"); + reservedWords.add("INTERSECT"); + reservedWords.add("INTO"); + reservedWords.add("IS"); + reservedWords.add("ISNULL"); + reservedWords.add("JOIN"); + reservedWords.add("KEY"); + reservedWords.add("LEFT"); + reservedWords.add("LIKE"); + reservedWords.add("LIMIT"); + reservedWords.add("MATCH"); + reservedWords.add("NATURAL"); + reservedWords.add("NO"); + reservedWords.add("NOT"); + reservedWords.add("NOTNULL"); + reservedWords.add("NULL"); + reservedWords.add("OF"); + reservedWords.add("OFFSET"); + reservedWords.add("ON"); + reservedWords.add("OR"); + reservedWords.add("ORDER"); + reservedWords.add("OUTER"); + reservedWords.add("PLAN"); + reservedWords.add("PRAGMA"); + reservedWords.add("PRIMARY"); + reservedWords.add("QUERY"); + reservedWords.add("RAISE"); + reservedWords.add("RECURSIVE"); + reservedWords.add("REFERENCES"); + reservedWords.add("REGEXP"); + reservedWords.add("REINDEX"); + reservedWords.add("RELEASE"); + reservedWords.add("RENAME"); + reservedWords.add("REPLACE"); + reservedWords.add("RESTRICT"); + reservedWords.add("RIGHT"); + reservedWords.add("ROLLBACK"); + reservedWords.add("ROW"); + reservedWords.add("SAVEPOINT"); + reservedWords.add("SELECT"); + reservedWords.add("SET"); + reservedWords.add("TABLE"); + reservedWords.add("TEMP"); + reservedWords.add("TEMPORARY"); + reservedWords.add("THEN"); + reservedWords.add("TO"); + reservedWords.add("TRANSACTION"); + reservedWords.add("TRIGGER"); + reservedWords.add("UNION"); + reservedWords.add("UNIQUE"); + reservedWords.add("UPDATE"); + reservedWords.add("USING"); + reservedWords.add("VACUUM"); + reservedWords.add("VALUES"); + reservedWords.add("VIEW"); + reservedWords.add("VIRTUAL"); + reservedWords.add("WHEN"); + reservedWords.add("WHERE"); + reservedWords.add("WITH"); + reservedWords.add("WITHOUT");} + + public boolean isaReservedWords(String word){ + return reservedWords.contains(word); } } - diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index 730b24bc..628ef169 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -181,10 +181,9 @@ protected String createTableSQL(Class table) { Log.i(SUGAR, "Create table if not exists"); List fields = ReflectionUtil.getTableFields(table); String tableName = NamingHelper.toSQLName(table); - if(link.ReservedWords(tableName)) + if(link.isaReservedWords(tableName)) { - return "ERROR, SQLITE KEYWORD USED IN " +tableName; - + Log.i(SUGAR,"ERROR, SQLITE RESERVED WORD USED IN " + tableName); } StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS "); sb.append(tableName).append(" ( ID INTEGER PRIMARY KEY AUTOINCREMENT "); diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index 80a8c413..24bae7bc 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -1,8 +1,6 @@ package com.orm; -import com.orm.dsl.Table; import com.orm.models.EmptyModel; -import com.orm.models.TRANSACTION; import com.orm.models.IntUniqueModel; import com.orm.models.MultiColumnUniqueModel; import com.orm.models.StringFieldAnnotatedModel; @@ -75,11 +73,4 @@ public void testMultiColumnUniqueTableCreation() { "UNIQUE(A, B) ON CONFLICT REPLACE ) ", createSQL); } - @Test - public void testKeyWordModel(){ - SchemaGenerator schemaGenerator = new SchemaGenerator(new DummyContext() ); - String createSQL = schemaGenerator.createTableSQL(TRANSACTION.class); - assertEquals("ERROR, SQLITE KEYWORD USED IN "+NamingHelper.toSQLName(TRANSACTION.class),createSQL); - - } } diff --git a/library/src/test/java/com/orm/models/TRANSACTION.java b/library/src/test/java/com/orm/models/TRANSACTION.java deleted file mode 100644 index 1fc87eec..00000000 --- a/library/src/test/java/com/orm/models/TRANSACTION.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.orm.models; - -import com.orm.SugarRecord; - -/** - * Created by sibelius on 02/12/15. - */ -public class TRANSACTION extends SugarRecord { - public TRANSACTION() { - - } -} From b9d826cd95a9fae99176e8bd305032de679224ed Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 2 Apr 2016 17:32:05 -0300 Subject: [PATCH 056/139] Make SugarContext getSugarDb public Add className as String to improve code readability --- library/src/main/java/com/orm/SugarContext.java | 2 +- .../main/java/com/orm/SugarTransactionHelper.java | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/library/src/main/java/com/orm/SugarContext.java b/library/src/main/java/com/orm/SugarContext.java index 2f653f5e..66bf4df0 100644 --- a/library/src/main/java/com/orm/SugarContext.java +++ b/library/src/main/java/com/orm/SugarContext.java @@ -47,7 +47,7 @@ private void doTerminate() { } } - protected SugarDb getSugarDb() { + public SugarDb getSugarDb() { return sugarDb; } diff --git a/library/src/main/java/com/orm/SugarTransactionHelper.java b/library/src/main/java/com/orm/SugarTransactionHelper.java index 7c00812c..74054cb3 100644 --- a/library/src/main/java/com/orm/SugarTransactionHelper.java +++ b/library/src/main/java/com/orm/SugarTransactionHelper.java @@ -4,21 +4,21 @@ import android.util.Log; public class SugarTransactionHelper { + private static final String LOG_TAG = SugarTransactionHelper.class.getSimpleName(); public static void doInTransaction(SugarTransactionHelper.Callback callback) { SQLiteDatabase database = SugarContext.getSugarContext().getSugarDb().getDB(); database.beginTransaction(); try { - Log.d(SugarTransactionHelper.class.getSimpleName(), - "Callback executing within transaction"); + Log.d(LOG_TAG, "Callback executing within transaction"); + callback.manipulateInTransaction(); database.setTransactionSuccessful(); - Log.d(SugarTransactionHelper.class.getSimpleName(), - "Callback successfully executed within transaction"); + + Log.d(LOG_TAG, "Callback successfully executed within transaction"); } catch (Throwable e) { - Log.d(SugarTransactionHelper.class.getSimpleName(), - "Could execute callback within transaction", e); + Log.d(LOG_TAG, "Could execute callback within transaction", e); } finally { database.endTransaction(); } From ea76217cee094aaaed8d91a6e4ee6b1089882721 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 2 Apr 2016 17:38:46 -0300 Subject: [PATCH 057/139] Changes in Select.java: - private static final fields goes on top of class - Delete unused import - Delete arguments that can be inferred from Generic class --- .../src/main/java/com/orm/query/Select.java | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/library/src/main/java/com/orm/query/Select.java b/library/src/main/java/com/orm/query/Select.java index 6b2b962c..83f7102b 100644 --- a/library/src/main/java/com/orm/query/Select.java +++ b/library/src/main/java/com/orm/query/Select.java @@ -6,11 +6,20 @@ import com.orm.util.NamingHelper; import java.util.ArrayList; -import java.util.Arrays; import java.util.Iterator; import java.util.List; public class Select implements Iterable { + private static final String SPACE = " "; + private static final String SINGLE_QUOTE = "'"; + private static final String LEFT_PARENTHESIS = "("; + private static final String RIGHT_PARENTHESIS = ")"; + private static final String SELECT_FROM = "SELECT * FROM "; + private static final String WHERE = "WHERE "; + private static final String ORDER_BY = "ORDER BY "; + private static final String GROUP_BY = "GROUP BY "; + private static final String LIMIT = "LIMIT "; + private static final String OFFSET = "OFFSET "; private Class record; private String[] arguments; @@ -19,24 +28,14 @@ public class Select implements Iterable { private String groupBy = ""; private String limit = ""; private String offset = ""; - private List args = new ArrayList(); - private static final String SPACE =" "; - private static final String SINGLE_QUOTE ="'"; - private static final String LEFT_PARENTHESIS="("; - private static final String RIGHT_PARENTHESIS=")"; - private static final String SELECT_FROM="SELECT * FROM "; - private static final String WHERE="WHERE "; - private static final String ORDER_BY ="ORDER BY "; - private static final String GROUP_BY ="GROUP BY "; - private static final String LIMIT ="LIMIT "; - private static final String OFFSET ="OFFSET "; + private List args = new ArrayList<>(); public Select(Class record) { this.record = record; } public static Select from(Class record) { - return new Select(record); + return new Select<>(record); } public Select orderBy(String prop) { From 4a5cbb5e5d2bf65ba3cc33580249ba187362c71e Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 3 Apr 2016 22:16:39 -0300 Subject: [PATCH 058/139] General project update: - Updated build.gradle files, buildTools from version 23.0.2 to 23.0.3 - Updated gradle.properties file to add gradle daemon support for faster builds - Deleted Collection class because it was unused in the project - Added ContextUtil class which handles everything Context related - Updated code in KeyWords class, now it's called KeyWordUtil and it's located in util package - Updated ManifestHelper methods in order to use the ContextUtil class to get the Context and not as method param. - Updated MultiDexHelper methods in order to use the ContextUtil class to get the Context and not as method param. - Minimal change in NamingHelper class - Updated ReflectionUtil class in order to use the ContextUtil, deleted some arguments that can be inferred from Generic class - Updated SugarConfig in order to delete some arguments that can be inferred from Generic class - Updated SchemaGenerator in oder to use the ContextUtil class to get the context, the constructor is now private, and you can call getInstance method - Updated SugarApp, deleted unused import - Updated SugarContext, add ContextUtil.init to set the Context - Updated SugarDb added getInstance method, SugarDb constructor is private, Added ContextUtil calls - Updated SugarRecord, deleted className that can be inferred from Generic class - Modify test to support SchemaGenerator.getInstance and another minimal modifications --- example/build.gradle | 2 +- .../src/main/java/com/example/ClientApp.java | 1 + gradle.properties | 1 + library/build.gradle | 2 +- library/src/main/java/com/orm/KeyWords.java | 143 ------------------ .../main/java/com/orm/SchemaGenerator.java | 30 ++-- library/src/main/java/com/orm/SugarApp.java | 2 - .../src/main/java/com/orm/SugarContext.java | 10 +- library/src/main/java/com/orm/SugarDb.java | 24 +-- .../src/main/java/com/orm/SugarRecord.java | 16 +- .../main/java/com/orm/util/Collection.java | 40 ----- .../main/java/com/orm/util/ContextUtil.java | 48 ++++++ .../main/java/com/orm/util/KeyWordUtil.java | 37 +++++ .../java/com/orm/util/ManifestHelper.java | 60 ++++---- .../java/com/orm/util/MultiDexHelper.java | 39 +++-- .../main/java/com/orm/util/NamingHelper.java | 2 +- .../java/com/orm/util/ReflectionUtil.java | 53 +++---- .../main/java/com/orm/util/SugarConfig.java | 5 +- .../main/java/com/orm/util/SugarCursor.java | 1 + .../java/com/orm/util/SugarCursorFactory.java | 1 - .../java/com/orm/SchemaGeneratorTest.java | 14 +- .../test/java/com/orm/models/EmptyModel.java | 4 +- .../java/com/orm/models/IntUniqueModel.java | 5 +- .../orm/models/MultiColumnUniqueModel.java | 3 +- .../orm/models/StringFieldAnnotatedModel.java | 4 +- .../orm/models/StringFieldExtendedModel.java | 4 +- ...ringFieldExtendedModelAnnotatedColumn.java | 4 +- .../java/com/orm/query/QueryBuilderTests.java | 1 + .../test/java/com/orm/query/TestRecord.java | 3 +- .../com/orm/util/MigrationFileParserTest.java | 14 +- 30 files changed, 235 insertions(+), 338 deletions(-) delete mode 100644 library/src/main/java/com/orm/KeyWords.java delete mode 100644 library/src/main/java/com/orm/util/Collection.java create mode 100644 library/src/main/java/com/orm/util/ContextUtil.java create mode 100644 library/src/main/java/com/orm/util/KeyWordUtil.java diff --git a/example/build.gradle b/example/build.gradle index 084b9d2d..15ba8d9c 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -1,7 +1,7 @@ apply plugin: 'com.android.application' android { compileSdkVersion 23 - buildToolsVersion "23.0.2" + buildToolsVersion "23.0.3" defaultConfig { applicationId "com.example" diff --git a/example/src/main/java/com/example/ClientApp.java b/example/src/main/java/com/example/ClientApp.java index e546e11d..6637ea26 100644 --- a/example/src/main/java/com/example/ClientApp.java +++ b/example/src/main/java/com/example/ClientApp.java @@ -5,6 +5,7 @@ import com.orm.SugarContext; public class ClientApp extends Application { + @Override public void onCreate() { super.onCreate(); diff --git a/gradle.properties b/gradle.properties index b1d9b965..35b4c26f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,6 +16,7 @@ # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true +org.gradle.daemon=true VERSION_NAME=2 VERSION_CODE=2 diff --git a/library/build.gradle b/library/build.gradle index ac7ffb66..5c523c6f 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -3,7 +3,7 @@ apply from: '../maven_push.gradle' android { compileSdkVersion 23 - buildToolsVersion "23.0.2" + buildToolsVersion "23.0.3" defaultConfig { minSdkVersion 9 diff --git a/library/src/main/java/com/orm/KeyWords.java b/library/src/main/java/com/orm/KeyWords.java deleted file mode 100644 index 14ab2398..00000000 --- a/library/src/main/java/com/orm/KeyWords.java +++ /dev/null @@ -1,143 +0,0 @@ -package com.orm; - - -import java.util.HashSet; -import java.util.Set; - -/** - * Created by root on 29/03/16. - */ -public class KeyWords { - private static Set reservedWords; - static{ - reservedWords = new HashSet<>(); - reservedWords.add(""); - reservedWords.add("ABORT"); - reservedWords.add("ACTION"); - reservedWords.add("ADD"); - reservedWords.add("AFTER"); - reservedWords.add("ALTER"); - reservedWords.add("ANALYZE"); - reservedWords.add("AND"); - reservedWords.add("AS"); - reservedWords.add("ASC"); - reservedWords.add("ATTACH"); - reservedWords.add("AUTOINCREMENT"); - reservedWords.add("BEFORE"); - reservedWords.add("BEGIN"); - reservedWords.add("BETWEEN"); - reservedWords.add("BY"); - reservedWords.add("CASCADE"); - reservedWords.add("CASE"); - reservedWords.add("CAST"); - reservedWords.add("CHECK"); - reservedWords.add("COLLATE"); - reservedWords.add("COLUMN"); - reservedWords.add("COMMIT"); - reservedWords.add("CONFLICT"); - reservedWords.add("CONSTRAINT"); - reservedWords.add("CREATE"); - reservedWords.add("CROSS"); - reservedWords.add("CURRENT_DATE"); - reservedWords.add("CURRENT_TIME"); - reservedWords.add("CURRENT_TIMESTAMP"); - reservedWords.add("DATABASE"); - reservedWords.add("DEFAULT"); - reservedWords.add("DEFERRABLE"); - reservedWords.add("DEFERRED"); - reservedWords.add("DELETE"); - reservedWords.add("DESC"); - reservedWords.add("DETACH"); - reservedWords.add("DISTINCT"); - reservedWords.add("DROP"); - reservedWords.add("EACH"); - reservedWords.add("ELSE"); - reservedWords.add("END"); - reservedWords.add("ESCAPE"); - reservedWords.add("EXCEPT"); - reservedWords.add("EXCLUSIVE"); - reservedWords.add("EXISTS"); - reservedWords.add("EXPLAIN"); - reservedWords.add("FAIL"); - reservedWords.add("FOR"); - reservedWords.add("FOREIGN"); - reservedWords.add("FROM"); - reservedWords.add("FULL"); - reservedWords.add("GLOB"); - reservedWords.add("GROUP"); - reservedWords.add("HAVING"); - reservedWords.add("IF"); - reservedWords.add("IGNORE"); - reservedWords.add("IMMEDIATE"); - reservedWords.add("IN"); - reservedWords.add("INDEX"); - reservedWords.add("INDEXED"); - reservedWords.add("INITIALLY"); - reservedWords.add("INNER"); - reservedWords.add("INSERT"); - reservedWords.add("INSTEAD"); - reservedWords.add("INTERSECT"); - reservedWords.add("INTO"); - reservedWords.add("IS"); - reservedWords.add("ISNULL"); - reservedWords.add("JOIN"); - reservedWords.add("KEY"); - reservedWords.add("LEFT"); - reservedWords.add("LIKE"); - reservedWords.add("LIMIT"); - reservedWords.add("MATCH"); - reservedWords.add("NATURAL"); - reservedWords.add("NO"); - reservedWords.add("NOT"); - reservedWords.add("NOTNULL"); - reservedWords.add("NULL"); - reservedWords.add("OF"); - reservedWords.add("OFFSET"); - reservedWords.add("ON"); - reservedWords.add("OR"); - reservedWords.add("ORDER"); - reservedWords.add("OUTER"); - reservedWords.add("PLAN"); - reservedWords.add("PRAGMA"); - reservedWords.add("PRIMARY"); - reservedWords.add("QUERY"); - reservedWords.add("RAISE"); - reservedWords.add("RECURSIVE"); - reservedWords.add("REFERENCES"); - reservedWords.add("REGEXP"); - reservedWords.add("REINDEX"); - reservedWords.add("RELEASE"); - reservedWords.add("RENAME"); - reservedWords.add("REPLACE"); - reservedWords.add("RESTRICT"); - reservedWords.add("RIGHT"); - reservedWords.add("ROLLBACK"); - reservedWords.add("ROW"); - reservedWords.add("SAVEPOINT"); - reservedWords.add("SELECT"); - reservedWords.add("SET"); - reservedWords.add("TABLE"); - reservedWords.add("TEMP"); - reservedWords.add("TEMPORARY"); - reservedWords.add("THEN"); - reservedWords.add("TO"); - reservedWords.add("TRANSACTION"); - reservedWords.add("TRIGGER"); - reservedWords.add("UNION"); - reservedWords.add("UNIQUE"); - reservedWords.add("UPDATE"); - reservedWords.add("USING"); - reservedWords.add("VACUUM"); - reservedWords.add("VALUES"); - reservedWords.add("VIEW"); - reservedWords.add("VIRTUAL"); - reservedWords.add("WHEN"); - reservedWords.add("WHERE"); - reservedWords.add("WITH"); - reservedWords.add("WITHOUT");} - - public boolean isaReservedWords(String word){ - return reservedWords.contains(word); - } -} - diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index 628ef169..b2da4d86 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -1,6 +1,5 @@ package com.orm; -import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; @@ -10,6 +9,7 @@ import com.orm.dsl.MultiUnique; import com.orm.dsl.NotNull; import com.orm.dsl.Unique; +import com.orm.util.KeyWordUtil; import com.orm.util.MigrationFileParser; import com.orm.util.NamingHelper; import com.orm.util.NumberComparator; @@ -27,29 +27,30 @@ import java.util.List; import static com.orm.util.ReflectionUtil.getDomainClasses; +import static com.orm.util.ContextUtil.getAssets; public class SchemaGenerator { - - private Context context; - public static final String NULL = " NULL"; public static final String NOT_NULL = " NOT NULL"; public static final String UNIQUE = " UNIQUE"; public static final String SUGAR = "Sugar"; - public SchemaGenerator(Context context) { - this.context = context; + //Prevent instantiation + private SchemaGenerator() { } + + public static SchemaGenerator getInstance() { + return new SchemaGenerator(); } public void createDatabase(SQLiteDatabase sqLiteDatabase) { - List domainClasses = getDomainClasses(context); + List domainClasses = getDomainClasses(); for (Class domain : domainClasses) { createTable(domain, sqLiteDatabase); } } public void doUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { - List domainClasses = getDomainClasses(context); + List domainClasses = getDomainClasses(); String sql = "select count(*) from sqlite_master where type='table' and name='%s';"; for (Class domain : domainClasses) { @@ -78,7 +79,7 @@ private ArrayList getColumnNames(SQLiteDatabase sqLiteDatabase, String t public void deleteTables(SQLiteDatabase sqLiteDatabase) { - List tables = getDomainClasses(context); + List tables = getDomainClasses(); for (Class table : tables) { sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + NamingHelper.toSQLName(table)); } @@ -88,7 +89,7 @@ private boolean executeSugarUpgrade(SQLiteDatabase db, int oldVersion, int newVe boolean isSuccess = false; try { - List files = Arrays.asList(this.context.getAssets().list("sugar_upgrades")); + List files = Arrays.asList(getAssets().list("sugar_upgrades")); Collections.sort(files, new NumberComparator()); for (String file : files) { Log.i(SUGAR, "filename : " + file); @@ -114,7 +115,7 @@ private boolean executeSugarUpgrade(SQLiteDatabase db, int oldVersion, int newVe private void executeScript(SQLiteDatabase db, String file) { try { - InputStream is = this.context.getAssets().open("sugar_upgrades/" + file); + InputStream is = getAssets().open("sugar_upgrades/" + file); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line; @@ -137,7 +138,6 @@ private void executeScript(SQLiteDatabase db, String file) { } private void addColumns(Class table, SQLiteDatabase sqLiteDatabase) { - List fields = ReflectionUtil.getTableFields(table); String tableName = NamingHelper.toSQLName(table); ArrayList presentColumns = getColumnNames(sqLiteDatabase, tableName); @@ -177,14 +177,14 @@ private void addColumns(Class table, SQLiteDatabase sqLiteDatabase) { } protected String createTableSQL(Class table) { - KeyWords link = new KeyWords(); Log.i(SUGAR, "Create table if not exists"); List fields = ReflectionUtil.getTableFields(table); String tableName = NamingHelper.toSQLName(table); - if(link.isaReservedWords(tableName)) - { + + if(KeyWordUtil.isKeyword(tableName)) { Log.i(SUGAR,"ERROR, SQLITE RESERVED WORD USED IN " + tableName); } + StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS "); sb.append(tableName).append(" ( ID INTEGER PRIMARY KEY AUTOINCREMENT "); diff --git a/library/src/main/java/com/orm/SugarApp.java b/library/src/main/java/com/orm/SugarApp.java index 5140e1ee..cab35ca7 100644 --- a/library/src/main/java/com/orm/SugarApp.java +++ b/library/src/main/java/com/orm/SugarApp.java @@ -1,7 +1,5 @@ package com.orm; -import com.orm.SugarContext; - import android.app.Application; public class SugarApp extends Application { diff --git a/library/src/main/java/com/orm/SugarContext.java b/library/src/main/java/com/orm/SugarContext.java index 66bf4df0..511126f5 100644 --- a/library/src/main/java/com/orm/SugarContext.java +++ b/library/src/main/java/com/orm/SugarContext.java @@ -2,6 +2,8 @@ import android.content.Context; +import com.orm.util.ContextUtil; + import java.util.Collections; import java.util.Map; import java.util.WeakHashMap; @@ -12,8 +14,8 @@ public class SugarContext { private SugarDb sugarDb; private Map entitiesMap; - private SugarContext(Context context) { - this.sugarDb = new SugarDb(context); + private SugarContext() { + this.sugarDb = SugarDb.getInstance(); this.entitiesMap = Collections.synchronizedMap(new WeakHashMap()); } @@ -25,7 +27,8 @@ public static SugarContext getSugarContext() { } public static void init(Context context) { - instance = new SugarContext(context); + ContextUtil.init(context); + instance = new SugarContext(); } public static void terminate() { @@ -33,6 +36,7 @@ public static void terminate() { return; } instance.doTerminate(); + ContextUtil.terminate(); } /* diff --git a/library/src/main/java/com/orm/SugarDb.java b/library/src/main/java/com/orm/SugarDb.java index 56528f96..2b653ee7 100644 --- a/library/src/main/java/com/orm/SugarDb.java +++ b/library/src/main/java/com/orm/SugarDb.java @@ -1,26 +1,32 @@ package com.orm; -import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; -import com.orm.util.ManifestHelper; import com.orm.util.SugarCursorFactory; import static com.orm.util.ManifestHelper.getDatabaseVersion; import static com.orm.util.ManifestHelper.getDebugEnabled; +import static com.orm.util.ManifestHelper.getDbName; + +import static com.orm.util.ContextUtil.*; public class SugarDb extends SQLiteOpenHelper { + private static final String LOG_TAG = "Sugar"; private final SchemaGenerator schemaGenerator; private SQLiteDatabase sqLiteDatabase; private int openedConnections = 0; - public SugarDb(Context context) { - super(context, ManifestHelper.getDatabaseName(context), - new SugarCursorFactory(getDebugEnabled(context)), getDatabaseVersion(context)); - schemaGenerator = new SchemaGenerator(context); + //Prevent instantiation + private SugarDb() { + super(getContext(), getDbName(), new SugarCursorFactory(getDebugEnabled()), getDatabaseVersion()); + schemaGenerator = SchemaGenerator.getInstance(); + } + + public static SugarDb getInstance() { + return new SugarDb(); } @Override @@ -43,17 +49,17 @@ public synchronized SQLiteDatabase getDB() { @Override public synchronized SQLiteDatabase getReadableDatabase() { - Log.d("SUGAR", "getReadableDatabase"); + Log.d(LOG_TAG, "getReadableDatabase"); openedConnections++; return super.getReadableDatabase(); } @Override public synchronized void close() { - Log.d("SUGAR", "getReadableDatabase"); + Log.d(LOG_TAG, "getReadableDatabase"); openedConnections--; if(openedConnections == 0) { - Log.d("SUGAR", "closing"); + Log.d(LOG_TAG, "closing"); super.close(); } } diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 4b9e04ef..9d61443e 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -27,8 +27,8 @@ import static com.orm.SugarContext.getSugarContext; public class SugarRecord { - public static final String SUGAR = "Sugar"; + private Long id = null; private static SQLiteDatabase getSugarDataBase() { @@ -174,13 +174,13 @@ public static Iterator findAsIterator(Class type, String whereClause, public static Iterator findWithQueryAsIterator(Class type, String query, String... arguments) { Cursor cursor = getSugarDataBase().rawQuery(query, arguments); - return new CursorIterator(type, cursor); + return new CursorIterator<>(type, cursor); } public static Iterator findAsIterator(Class type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) { Cursor cursor = getSugarDataBase().query(NamingHelper.toSQLName(type), null, whereClause, whereArgs, groupBy, null, orderBy, limit); - return new CursorIterator(type, cursor); + return new CursorIterator<>(type, cursor); } public static List find(Class type, String whereClause, String... whereArgs) { @@ -206,7 +206,7 @@ public static List find(Class type, String whereClause, String[] where public static List getEntitiesFromCursor(Cursor cursor, Class type){ T entity; - List result = new ArrayList(); + List result = new ArrayList<>(); try { while (cursor.moveToNext()) { entity = type.getDeclaredConstructor().newInstance(); @@ -222,15 +222,15 @@ public static List getEntitiesFromCursor(Cursor cursor, Class type){ return result; } - public static long count(Class type) { + public static long count(Class type) { return count(type, null, null, null, null, null); } - public static long count(Class type, String whereClause, String[] whereArgs) { + public static long count(Class type, String whereClause, String[] whereArgs) { return count(type, whereClause, whereArgs, null, null, null); } - public static long count(Class type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) { + public static long count(Class type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) { long result = -1; String filter = (!TextUtils.isEmpty(whereClause)) ? " where " + whereClause : ""; SQLiteStatement sqliteStatement; @@ -284,7 +284,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(); } diff --git a/library/src/main/java/com/orm/util/Collection.java b/library/src/main/java/com/orm/util/Collection.java deleted file mode 100644 index fe7a7e63..00000000 --- a/library/src/main/java/com/orm/util/Collection.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.orm.util; -import java.util.*; - -public class Collection { - - public static List list(T... args) { - return Arrays.asList(args); - } - - public static Set set(T... args) { - Set result = new HashSet(args.length); - result.addAll(Arrays.asList(args)); - return result; - } - - public static Map map(Entry... entries) { - Map result = new HashMap(entries.length); - - for (Entry entry : entries) - if (entry.value != null) - result.put(entry.key, entry.value); - - return result; - } - - public static Entry entry(K key, V value) { - return new Entry(key, value); - } - - public static class Entry { - K key; - V value; - - public Entry(K key, V value) { - this.key = key; - this.value = value; - } - } - -} diff --git a/library/src/main/java/com/orm/util/ContextUtil.java b/library/src/main/java/com/orm/util/ContextUtil.java new file mode 100644 index 00000000..3e6eff58 --- /dev/null +++ b/library/src/main/java/com/orm/util/ContextUtil.java @@ -0,0 +1,48 @@ +package com.orm.util; + +import android.content.Context; +import android.content.SharedPreferences; +import android.content.pm.PackageManager; +import android.content.res.AssetManager; + +/** + * @author jonatan.salas + */ +public final class ContextUtil { + private static Context ctx; + + //Prevent instantiation + private ContextUtil() { } + + public static void init(Context context) { + if (null == context) { + throw new IllegalArgumentException("context shouldn't be null!"); + } + + ctx = context; + } + + public static void terminate() { + ctx = null; + } + + public static Context getContext() { + return ctx; + } + + public static AssetManager getAssets() { + return getContext().getAssets(); + } + + public static PackageManager getPackageManager() { + return getContext().getPackageManager(); + } + + public static String getPackageName() { + return getContext().getPackageName(); + } + + public static SharedPreferences getSharedPreferences(String name, int mode) { + return getContext().getSharedPreferences(name, mode); + } +} diff --git a/library/src/main/java/com/orm/util/KeyWordUtil.java b/library/src/main/java/com/orm/util/KeyWordUtil.java new file mode 100644 index 00000000..d3b65cf2 --- /dev/null +++ b/library/src/main/java/com/orm/util/KeyWordUtil.java @@ -0,0 +1,37 @@ +package com.orm.util; + +/** + * @author jonatan.salas + */ +public final class KeyWordUtil { + + private static final String[] KEY_WORDS = new String[] { + "", "ABORT", "ACTION", "ADD", "AFTER", "ALTER", "ANALYZE", "AND", "AS", "ASC", "ATTACH", + "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY", "CASCADE", "CASE", "CAST", "CHECK", + "COLLATE", "COLUMN", "COMMIT", "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE", + "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE", "DEFERRED", "DELETE", + "DESC", "DETACH", "DISTINCT", "DROP", "EACH", "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", + "EXISTS", "EXPLAIN", "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", + "IF", "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", "INSERT", "INSTEAD", + "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY", "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", + "NO", "NOT", "NOTNULL", "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA", + "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP", "REINDEX", "RELEASE", "RENAME", + "REPLACE", "RESTRICT", "RIGHT", "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP", + "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE", "UPDATE", "USING", "VACUUM", + "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE", "WITH", "WITHOUT" + }; + + //Prevent instantiation + private KeyWordUtil() { } + + public static boolean isKeyword(String word) { + for (String keyWord: KEY_WORDS) { + if (keyWord.equals(word)) { + return true; + } + } + + return false; + } +} + diff --git a/library/src/main/java/com/orm/util/ManifestHelper.java b/library/src/main/java/com/orm/util/ManifestHelper.java index fb65724c..beacfc53 100644 --- a/library/src/main/java/com/orm/util/ManifestHelper.java +++ b/library/src/main/java/com/orm/util/ManifestHelper.java @@ -1,25 +1,29 @@ package com.orm.util; -import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.util.Log; +import static com.orm.util.ContextUtil.*; + /** * Helper class for accessing properties in the AndroidManifest */ public class ManifestHelper { + private static final String LOG_TAG = "Sugar"; /** * Key for the database name meta data. */ public final static String METADATA_DATABASE = "DATABASE"; + /** - * Key for the database verison meta data. + * Key for the database version meta data. */ public final static String METADATA_VERSION = "VERSION"; public final static String METADATA_DOMAIN_PACKAGE_NAME = "DOMAIN_PACKAGE_NAME"; public final static String METADATA_QUERY_LOG = "QUERY_LOG"; + /** * The default name for the database unless specified in the AndroidManifest. */ @@ -28,12 +32,11 @@ public class ManifestHelper { /** * Grabs the database version from the manifest. * - * @param context the {@link android.content.Context} of the Android application * @return the database version as specified by the {@link #METADATA_VERSION} version or 1 of * not present */ - public static int getDatabaseVersion(Context context) { - Integer databaseVersion = getMetaDataInteger(context, METADATA_VERSION); + public static int getDatabaseVersion() { + Integer databaseVersion = getMetaDataInteger(METADATA_VERSION); if ((databaseVersion == null) || (databaseVersion == 0)) { databaseVersion = 1; @@ -45,11 +48,10 @@ public static int getDatabaseVersion(Context context) { /** * Grabs the domain name of the model classes from the manifest. * - * @param context the {@link android.content.Context} of the Android application * @return the package String that Sugar uses to search for model classes */ - public static String getDomainPackageName(Context context){ - String domainPackageName = getMetaDataString(context, METADATA_DOMAIN_PACKAGE_NAME); + public static String getDomainPackageName() { + String domainPackageName = getMetaDataString(METADATA_DOMAIN_PACKAGE_NAME); if (domainPackageName == null) { domainPackageName = ""; @@ -61,12 +63,11 @@ public static String getDomainPackageName(Context context){ /** * Grabs the name of the database file specified in the manifest. * - * @param context the {@link android.content.Context} of the Android application * @return the value for the {@value #METADATA_DATABASE} meta data in the AndroidManifest or * {@link #DATABASE_DEFAULT_NAME} if not present */ - public static String getDatabaseName(Context context) { - String databaseName = getMetaDataString(context, METADATA_DATABASE); + public static String getDatabaseName() { + String databaseName = getMetaDataString(METADATA_DATABASE); if (databaseName == null) { databaseName = DATABASE_DEFAULT_NAME; @@ -75,59 +76,58 @@ public static String getDatabaseName(Context context) { return databaseName; } + public static String getDbName() { + return getDatabaseName(); + } + /** * Grabs the debug flag from the manifest. * - * @param context the {@link android.content.Context} of the Android application * @return true if the debug flag is enabled */ - public static boolean getDebugEnabled(Context context) { - return getMetaDataBoolean(context, METADATA_QUERY_LOG); + public static boolean getDebugEnabled() { + return getMetaDataBoolean(METADATA_QUERY_LOG); } - private static String getMetaDataString(Context context, String name) { + private static String getMetaDataString(String name) { + PackageManager pm = getPackageManager(); String value = null; - PackageManager pm = context.getPackageManager(); try { - ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), - PackageManager.GET_META_DATA); + ApplicationInfo ai = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); value = ai.metaData.getString(name); } catch (Exception e) { - Log.d("sugar", "Couldn't find config value: " + name); + Log.d(LOG_TAG, "Couldn't find config value: " + name); } return value; } - private static Integer getMetaDataInteger(Context context, String name) { + private static Integer getMetaDataInteger(String name) { + PackageManager pm = getPackageManager(); Integer value = null; - PackageManager pm = context.getPackageManager(); try { - ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), - PackageManager.GET_META_DATA); + ApplicationInfo ai = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); value = ai.metaData.getInt(name); } catch (Exception e) { - Log.d("sugar", "Couldn't find config value: " + name); + Log.d(LOG_TAG, "Couldn't find config value: " + name); } return value; } - private static Boolean getMetaDataBoolean(Context context, String name) { + private static Boolean getMetaDataBoolean(String name) { + PackageManager pm = getPackageManager(); Boolean value = false; - PackageManager pm = context.getPackageManager(); try { - ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), - PackageManager.GET_META_DATA); + ApplicationInfo ai = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); value = ai.metaData.getBoolean(name); } catch (Exception e) { - Log.d("sugar", "Couldn't find config value: " + name); + Log.d(LOG_TAG, "Couldn't find config value: " + name); } return value; } - } diff --git a/library/src/main/java/com/orm/util/MultiDexHelper.java b/library/src/main/java/com/orm/util/MultiDexHelper.java index 87f7fb3a..7d8c2642 100644 --- a/library/src/main/java/com/orm/util/MultiDexHelper.java +++ b/library/src/main/java/com/orm/util/MultiDexHelper.java @@ -14,48 +14,49 @@ import dalvik.system.DexFile; +import static com.orm.util.ContextUtil.getSharedPreferences; +import static com.orm.util.ContextUtil.getPackageManager; +import static com.orm.util.ContextUtil.getPackageName; + /** * Created by xudshen@hotmail.com on 14/11/13. */ - //http://stackoverflow.com/a/26892658 public class MultiDexHelper { private static final String EXTRACTED_NAME_EXT = ".classes"; private static final String EXTRACTED_SUFFIX = ".zip"; - private static final String SECONDARY_FOLDER_NAME = "code_cache" + File.separator + - "secondary-dexes"; - + private static final String SECONDARY_FOLDER_NAME = "code_cache" + File.separator + "secondary-dexes"; private static final String PREFS_FILE = "multidex.version"; private static final String KEY_DEX_NUMBER = "dex.number"; - private static SharedPreferences getMultiDexPreferences(Context context) { - return context.getSharedPreferences(PREFS_FILE, - Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB - ? Context.MODE_PRIVATE - : Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS); + private static SharedPreferences getMultiDexPreferences() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { + return getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE); + } else { + return getSharedPreferences(PREFS_FILE, Context.MODE_MULTI_PROCESS); + } } /** * get all the dex path * - * @param context the application context * @return all the dex path * @throws PackageManager.NameNotFoundException * @throws IOException */ - public static List getSourcePaths(Context context) throws PackageManager.NameNotFoundException, IOException { - ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0); + public static List getSourcePaths() throws PackageManager.NameNotFoundException, IOException { + ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(getPackageName(), 0); File sourceApk = new File(applicationInfo.sourceDir); File dexDir = new File(applicationInfo.dataDir, SECONDARY_FOLDER_NAME); - List sourcePaths = new ArrayList(); + List sourcePaths = new ArrayList<>(); sourcePaths.add(applicationInfo.sourceDir); //add the default apk path //the prefix of extracted file, ie: test.classes String extractedFilePrefix = sourceApk.getName() + EXTRACTED_NAME_EXT; //the total dex numbers - int totalDexNumber = getMultiDexPreferences(context).getInt(KEY_DEX_NUMBER, 1); + int totalDexNumber = getMultiDexPreferences().getInt(KEY_DEX_NUMBER, 1); for (int secondaryNumber = 2; secondaryNumber <= totalDexNumber; secondaryNumber++) { //for each dex file, ie: test.classes2.zip, test.classes3.zip... @@ -73,14 +74,13 @@ public static List getSourcePaths(Context context) throws PackageManager /** * get all the classes name in "classes.dex", "classes2.dex", .... * - * @param context the application context * @return all the classes name * @throws PackageManager.NameNotFoundException * @throws IOException */ - public static List getAllClasses(Context context) throws PackageManager.NameNotFoundException, IOException { - List classNames = new ArrayList(); - for (String path : getSourcePaths(context)) { + public static List getAllClasses() throws PackageManager.NameNotFoundException, IOException { + List classNames = new ArrayList<>(); + for (String path : getSourcePaths()) { try { DexFile dexfile; if (path.endsWith(EXTRACTED_SUFFIX)) { @@ -94,8 +94,7 @@ public static List getAllClasses(Context context) throws PackageManager. classNames.add(dexEntries.nextElement()); } } catch (IOException e) { - throw new IOException("Error at loading dex file '" + - path + "'"); + throw new IOException("Error at loading dex file '" + path + "'"); } } return classNames; diff --git a/library/src/main/java/com/orm/util/NamingHelper.java b/library/src/main/java/com/orm/util/NamingHelper.java index b7237c09..55253ebd 100644 --- a/library/src/main/java/com/orm/util/NamingHelper.java +++ b/library/src/main/java/com/orm/util/NamingHelper.java @@ -69,7 +69,7 @@ public static String toSQLName(Field field) { /** * Maps a Java Class to the name of the class. * - * @param table the generic {@link java.lang.Class} that defines a database table + * @param table the generic {@link java.lang.Class} that defines a database table * @return if the given class is annotated with {@link com.orm.dsl.Table} then the value for * {@link com.orm.dsl.Table#name()} will be returned. Else, the class' simple name will * be converted from CamelCase to UNDER_SCORE notation diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index 1458a308..84285cac 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -26,6 +26,8 @@ import java.util.List; import java.util.Map; +import static com.orm.util.ContextUtil.getContext; + public class ReflectionUtil { public static List getTableFields(Class table) { @@ -33,11 +35,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); @@ -163,7 +165,7 @@ public static void setFieldValueFromCursor(Cursor cursor, Field field, Object ob if (colName.equalsIgnoreCase("id")) { long cid = cursor.getLong(columnIndex); - field.set(object, Long.valueOf(cid)); + field.set(object, cid); } else if (fieldType.equals(long.class) || fieldType.equals(Long.class)) { field.set(object, cursor.getLong(columnIndex)); @@ -217,22 +219,18 @@ public static void setFieldValueFromCursor(Cursor cursor, Field field, Object ob } } else Log.e("Sugar", "Class cannot be read from Sqlite3 database. Please check the type of field " + field.getName() + "(" + field.getType().getName() + ")"); - } catch (IllegalArgumentException e) { - Log.e("field set error", e.getMessage()); - } catch (IllegalAccessException e) { + } catch (IllegalArgumentException | IllegalAccessException e) { Log.e("field set error", e.getMessage()); } } private static Field getDeepField(String fieldName, Class type) throws NoSuchFieldException { try { - Field field = type.getDeclaredField(fieldName); - return field; + return type.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { Class superclass = type.getSuperclass(); if (superclass != null) { - Field field = getDeepField(fieldName, superclass); - return field; + return getDeepField(fieldName, superclass); } else { throw e; } @@ -244,23 +242,19 @@ public static void setFieldValueForId(Object object, Long value) { Field field = getDeepField("id", object.getClass()); field.setAccessible(true); field.set(object, value); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (NoSuchFieldException e) { + } catch (Exception e) { e.printStackTrace(); } } - public static List getDomainClasses(Context context) { - List domainClasses = new ArrayList(); + public static List getDomainClasses() { + List domainClasses = new ArrayList<>(); try { - for (String className : getAllClasses(context)) { - Class domainClass = getDomainClass(className, context); + for (String className : getAllClasses()) { + Class domainClass = getDomainClass(className); if (domainClass != null) domainClasses.add(domainClass); } - } catch (IOException e) { - Log.e("Sugar", e.getMessage()); - } catch (PackageManager.NameNotFoundException e) { + } catch (IOException | PackageManager.NameNotFoundException e) { Log.e("Sugar", e.getMessage()); } @@ -268,10 +262,10 @@ public static List getDomainClasses(Context context) { } - private static Class getDomainClass(String className, Context context) { + private static Class getDomainClass(String className) { Class discoveredClass = null; try { - discoveredClass = Class.forName(className, true, context.getClass().getClassLoader()); + discoveredClass = Class.forName(className, true, getContext().getClass().getClassLoader()); } catch (Throwable e) { String error = (e.getMessage() == null) ? "getDomainClass " + className + " error" : e.getMessage(); Log.e("Sugar", error); @@ -292,11 +286,11 @@ private static Class getDomainClass(String className, Context context) { } - private static List getAllClasses(Context context) throws PackageManager.NameNotFoundException, IOException { - String packageName = ManifestHelper.getDomainPackageName(context); - List classNames = new ArrayList(); + private static List getAllClasses() throws PackageManager.NameNotFoundException, IOException { + String packageName = ManifestHelper.getDomainPackageName(); + List classNames = new ArrayList<>(); try { - List allClasses = MultiDexHelper.getAllClasses(context); + List allClasses = MultiDexHelper.getAllClasses(); for (String classString : allClasses) { if (classString.startsWith(packageName)) classNames.add(classString); } @@ -304,7 +298,7 @@ private static List getAllClasses(Context context) throws PackageManager ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); Enumeration urls = classLoader.getResources(""); while (urls.hasMoreElements()) { - List fileNames = new ArrayList(); + List fileNames = new ArrayList<>(); String classDirectoryName = urls.nextElement().getFile(); if (classDirectoryName.contains("bin") || classDirectoryName.contains("classes") || classDirectoryName.contains("retrolambda")) { @@ -317,9 +311,10 @@ private static List getAllClasses(Context context) throws PackageManager } } } - } finally { -// if (null != dexfile) dexfile.close(); } +// } finally { +// if (null != dexfile) dexfile.close(); +// } return classNames; } diff --git a/library/src/main/java/com/orm/util/SugarConfig.java b/library/src/main/java/com/orm/util/SugarConfig.java index c8cf3868..9328c82f 100644 --- a/library/src/main/java/com/orm/util/SugarConfig.java +++ b/library/src/main/java/com/orm/util/SugarConfig.java @@ -6,10 +6,9 @@ import java.util.List; import java.util.Map; - 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); @@ -26,7 +25,7 @@ public static List getFields(Class clazz) { public static void clearCache() { fields.clear(); - fields = new HashMap, List>(); + fields = new HashMap<>(); } } diff --git a/library/src/main/java/com/orm/util/SugarCursor.java b/library/src/main/java/com/orm/util/SugarCursor.java index e326f49b..d058c5c1 100644 --- a/library/src/main/java/com/orm/util/SugarCursor.java +++ b/library/src/main/java/com/orm/util/SugarCursor.java @@ -4,6 +4,7 @@ import android.database.CursorWrapper; public class SugarCursor extends CursorWrapper { + public SugarCursor(Cursor cursor) { super(cursor); } diff --git a/library/src/main/java/com/orm/util/SugarCursorFactory.java b/library/src/main/java/com/orm/util/SugarCursorFactory.java index 12706516..3274fccc 100644 --- a/library/src/main/java/com/orm/util/SugarCursorFactory.java +++ b/library/src/main/java/com/orm/util/SugarCursorFactory.java @@ -16,7 +16,6 @@ public SugarCursorFactory() { } public SugarCursorFactory(boolean debugEnabled) { - this.debugEnabled = debugEnabled; } diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index 24bae7bc..00dea418 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -7,6 +7,7 @@ import com.orm.models.StringFieldExtendedModel; import com.orm.models.StringFieldExtendedModelAnnotatedColumn; import com.orm.query.DummyContext; +import com.orm.util.ContextUtil; import com.orm.util.NamingHelper; import org.junit.Test; @@ -14,9 +15,11 @@ import static junit.framework.Assert.assertEquals; public class SchemaGeneratorTest { + @Test public void testEmptyTableCreation() throws Exception { - SchemaGenerator schemaGenerator = new SchemaGenerator(new DummyContext()); + ContextUtil.init(new DummyContext()); + SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); String createSQL = schemaGenerator.createTableSQL(EmptyModel.class); assertEquals( "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(EmptyModel.class) + @@ -26,7 +29,8 @@ public void testEmptyTableCreation() throws Exception { @Test public void testSimpleColumnTableCreation() throws Exception { - SchemaGenerator schemaGenerator = new SchemaGenerator(new DummyContext()); + ContextUtil.init(new DummyContext()); + SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); String createSQL = schemaGenerator.createTableSQL(StringFieldExtendedModel.class); assertEquals( "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(StringFieldExtendedModel.class) + @@ -53,7 +57,8 @@ public void testSimpleColumnTableCreation() throws Exception { @Test public void testUniqueTableCreation() { - SchemaGenerator schemaGenerator = new SchemaGenerator(new DummyContext()); + ContextUtil.init(new DummyContext()); + SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); String createSQL = schemaGenerator.createTableSQL(IntUniqueModel.class); assertEquals( "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(IntUniqueModel.class) + @@ -64,7 +69,8 @@ public void testUniqueTableCreation() { @Test public void testMultiColumnUniqueTableCreation() { - SchemaGenerator schemaGenerator = new SchemaGenerator(new DummyContext()); + ContextUtil.init(new DummyContext()); + SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); String createSQL = schemaGenerator.createTableSQL(MultiColumnUniqueModel.class); assertEquals( "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(MultiColumnUniqueModel.class) + diff --git a/library/src/test/java/com/orm/models/EmptyModel.java b/library/src/test/java/com/orm/models/EmptyModel.java index 8efb9dd4..73459b51 100644 --- a/library/src/test/java/com/orm/models/EmptyModel.java +++ b/library/src/test/java/com/orm/models/EmptyModel.java @@ -6,7 +6,5 @@ * Created by sibelius on 02/12/15. */ public class EmptyModel extends SugarRecord { - public EmptyModel() { - - } + public EmptyModel() { } } diff --git a/library/src/test/java/com/orm/models/IntUniqueModel.java b/library/src/test/java/com/orm/models/IntUniqueModel.java index e73e8968..d7b88d3f 100644 --- a/library/src/test/java/com/orm/models/IntUniqueModel.java +++ b/library/src/test/java/com/orm/models/IntUniqueModel.java @@ -7,12 +7,11 @@ * Created by sibelius on 02/12/15. */ public class IntUniqueModel extends SugarRecord { + @Unique private int value; - public IntUniqueModel() { - - } + public IntUniqueModel() { } public IntUniqueModel(int value) { this.value = value; diff --git a/library/src/test/java/com/orm/models/MultiColumnUniqueModel.java b/library/src/test/java/com/orm/models/MultiColumnUniqueModel.java index 73ab5d8e..eea5ec55 100644 --- a/library/src/test/java/com/orm/models/MultiColumnUniqueModel.java +++ b/library/src/test/java/com/orm/models/MultiColumnUniqueModel.java @@ -13,8 +13,7 @@ public class MultiColumnUniqueModel extends SugarRecord { private int a; private int b; - public MultiColumnUniqueModel() { - } + public MultiColumnUniqueModel() { } public MultiColumnUniqueModel(int a, int b) { this.a = a; diff --git a/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java b/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java index 1203db01..2f732b42 100644 --- a/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java +++ b/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java @@ -11,9 +11,7 @@ public class StringFieldAnnotatedModel extends SugarRecord { public String name; - public StringFieldAnnotatedModel() { - - } + public StringFieldAnnotatedModel() { } public StringFieldAnnotatedModel(String name) { this.name = name; diff --git a/library/src/test/java/com/orm/models/StringFieldExtendedModel.java b/library/src/test/java/com/orm/models/StringFieldExtendedModel.java index ee781632..2aff85d7 100644 --- a/library/src/test/java/com/orm/models/StringFieldExtendedModel.java +++ b/library/src/test/java/com/orm/models/StringFieldExtendedModel.java @@ -8,9 +8,7 @@ public class StringFieldExtendedModel extends SugarRecord { public String name; - public StringFieldExtendedModel() { - - } + public StringFieldExtendedModel() { } public StringFieldExtendedModel(String name) { this.name = name; diff --git a/library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java b/library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java index 9f2b43cd..b2117926 100644 --- a/library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java +++ b/library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java @@ -10,9 +10,7 @@ public class StringFieldExtendedModelAnnotatedColumn extends SugarRecord { @Column(name="anyName") public String name; - public StringFieldExtendedModelAnnotatedColumn() { - - } + public StringFieldExtendedModelAnnotatedColumn() { } public StringFieldExtendedModelAnnotatedColumn(String name) { this.name = name; diff --git a/library/src/test/java/com/orm/query/QueryBuilderTests.java b/library/src/test/java/com/orm/query/QueryBuilderTests.java index 96e3c79f..d43ca5ee 100644 --- a/library/src/test/java/com/orm/query/QueryBuilderTests.java +++ b/library/src/test/java/com/orm/query/QueryBuilderTests.java @@ -7,6 +7,7 @@ import static junit.framework.Assert.assertEquals; public class QueryBuilderTests { + @Test(expected=RuntimeException.class) public void noArgumentsTest() { QueryBuilder.generatePlaceholders(0); diff --git a/library/src/test/java/com/orm/query/TestRecord.java b/library/src/test/java/com/orm/query/TestRecord.java index 50997205..383a5234 100644 --- a/library/src/test/java/com/orm/query/TestRecord.java +++ b/library/src/test/java/com/orm/query/TestRecord.java @@ -1,13 +1,12 @@ package com.orm.query; -import android.content.Context; import com.orm.SugarRecord; public class TestRecord extends SugarRecord { private String name; - public TestRecord(Context context) { + public TestRecord() { super(); } } diff --git a/library/src/test/java/com/orm/util/MigrationFileParserTest.java b/library/src/test/java/com/orm/util/MigrationFileParserTest.java index 536af4cb..72b9f9ca 100644 --- a/library/src/test/java/com/orm/util/MigrationFileParserTest.java +++ b/library/src/test/java/com/orm/util/MigrationFileParserTest.java @@ -1,20 +1,15 @@ package com.orm.util; -import com.orm.util.MigrationFileParser; - import org.junit.Test; -import org.junit.Before; import java.lang.String; import static junit.framework.Assert.assertEquals; -public class MigrationFileParserTest{ - MigrationFileParser emptyFile; +public class MigrationFileParserTest { @Test - public void testSingleLineStatement() - { + public void testSingleLineStatement() { MigrationFileParser singleLineComment = new MigrationFileParser("insert into table--comment"); String statements[] = singleLineComment.getStatements(); @@ -27,8 +22,9 @@ public void testSingleLineStatement() assertEquals("Testing single line statement size",1,statements.length); assertEquals("Testing single line statement content","insert into table",statements[0]); } + @Test - public void testMultiLineComment(){ + public void testMultiLineComment() { MigrationFileParser multiLineComment = new MigrationFileParser("insert into table /**comment \n new line 2 \n new line 3 */hello"); String statements[] = multiLineComment.getStatements(); @@ -37,7 +33,7 @@ public void testMultiLineComment(){ } @Test - public void testMixedComment(){ + public void testMixedComment() { MigrationFileParser mixedComment = new MigrationFileParser("insert into/*multiline\n **comment*/--comment"); String statements[] = mixedComment.getStatements(); From 331f61c765d31009d1a0f8e5a8cef82264560de9 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 3 Apr 2016 22:26:13 -0300 Subject: [PATCH 059/139] Updated example app build.gradle file app-compat dependency --- example/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/build.gradle b/example/build.gradle index 15ba8d9c..b9fe1be7 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -25,7 +25,7 @@ android { dependencies { compile project (':library') - compile 'com.android.support:appcompat-v7:23.1.1' + compile 'com.android.support:appcompat-v7:23.2.1' testCompile 'org.robolectric:robolectric:3.0' testCompile 'junit:junit:4.12' } From a814f31d844dc2e0d3d3b77edadeaa70c30723fa Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 3 Apr 2016 22:31:09 -0300 Subject: [PATCH 060/139] Updated travis.yml buildTools version to 23.0.3 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 80ca1ced..db407323 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ android: components: - sys-img-armeabi-v7a-android-23 - tools - - build-tools-23.0.2 + - build-tools-23.0.3 - android-23 - extra-android-m2repository before_script: From e776a3e033170f9c517ab5ce7b491f7540e3d2e8 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 3 Apr 2016 23:08:31 -0300 Subject: [PATCH 061/139] Added two test to keep coverage --- .../java/com/orm/util/ContextUtilTest.java | 59 +++++++++++++++++++ .../java/com/orm/util/KeyWordUtilTest.java | 24 ++++++++ 2 files changed, 83 insertions(+) create mode 100644 library/src/test/java/com/orm/util/ContextUtilTest.java create mode 100644 library/src/test/java/com/orm/util/KeyWordUtilTest.java diff --git a/library/src/test/java/com/orm/util/ContextUtilTest.java b/library/src/test/java/com/orm/util/ContextUtilTest.java new file mode 100644 index 00000000..fc627076 --- /dev/null +++ b/library/src/test/java/com/orm/util/ContextUtilTest.java @@ -0,0 +1,59 @@ +package com.orm.util; + +import android.content.Context; + +import com.orm.query.DummyContext; + +import org.junit.Test; + + +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertNull; +import static com.orm.util.ContextUtil.*; + +/** + * @author jonatan.salas + */ +public class ContextUtilTest { + + public void initContextUtil() { + init(new DummyContext()); + } + + @Test + public void testInitContext() { + initContextUtil(); + assertNotNull(getContext()); + } + + @Test + public void testGetAssets() { + initContextUtil(); + assertNull(getAssets()); + } + + @Test + public void testGetPackageManager() { + initContextUtil(); + assertNull(getPackageManager()); + } + + @Test + public void testGetPackageName() { + initContextUtil(); + assertNull(getPackageName()); + } + + @Test + public void testGetPreferences() { + initContextUtil(); + assertNull(getSharedPreferences("lala", Context.MODE_PRIVATE)); + } + + @Test + public void testTerminateContext() { + initContextUtil(); + terminate(); + assertNull(getContext()); + } +} diff --git a/library/src/test/java/com/orm/util/KeyWordUtilTest.java b/library/src/test/java/com/orm/util/KeyWordUtilTest.java new file mode 100644 index 00000000..511f0a1e --- /dev/null +++ b/library/src/test/java/com/orm/util/KeyWordUtilTest.java @@ -0,0 +1,24 @@ +package com.orm.util; + +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; + +/** + * @author jonatan.salas + */ +public class KeyWordUtilTest { + + @Test + public void testKeyWord() { + assertEquals(true, KeyWordUtil.isKeyword("SELECT")); + assertEquals(true, KeyWordUtil.isKeyword("TRANSACTION")); + assertEquals(true, KeyWordUtil.isKeyword("MATCH")); + assertEquals(true, KeyWordUtil.isKeyword("AS")); + assertEquals(true, KeyWordUtil.isKeyword("NOTNULL")); + assertEquals(true, KeyWordUtil.isKeyword("NOT")); + assertEquals(false, KeyWordUtil.isKeyword("PERSONS")); + assertEquals(false, KeyWordUtil.isKeyword("NAME")); + assertEquals(false, KeyWordUtil.isKeyword("LOCATION")); + } +} From 94f296f8bf5c32ef45a60e5998e98cefa75e9c70 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 3 Apr 2016 23:40:51 -0300 Subject: [PATCH 062/139] Minimal modifications in KeyWordUtil Added to ManifestHelper a private constructor in order to not instance by error Added ManifestHelperTest to test basic methods functionality Added testNullKeyWord in KeyWordUtilTest --- .../main/java/com/orm/util/KeyWordUtil.java | 4 ++ .../java/com/orm/util/ManifestHelper.java | 5 +- .../java/com/orm/util/KeyWordUtilTest.java | 5 ++ .../java/com/orm/util/ManifestHelperTest.java | 49 +++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 library/src/test/java/com/orm/util/ManifestHelperTest.java diff --git a/library/src/main/java/com/orm/util/KeyWordUtil.java b/library/src/main/java/com/orm/util/KeyWordUtil.java index d3b65cf2..8983e7cf 100644 --- a/library/src/main/java/com/orm/util/KeyWordUtil.java +++ b/library/src/main/java/com/orm/util/KeyWordUtil.java @@ -25,6 +25,10 @@ public final class KeyWordUtil { private KeyWordUtil() { } public static boolean isKeyword(String word) { + if (null == word) { + return false; + } + for (String keyWord: KEY_WORDS) { if (keyWord.equals(word)) { return true; diff --git a/library/src/main/java/com/orm/util/ManifestHelper.java b/library/src/main/java/com/orm/util/ManifestHelper.java index beacfc53..f986a58b 100644 --- a/library/src/main/java/com/orm/util/ManifestHelper.java +++ b/library/src/main/java/com/orm/util/ManifestHelper.java @@ -9,7 +9,7 @@ /** * Helper class for accessing properties in the AndroidManifest */ -public class ManifestHelper { +public final class ManifestHelper { private static final String LOG_TAG = "Sugar"; /** @@ -29,6 +29,9 @@ public class ManifestHelper { */ public final static String DATABASE_DEFAULT_NAME = "Sugar.db"; + //Prevent instantiation + private ManifestHelper() { } + /** * Grabs the database version from the manifest. * diff --git a/library/src/test/java/com/orm/util/KeyWordUtilTest.java b/library/src/test/java/com/orm/util/KeyWordUtilTest.java index 511f0a1e..0f132e3a 100644 --- a/library/src/test/java/com/orm/util/KeyWordUtilTest.java +++ b/library/src/test/java/com/orm/util/KeyWordUtilTest.java @@ -21,4 +21,9 @@ public void testKeyWord() { assertEquals(false, KeyWordUtil.isKeyword("NAME")); assertEquals(false, KeyWordUtil.isKeyword("LOCATION")); } + + @Test + public void testNullKeyword() { + assertEquals(false, KeyWordUtil.isKeyword(null)); + } } diff --git a/library/src/test/java/com/orm/util/ManifestHelperTest.java b/library/src/test/java/com/orm/util/ManifestHelperTest.java new file mode 100644 index 00000000..01431634 --- /dev/null +++ b/library/src/test/java/com/orm/util/ManifestHelperTest.java @@ -0,0 +1,49 @@ +package com.orm.util; + +import com.orm.query.DummyContext; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static com.orm.util.ManifestHelper.*; +import static com.orm.util.ContextUtil.init; + +/** + * @author jonatan.salas + */ +public class ManifestHelperTest { + + public void initContext() { + init(new DummyContext()); + } + + @Test + public void testGetDbName() { + initContext(); + assertEquals(DATABASE_DEFAULT_NAME, getDatabaseName()); + } + + @Test + public void testGetDatabaseName() { + initContext(); + assertEquals(DATABASE_DEFAULT_NAME, getDatabaseName()); + } + + @Test + public void testGetDatabaseVersion() { + initContext(); + assertEquals(1, getDatabaseVersion()); + } + + @Test + public void testGetDomainPackageName() { + initContext(); + assertNotNull(getDomainPackageName()); + } + + @Test + public void testGetDebugEnabled() { + initContext(); + assertEquals(false, getDebugEnabled()); + } +} From 92301eadc1ee228155d5c8c1c0fde35bfc5e4469 Mon Sep 17 00:00:00 2001 From: imfhk Date: Tue, 5 Apr 2016 11:35:34 +0300 Subject: [PATCH 063/139] Add Instant Run support --- .../src/main/java/com/orm/util/MultiDexHelper.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/orm/util/MultiDexHelper.java b/library/src/main/java/com/orm/util/MultiDexHelper.java index 7d8c2642..c074eeee 100644 --- a/library/src/main/java/com/orm/util/MultiDexHelper.java +++ b/library/src/main/java/com/orm/util/MultiDexHelper.java @@ -25,7 +25,7 @@ public class MultiDexHelper { private static final String EXTRACTED_NAME_EXT = ".classes"; private static final String EXTRACTED_SUFFIX = ".zip"; - + private static final String INSTANT_RUN_DEX_DIR_PATH = "files/instant-run/dex/"; private static final String SECONDARY_FOLDER_NAME = "code_cache" + File.separator + "secondary-dexes"; private static final String PREFS_FILE = "multidex.version"; private static final String KEY_DEX_NUMBER = "dex.number"; @@ -41,7 +41,7 @@ private static SharedPreferences getMultiDexPreferences() { /** * get all the dex path * - * @return all the dex path + * @return all the dex path, including the ones in the newly added instant-run folder * @throws PackageManager.NameNotFoundException * @throws IOException */ @@ -49,10 +49,17 @@ public static List getSourcePaths() throws PackageManager.NameNotFoundEx ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(getPackageName(), 0); File sourceApk = new File(applicationInfo.sourceDir); File dexDir = new File(applicationInfo.dataDir, SECONDARY_FOLDER_NAME); + File instantRunDir = new File(applicationInfo.dataDir, INSTANT_RUN_DEX_DIR_PATH); //default instant-run dir List sourcePaths = new ArrayList<>(); sourcePaths.add(applicationInfo.sourceDir); //add the default apk path + if (instantRunDir.exists()) { //check if app using instant run + for(final File dexFile : instantRunDir.listFiles()) { //add all sources from instan-run + sourcePaths.add(dexFile.getAbsolutePath()); + } + } + //the prefix of extracted file, ie: test.classes String extractedFilePrefix = sourceApk.getName() + EXTRACTED_NAME_EXT; //the total dex numbers @@ -99,4 +106,4 @@ public static List getAllClasses() throws PackageManager.NameNotFoundExc } return classNames; } -} \ No newline at end of file +} From 519c83db53422b452df293c742d4d6a6cac95022 Mon Sep 17 00:00:00 2001 From: Enrico Bruno Del Zotto Date: Tue, 5 Apr 2016 14:25:54 +0100 Subject: [PATCH 064/139] Class not found in getDomainClass https://github.com/satyan/sugar/issues/519#issuecomment-205794898 problem --- library/src/main/java/com/orm/util/ReflectionUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index 84285cac..2ff1162b 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -265,7 +265,7 @@ public static List getDomainClasses() { private static Class getDomainClass(String className) { Class discoveredClass = null; try { - discoveredClass = Class.forName(className, true, getContext().getClass().getClassLoader()); + discoveredClass = Class.forName(className, true, Thread.currentThread().getContextClassLoader()); } catch (Throwable e) { String error = (e.getMessage() == null) ? "getDomainClass " + className + " error" : e.getMessage(); Log.e("Sugar", error); From 86cd849362a3df492f4a48dd494c0013e086c08f Mon Sep 17 00:00:00 2001 From: Admin <111> Date: Wed, 6 Apr 2016 18:17:16 +0300 Subject: [PATCH 065/139] Remove unnecessary console spam, as result: speed up + 20-30% Remove unnecessary console spam, as result: speed up + 20-30% --- library/src/main/java/com/orm/SugarContext.java | 9 ++++++++- library/src/main/java/com/orm/SugarDb.java | 17 ++++++++++++----- library/src/main/java/com/orm/SugarRecord.java | 7 +++++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/library/src/main/java/com/orm/SugarContext.java b/library/src/main/java/com/orm/SugarContext.java index 511126f5..4560dd2c 100644 --- a/library/src/main/java/com/orm/SugarContext.java +++ b/library/src/main/java/com/orm/SugarContext.java @@ -3,6 +3,7 @@ import android.content.Context; import com.orm.util.ContextUtil; +import com.orm.util.ManifestHelper; import java.util.Collections; import java.util.Map; @@ -13,9 +14,11 @@ public class SugarContext { private static SugarContext instance = null; private SugarDb sugarDb; private Map entitiesMap; + private boolean debugEnabled; private SugarContext() { - this.sugarDb = SugarDb.getInstance(); + this.debugEnabled = ManifestHelper.getDebugEnabled(); + this.sugarDb = SugarDb.getInstance(debugEnabled); this.entitiesMap = Collections.synchronizedMap(new WeakHashMap()); } @@ -58,4 +61,8 @@ public SugarDb getSugarDb() { Map getEntitiesMap() { return entitiesMap; } + + public boolean isDebugEnabled() { + return debugEnabled; + } } diff --git a/library/src/main/java/com/orm/SugarDb.java b/library/src/main/java/com/orm/SugarDb.java index 2b653ee7..6f3341d9 100644 --- a/library/src/main/java/com/orm/SugarDb.java +++ b/library/src/main/java/com/orm/SugarDb.java @@ -4,14 +4,13 @@ import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; +import com.orm.util.ManifestHelper; import com.orm.util.SugarCursorFactory; +import static com.orm.util.ContextUtil.getContext; import static com.orm.util.ManifestHelper.getDatabaseVersion; -import static com.orm.util.ManifestHelper.getDebugEnabled; import static com.orm.util.ManifestHelper.getDbName; -import static com.orm.util.ContextUtil.*; - public class SugarDb extends SQLiteOpenHelper { private static final String LOG_TAG = "Sugar"; @@ -20,15 +19,23 @@ public class SugarDb extends SQLiteOpenHelper { private int openedConnections = 0; //Prevent instantiation - private SugarDb() { - super(getContext(), getDbName(), new SugarCursorFactory(getDebugEnabled()), getDatabaseVersion()); + private SugarDb(boolean debugEnabled) { + super(getContext(), getDbName(), new SugarCursorFactory(debugEnabled), getDatabaseVersion()); schemaGenerator = SchemaGenerator.getInstance(); } + private SugarDb() { + this(ManifestHelper.getDebugEnabled()); + } + public static SugarDb getInstance() { return new SugarDb(); } + public static SugarDb getInstance(boolean debugEnabled) { + return new SugarDb(debugEnabled); + } + @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { schemaGenerator.createDatabase(sqLiteDatabase); diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 44cee5ff..3baf3263 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -265,7 +265,8 @@ public static long save(Object object) { } static long save(SQLiteDatabase db, Object object) { - Map entitiesMap = getSugarContext().getEntitiesMap(); + SugarContext context = getSugarContext(); + Map entitiesMap = context.getEntitiesMap(); List columns = ReflectionUtil.getTableFields(object.getClass()); ContentValues values = new ContentValues(columns.size()); Field idField = null; @@ -299,7 +300,9 @@ static long save(SQLiteDatabase db, Object object) { ((SugarRecord) object).setId(id); } - Log.i(SUGAR, object.getClass().getSimpleName() + " saved : " + id); + if (context.isDebugEnabled()) { + Log.i(SUGAR, object.getClass().getSimpleName() + " saved : " + id); + } return id; } From 02e15ed7a3565098714177bed003c898ab08825d Mon Sep 17 00:00:00 2001 From: Rexee Date: Wed, 6 Apr 2016 20:58:54 +0300 Subject: [PATCH 066/139] Update SugarContext.java move to ManifestHelper --- library/src/main/java/com/orm/SugarContext.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/orm/SugarContext.java b/library/src/main/java/com/orm/SugarContext.java index 4560dd2c..16e16913 100644 --- a/library/src/main/java/com/orm/SugarContext.java +++ b/library/src/main/java/com/orm/SugarContext.java @@ -14,11 +14,10 @@ public class SugarContext { private static SugarContext instance = null; private SugarDb sugarDb; private Map entitiesMap; - private boolean debugEnabled; private SugarContext() { - this.debugEnabled = ManifestHelper.getDebugEnabled(); - this.sugarDb = SugarDb.getInstance(debugEnabled); + ManifestHelper.loadDebugEnabled(); + this.sugarDb = SugarDb.getInstance(); this.entitiesMap = Collections.synchronizedMap(new WeakHashMap()); } From 233a6f97d5747c2edbac423c0a7b9f3300b37af8 Mon Sep 17 00:00:00 2001 From: Rexee Date: Wed, 6 Apr 2016 20:59:39 +0300 Subject: [PATCH 067/139] moved to ManifestHelper --- library/src/main/java/com/orm/SugarDb.java | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/library/src/main/java/com/orm/SugarDb.java b/library/src/main/java/com/orm/SugarDb.java index 6f3341d9..57e8e89a 100644 --- a/library/src/main/java/com/orm/SugarDb.java +++ b/library/src/main/java/com/orm/SugarDb.java @@ -19,23 +19,15 @@ public class SugarDb extends SQLiteOpenHelper { private int openedConnections = 0; //Prevent instantiation - private SugarDb(boolean debugEnabled) { - super(getContext(), getDbName(), new SugarCursorFactory(debugEnabled), getDatabaseVersion()); - schemaGenerator = SchemaGenerator.getInstance(); - } - private SugarDb() { - this(ManifestHelper.getDebugEnabled()); + super(getContext(), getDbName(), new SugarCursorFactory(ManifestHelper.isDebugEnabled()), getDatabaseVersion()); + schemaGenerator = SchemaGenerator.getInstance(); } public static SugarDb getInstance() { return new SugarDb(); } - public static SugarDb getInstance(boolean debugEnabled) { - return new SugarDb(debugEnabled); - } - @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { schemaGenerator.createDatabase(sqLiteDatabase); From 488c6b0105c5b2bad6d421cc67b7f3ce34b2232e Mon Sep 17 00:00:00 2001 From: Rexee Date: Wed, 6 Apr 2016 21:00:36 +0300 Subject: [PATCH 068/139] Update SugarRecord.java --- library/src/main/java/com/orm/SugarRecord.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 3baf3263..78d34fa5 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -265,8 +265,7 @@ public static long save(Object object) { } static long save(SQLiteDatabase db, Object object) { - SugarContext context = getSugarContext(); - Map entitiesMap = context.getEntitiesMap(); + Map entitiesMap = getSugarContext().getEntitiesMap(); List columns = ReflectionUtil.getTableFields(object.getClass()); ContentValues values = new ContentValues(columns.size()); Field idField = null; @@ -300,7 +299,7 @@ static long save(SQLiteDatabase db, Object object) { ((SugarRecord) object).setId(id); } - if (context.isDebugEnabled()) { + if (ManifestHelper.isDebugEnabled()) { Log.i(SUGAR, object.getClass().getSimpleName() + " saved : " + id); } From e6752ac0375bf35ce98b512da05e4f4bc85c9b7d Mon Sep 17 00:00:00 2001 From: Rexee Date: Wed, 6 Apr 2016 21:02:03 +0300 Subject: [PATCH 069/139] Update SugarRecord.java --- library/src/main/java/com/orm/SugarRecord.java | 1 + 1 file changed, 1 insertion(+) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 78d34fa5..7f429c26 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -10,6 +10,7 @@ import com.orm.dsl.Table; import com.orm.dsl.Unique; +import com.orm.util.ManifestHelper; import com.orm.util.NamingHelper; import com.orm.util.QueryBuilder; import com.orm.util.ReflectionUtil; From c81685ce36b23ccdade6ab323060cf65a3da65f5 Mon Sep 17 00:00:00 2001 From: Rexee Date: Wed, 6 Apr 2016 21:04:20 +0300 Subject: [PATCH 070/139] Update ManifestHelper.java --- library/src/main/java/com/orm/util/ManifestHelper.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/orm/util/ManifestHelper.java b/library/src/main/java/com/orm/util/ManifestHelper.java index f986a58b..ef539127 100644 --- a/library/src/main/java/com/orm/util/ManifestHelper.java +++ b/library/src/main/java/com/orm/util/ManifestHelper.java @@ -11,6 +11,7 @@ */ public final class ManifestHelper { private static final String LOG_TAG = "Sugar"; + private static boolean debugEnabled; /** * Key for the database name meta data. @@ -88,8 +89,12 @@ public static String getDbName() { * * @return true if the debug flag is enabled */ - public static boolean getDebugEnabled() { - return getMetaDataBoolean(METADATA_QUERY_LOG); + public static boolean isDebugEnabled() { + return debugEnabled; + } + + public static void loadDebugEnabled() { + debugEnabled = getMetaDataBoolean(METADATA_QUERY_LOG); } private static String getMetaDataString(String name) { From a85264f42f0bcbef61cc059642d6a16b55f76d05 Mon Sep 17 00:00:00 2001 From: Rexee Date: Wed, 6 Apr 2016 21:05:47 +0300 Subject: [PATCH 071/139] Update ManifestHelperTest.java --- library/src/test/java/com/orm/util/ManifestHelperTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/test/java/com/orm/util/ManifestHelperTest.java b/library/src/test/java/com/orm/util/ManifestHelperTest.java index 01431634..2ec32a47 100644 --- a/library/src/test/java/com/orm/util/ManifestHelperTest.java +++ b/library/src/test/java/com/orm/util/ManifestHelperTest.java @@ -44,6 +44,6 @@ public void testGetDomainPackageName() { @Test public void testGetDebugEnabled() { initContext(); - assertEquals(false, getDebugEnabled()); + assertEquals(false, isDebugEnabled()); } } From f025692fb08226144ad659185820f49f09767bd7 Mon Sep 17 00:00:00 2001 From: Rexee Date: Wed, 6 Apr 2016 21:09:09 +0300 Subject: [PATCH 072/139] Update SugarContext.java --- library/src/main/java/com/orm/SugarContext.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/library/src/main/java/com/orm/SugarContext.java b/library/src/main/java/com/orm/SugarContext.java index 16e16913..e6b7b06c 100644 --- a/library/src/main/java/com/orm/SugarContext.java +++ b/library/src/main/java/com/orm/SugarContext.java @@ -60,8 +60,4 @@ public SugarDb getSugarDb() { Map getEntitiesMap() { return entitiesMap; } - - public boolean isDebugEnabled() { - return debugEnabled; - } } From d630e3977288b12a41ea3f72377f9e3d0c1f9886 Mon Sep 17 00:00:00 2001 From: Rexee Date: Wed, 6 Apr 2016 21:14:03 +0300 Subject: [PATCH 073/139] Update ManifestHelper.java --- library/src/main/java/com/orm/util/ManifestHelper.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/library/src/main/java/com/orm/util/ManifestHelper.java b/library/src/main/java/com/orm/util/ManifestHelper.java index ef539127..b9c12c9a 100644 --- a/library/src/main/java/com/orm/util/ManifestHelper.java +++ b/library/src/main/java/com/orm/util/ManifestHelper.java @@ -11,7 +11,7 @@ */ public final class ManifestHelper { private static final String LOG_TAG = "Sugar"; - private static boolean debugEnabled; + private static Boolean debugEnabled = null; /** * Key for the database name meta data. @@ -90,11 +90,7 @@ public static String getDbName() { * @return true if the debug flag is enabled */ public static boolean isDebugEnabled() { - return debugEnabled; - } - - public static void loadDebugEnabled() { - debugEnabled = getMetaDataBoolean(METADATA_QUERY_LOG); + return (null == debugEnabled) ? debugEnabled = getMetaDataBoolean(METADATA_QUERY_LOG) : debugEnabled; } private static String getMetaDataString(String name) { From 0d3bed9ffa85cfd53f3d07b25073e852ac3aab88 Mon Sep 17 00:00:00 2001 From: Rexee Date: Wed, 6 Apr 2016 21:14:24 +0300 Subject: [PATCH 074/139] Update SugarContext.java --- library/src/main/java/com/orm/SugarContext.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/library/src/main/java/com/orm/SugarContext.java b/library/src/main/java/com/orm/SugarContext.java index e6b7b06c..511126f5 100644 --- a/library/src/main/java/com/orm/SugarContext.java +++ b/library/src/main/java/com/orm/SugarContext.java @@ -3,7 +3,6 @@ import android.content.Context; import com.orm.util.ContextUtil; -import com.orm.util.ManifestHelper; import java.util.Collections; import java.util.Map; @@ -16,7 +15,6 @@ public class SugarContext { private Map entitiesMap; private SugarContext() { - ManifestHelper.loadDebugEnabled(); this.sugarDb = SugarDb.getInstance(); this.entitiesMap = Collections.synchronizedMap(new WeakHashMap()); } From a4c94ef284887803d232d7b2535584b055d6b4a6 Mon Sep 17 00:00:00 2001 From: jonatan Date: Wed, 6 Apr 2016 17:11:23 -0300 Subject: [PATCH 075/139] Project Update: Updated dsl package name to annotation to keep consistence. Created package helper where it goes all helper classes. Minimal changes in helper classes, all of them are final classes and have private constructor to prevent from generating instances. --- .../models/BigDecimalFieldAnnotatedModel.java | 2 +- .../models/BooleanFieldAnnotatedModel.java | 2 +- .../example/models/ByteArrayAnnotatedModel.java | 2 +- .../models/DoubleFieldAnnotatedModel.java | 2 +- .../example/models/EnumFieldAnnotatedModel.java | 2 +- .../models/FloatFieldAnnotatedModel.java | 2 +- .../models/IncompleteAnnotatedModel.java | 2 +- .../models/IntegerFieldAnnotatedModel.java | 2 +- .../example/models/LongFieldAnnotatedModel.java | 2 +- .../example/models/NestedAnnotatedModel.java | 2 +- .../com/example/models/NestedMixedBAModel.java | 2 +- .../com/example/models/NestedMixedBBModel.java | 2 +- .../models/RelationshipAnnotatedModel.java | 2 +- .../example/models/RelationshipMixedBModel.java | 2 +- .../models/ShortFieldAnnotatedModel.java | 2 +- .../example/models/SimpleAnnotatedModel.java | 2 +- .../models/StringFieldAnnotatedModel.java | 2 +- .../models/StringFieldAnnotatedNoIdModel.java | 2 +- .../sugartest/SimpleAnnotatedModelTests.java | 2 +- .../sugartest/SimpleExtendedModelTests.java | 2 +- .../src/main/java/com/orm/SchemaGenerator.java | 10 +++++----- library/src/main/java/com/orm/SugarDb.java | 6 +++--- library/src/main/java/com/orm/SugarRecord.java | 8 ++++---- .../com/orm/{dsl => annotation}/Column.java | 2 +- .../com/orm/{dsl => annotation}/Ignore.java | 2 +- .../orm/{dsl => annotation}/MultiUnique.java | 2 +- .../com/orm/{dsl => annotation}/NotNull.java | 2 +- .../java/com/orm/{dsl => annotation}/Table.java | 2 +- .../com/orm/{dsl => annotation}/Unique.java | 2 +- .../orm/{util => helper}/ManifestHelper.java | 2 +- .../orm/{util => helper}/MultiDexHelper.java | 7 +++++-- .../com/orm/{util => helper}/NamingHelper.java | 17 ++++++++++------- .../{ => helper}/SugarTransactionHelper.java | 17 +++++++++++++---- library/src/main/java/com/orm/query/Select.java | 2 +- .../main/java/com/orm/util/ReflectionUtil.java | 9 +++++---- .../src/test/java/com/orm/NamingHelperTest.java | 2 +- .../test/java/com/orm/SchemaGeneratorTest.java | 2 +- .../java/com/orm/models/IntUniqueModel.java | 2 +- .../com/orm/models/MultiColumnUniqueModel.java | 3 +-- .../orm/models/StringFieldAnnotatedModel.java | 3 +-- ...StringFieldExtendedModelAnnotatedColumn.java | 2 +- .../java/com/orm/util/ManifestHelperTest.java | 2 +- 42 files changed, 80 insertions(+), 66 deletions(-) rename library/src/main/java/com/orm/{dsl => annotation}/Column.java (92%) rename library/src/main/java/com/orm/{dsl => annotation}/Ignore.java (90%) rename library/src/main/java/com/orm/{dsl => annotation}/MultiUnique.java (90%) rename library/src/main/java/com/orm/{dsl => annotation}/NotNull.java (90%) rename library/src/main/java/com/orm/{dsl => annotation}/Table.java (90%) rename library/src/main/java/com/orm/{dsl => annotation}/Unique.java (90%) rename library/src/main/java/com/orm/{util => helper}/ManifestHelper.java (99%) rename library/src/main/java/com/orm/{util => helper}/MultiDexHelper.java (97%) rename library/src/main/java/com/orm/{util => helper}/NamingHelper.java (87%) rename library/src/main/java/com/orm/{ => helper}/SugarTransactionHelper.java (63%) diff --git a/example/src/main/java/com/example/models/BigDecimalFieldAnnotatedModel.java b/example/src/main/java/com/example/models/BigDecimalFieldAnnotatedModel.java index b3b60e98..bd4fd846 100644 --- a/example/src/main/java/com/example/models/BigDecimalFieldAnnotatedModel.java +++ b/example/src/main/java/com/example/models/BigDecimalFieldAnnotatedModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; import java.math.BigDecimal; diff --git a/example/src/main/java/com/example/models/BooleanFieldAnnotatedModel.java b/example/src/main/java/com/example/models/BooleanFieldAnnotatedModel.java index d3a63edc..d46455f4 100644 --- a/example/src/main/java/com/example/models/BooleanFieldAnnotatedModel.java +++ b/example/src/main/java/com/example/models/BooleanFieldAnnotatedModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class BooleanFieldAnnotatedModel { diff --git a/example/src/main/java/com/example/models/ByteArrayAnnotatedModel.java b/example/src/main/java/com/example/models/ByteArrayAnnotatedModel.java index da0b3ece..f92c277a 100644 --- a/example/src/main/java/com/example/models/ByteArrayAnnotatedModel.java +++ b/example/src/main/java/com/example/models/ByteArrayAnnotatedModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class ByteArrayAnnotatedModel { diff --git a/example/src/main/java/com/example/models/DoubleFieldAnnotatedModel.java b/example/src/main/java/com/example/models/DoubleFieldAnnotatedModel.java index 2f5fd428..1657d23e 100644 --- a/example/src/main/java/com/example/models/DoubleFieldAnnotatedModel.java +++ b/example/src/main/java/com/example/models/DoubleFieldAnnotatedModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class DoubleFieldAnnotatedModel { diff --git a/example/src/main/java/com/example/models/EnumFieldAnnotatedModel.java b/example/src/main/java/com/example/models/EnumFieldAnnotatedModel.java index 34bf2f7e..8303c787 100644 --- a/example/src/main/java/com/example/models/EnumFieldAnnotatedModel.java +++ b/example/src/main/java/com/example/models/EnumFieldAnnotatedModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class EnumFieldAnnotatedModel { diff --git a/example/src/main/java/com/example/models/FloatFieldAnnotatedModel.java b/example/src/main/java/com/example/models/FloatFieldAnnotatedModel.java index 6d7f5e42..6da3fcc5 100644 --- a/example/src/main/java/com/example/models/FloatFieldAnnotatedModel.java +++ b/example/src/main/java/com/example/models/FloatFieldAnnotatedModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class FloatFieldAnnotatedModel { diff --git a/example/src/main/java/com/example/models/IncompleteAnnotatedModel.java b/example/src/main/java/com/example/models/IncompleteAnnotatedModel.java index e524d22f..ccb47fa8 100644 --- a/example/src/main/java/com/example/models/IncompleteAnnotatedModel.java +++ b/example/src/main/java/com/example/models/IncompleteAnnotatedModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class IncompleteAnnotatedModel { diff --git a/example/src/main/java/com/example/models/IntegerFieldAnnotatedModel.java b/example/src/main/java/com/example/models/IntegerFieldAnnotatedModel.java index 1b860ce1..b4bcd03a 100644 --- a/example/src/main/java/com/example/models/IntegerFieldAnnotatedModel.java +++ b/example/src/main/java/com/example/models/IntegerFieldAnnotatedModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class IntegerFieldAnnotatedModel { diff --git a/example/src/main/java/com/example/models/LongFieldAnnotatedModel.java b/example/src/main/java/com/example/models/LongFieldAnnotatedModel.java index c3986576..9900bf8c 100644 --- a/example/src/main/java/com/example/models/LongFieldAnnotatedModel.java +++ b/example/src/main/java/com/example/models/LongFieldAnnotatedModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class LongFieldAnnotatedModel { diff --git a/example/src/main/java/com/example/models/NestedAnnotatedModel.java b/example/src/main/java/com/example/models/NestedAnnotatedModel.java index 8e834486..c7ec80fd 100644 --- a/example/src/main/java/com/example/models/NestedAnnotatedModel.java +++ b/example/src/main/java/com/example/models/NestedAnnotatedModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class NestedAnnotatedModel { diff --git a/example/src/main/java/com/example/models/NestedMixedBAModel.java b/example/src/main/java/com/example/models/NestedMixedBAModel.java index 55a1e63b..338f748e 100644 --- a/example/src/main/java/com/example/models/NestedMixedBAModel.java +++ b/example/src/main/java/com/example/models/NestedMixedBAModel.java @@ -1,7 +1,7 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class NestedMixedBAModel { diff --git a/example/src/main/java/com/example/models/NestedMixedBBModel.java b/example/src/main/java/com/example/models/NestedMixedBBModel.java index b784f407..439eb5d9 100644 --- a/example/src/main/java/com/example/models/NestedMixedBBModel.java +++ b/example/src/main/java/com/example/models/NestedMixedBBModel.java @@ -1,7 +1,7 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class NestedMixedBBModel { diff --git a/example/src/main/java/com/example/models/RelationshipAnnotatedModel.java b/example/src/main/java/com/example/models/RelationshipAnnotatedModel.java index aa0fff9c..44faeed0 100644 --- a/example/src/main/java/com/example/models/RelationshipAnnotatedModel.java +++ b/example/src/main/java/com/example/models/RelationshipAnnotatedModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class RelationshipAnnotatedModel { diff --git a/example/src/main/java/com/example/models/RelationshipMixedBModel.java b/example/src/main/java/com/example/models/RelationshipMixedBModel.java index 9c9d2771..525ad2e0 100644 --- a/example/src/main/java/com/example/models/RelationshipMixedBModel.java +++ b/example/src/main/java/com/example/models/RelationshipMixedBModel.java @@ -1,7 +1,7 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class RelationshipMixedBModel { diff --git a/example/src/main/java/com/example/models/ShortFieldAnnotatedModel.java b/example/src/main/java/com/example/models/ShortFieldAnnotatedModel.java index 8393974b..0d732b7f 100644 --- a/example/src/main/java/com/example/models/ShortFieldAnnotatedModel.java +++ b/example/src/main/java/com/example/models/ShortFieldAnnotatedModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class ShortFieldAnnotatedModel { diff --git a/example/src/main/java/com/example/models/SimpleAnnotatedModel.java b/example/src/main/java/com/example/models/SimpleAnnotatedModel.java index 08d3b0fd..7e9e7db4 100644 --- a/example/src/main/java/com/example/models/SimpleAnnotatedModel.java +++ b/example/src/main/java/com/example/models/SimpleAnnotatedModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table diff --git a/example/src/main/java/com/example/models/StringFieldAnnotatedModel.java b/example/src/main/java/com/example/models/StringFieldAnnotatedModel.java index 05420d44..d4e86aa4 100644 --- a/example/src/main/java/com/example/models/StringFieldAnnotatedModel.java +++ b/example/src/main/java/com/example/models/StringFieldAnnotatedModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class StringFieldAnnotatedModel { diff --git a/example/src/main/java/com/example/models/StringFieldAnnotatedNoIdModel.java b/example/src/main/java/com/example/models/StringFieldAnnotatedNoIdModel.java index 1c12d581..54737775 100644 --- a/example/src/main/java/com/example/models/StringFieldAnnotatedNoIdModel.java +++ b/example/src/main/java/com/example/models/StringFieldAnnotatedNoIdModel.java @@ -1,6 +1,6 @@ package com.example.models; -import com.orm.dsl.Table; +import com.orm.annotation.Table; @Table public class StringFieldAnnotatedNoIdModel { diff --git a/example/src/test/java/com/example/sugartest/SimpleAnnotatedModelTests.java b/example/src/test/java/com/example/sugartest/SimpleAnnotatedModelTests.java index 7bfe0108..bce99ffe 100644 --- a/example/src/test/java/com/example/sugartest/SimpleAnnotatedModelTests.java +++ b/example/src/test/java/com/example/sugartest/SimpleAnnotatedModelTests.java @@ -2,7 +2,7 @@ import com.example.models.SimpleAnnotatedModel; import com.orm.SugarRecord; -import com.orm.util.NamingHelper; +import com.orm.helper.NamingHelper; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/example/src/test/java/com/example/sugartest/SimpleExtendedModelTests.java b/example/src/test/java/com/example/sugartest/SimpleExtendedModelTests.java index aa860270..e8645fb4 100644 --- a/example/src/test/java/com/example/sugartest/SimpleExtendedModelTests.java +++ b/example/src/test/java/com/example/sugartest/SimpleExtendedModelTests.java @@ -2,7 +2,7 @@ import com.example.models.SimpleExtendedModel; import com.orm.SugarRecord; -import com.orm.util.NamingHelper; +import com.orm.helper.NamingHelper; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index b2da4d86..2f95b568 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -5,13 +5,13 @@ import android.database.sqlite.SQLiteDatabase; import android.util.Log; -import com.orm.dsl.Column; -import com.orm.dsl.MultiUnique; -import com.orm.dsl.NotNull; -import com.orm.dsl.Unique; +import com.orm.annotation.Column; +import com.orm.annotation.MultiUnique; +import com.orm.annotation.NotNull; +import com.orm.annotation.Unique; import com.orm.util.KeyWordUtil; import com.orm.util.MigrationFileParser; -import com.orm.util.NamingHelper; +import com.orm.helper.NamingHelper; import com.orm.util.NumberComparator; import com.orm.util.QueryBuilder; import com.orm.util.ReflectionUtil; diff --git a/library/src/main/java/com/orm/SugarDb.java b/library/src/main/java/com/orm/SugarDb.java index 57e8e89a..a95a7055 100644 --- a/library/src/main/java/com/orm/SugarDb.java +++ b/library/src/main/java/com/orm/SugarDb.java @@ -4,12 +4,12 @@ import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; -import com.orm.util.ManifestHelper; +import com.orm.helper.ManifestHelper; import com.orm.util.SugarCursorFactory; import static com.orm.util.ContextUtil.getContext; -import static com.orm.util.ManifestHelper.getDatabaseVersion; -import static com.orm.util.ManifestHelper.getDbName; +import static com.orm.helper.ManifestHelper.getDatabaseVersion; +import static com.orm.helper.ManifestHelper.getDbName; public class SugarDb extends SQLiteOpenHelper { private static final String LOG_TAG = "Sugar"; diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 7f429c26..139f1aa3 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -8,10 +8,10 @@ import android.text.TextUtils; import android.util.Log; -import com.orm.dsl.Table; -import com.orm.dsl.Unique; -import com.orm.util.ManifestHelper; -import com.orm.util.NamingHelper; +import com.orm.annotation.Table; +import com.orm.annotation.Unique; +import com.orm.helper.ManifestHelper; +import com.orm.helper.NamingHelper; import com.orm.util.QueryBuilder; import com.orm.util.ReflectionUtil; import com.orm.util.SugarCursor; diff --git a/library/src/main/java/com/orm/dsl/Column.java b/library/src/main/java/com/orm/annotation/Column.java similarity index 92% rename from library/src/main/java/com/orm/dsl/Column.java rename to library/src/main/java/com/orm/annotation/Column.java index 58e66529..ce4c2376 100644 --- a/library/src/main/java/com/orm/dsl/Column.java +++ b/library/src/main/java/com/orm/annotation/Column.java @@ -1,4 +1,4 @@ -package com.orm.dsl; +package com.orm.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/library/src/main/java/com/orm/dsl/Ignore.java b/library/src/main/java/com/orm/annotation/Ignore.java similarity index 90% rename from library/src/main/java/com/orm/dsl/Ignore.java rename to library/src/main/java/com/orm/annotation/Ignore.java index e4d748f1..d214a559 100644 --- a/library/src/main/java/com/orm/dsl/Ignore.java +++ b/library/src/main/java/com/orm/annotation/Ignore.java @@ -1,4 +1,4 @@ -package com.orm.dsl; +package com.orm.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/library/src/main/java/com/orm/dsl/MultiUnique.java b/library/src/main/java/com/orm/annotation/MultiUnique.java similarity index 90% rename from library/src/main/java/com/orm/dsl/MultiUnique.java rename to library/src/main/java/com/orm/annotation/MultiUnique.java index 86c6e323..4c312f15 100644 --- a/library/src/main/java/com/orm/dsl/MultiUnique.java +++ b/library/src/main/java/com/orm/annotation/MultiUnique.java @@ -1,4 +1,4 @@ -package com.orm.dsl; +package com.orm.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/library/src/main/java/com/orm/dsl/NotNull.java b/library/src/main/java/com/orm/annotation/NotNull.java similarity index 90% rename from library/src/main/java/com/orm/dsl/NotNull.java rename to library/src/main/java/com/orm/annotation/NotNull.java index f71ebf54..f3a6f585 100644 --- a/library/src/main/java/com/orm/dsl/NotNull.java +++ b/library/src/main/java/com/orm/annotation/NotNull.java @@ -1,4 +1,4 @@ -package com.orm.dsl; +package com.orm.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/library/src/main/java/com/orm/dsl/Table.java b/library/src/main/java/com/orm/annotation/Table.java similarity index 90% rename from library/src/main/java/com/orm/dsl/Table.java rename to library/src/main/java/com/orm/annotation/Table.java index 9c64fa74..82ffc15b 100644 --- a/library/src/main/java/com/orm/dsl/Table.java +++ b/library/src/main/java/com/orm/annotation/Table.java @@ -1,4 +1,4 @@ -package com.orm.dsl; +package com.orm.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/library/src/main/java/com/orm/dsl/Unique.java b/library/src/main/java/com/orm/annotation/Unique.java similarity index 90% rename from library/src/main/java/com/orm/dsl/Unique.java rename to library/src/main/java/com/orm/annotation/Unique.java index c05960ed..a6027e05 100644 --- a/library/src/main/java/com/orm/dsl/Unique.java +++ b/library/src/main/java/com/orm/annotation/Unique.java @@ -1,4 +1,4 @@ -package com.orm.dsl; +package com.orm.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/library/src/main/java/com/orm/util/ManifestHelper.java b/library/src/main/java/com/orm/helper/ManifestHelper.java similarity index 99% rename from library/src/main/java/com/orm/util/ManifestHelper.java rename to library/src/main/java/com/orm/helper/ManifestHelper.java index b9c12c9a..1c20b4fe 100644 --- a/library/src/main/java/com/orm/util/ManifestHelper.java +++ b/library/src/main/java/com/orm/helper/ManifestHelper.java @@ -1,4 +1,4 @@ -package com.orm.util; +package com.orm.helper; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; diff --git a/library/src/main/java/com/orm/util/MultiDexHelper.java b/library/src/main/java/com/orm/helper/MultiDexHelper.java similarity index 97% rename from library/src/main/java/com/orm/util/MultiDexHelper.java rename to library/src/main/java/com/orm/helper/MultiDexHelper.java index c074eeee..d17105f4 100644 --- a/library/src/main/java/com/orm/util/MultiDexHelper.java +++ b/library/src/main/java/com/orm/helper/MultiDexHelper.java @@ -1,4 +1,4 @@ -package com.orm.util; +package com.orm.helper; import android.content.Context; import android.content.SharedPreferences; @@ -22,7 +22,7 @@ * Created by xudshen@hotmail.com on 14/11/13. */ //http://stackoverflow.com/a/26892658 -public class MultiDexHelper { +public final class MultiDexHelper { private static final String EXTRACTED_NAME_EXT = ".classes"; private static final String EXTRACTED_SUFFIX = ".zip"; private static final String INSTANT_RUN_DEX_DIR_PATH = "files/instant-run/dex/"; @@ -30,6 +30,9 @@ public class MultiDexHelper { private static final String PREFS_FILE = "multidex.version"; private static final String KEY_DEX_NUMBER = "dex.number"; + //Prevent instantiation.. + private MultiDexHelper() { } + private static SharedPreferences getMultiDexPreferences() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { return getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE); diff --git a/library/src/main/java/com/orm/util/NamingHelper.java b/library/src/main/java/com/orm/helper/NamingHelper.java similarity index 87% rename from library/src/main/java/com/orm/util/NamingHelper.java rename to library/src/main/java/com/orm/helper/NamingHelper.java index 55253ebd..7a107b5c 100644 --- a/library/src/main/java/com/orm/util/NamingHelper.java +++ b/library/src/main/java/com/orm/helper/NamingHelper.java @@ -1,11 +1,14 @@ -package com.orm.util; +package com.orm.helper; -import com.orm.dsl.Column; -import com.orm.dsl.Table; +import com.orm.annotation.Column; +import com.orm.annotation.Table; import java.lang.reflect.Field; -public class NamingHelper { +public final class NamingHelper { + + //Prevent instantiation + private NamingHelper() { } /** * Converts a given CamelCasedString to UPPER_CASE_UNDER_SCORE. @@ -53,7 +56,7 @@ public static String toSQLNameDefault(String camelCased) { * * @param field the {@link java.lang.reflect.Field} that will be mapped * @return the name of the given Field as represented in the database. If the Field is annotated - * with {@link com.orm.dsl.Column} then the {@link com.orm.dsl.Column#name()} will be + * with {@link com.orm.annotation.Column} then the {@link com.orm.annotation.Column#name()} will be * returned. Else, the Field's {@link java.lang.reflect.Field#getName()} will be * converted from CamelCase to UNDER_SCORE notation */ @@ -70,8 +73,8 @@ public static String toSQLName(Field field) { * Maps a Java Class to the name of the class. * * @param table the generic {@link java.lang.Class} that defines a database table - * @return if the given class is annotated with {@link com.orm.dsl.Table} then the value for - * {@link com.orm.dsl.Table#name()} will be returned. Else, the class' simple name will + * @return if the given class is annotated with {@link com.orm.annotation.Table} then the value for + * {@link com.orm.annotation.Table#name()} will be returned. Else, the class' simple name will * be converted from CamelCase to UNDER_SCORE notation */ public static String toSQLName(Class table) { diff --git a/library/src/main/java/com/orm/SugarTransactionHelper.java b/library/src/main/java/com/orm/helper/SugarTransactionHelper.java similarity index 63% rename from library/src/main/java/com/orm/SugarTransactionHelper.java rename to library/src/main/java/com/orm/helper/SugarTransactionHelper.java index 74054cb3..06fd8731 100644 --- a/library/src/main/java/com/orm/SugarTransactionHelper.java +++ b/library/src/main/java/com/orm/helper/SugarTransactionHelper.java @@ -1,13 +1,18 @@ -package com.orm; +package com.orm.helper; import android.database.sqlite.SQLiteDatabase; import android.util.Log; -public class SugarTransactionHelper { +import static com.orm.SugarContext.getSugarContext; + +public final class SugarTransactionHelper { private static final String LOG_TAG = SugarTransactionHelper.class.getSimpleName(); - public static void doInTransaction(SugarTransactionHelper.Callback callback) { - SQLiteDatabase database = SugarContext.getSugarContext().getSugarDb().getDB(); + //Prevent instantiation.. + private SugarTransactionHelper() { } + + public static void doInTransaction(Callback callback) { + final SQLiteDatabase database = getSugarContext().getSugarDb().getDB(); database.beginTransaction(); try { @@ -21,6 +26,10 @@ public static void doInTransaction(SugarTransactionHelper.Callback callback) { Log.d(LOG_TAG, "Could execute callback within transaction", e); } finally { database.endTransaction(); + + if (database.isOpen()) { + database.close(); + } } } diff --git a/library/src/main/java/com/orm/query/Select.java b/library/src/main/java/com/orm/query/Select.java index 83f7102b..f87e7be9 100644 --- a/library/src/main/java/com/orm/query/Select.java +++ b/library/src/main/java/com/orm/query/Select.java @@ -3,7 +3,7 @@ import android.database.Cursor; import com.orm.SugarRecord; -import com.orm.util.NamingHelper; +import com.orm.helper.NamingHelper; import java.util.ArrayList; import java.util.Iterator; diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index 2ff1162b..6fd1067f 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -7,8 +7,11 @@ import android.util.Log; import com.orm.SugarRecord; -import com.orm.dsl.Ignore; -import com.orm.dsl.Table; +import com.orm.annotation.Ignore; +import com.orm.annotation.Table; +import com.orm.helper.ManifestHelper; +import com.orm.helper.MultiDexHelper; +import com.orm.helper.NamingHelper; import java.io.File; import java.io.IOException; @@ -26,8 +29,6 @@ import java.util.List; import java.util.Map; -import static com.orm.util.ContextUtil.getContext; - public class ReflectionUtil { public static List getTableFields(Class table) { diff --git a/library/src/test/java/com/orm/NamingHelperTest.java b/library/src/test/java/com/orm/NamingHelperTest.java index 3166a107..93aadbb3 100644 --- a/library/src/test/java/com/orm/NamingHelperTest.java +++ b/library/src/test/java/com/orm/NamingHelperTest.java @@ -1,6 +1,6 @@ package com.orm; -import com.orm.util.NamingHelper; +import com.orm.helper.NamingHelper; import org.junit.Test; diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index 00dea418..02686d92 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -8,7 +8,7 @@ import com.orm.models.StringFieldExtendedModelAnnotatedColumn; import com.orm.query.DummyContext; import com.orm.util.ContextUtil; -import com.orm.util.NamingHelper; +import com.orm.helper.NamingHelper; import org.junit.Test; diff --git a/library/src/test/java/com/orm/models/IntUniqueModel.java b/library/src/test/java/com/orm/models/IntUniqueModel.java index d7b88d3f..6c9cdfdd 100644 --- a/library/src/test/java/com/orm/models/IntUniqueModel.java +++ b/library/src/test/java/com/orm/models/IntUniqueModel.java @@ -1,7 +1,7 @@ package com.orm.models; import com.orm.SugarRecord; -import com.orm.dsl.Unique; +import com.orm.annotation.Unique; /** * Created by sibelius on 02/12/15. diff --git a/library/src/test/java/com/orm/models/MultiColumnUniqueModel.java b/library/src/test/java/com/orm/models/MultiColumnUniqueModel.java index eea5ec55..7057b2d6 100644 --- a/library/src/test/java/com/orm/models/MultiColumnUniqueModel.java +++ b/library/src/test/java/com/orm/models/MultiColumnUniqueModel.java @@ -1,8 +1,7 @@ package com.orm.models; import com.orm.SugarRecord; -import com.orm.dsl.MultiUnique; -import com.orm.dsl.Unique; +import com.orm.annotation.MultiUnique; /** * Created by sibelius on 02/12/15. diff --git a/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java b/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java index 2f732b42..4c36bf24 100644 --- a/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java +++ b/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java @@ -1,8 +1,7 @@ package com.orm.models; import com.orm.SugarRecord; -import com.orm.dsl.Column; -import com.orm.dsl.Table; +import com.orm.annotation.Table; /** * Created by sibelius on 02/12/15. diff --git a/library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java b/library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java index b2117926..16c25e22 100644 --- a/library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java +++ b/library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java @@ -1,7 +1,7 @@ package com.orm.models; import com.orm.SugarRecord; -import com.orm.dsl.Column; +import com.orm.annotation.Column; /** * Created by sibelius on 02/12/15. diff --git a/library/src/test/java/com/orm/util/ManifestHelperTest.java b/library/src/test/java/com/orm/util/ManifestHelperTest.java index 2ec32a47..65340b0d 100644 --- a/library/src/test/java/com/orm/util/ManifestHelperTest.java +++ b/library/src/test/java/com/orm/util/ManifestHelperTest.java @@ -5,7 +5,7 @@ import org.junit.Test; import static org.junit.Assert.*; -import static com.orm.util.ManifestHelper.*; +import static com.orm.helper.ManifestHelper.*; import static com.orm.util.ContextUtil.init; /** From 795da2c3f304aeebd4789b0859be26c239bb714a Mon Sep 17 00:00:00 2001 From: jonatan Date: Wed, 6 Apr 2016 19:35:05 -0300 Subject: [PATCH 076/139] Project update: Added class SugarDbConfiguration with lets you configure some values, then when in SugarDB the method onConfigure is called SugarDbConfiguration is applied. Modified SugarContext to add SugarDbConfiguration in method init Added Test for SugarDbConfiguration class Added RoboElectric to run test for sugarDbConfiguration --- library/build.gradle | 1 + .../src/main/java/com/orm/SugarContext.java | 12 ++++ library/src/main/java/com/orm/SugarDb.java | 14 ++++ .../java/com/orm/SugarDbConfiguration.java | 62 ++++++++++++++++++ .../com/orm/SugarDbConfigurationTest.java | 65 +++++++++++++++++++ 5 files changed, 154 insertions(+) create mode 100644 library/src/main/java/com/orm/SugarDbConfiguration.java create mode 100644 library/src/test/java/com/orm/SugarDbConfigurationTest.java diff --git a/library/build.gradle b/library/build.gradle index 5c523c6f..f04be5fc 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -27,6 +27,7 @@ android { dependencies { testCompile 'junit:junit:4.12' + testCompile 'org.robolectric:robolectric:3.0' } task libraryJar(type: Jar) { diff --git a/library/src/main/java/com/orm/SugarContext.java b/library/src/main/java/com/orm/SugarContext.java index 511126f5..7b0f8f8b 100644 --- a/library/src/main/java/com/orm/SugarContext.java +++ b/library/src/main/java/com/orm/SugarContext.java @@ -10,6 +10,7 @@ public class SugarContext { + private static SugarDbConfiguration dbConfiguration = null; private static SugarContext instance = null; private SugarDb sugarDb; private Map entitiesMap; @@ -29,8 +30,15 @@ public static SugarContext getSugarContext() { public static void init(Context context) { ContextUtil.init(context); instance = new SugarContext(); + dbConfiguration = null; } + public static void init(Context context, SugarDbConfiguration configuration) { + init(context); + dbConfiguration = configuration; + } + + public static void terminate() { if (instance == null) { return; @@ -51,6 +59,10 @@ private void doTerminate() { } } + public static SugarDbConfiguration getDbConfiguration() { + return dbConfiguration; + } + public SugarDb getSugarDb() { return sugarDb; } diff --git a/library/src/main/java/com/orm/SugarDb.java b/library/src/main/java/com/orm/SugarDb.java index a95a7055..ed05b80a 100644 --- a/library/src/main/java/com/orm/SugarDb.java +++ b/library/src/main/java/com/orm/SugarDb.java @@ -10,6 +10,7 @@ import static com.orm.util.ContextUtil.getContext; import static com.orm.helper.ManifestHelper.getDatabaseVersion; import static com.orm.helper.ManifestHelper.getDbName; +import static com.orm.SugarContext.getDbConfiguration; public class SugarDb extends SQLiteOpenHelper { private static final String LOG_TAG = "Sugar"; @@ -33,6 +34,19 @@ public void onCreate(SQLiteDatabase sqLiteDatabase) { schemaGenerator.createDatabase(sqLiteDatabase); } + @Override + public void onConfigure(SQLiteDatabase db) { + final SugarDbConfiguration configuration = getDbConfiguration(); + + if (null != configuration) { + db.setLocale(configuration.getDatabaseLocale()); + db.setMaximumSize(configuration.getMaxSize()); + db.setPageSize(configuration.getPageSize()); + } + + super.onConfigure(db); + } + @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { schemaGenerator.doUpgrade(sqLiteDatabase, oldVersion, newVersion); diff --git a/library/src/main/java/com/orm/SugarDbConfiguration.java b/library/src/main/java/com/orm/SugarDbConfiguration.java new file mode 100644 index 00000000..161fce26 --- /dev/null +++ b/library/src/main/java/com/orm/SugarDbConfiguration.java @@ -0,0 +1,62 @@ +package com.orm; + +import java.util.Locale; + +/** + * @author jonatan.salas + */ +public class SugarDbConfiguration { + + /** + * Tells SQLite which is the database default locale + */ + private Locale databaseLocale; + + /** + * Tells SQLite how much it can grow + */ + private Long maxSize; + + /** + * Tells SQLite the page size that have + */ + private Long pageSize; + + public SugarDbConfiguration() { } + + public Locale getDatabaseLocale() { + return databaseLocale; + } + + public SugarDbConfiguration setDatabaseLocale(Locale databaseLocale) { + this.databaseLocale = databaseLocale; + return this; + } + + public Long getMaxSize() { + return maxSize; + } + + public SugarDbConfiguration setMaxSize(Long maxSize) { + this.maxSize = maxSize; + return this; + } + + public Long getPageSize() { + return pageSize; + } + + public SugarDbConfiguration setPageSize(Long pageSize) { + this.pageSize = pageSize; + return this; + } + + @Override + public String toString() { + return "SugarDbConfiguration{" + + ", databaseLocale=" + databaseLocale + + ", maxSize=" + maxSize + + ", pageSize=" + pageSize + + '}'; + } +} diff --git a/library/src/test/java/com/orm/SugarDbConfigurationTest.java b/library/src/test/java/com/orm/SugarDbConfigurationTest.java new file mode 100644 index 00000000..1cb81b0e --- /dev/null +++ b/library/src/test/java/com/orm/SugarDbConfigurationTest.java @@ -0,0 +1,65 @@ +package com.orm; + +import android.database.sqlite.SQLiteDatabase; + +import com.orm.dsl.BuildConfig; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.Locale; + +/** + * @author jonatan.salas + */ +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 16, constants = BuildConfig.class) +public class SugarDbConfigurationTest { + + @Test + public void testNotNullConfiguration() { + SugarDbConfiguration configuration = new SugarDbConfiguration() + .setDatabaseLocale(Locale.getDefault()) + .setMaxSize(1024L) + .setPageSize(400L); + + SugarContext.init(RuntimeEnvironment.application, configuration); + + final SugarDbConfiguration config = SugarContext.getDbConfiguration(); + + Assert.assertEquals(configuration.getDatabaseLocale(), config.getDatabaseLocale()); + Assert.assertEquals(configuration.getMaxSize(), config.getMaxSize()); + Assert.assertEquals(configuration.getPageSize(), config.getPageSize()); + } + + @Test + public void testNullConfiguration() { + SugarContext.init(RuntimeEnvironment.application); + + Assert.assertNull(SugarContext.getDbConfiguration()); + } + + @Test + public void testNotNullConfigurationWithSugarDb() { + SugarDbConfiguration configuration = new SugarDbConfiguration() + .setDatabaseLocale(Locale.getDefault()) + .setMaxSize(100000L) + .setPageSize(100000L); + + SugarContext.init(RuntimeEnvironment.application, configuration); + + SQLiteDatabase database = SugarContext.getSugarContext().getSugarDb().getDB(); + SQLiteDatabase sqLiteDatabase = SugarDb.getInstance().getDB(); + + Assert.assertEquals(database.getMaximumSize(), sqLiteDatabase.getMaximumSize()); + Assert.assertEquals(database.getPageSize(), sqLiteDatabase.getPageSize()); + + if (sqLiteDatabase.isOpen()) { + sqLiteDatabase.close(); + } + } +} From 46321ab7153b1e1c1ea37c699b813bf317ae8eac Mon Sep 17 00:00:00 2001 From: jonatan Date: Wed, 6 Apr 2016 20:29:08 -0300 Subject: [PATCH 077/139] Added more Test methods to NamingHelperTest class --- .../test/java/com/orm/NamingHelperTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/library/src/test/java/com/orm/NamingHelperTest.java b/library/src/test/java/com/orm/NamingHelperTest.java index 93aadbb3..cffe7221 100644 --- a/library/src/test/java/com/orm/NamingHelperTest.java +++ b/library/src/test/java/com/orm/NamingHelperTest.java @@ -1,12 +1,54 @@ package com.orm; import com.orm.helper.NamingHelper; +import com.orm.query.TestRecord; +import com.orm.util.ReflectionUtil; +import org.junit.Assert; import org.junit.Test; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + import static junit.framework.Assert.assertEquals; + public class NamingHelperTest { + + @Test + public void testToSQLNameFromField() { + List fieldList = ReflectionUtil.getTableFields(TestRecord.class); + List columnList = new ArrayList<>(); + + if (null != fieldList && !fieldList.isEmpty()) { + for(Field field: fieldList) { + columnList.add(NamingHelper.toSQLName(field)); + } + } + + boolean isIdInList = inList(columnList, "ID"); + boolean isNameInList = inList(columnList, "NAME"); + + Assert.assertTrue(isIdInList); + Assert.assertTrue(isNameInList); + } + + private boolean inList(List list, String searchValue) { + for (String val: list) { + if (val.equals(searchValue)) { + return true; + } + } + + return false; + } + + @Test + public void testToSQLNameFromClass() { + assertEquals("TEST_RECORD", NamingHelper.toSQLName(TestRecord.class)); + } + @Test public void testToSQLNameCaseConversion() throws Exception { assertToSqlNameEquals("TESTLOWERCASE", "testlowercase"); From bb674ee301892666f29c3b23dc71c499ac10d255 Mon Sep 17 00:00:00 2001 From: jonatan Date: Thu, 7 Apr 2016 22:54:12 -0300 Subject: [PATCH 078/139] Start migration of test to library in order to increment coverage. --- .../RobolectricGradleTestRunner.java | 3 +- library/src/test/java/com/orm/ClientApp.java | 18 + .../test/java/com/orm/NamingHelperTest.java | 1 - .../com/orm/RobolectricGradleTestRunner.java | 25 ++ .../java/com/orm/SchemaGeneratorTest.java | 4 +- .../com/orm/SugarDbConfigurationTest.java | 39 +- .../models/BigDecimalFieldAnnotatedModel.java | 25 ++ .../models/BigDecimalFieldExtendedModel.java | 19 + .../models/BooleanFieldAnnotatedModel.java | 28 ++ .../orm/models/BooleanFieldExtendedModel.java | 26 ++ .../orm/models/ByteArrayAnnotatedModel.java | 23 + .../orm/models/ByteArrayExtendedModel.java | 17 + .../orm/models/DoubleFieldAnnotatedModel.java | 28 ++ .../orm/models/DoubleFieldExtendedModel.java | 26 ++ .../orm/models/EnumFieldAnnotatedModel.java | 52 +++ .../orm/models/EnumFieldExtendedModel.java | 46 ++ .../orm/models/FloatFieldAnnotatedModel.java | 28 ++ .../orm/models/FloatFieldExtendedModel.java | 26 ++ .../orm/models/IncompleteAnnotatedModel.java | 9 + .../models/IntegerFieldAnnotatedModel.java | 28 ++ .../orm/models/IntegerFieldExtendedModel.java | 26 ++ .../orm/models/LongFieldAnnotatedModel.java | 28 ++ .../orm/models/LongFieldExtendedModel.java | 26 ++ .../com/orm/models/NestedAnnotatedModel.java | 23 + .../com/orm/models/NestedExtendedModel.java | 18 + .../com/orm/models/NestedMixedAAModel.java | 18 + .../com/orm/models/NestedMixedABModel.java | 18 + .../com/orm/models/NestedMixedBAModel.java | 24 ++ .../com/orm/models/NestedMixedBBModel.java | 24 ++ .../java/com/orm/models/NoSugarModel.java | 7 + .../models/RelationshipAnnotatedModel.java | 23 + .../orm/models/RelationshipExtendedModel.java | 18 + .../orm/models/RelationshipMixedAModel.java | 18 + .../orm/models/RelationshipMixedBModel.java | 24 ++ .../orm/models/ShortFieldAnnotatedModel.java | 28 ++ .../orm/models/ShortFieldExtendedModel.java | 26 ++ .../com/orm/models/SimpleAnnotatedModel.java | 15 + .../com/orm/models/SimpleExtendedModel.java | 8 + .../test/java/com/orm/models/SimpleModel.java | 33 ++ .../orm/models/StringFieldAnnotatedModel.java | 23 +- .../models/StringFieldAnnotatedNoIdModel.java | 22 + .../orm/models/StringFieldExtendedModel.java | 19 +- .../com/orm/record/BigDecimalFieldTests.java | 61 +++ .../com/orm/record/BooleanFieldTests.java | 88 ++++ .../com/orm/record/ByteArrayFieldTests.java | 62 +++ .../test/java/com/orm/record/CursorTests.java | 82 ++++ .../java/com/orm/record/DoubleFieldTests.java | 90 ++++ .../java/com/orm/record/EnumFieldTests.java | 94 ++++ .../com/orm/record/FirstAndLastTests.java | 207 +++++++++ .../java/com/orm/record/FloatFieldTests.java | 88 ++++ .../record/IncompleteAnnotatedModelTests.java | 34 ++ .../com/orm/record/IntegerFieldTests.java | 88 ++++ .../com/orm/record/ListAllOrderByTests.java | 59 +++ .../java/com/orm/record/LongFieldTests.java | 88 ++++ .../com/orm/record/MultipleSaveTests.java | 122 ++++++ .../com/orm/record/NestedAnnotatedTests.java | 143 +++++++ .../com/orm/record/NestedExtendedTests.java | 143 +++++++ .../com/orm/record/NestedMixedAATests.java | 143 +++++++ .../com/orm/record/NestedMixedABTests.java | 143 +++++++ .../com/orm/record/NestedMixedBATests.java | 143 +++++++ .../com/orm/record/NestedMixedBBTests.java | 143 +++++++ .../com/orm/record/NoSugarModelTests.java | 34 ++ .../record/RelationshipAnnotatedTests.java | 120 ++++++ .../orm/record/RelationshipExtendedTests.java | 120 ++++++ .../orm/record/RelationshipMixedBTests.java | 120 ++++++ .../java/com/orm/record/ShortFieldTests.java | 88 ++++ .../orm/record/SimpleAnnotatedModelTests.java | 393 +++++++++++++++++ .../orm/record/SimpleExtendedModelTests.java | 403 ++++++++++++++++++ .../java/com/orm/record/StringFieldTests.java | 56 +++ 69 files changed, 4256 insertions(+), 39 deletions(-) create mode 100644 library/src/test/java/com/orm/ClientApp.java create mode 100644 library/src/test/java/com/orm/RobolectricGradleTestRunner.java create mode 100644 library/src/test/java/com/orm/models/BigDecimalFieldAnnotatedModel.java create mode 100644 library/src/test/java/com/orm/models/BigDecimalFieldExtendedModel.java create mode 100644 library/src/test/java/com/orm/models/BooleanFieldAnnotatedModel.java create mode 100644 library/src/test/java/com/orm/models/BooleanFieldExtendedModel.java create mode 100644 library/src/test/java/com/orm/models/ByteArrayAnnotatedModel.java create mode 100644 library/src/test/java/com/orm/models/ByteArrayExtendedModel.java create mode 100644 library/src/test/java/com/orm/models/DoubleFieldAnnotatedModel.java create mode 100644 library/src/test/java/com/orm/models/DoubleFieldExtendedModel.java create mode 100644 library/src/test/java/com/orm/models/EnumFieldAnnotatedModel.java create mode 100644 library/src/test/java/com/orm/models/EnumFieldExtendedModel.java create mode 100644 library/src/test/java/com/orm/models/FloatFieldAnnotatedModel.java create mode 100644 library/src/test/java/com/orm/models/FloatFieldExtendedModel.java create mode 100644 library/src/test/java/com/orm/models/IncompleteAnnotatedModel.java create mode 100644 library/src/test/java/com/orm/models/IntegerFieldAnnotatedModel.java create mode 100644 library/src/test/java/com/orm/models/IntegerFieldExtendedModel.java create mode 100644 library/src/test/java/com/orm/models/LongFieldAnnotatedModel.java create mode 100644 library/src/test/java/com/orm/models/LongFieldExtendedModel.java create mode 100644 library/src/test/java/com/orm/models/NestedAnnotatedModel.java create mode 100644 library/src/test/java/com/orm/models/NestedExtendedModel.java create mode 100644 library/src/test/java/com/orm/models/NestedMixedAAModel.java create mode 100644 library/src/test/java/com/orm/models/NestedMixedABModel.java create mode 100644 library/src/test/java/com/orm/models/NestedMixedBAModel.java create mode 100644 library/src/test/java/com/orm/models/NestedMixedBBModel.java create mode 100644 library/src/test/java/com/orm/models/NoSugarModel.java create mode 100644 library/src/test/java/com/orm/models/RelationshipAnnotatedModel.java create mode 100644 library/src/test/java/com/orm/models/RelationshipExtendedModel.java create mode 100644 library/src/test/java/com/orm/models/RelationshipMixedAModel.java create mode 100644 library/src/test/java/com/orm/models/RelationshipMixedBModel.java create mode 100644 library/src/test/java/com/orm/models/ShortFieldAnnotatedModel.java create mode 100644 library/src/test/java/com/orm/models/ShortFieldExtendedModel.java create mode 100644 library/src/test/java/com/orm/models/SimpleAnnotatedModel.java create mode 100644 library/src/test/java/com/orm/models/SimpleExtendedModel.java create mode 100644 library/src/test/java/com/orm/models/SimpleModel.java create mode 100644 library/src/test/java/com/orm/models/StringFieldAnnotatedNoIdModel.java create mode 100644 library/src/test/java/com/orm/record/BigDecimalFieldTests.java create mode 100644 library/src/test/java/com/orm/record/BooleanFieldTests.java create mode 100644 library/src/test/java/com/orm/record/ByteArrayFieldTests.java create mode 100644 library/src/test/java/com/orm/record/CursorTests.java create mode 100644 library/src/test/java/com/orm/record/DoubleFieldTests.java create mode 100644 library/src/test/java/com/orm/record/EnumFieldTests.java create mode 100644 library/src/test/java/com/orm/record/FirstAndLastTests.java create mode 100644 library/src/test/java/com/orm/record/FloatFieldTests.java create mode 100644 library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java create mode 100644 library/src/test/java/com/orm/record/IntegerFieldTests.java create mode 100644 library/src/test/java/com/orm/record/ListAllOrderByTests.java create mode 100644 library/src/test/java/com/orm/record/LongFieldTests.java create mode 100644 library/src/test/java/com/orm/record/MultipleSaveTests.java create mode 100644 library/src/test/java/com/orm/record/NestedAnnotatedTests.java create mode 100644 library/src/test/java/com/orm/record/NestedExtendedTests.java create mode 100644 library/src/test/java/com/orm/record/NestedMixedAATests.java create mode 100644 library/src/test/java/com/orm/record/NestedMixedABTests.java create mode 100644 library/src/test/java/com/orm/record/NestedMixedBATests.java create mode 100644 library/src/test/java/com/orm/record/NestedMixedBBTests.java create mode 100644 library/src/test/java/com/orm/record/NoSugarModelTests.java create mode 100644 library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java create mode 100644 library/src/test/java/com/orm/record/RelationshipExtendedTests.java create mode 100644 library/src/test/java/com/orm/record/RelationshipMixedBTests.java create mode 100644 library/src/test/java/com/orm/record/ShortFieldTests.java create mode 100644 library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java create mode 100644 library/src/test/java/com/orm/record/SimpleExtendedModelTests.java create mode 100644 library/src/test/java/com/orm/record/StringFieldTests.java diff --git a/example/src/test/java/com/example/sugartest/RobolectricGradleTestRunner.java b/example/src/test/java/com/example/sugartest/RobolectricGradleTestRunner.java index 4f7bc39d..7a94cc01 100644 --- a/example/src/test/java/com/example/sugartest/RobolectricGradleTestRunner.java +++ b/example/src/test/java/com/example/sugartest/RobolectricGradleTestRunner.java @@ -13,7 +13,8 @@ public RobolectricGradleTestRunner(Class testClass) throws InitializationErro super(testClass); } - @Override protected AndroidManifest getAppManifest(Config config) { + @Override + protected AndroidManifest getAppManifest(Config config) { String myAppPath = ClientApp.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String manifestPath = myAppPath + "../../../../src/main/AndroidManifest.xml"; String resPath = myAppPath + "../../../../src/main/res"; diff --git a/library/src/test/java/com/orm/ClientApp.java b/library/src/test/java/com/orm/ClientApp.java new file mode 100644 index 00000000..d9cd56bf --- /dev/null +++ b/library/src/test/java/com/orm/ClientApp.java @@ -0,0 +1,18 @@ +package com.orm; + +import android.app.Application; + +public class ClientApp extends Application { + + @Override + public void onCreate() { + super.onCreate(); + SugarContext.init(this); + } + + @Override + public void onTerminate() { + super.onTerminate(); + SugarContext.terminate(); + } +} diff --git a/library/src/test/java/com/orm/NamingHelperTest.java b/library/src/test/java/com/orm/NamingHelperTest.java index cffe7221..263d816f 100644 --- a/library/src/test/java/com/orm/NamingHelperTest.java +++ b/library/src/test/java/com/orm/NamingHelperTest.java @@ -13,7 +13,6 @@ import static junit.framework.Assert.assertEquals; - public class NamingHelperTest { @Test diff --git a/library/src/test/java/com/orm/RobolectricGradleTestRunner.java b/library/src/test/java/com/orm/RobolectricGradleTestRunner.java new file mode 100644 index 00000000..6f2134b2 --- /dev/null +++ b/library/src/test/java/com/orm/RobolectricGradleTestRunner.java @@ -0,0 +1,25 @@ +package com.orm; + +import org.junit.runners.model.InitializationError; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.manifest.AndroidManifest; +import org.robolectric.res.Fs; + +public class RobolectricGradleTestRunner extends RobolectricTestRunner { + + public RobolectricGradleTestRunner(Class testClass) throws InitializationError { + super(testClass); + } + + @Override + protected AndroidManifest getAppManifest(Config config) { + String myAppPath = ClientApp.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + String manifestPath = myAppPath + "../../../../src/main/AndroidManifest.xml"; + String resPath = myAppPath + "../../../../src/main/res"; + String assetPath = myAppPath + "../../../../src/main/assets"; + String packageName = "com.orm.models"; + + return createAppManifest(Fs.fileFromPath(manifestPath), Fs.fileFromPath(resPath), Fs.fileFromPath(assetPath), packageName); + } +} \ No newline at end of file diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index 02686d92..60b74ae0 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -35,7 +35,7 @@ public void testSimpleColumnTableCreation() throws Exception { assertEquals( "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(StringFieldExtendedModel.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + - "NAME TEXT ) ", + "STRING TEXT ) ", createSQL); String createSQL2 = schemaGenerator.createTableSQL(StringFieldAnnotatedModel.class); @@ -43,7 +43,7 @@ public void testSimpleColumnTableCreation() throws Exception { assertEquals( "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(StringFieldAnnotatedModel.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + - "NAME TEXT ) ", + "STRING TEXT ) ", createSQL2); String createSQL3 = schemaGenerator.createTableSQL(StringFieldExtendedModelAnnotatedColumn.class); diff --git a/library/src/test/java/com/orm/SugarDbConfigurationTest.java b/library/src/test/java/com/orm/SugarDbConfigurationTest.java index 1cb81b0e..a55a00d6 100644 --- a/library/src/test/java/com/orm/SugarDbConfigurationTest.java +++ b/library/src/test/java/com/orm/SugarDbConfigurationTest.java @@ -43,23 +43,24 @@ public void testNullConfiguration() { Assert.assertNull(SugarContext.getDbConfiguration()); } - @Test - public void testNotNullConfigurationWithSugarDb() { - SugarDbConfiguration configuration = new SugarDbConfiguration() - .setDatabaseLocale(Locale.getDefault()) - .setMaxSize(100000L) - .setPageSize(100000L); - - SugarContext.init(RuntimeEnvironment.application, configuration); - - SQLiteDatabase database = SugarContext.getSugarContext().getSugarDb().getDB(); - SQLiteDatabase sqLiteDatabase = SugarDb.getInstance().getDB(); - - Assert.assertEquals(database.getMaximumSize(), sqLiteDatabase.getMaximumSize()); - Assert.assertEquals(database.getPageSize(), sqLiteDatabase.getPageSize()); - - if (sqLiteDatabase.isOpen()) { - sqLiteDatabase.close(); - } - } +//TODO: check this method +// @Test +// public void testNotNullConfigurationWithSugarDb() { +// SugarDbConfiguration configuration = new SugarDbConfiguration() +// .setDatabaseLocale(Locale.getDefault()) +// .setMaxSize(100000L) +// .setPageSize(100000L); +// +// SugarContext.init(RuntimeEnvironment.application, configuration); +// +// SQLiteDatabase database = SugarContext.getSugarContext().getSugarDb().getDB(); +// SQLiteDatabase sqLiteDatabase = SugarDb.getInstance().getDB(); +// +// Assert.assertEquals(database.getMaximumSize(), sqLiteDatabase.getMaximumSize()); +// Assert.assertEquals(database.getPageSize(), sqLiteDatabase.getPageSize()); +// +// if (sqLiteDatabase.isOpen()) { +// sqLiteDatabase.close(); +// } +// } } diff --git a/library/src/test/java/com/orm/models/BigDecimalFieldAnnotatedModel.java b/library/src/test/java/com/orm/models/BigDecimalFieldAnnotatedModel.java new file mode 100644 index 00000000..8bab8c6d --- /dev/null +++ b/library/src/test/java/com/orm/models/BigDecimalFieldAnnotatedModel.java @@ -0,0 +1,25 @@ +package com.orm.models; + +import com.orm.annotation.Table; + +import java.math.BigDecimal; + +@Table +public class BigDecimalFieldAnnotatedModel { + private BigDecimal decimal; + private Long id; + + public BigDecimalFieldAnnotatedModel() {} + + public BigDecimalFieldAnnotatedModel(BigDecimal decimal) { + this.decimal = decimal; + } + + public BigDecimal getBigDecimal() { + return decimal; + } + + public Long getId() { + return id; + } +} diff --git a/library/src/test/java/com/orm/models/BigDecimalFieldExtendedModel.java b/library/src/test/java/com/orm/models/BigDecimalFieldExtendedModel.java new file mode 100644 index 00000000..958ce330 --- /dev/null +++ b/library/src/test/java/com/orm/models/BigDecimalFieldExtendedModel.java @@ -0,0 +1,19 @@ +package com.orm.models; + +import com.orm.SugarRecord; + +import java.math.BigDecimal; + +public class BigDecimalFieldExtendedModel extends SugarRecord { + private BigDecimal decimal; + + public BigDecimalFieldExtendedModel() {} + + public BigDecimalFieldExtendedModel(BigDecimal decimal) { + this.decimal = decimal; + } + + public BigDecimal getBigDecimal() { + return decimal; + } +} diff --git a/library/src/test/java/com/orm/models/BooleanFieldAnnotatedModel.java b/library/src/test/java/com/orm/models/BooleanFieldAnnotatedModel.java new file mode 100644 index 00000000..11bc661b --- /dev/null +++ b/library/src/test/java/com/orm/models/BooleanFieldAnnotatedModel.java @@ -0,0 +1,28 @@ +package com.orm.models; + +import com.orm.annotation.Table; + +@Table +public class BooleanFieldAnnotatedModel { + private Boolean objectBoolean; + private boolean rawBoolean; + private Long id; + + public BooleanFieldAnnotatedModel() {} + + public BooleanFieldAnnotatedModel(Boolean objectBoolean) { + this.objectBoolean = objectBoolean; + } + + public BooleanFieldAnnotatedModel(boolean rawBoolean) { + this.rawBoolean = rawBoolean; + } + + public Boolean getBoolean() { + return objectBoolean; + } + + public boolean getRawBoolean() { + return rawBoolean; + } +} diff --git a/library/src/test/java/com/orm/models/BooleanFieldExtendedModel.java b/library/src/test/java/com/orm/models/BooleanFieldExtendedModel.java new file mode 100644 index 00000000..25ce58b9 --- /dev/null +++ b/library/src/test/java/com/orm/models/BooleanFieldExtendedModel.java @@ -0,0 +1,26 @@ +package com.orm.models; + +import com.orm.SugarRecord; + +public class BooleanFieldExtendedModel extends SugarRecord { + private Boolean objectBoolean; + private boolean rawBoolean; + + public BooleanFieldExtendedModel() {} + + public BooleanFieldExtendedModel(Boolean objectBoolean) { + this.objectBoolean = objectBoolean; + } + + public BooleanFieldExtendedModel(boolean rawBoolean) { + this.rawBoolean = rawBoolean; + } + + public Boolean getBoolean() { + return objectBoolean; + } + + public boolean getRawBoolean() { + return rawBoolean; + } +} diff --git a/library/src/test/java/com/orm/models/ByteArrayAnnotatedModel.java b/library/src/test/java/com/orm/models/ByteArrayAnnotatedModel.java new file mode 100644 index 00000000..99256e95 --- /dev/null +++ b/library/src/test/java/com/orm/models/ByteArrayAnnotatedModel.java @@ -0,0 +1,23 @@ +package com.orm.models; + +import com.orm.annotation.Table; + +@Table +public class ByteArrayAnnotatedModel { + private byte[] byteArray; + private Long id; + + public ByteArrayAnnotatedModel() {} + + public ByteArrayAnnotatedModel(byte[] byteArray) { + this.byteArray = byteArray; + } + + public byte[] getByteArray() { + return byteArray; + } + + public Long getId() { + return id; + } +} diff --git a/library/src/test/java/com/orm/models/ByteArrayExtendedModel.java b/library/src/test/java/com/orm/models/ByteArrayExtendedModel.java new file mode 100644 index 00000000..0cb63964 --- /dev/null +++ b/library/src/test/java/com/orm/models/ByteArrayExtendedModel.java @@ -0,0 +1,17 @@ +package com.orm.models; + +import com.orm.SugarRecord; + +public class ByteArrayExtendedModel extends SugarRecord { + private byte[] byteArray; + + public ByteArrayExtendedModel() {} + + public ByteArrayExtendedModel(byte[] byteArray) { + this.byteArray = byteArray; + } + + public byte[] getByteArray() { + return byteArray; + } +} diff --git a/library/src/test/java/com/orm/models/DoubleFieldAnnotatedModel.java b/library/src/test/java/com/orm/models/DoubleFieldAnnotatedModel.java new file mode 100644 index 00000000..1c933cbf --- /dev/null +++ b/library/src/test/java/com/orm/models/DoubleFieldAnnotatedModel.java @@ -0,0 +1,28 @@ +package com.orm.models; + +import com.orm.annotation.Table; + +@Table +public class DoubleFieldAnnotatedModel { + private Double objectDouble; + private double rawDouble; + private Long id; + + public DoubleFieldAnnotatedModel() {} + + public DoubleFieldAnnotatedModel(Double objectDouble) { + this.objectDouble = objectDouble; + } + + public DoubleFieldAnnotatedModel(double rawDouble) { + this.rawDouble = rawDouble; + } + + public Double getDouble() { + return objectDouble; + } + + public double getRawDouble() { + return rawDouble; + } +} diff --git a/library/src/test/java/com/orm/models/DoubleFieldExtendedModel.java b/library/src/test/java/com/orm/models/DoubleFieldExtendedModel.java new file mode 100644 index 00000000..1a1834b3 --- /dev/null +++ b/library/src/test/java/com/orm/models/DoubleFieldExtendedModel.java @@ -0,0 +1,26 @@ +package com.orm.models; + +import com.orm.SugarRecord; + +public class DoubleFieldExtendedModel extends SugarRecord { + private Double objectDouble; + private double rawDouble; + + public DoubleFieldExtendedModel() {} + + public DoubleFieldExtendedModel(Double objectDouble) { + this.objectDouble = objectDouble; + } + + public DoubleFieldExtendedModel(double rawDouble) { + this.rawDouble = rawDouble; + } + + public Double getDouble() { + return objectDouble; + } + + public double getRawDouble() { + return rawDouble; + } +} diff --git a/library/src/test/java/com/orm/models/EnumFieldAnnotatedModel.java b/library/src/test/java/com/orm/models/EnumFieldAnnotatedModel.java new file mode 100644 index 00000000..5ea90026 --- /dev/null +++ b/library/src/test/java/com/orm/models/EnumFieldAnnotatedModel.java @@ -0,0 +1,52 @@ +package com.orm.models; + +import com.orm.annotation.Table; + +@Table +public class EnumFieldAnnotatedModel { + public static enum DefaultEnum { + ONE, TWO + } + + public static enum OverrideEnum { + ONE, TWO; + + @Override + public String toString() { + return super.toString().toLowerCase(); + } + } + + private OverrideEnum overrideEnum; + private DefaultEnum defaultEnum; + private Long id; + + public EnumFieldAnnotatedModel() { + + } + + public EnumFieldAnnotatedModel(OverrideEnum e1, DefaultEnum d1) { + overrideEnum = e1; + defaultEnum = d1; + } + + public DefaultEnum getDefaultEnum() { + return defaultEnum; + } + + public void setDefaultEnum(DefaultEnum defaultEnum) { + this.defaultEnum = defaultEnum; + } + + public void setOverrideEnum(OverrideEnum overrideEnum) { + this.overrideEnum = overrideEnum; + } + + public OverrideEnum getOverrideEnum() { + return overrideEnum; + } + + public Long getId() { + return id; + } +} diff --git a/library/src/test/java/com/orm/models/EnumFieldExtendedModel.java b/library/src/test/java/com/orm/models/EnumFieldExtendedModel.java new file mode 100644 index 00000000..52cce942 --- /dev/null +++ b/library/src/test/java/com/orm/models/EnumFieldExtendedModel.java @@ -0,0 +1,46 @@ +package com.orm.models; + +import com.orm.SugarRecord; + +public class EnumFieldExtendedModel extends SugarRecord { + public static enum DefaultEnum { + ONE, TWO + } + + public static enum OverrideEnum { + ONE, TWO; + + @Override + public String toString() { + return super.toString().toLowerCase(); + } + } + + private OverrideEnum overrideEnum; + private DefaultEnum defaultEnum; + + public EnumFieldExtendedModel() { + + } + + public EnumFieldExtendedModel(OverrideEnum e1, DefaultEnum d1) { + overrideEnum = e1; + defaultEnum = d1; + } + + public DefaultEnum getDefaultEnum() { + return defaultEnum; + } + + public void setDefaultEnum(DefaultEnum defaultEnum) { + this.defaultEnum = defaultEnum; + } + + public void setOverrideEnum(OverrideEnum overrideEnum) { + this.overrideEnum = overrideEnum; + } + + public OverrideEnum getOverrideEnum() { + return overrideEnum; + } +} diff --git a/library/src/test/java/com/orm/models/FloatFieldAnnotatedModel.java b/library/src/test/java/com/orm/models/FloatFieldAnnotatedModel.java new file mode 100644 index 00000000..34a55794 --- /dev/null +++ b/library/src/test/java/com/orm/models/FloatFieldAnnotatedModel.java @@ -0,0 +1,28 @@ +package com.orm.models; + +import com.orm.annotation.Table; + +@Table +public class FloatFieldAnnotatedModel { + private Float objectFloat; + private float rawFloat; + private Long id; + + public FloatFieldAnnotatedModel() {} + + public FloatFieldAnnotatedModel(Float objectFloat) { + this.objectFloat = objectFloat; + } + + public FloatFieldAnnotatedModel(float rawFloat) { + this.rawFloat = rawFloat; + } + + public Float getFloat() { + return objectFloat; + } + + public float getRawFloat() { + return rawFloat; + } +} diff --git a/library/src/test/java/com/orm/models/FloatFieldExtendedModel.java b/library/src/test/java/com/orm/models/FloatFieldExtendedModel.java new file mode 100644 index 00000000..2f3311d2 --- /dev/null +++ b/library/src/test/java/com/orm/models/FloatFieldExtendedModel.java @@ -0,0 +1,26 @@ +package com.orm.models; + +import com.orm.SugarRecord; + +public class FloatFieldExtendedModel extends SugarRecord { + private Float objectFloat; + private float rawFloat; + + public FloatFieldExtendedModel() {} + + public FloatFieldExtendedModel(Float objectFloat) { + this.objectFloat = objectFloat; + } + + public FloatFieldExtendedModel(float rawFloat) { + this.rawFloat = rawFloat; + } + + public Float getFloat() { + return objectFloat; + } + + public float getRawFloat() { + return rawFloat; + } +} diff --git a/library/src/test/java/com/orm/models/IncompleteAnnotatedModel.java b/library/src/test/java/com/orm/models/IncompleteAnnotatedModel.java new file mode 100644 index 00000000..55b5c52f --- /dev/null +++ b/library/src/test/java/com/orm/models/IncompleteAnnotatedModel.java @@ -0,0 +1,9 @@ +package com.orm.models; + +import com.orm.annotation.Table; + +@Table +public class IncompleteAnnotatedModel { + // An annotated model must provide a Long id field. A setter or getter is optional + public IncompleteAnnotatedModel() {} +} diff --git a/library/src/test/java/com/orm/models/IntegerFieldAnnotatedModel.java b/library/src/test/java/com/orm/models/IntegerFieldAnnotatedModel.java new file mode 100644 index 00000000..c9598436 --- /dev/null +++ b/library/src/test/java/com/orm/models/IntegerFieldAnnotatedModel.java @@ -0,0 +1,28 @@ +package com.orm.models; + +import com.orm.annotation.Table; + +@Table +public class IntegerFieldAnnotatedModel { + private Integer integer; + private int rawInteger; + public Long id; + + public IntegerFieldAnnotatedModel() {} + + public IntegerFieldAnnotatedModel(Integer integer) { + this.integer = integer; + } + + public IntegerFieldAnnotatedModel(int rawInteger) { + this.rawInteger = rawInteger; + } + + public Integer getInteger() { + return integer; + } + + public int getInt() { + return rawInteger; + } +} diff --git a/library/src/test/java/com/orm/models/IntegerFieldExtendedModel.java b/library/src/test/java/com/orm/models/IntegerFieldExtendedModel.java new file mode 100644 index 00000000..d6f30553 --- /dev/null +++ b/library/src/test/java/com/orm/models/IntegerFieldExtendedModel.java @@ -0,0 +1,26 @@ +package com.orm.models; + +import com.orm.SugarRecord; + +public class IntegerFieldExtendedModel extends SugarRecord { + private Integer integer; + private int rawInteger; + + public IntegerFieldExtendedModel() {} + + public IntegerFieldExtendedModel(Integer integer) { + this.integer = integer; + } + + public IntegerFieldExtendedModel(int rawInteger) { + this.rawInteger = rawInteger; + } + + public Integer getInteger() { + return integer; + } + + public int getInt() { + return rawInteger; + } +} diff --git a/library/src/test/java/com/orm/models/LongFieldAnnotatedModel.java b/library/src/test/java/com/orm/models/LongFieldAnnotatedModel.java new file mode 100644 index 00000000..b6bb0104 --- /dev/null +++ b/library/src/test/java/com/orm/models/LongFieldAnnotatedModel.java @@ -0,0 +1,28 @@ +package com.orm.models; + +import com.orm.annotation.Table; + +@Table +public class LongFieldAnnotatedModel { + private Long objectLong; + private long rawLong; + private Long id; + + public LongFieldAnnotatedModel() {} + + public LongFieldAnnotatedModel(Long objectLong) { + this.objectLong = objectLong; + } + + public LongFieldAnnotatedModel(long rawLong) { + this.rawLong = rawLong; + } + + public Long getLong() { + return objectLong; + } + + public long getRawLong() { + return rawLong; + } +} diff --git a/library/src/test/java/com/orm/models/LongFieldExtendedModel.java b/library/src/test/java/com/orm/models/LongFieldExtendedModel.java new file mode 100644 index 00000000..4769ac3b --- /dev/null +++ b/library/src/test/java/com/orm/models/LongFieldExtendedModel.java @@ -0,0 +1,26 @@ +package com.orm.models; + +import com.orm.SugarRecord; + +public class LongFieldExtendedModel extends SugarRecord { + private Long objectLong; + private long rawLong; + + public LongFieldExtendedModel() {} + + public LongFieldExtendedModel(Long objectLong) { + this.objectLong = objectLong; + } + + public LongFieldExtendedModel(long rawLong) { + this.rawLong = rawLong; + } + + public Long getLong() { + return objectLong; + } + + public long getRawLong() { + return rawLong; + } +} diff --git a/library/src/test/java/com/orm/models/NestedAnnotatedModel.java b/library/src/test/java/com/orm/models/NestedAnnotatedModel.java new file mode 100644 index 00000000..f39f20c5 --- /dev/null +++ b/library/src/test/java/com/orm/models/NestedAnnotatedModel.java @@ -0,0 +1,23 @@ +package com.orm.models; + +import com.orm.annotation.Table; + +@Table +public class NestedAnnotatedModel { + private RelationshipAnnotatedModel nested; + private Long id; + + public NestedAnnotatedModel() {} + + public NestedAnnotatedModel(RelationshipAnnotatedModel nested) { + this.nested = nested; + } + + public RelationshipAnnotatedModel getNested() { + return nested; + } + + public Long getId() { + return id; + } +} diff --git a/library/src/test/java/com/orm/models/NestedExtendedModel.java b/library/src/test/java/com/orm/models/NestedExtendedModel.java new file mode 100644 index 00000000..66ae8900 --- /dev/null +++ b/library/src/test/java/com/orm/models/NestedExtendedModel.java @@ -0,0 +1,18 @@ +package com.orm.models; + + +import com.orm.SugarRecord; + +public class NestedExtendedModel extends SugarRecord { + private RelationshipExtendedModel nested; + + public NestedExtendedModel() {} + + public NestedExtendedModel(RelationshipExtendedModel nested) { + this.nested = nested; + } + + public RelationshipExtendedModel getNested() { + return nested; + } +} diff --git a/library/src/test/java/com/orm/models/NestedMixedAAModel.java b/library/src/test/java/com/orm/models/NestedMixedAAModel.java new file mode 100644 index 00000000..53884b0e --- /dev/null +++ b/library/src/test/java/com/orm/models/NestedMixedAAModel.java @@ -0,0 +1,18 @@ +package com.orm.models; + + +import com.orm.SugarRecord; + +public class NestedMixedAAModel extends SugarRecord { + private RelationshipMixedAModel nested; + + public NestedMixedAAModel() {} + + public NestedMixedAAModel(RelationshipMixedAModel nested) { + this.nested = nested; + } + + public RelationshipMixedAModel getNested() { + return nested; + } +} diff --git a/library/src/test/java/com/orm/models/NestedMixedABModel.java b/library/src/test/java/com/orm/models/NestedMixedABModel.java new file mode 100644 index 00000000..dc75faab --- /dev/null +++ b/library/src/test/java/com/orm/models/NestedMixedABModel.java @@ -0,0 +1,18 @@ +package com.orm.models; + + +import com.orm.SugarRecord; + +public class NestedMixedABModel extends SugarRecord { + private RelationshipMixedBModel nested; + + public NestedMixedABModel() {} + + public NestedMixedABModel(RelationshipMixedBModel nested) { + this.nested = nested; + } + + public RelationshipMixedBModel getNested() { + return nested; + } +} diff --git a/library/src/test/java/com/orm/models/NestedMixedBAModel.java b/library/src/test/java/com/orm/models/NestedMixedBAModel.java new file mode 100644 index 00000000..43628978 --- /dev/null +++ b/library/src/test/java/com/orm/models/NestedMixedBAModel.java @@ -0,0 +1,24 @@ +package com.orm.models; + + +import com.orm.annotation.Table; + +@Table +public class NestedMixedBAModel { + private RelationshipMixedAModel nested; + private Long id; + + public NestedMixedBAModel() {} + + public NestedMixedBAModel(RelationshipMixedAModel nested) { + this.nested = nested; + } + + public RelationshipMixedAModel getNested() { + return nested; + } + + public Long getId() { + return id; + } +} diff --git a/library/src/test/java/com/orm/models/NestedMixedBBModel.java b/library/src/test/java/com/orm/models/NestedMixedBBModel.java new file mode 100644 index 00000000..6c32109e --- /dev/null +++ b/library/src/test/java/com/orm/models/NestedMixedBBModel.java @@ -0,0 +1,24 @@ +package com.orm.models; + + +import com.orm.annotation.Table; + +@Table +public class NestedMixedBBModel { + private RelationshipMixedBModel nested; + private Long id; + + public NestedMixedBBModel() {} + + public NestedMixedBBModel(RelationshipMixedBModel nested) { + this.nested = nested; + } + + public RelationshipMixedBModel getNested() { + return nested; + } + + public Long getId() { + return id; + } +} diff --git a/library/src/test/java/com/orm/models/NoSugarModel.java b/library/src/test/java/com/orm/models/NoSugarModel.java new file mode 100644 index 00000000..6641126c --- /dev/null +++ b/library/src/test/java/com/orm/models/NoSugarModel.java @@ -0,0 +1,7 @@ +package com.orm.models; + + +public class NoSugarModel { + public NoSugarModel() { + } +} \ No newline at end of file diff --git a/library/src/test/java/com/orm/models/RelationshipAnnotatedModel.java b/library/src/test/java/com/orm/models/RelationshipAnnotatedModel.java new file mode 100644 index 00000000..d4875a98 --- /dev/null +++ b/library/src/test/java/com/orm/models/RelationshipAnnotatedModel.java @@ -0,0 +1,23 @@ +package com.orm.models; + +import com.orm.annotation.Table; + +@Table +public class RelationshipAnnotatedModel { + private SimpleAnnotatedModel simple; + private Long id; + + public RelationshipAnnotatedModel() {} + + public RelationshipAnnotatedModel(SimpleAnnotatedModel simple) { + this.simple = simple; + } + + public SimpleAnnotatedModel getSimple() { + return simple; + } + + public Long getId() { + return id; + } +} diff --git a/library/src/test/java/com/orm/models/RelationshipExtendedModel.java b/library/src/test/java/com/orm/models/RelationshipExtendedModel.java new file mode 100644 index 00000000..491dd823 --- /dev/null +++ b/library/src/test/java/com/orm/models/RelationshipExtendedModel.java @@ -0,0 +1,18 @@ +package com.orm.models; + + +import com.orm.SugarRecord; + +public class RelationshipExtendedModel extends SugarRecord { + private SimpleExtendedModel simple; + + public RelationshipExtendedModel() {} + + public RelationshipExtendedModel(SimpleExtendedModel simple) { + this.simple = simple; + } + + public SimpleExtendedModel getSimple() { + return simple; + } +} diff --git a/library/src/test/java/com/orm/models/RelationshipMixedAModel.java b/library/src/test/java/com/orm/models/RelationshipMixedAModel.java new file mode 100644 index 00000000..fbbbc103 --- /dev/null +++ b/library/src/test/java/com/orm/models/RelationshipMixedAModel.java @@ -0,0 +1,18 @@ +package com.orm.models; + + +import com.orm.SugarRecord; + +public class RelationshipMixedAModel extends SugarRecord { + private SimpleAnnotatedModel simple; + + public RelationshipMixedAModel() {} + + public RelationshipMixedAModel(SimpleAnnotatedModel simple) { + this.simple = simple; + } + + public SimpleAnnotatedModel getSimple() { + return simple; + } +} diff --git a/library/src/test/java/com/orm/models/RelationshipMixedBModel.java b/library/src/test/java/com/orm/models/RelationshipMixedBModel.java new file mode 100644 index 00000000..c302f85a --- /dev/null +++ b/library/src/test/java/com/orm/models/RelationshipMixedBModel.java @@ -0,0 +1,24 @@ +package com.orm.models; + + +import com.orm.annotation.Table; + +@Table +public class RelationshipMixedBModel { + private SimpleExtendedModel simple; + private Long id; + + public RelationshipMixedBModel() {} + + public RelationshipMixedBModel(SimpleExtendedModel simple) { + this.simple = simple; + } + + public SimpleExtendedModel getSimple() { + return simple; + } + + public Long getId() { + return id; + } +} diff --git a/library/src/test/java/com/orm/models/ShortFieldAnnotatedModel.java b/library/src/test/java/com/orm/models/ShortFieldAnnotatedModel.java new file mode 100644 index 00000000..f6a0cc45 --- /dev/null +++ b/library/src/test/java/com/orm/models/ShortFieldAnnotatedModel.java @@ -0,0 +1,28 @@ +package com.orm.models; + +import com.orm.annotation.Table; + +@Table +public class ShortFieldAnnotatedModel { + private Short objectShort; + private short rawShort; + private Long id; + + public ShortFieldAnnotatedModel() {} + + public ShortFieldAnnotatedModel(Short objectShort) { + this.objectShort = objectShort; + } + + public ShortFieldAnnotatedModel(short rawShort) { + this.rawShort = rawShort; + } + + public Short getShort() { + return objectShort; + } + + public short getRawShort() { + return rawShort; + } +} diff --git a/library/src/test/java/com/orm/models/ShortFieldExtendedModel.java b/library/src/test/java/com/orm/models/ShortFieldExtendedModel.java new file mode 100644 index 00000000..c7df3a59 --- /dev/null +++ b/library/src/test/java/com/orm/models/ShortFieldExtendedModel.java @@ -0,0 +1,26 @@ +package com.orm.models; + +import com.orm.SugarRecord; + +public class ShortFieldExtendedModel extends SugarRecord { + private Short objectShort; + private short rawShort; + + public ShortFieldExtendedModel() {} + + public ShortFieldExtendedModel(Short objectShort) { + this.objectShort = objectShort; + } + + public ShortFieldExtendedModel(short rawShort) { + this.rawShort = rawShort; + } + + public Short getShort() { + return objectShort; + } + + public short getRawShort() { + return rawShort; + } +} diff --git a/library/src/test/java/com/orm/models/SimpleAnnotatedModel.java b/library/src/test/java/com/orm/models/SimpleAnnotatedModel.java new file mode 100644 index 00000000..8a55e0bb --- /dev/null +++ b/library/src/test/java/com/orm/models/SimpleAnnotatedModel.java @@ -0,0 +1,15 @@ +package com.orm.models; + +import com.orm.annotation.Table; + + +@Table +public class SimpleAnnotatedModel { + private Long id; + + public SimpleAnnotatedModel() {} + + public Long getId() { + return id; + } +} diff --git a/library/src/test/java/com/orm/models/SimpleExtendedModel.java b/library/src/test/java/com/orm/models/SimpleExtendedModel.java new file mode 100644 index 00000000..65edf2c5 --- /dev/null +++ b/library/src/test/java/com/orm/models/SimpleExtendedModel.java @@ -0,0 +1,8 @@ +package com.orm.models; + +import com.orm.SugarRecord; + + +public class SimpleExtendedModel extends SugarRecord { + public SimpleExtendedModel() {} +} diff --git a/library/src/test/java/com/orm/models/SimpleModel.java b/library/src/test/java/com/orm/models/SimpleModel.java new file mode 100644 index 00000000..bdf26167 --- /dev/null +++ b/library/src/test/java/com/orm/models/SimpleModel.java @@ -0,0 +1,33 @@ +package com.orm.models; + +import com.orm.SugarRecord; + +public class SimpleModel extends SugarRecord { + private String str; + private int integer; + private boolean bool; + + public String getStr() { + return str; + } + + public void setStr(String str) { + this.str = str; + } + + public int getInteger() { + return integer; + } + + public void setInteger(int integer) { + this.integer = integer; + } + + public boolean isBool() { + return bool; + } + + public void setBool(boolean bool) { + this.bool = bool; + } +} diff --git a/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java b/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java index 4c36bf24..9526a515 100644 --- a/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java +++ b/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java @@ -1,18 +1,23 @@ package com.orm.models; -import com.orm.SugarRecord; import com.orm.annotation.Table; -/** - * Created by sibelius on 02/12/15. - */ @Table -public class StringFieldAnnotatedModel extends SugarRecord { - public String name; +public class StringFieldAnnotatedModel { + private String string; + private Long id; - public StringFieldAnnotatedModel() { } + public StringFieldAnnotatedModel() {} - public StringFieldAnnotatedModel(String name) { - this.name = name; + public StringFieldAnnotatedModel(String string) { + this.string = string; + } + + public String getString() { + return string; + } + + public void setString(String string) { + this.string = string; } } diff --git a/library/src/test/java/com/orm/models/StringFieldAnnotatedNoIdModel.java b/library/src/test/java/com/orm/models/StringFieldAnnotatedNoIdModel.java new file mode 100644 index 00000000..feb91920 --- /dev/null +++ b/library/src/test/java/com/orm/models/StringFieldAnnotatedNoIdModel.java @@ -0,0 +1,22 @@ +package com.orm.models; + +import com.orm.annotation.Table; + +@Table +public class StringFieldAnnotatedNoIdModel { + private String string; + + public StringFieldAnnotatedNoIdModel() {} + + public StringFieldAnnotatedNoIdModel(String string) { + this.string = string; + } + + public String getString() { + return string; + } + + public void setString(String string) { + this.string = string; + } +} diff --git a/library/src/test/java/com/orm/models/StringFieldExtendedModel.java b/library/src/test/java/com/orm/models/StringFieldExtendedModel.java index 2aff85d7..309c67b9 100644 --- a/library/src/test/java/com/orm/models/StringFieldExtendedModel.java +++ b/library/src/test/java/com/orm/models/StringFieldExtendedModel.java @@ -2,15 +2,20 @@ import com.orm.SugarRecord; -/** - * Created by sibelius on 02/12/15. - */ public class StringFieldExtendedModel extends SugarRecord { - public String name; + private String string; - public StringFieldExtendedModel() { } + public StringFieldExtendedModel() {} - public StringFieldExtendedModel(String name) { - this.name = name; + public StringFieldExtendedModel(String string) { + this.string = string; + } + + public String getString() { + return string; + } + + public void setString(String string) { + this.string = string; } } diff --git a/library/src/test/java/com/orm/record/BigDecimalFieldTests.java b/library/src/test/java/com/orm/record/BigDecimalFieldTests.java new file mode 100644 index 00000000..38f8e595 --- /dev/null +++ b/library/src/test/java/com/orm/record/BigDecimalFieldTests.java @@ -0,0 +1,61 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.BigDecimalFieldAnnotatedModel; +import com.orm.models.BigDecimalFieldExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.math.BigDecimal; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertEquals; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class BigDecimalFieldTests { + + @Test + public void nullBigDecimalExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new BigDecimalFieldExtendedModel()); + BigDecimalFieldExtendedModel model = + SugarRecord.findById(BigDecimalFieldExtendedModel.class, 1); + assertNull(model.getBigDecimal()); + } + + @Test + public void nullBigDecimalAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new BigDecimalFieldAnnotatedModel()); + BigDecimalFieldAnnotatedModel model = + SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); + assertNull(model.getBigDecimal()); + } + + @Test + public void bigDecimalExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); + save(new BigDecimalFieldExtendedModel(decimal)); + BigDecimalFieldExtendedModel model = SugarRecord.findById(BigDecimalFieldExtendedModel.class, 1); + assertEquals(decimal, model.getBigDecimal()); + } + + @Test + public void bigDecimalAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); + save(new BigDecimalFieldAnnotatedModel(decimal)); + BigDecimalFieldAnnotatedModel model = + SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); + assertEquals(decimal, model.getBigDecimal()); + } +} diff --git a/library/src/test/java/com/orm/record/BooleanFieldTests.java b/library/src/test/java/com/orm/record/BooleanFieldTests.java new file mode 100644 index 00000000..146a0481 --- /dev/null +++ b/library/src/test/java/com/orm/record/BooleanFieldTests.java @@ -0,0 +1,88 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.BooleanFieldAnnotatedModel; +import com.orm.models.BooleanFieldExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class BooleanFieldTests { + + @Test + public void nullBooleanExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new BooleanFieldExtendedModel()); + BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); + assertNull(model.getBoolean()); + } + + @Test + public void nullRawBooleanExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new BooleanFieldExtendedModel()); + BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); + assertEquals(false, model.getRawBoolean()); + } + + @Test + public void nullBooleanAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new BooleanFieldAnnotatedModel()); + BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); + assertNull(model.getBoolean()); + } + + @Test + public void nullRawBooleanAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new BooleanFieldAnnotatedModel()); + BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); + assertEquals(false, model.getRawBoolean()); + } + +//TODO check this method +// @Test +// public void objectBooleanExtendedTest() { +// SugarContext.init(RuntimeEnvironment.application); +// save(new BooleanFieldExtendedModel(true)); +// BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); +// assertEquals(true, model.getBoolean()); +// } + + @Test + public void rawBooleanExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new BooleanFieldExtendedModel(true)); + BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); + assertEquals(true, model.getRawBoolean()); + } + +//TODO check this +// @Test +// public void objectBooleanAnnotatedTest() { +// SugarContext.init(RuntimeEnvironment.application); +// save(new BooleanFieldAnnotatedModel(true)); +// BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); +// assertEquals(true, model.getBoolean()); +// } + + @Test + public void rawBooleanAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new BooleanFieldAnnotatedModel(true)); + BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); + assertEquals(true, model.getRawBoolean()); + } +} diff --git a/library/src/test/java/com/orm/record/ByteArrayFieldTests.java b/library/src/test/java/com/orm/record/ByteArrayFieldTests.java new file mode 100644 index 00000000..50ef7662 --- /dev/null +++ b/library/src/test/java/com/orm/record/ByteArrayFieldTests.java @@ -0,0 +1,62 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.ByteArrayAnnotatedModel; +import com.orm.models.ByteArrayExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class ByteArrayFieldTests { + + @Test + public void nullByteArrayExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + byte[] array = "".getBytes(); + save(new ByteArrayExtendedModel()); + ByteArrayExtendedModel model = SugarRecord.findById(ByteArrayExtendedModel.class, 1); + assertEquals(new String(array), new String(model.getByteArray())); + assertArrayEquals(array, model.getByteArray()); + } + + @Test + public void nullByteArrayAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + byte[] array = "".getBytes(); + save(new ByteArrayAnnotatedModel()); + ByteArrayAnnotatedModel model = SugarRecord.findById(ByteArrayAnnotatedModel.class, 1); + assertEquals(new String(array), new String(model.getByteArray())); + assertArrayEquals(array, model.getByteArray()); + } + + @Test + public void byteArrayExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + byte[] array = "hello".getBytes(); + save(new ByteArrayExtendedModel(array)); + ByteArrayExtendedModel model = SugarRecord.findById(ByteArrayExtendedModel.class, 1); + assertEquals(new String(array), new String(model.getByteArray())); + assertArrayEquals(array, model.getByteArray()); + } + + @Test + public void byteArrayAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + byte[] array = "hello".getBytes(); + save(new ByteArrayAnnotatedModel(array)); + ByteArrayAnnotatedModel model = SugarRecord.findById(ByteArrayAnnotatedModel.class, 1); + assertEquals(new String(array), new String(model.getByteArray())); + assertArrayEquals(array, model.getByteArray()); + } +} diff --git a/library/src/test/java/com/orm/record/CursorTests.java b/library/src/test/java/com/orm/record/CursorTests.java new file mode 100644 index 00000000..ef9ef849 --- /dev/null +++ b/library/src/test/java/com/orm/record/CursorTests.java @@ -0,0 +1,82 @@ +package com.orm.record; + +import android.annotation.TargetApi; +import android.content.Context; +import android.database.Cursor; +import android.os.Build; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CursorAdapter; +import android.widget.TextView; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.models.SimpleModel; +import com.orm.query.Select; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import static com.orm.SugarRecord.save; +import static junit.framework.Assert.assertNotSame; +import static junit.framework.Assert.assertSame; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class CursorTests { + + @Test + public void testColumnNames() { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleModel()); + Cursor c = Select.from(SimpleModel.class).getCursor(); + for (String col : new String[]{"STR", "INTEGER", "BOOL", "ID"}) { + assertNotSame("Missing column for field: " + col, -1, c.getColumnIndex(col)); + } + } + @Test + public void testSugarCursor() { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleModel()); + Cursor cursor = Select.from(SimpleModel.class).getCursor(); + assertNotSame("No _id", -1, cursor.getColumnIndex("_id")); + assertSame("_id != ID", cursor.getColumnIndex("_id"), cursor.getColumnIndex("ID")); + } + + @Test + public void testNoColumn() { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleModel()); + Cursor cursor = Select.from(SimpleModel.class).getCursor(); + assertSame(-1, cursor.getColumnIndex("nonexistent")); + } + + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + @Test + public void testMakeAdapter() { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleModel()); + Cursor c = Select.from(SimpleModel.class).getCursor(); + CursorAdapter adapter = new CursorAdapter(RuntimeEnvironment.application, c, true) { + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + TextView tv = new TextView(context); + String s = cursor.getString(cursor.getColumnIndex("STR")); + tv.setText(s); + return null; + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { + String s = cursor.getString(cursor.getColumnIndex("STR")); + ((TextView) view).setText(s); + } + }; + + Assert.assertNotNull(adapter); + } +} diff --git a/library/src/test/java/com/orm/record/DoubleFieldTests.java b/library/src/test/java/com/orm/record/DoubleFieldTests.java new file mode 100644 index 00000000..e9418938 --- /dev/null +++ b/library/src/test/java/com/orm/record/DoubleFieldTests.java @@ -0,0 +1,90 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.DoubleFieldAnnotatedModel; +import com.orm.models.DoubleFieldExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class, manifest = Config.NONE) +public class DoubleFieldTests { + + @Test + public void nullDoubleExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new DoubleFieldExtendedModel()); + DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); + assertNull(model.getDouble()); + } + + @Test + public void nullRawDoubleExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new DoubleFieldExtendedModel()); + DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); + assertEquals(0.0, model.getRawDouble(), 0.0000000001); + } + + @Test + public void nullDoubleAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new DoubleFieldAnnotatedModel()); + DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); + assertNull(model.getDouble()); + } + + @Test + public void nullRawDoubleAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new DoubleFieldAnnotatedModel()); + DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); + assertEquals(0.0, model.getRawDouble(), 0.0000000001); + } + + @Test + @SuppressWarnings("all") + public void objectDoubleExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + Double objectDouble = Double.valueOf(25.0); + save(new DoubleFieldExtendedModel(objectDouble)); + DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); + assertEquals(objectDouble, model.getDouble()); + } + + @Test + public void rawDoubleExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new DoubleFieldExtendedModel(25.0)); + DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); + assertEquals(25.0, model.getRawDouble(), 0.0000000001); + } + + @Test + @SuppressWarnings("all") + public void objectDoubleAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + Double objectDouble = Double.valueOf(25.0); + save(new DoubleFieldAnnotatedModel(objectDouble)); + DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); + assertEquals(objectDouble, model.getDouble()); + } + + @Test + public void rawDoubleAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new DoubleFieldAnnotatedModel(25.0)); + DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); + assertEquals(25.0, model.getRawDouble(), 0.0000000001); + } +} diff --git a/library/src/test/java/com/orm/record/EnumFieldTests.java b/library/src/test/java/com/orm/record/EnumFieldTests.java new file mode 100644 index 00000000..a11f52a4 --- /dev/null +++ b/library/src/test/java/com/orm/record/EnumFieldTests.java @@ -0,0 +1,94 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.EnumFieldAnnotatedModel; +import com.orm.models.EnumFieldExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18, application = ClientApp.class) +public class EnumFieldTests { + + @Test + public void nullDefaultEnumExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new EnumFieldExtendedModel()); + EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); + assertNull(model.getDefaultEnum()); + } + + @Test + public void nullOverriddenEnumExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new EnumFieldExtendedModel()); + EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); + assertNull(model.getOverrideEnum()); + } + @Test + public void nullDefaultEnumAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new EnumFieldAnnotatedModel()); + EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); + assertNull(model.getDefaultEnum()); + } + + @Test + public void nullOverriddenEnumAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new EnumFieldAnnotatedModel()); + EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); + assertNull(model.getOverrideEnum()); + } + + @Test + public void defaultEnumExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new EnumFieldExtendedModel(EnumFieldExtendedModel.OverrideEnum.ONE, + EnumFieldExtendedModel.DefaultEnum.TWO)); + EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); + assertNotNull(model); + assertEquals(model.getDefaultEnum(), EnumFieldExtendedModel.DefaultEnum.TWO); + } + + @Test + public void overriddenEnumExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new EnumFieldExtendedModel(EnumFieldExtendedModel.OverrideEnum.ONE, + EnumFieldExtendedModel.DefaultEnum.TWO)); + EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); + assertNotNull(model); + assertEquals(model.getOverrideEnum(), EnumFieldExtendedModel.OverrideEnum.ONE); + } + + @Test + public void defaultEnumAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new EnumFieldAnnotatedModel(EnumFieldAnnotatedModel.OverrideEnum.ONE, + EnumFieldAnnotatedModel.DefaultEnum.TWO)); + EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); + assertNotNull(model); + assertEquals(model.getDefaultEnum(), EnumFieldAnnotatedModel.DefaultEnum.TWO); + } + + @Test + public void overriddenEnumAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new EnumFieldAnnotatedModel(EnumFieldAnnotatedModel.OverrideEnum.ONE, + EnumFieldAnnotatedModel.DefaultEnum.TWO)); + EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); + assertNotNull(model); + assertEquals(model.getOverrideEnum(), EnumFieldAnnotatedModel.OverrideEnum.ONE); + } +} diff --git a/library/src/test/java/com/orm/record/FirstAndLastTests.java b/library/src/test/java/com/orm/record/FirstAndLastTests.java new file mode 100644 index 00000000..61ec1d98 --- /dev/null +++ b/library/src/test/java/com/orm/record/FirstAndLastTests.java @@ -0,0 +1,207 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.FloatFieldAnnotatedModel; +import com.orm.models.FloatFieldExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class FirstAndLastTests { + + @Test + @SuppressWarnings("all") + public void firstExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + Float firstObjectFloat = 25F; + Float lastObjectFloat = 50F; + save(new FloatFieldExtendedModel(firstObjectFloat)); + save(new FloatFieldExtendedModel(lastObjectFloat)); + FloatFieldExtendedModel model = SugarRecord.first(FloatFieldExtendedModel.class); + + if (null != model) { + assertEquals(firstObjectFloat, model.getFloat()); + } + } + + @Test + public void firstDeletedRecordExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + Float firstObjectFloat = 15F; + Float secondObjectFloat = 25F; + Float thirdObjectFloat = 35F; + Float fourthObjectFloat = 45F; + save(new FloatFieldExtendedModel(firstObjectFloat)); + save(new FloatFieldExtendedModel(secondObjectFloat)); + save(new FloatFieldExtendedModel(thirdObjectFloat)); + save(new FloatFieldExtendedModel(fourthObjectFloat)); + FloatFieldExtendedModel firstRecord = SugarRecord.findById(FloatFieldExtendedModel.class, 1); + firstRecord.delete(); + FloatFieldExtendedModel model = SugarRecord.first(FloatFieldExtendedModel.class); + + if (null != model) { + assertEquals(secondObjectFloat, model.getFloat()); + } + } + + @Test + public void lastExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + Float firstObjectFloat = 25F; + Float lastObjectFloat = 50F; + save(new FloatFieldExtendedModel(firstObjectFloat)); + save(new FloatFieldExtendedModel(lastObjectFloat)); + FloatFieldExtendedModel model = SugarRecord.last(FloatFieldExtendedModel.class); + + if (null != model) { + assertEquals(lastObjectFloat, model.getFloat()); + } + } + + @Test + public void lastDeletedRecordExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + Float firstObjectFloat = 15F; + Float secondObjectFloat = 25F; + Float thirdObjectFloat = 35F; + Float fourthObjectFloat = 45F; + save(new FloatFieldExtendedModel(firstObjectFloat)); + save(new FloatFieldExtendedModel(secondObjectFloat)); + save(new FloatFieldExtendedModel(thirdObjectFloat)); + save(new FloatFieldExtendedModel(fourthObjectFloat)); + FloatFieldExtendedModel lastRecord = SugarRecord.findById(FloatFieldExtendedModel.class, 4); + lastRecord.delete(); + FloatFieldExtendedModel model = SugarRecord.last(FloatFieldExtendedModel.class); + + if (null != model) { + assertEquals(thirdObjectFloat, model.getFloat()); + } + } + + @Test + public void nullFirstExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + assertNull(SugarRecord.first(FloatFieldExtendedModel.class)); + } + + @Test + public void nullLastExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + assertNull(SugarRecord.last(FloatFieldExtendedModel.class)); + } + + @Test + public void oneItemExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new FloatFieldExtendedModel(25F)); + FloatFieldExtendedModel firstModel = SugarRecord.first(FloatFieldExtendedModel.class); + FloatFieldExtendedModel lastModel = SugarRecord.last(FloatFieldExtendedModel.class); + + if (null != firstModel && null != lastModel) { + assertEquals(firstModel.getFloat(), lastModel.getFloat()); + } + } + + @Test + public void firstAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + Float firstObjectFloat = 25F; + Float lastObjectFloat = 50F; + save(new FloatFieldAnnotatedModel(firstObjectFloat)); + save(new FloatFieldAnnotatedModel(lastObjectFloat)); + FloatFieldAnnotatedModel model = SugarRecord.first(FloatFieldAnnotatedModel.class); + + if (null != model) { + assertEquals(firstObjectFloat, model.getFloat()); + } + } + + @Test + public void firstDeletedRecordAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + Float firstObjectFloat = 15F; + Float secondObjectFloat = 25F; + Float thirdObjectFloat = 35F; + Float fourthObjectFloat = 45F; + save(new FloatFieldAnnotatedModel(firstObjectFloat)); + save(new FloatFieldAnnotatedModel(secondObjectFloat)); + save(new FloatFieldAnnotatedModel(thirdObjectFloat)); + save(new FloatFieldAnnotatedModel(fourthObjectFloat)); + FloatFieldAnnotatedModel firstRecord = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); + SugarRecord.delete(firstRecord); + FloatFieldAnnotatedModel model = SugarRecord.first(FloatFieldAnnotatedModel.class); + + if (null != model) { + assertEquals(secondObjectFloat, model.getFloat()); + } + } + + @Test + public void lastAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + Float firstObjectFloat = 25F; + Float lastObjectFloat = 50F; + save(new FloatFieldAnnotatedModel(firstObjectFloat)); + save(new FloatFieldAnnotatedModel(lastObjectFloat)); + FloatFieldAnnotatedModel model = SugarRecord.last(FloatFieldAnnotatedModel.class); + + if (null != model) { + assertEquals(lastObjectFloat, model.getFloat()); + } + } + + @Test + public void lastDeletedRecordAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + Float firstObjectFloat = 15F; + Float secondObjectFloat = 25F; + Float thirdObjectFloat = 35F; + Float fourthObjectFloat = 45F; + save(new FloatFieldAnnotatedModel(firstObjectFloat)); + save(new FloatFieldAnnotatedModel(secondObjectFloat)); + save(new FloatFieldAnnotatedModel(thirdObjectFloat)); + save(new FloatFieldAnnotatedModel(fourthObjectFloat)); + FloatFieldAnnotatedModel lastRecord = SugarRecord.findById(FloatFieldAnnotatedModel.class, 4); + SugarRecord.delete(lastRecord); + FloatFieldAnnotatedModel model = SugarRecord.last(FloatFieldAnnotatedModel.class); + + if (null != model) { + assertEquals(thirdObjectFloat, model.getFloat()); + } + } + + @Test + public void nullFirstAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + assertNull(SugarRecord.first(FloatFieldAnnotatedModel.class)); + } + + @Test + public void nullLastAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + assertNull(SugarRecord.last(FloatFieldAnnotatedModel.class)); + } + + @Test + public void oneItemAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new FloatFieldAnnotatedModel(25F)); + FloatFieldAnnotatedModel firstModel = SugarRecord.first(FloatFieldAnnotatedModel.class); + FloatFieldAnnotatedModel lastModel = SugarRecord.last(FloatFieldAnnotatedModel.class); + + if (null != firstModel && null != lastModel) { + assertEquals(firstModel.getFloat(), lastModel.getFloat()); + } + } +} diff --git a/library/src/test/java/com/orm/record/FloatFieldTests.java b/library/src/test/java/com/orm/record/FloatFieldTests.java new file mode 100644 index 00000000..58c8dc80 --- /dev/null +++ b/library/src/test/java/com/orm/record/FloatFieldTests.java @@ -0,0 +1,88 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.FloatFieldAnnotatedModel; +import com.orm.models.FloatFieldExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class FloatFieldTests { + + @Test + public void nullFloatExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new FloatFieldExtendedModel()); + FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); + assertNull(model.getFloat()); + } + + @Test + public void nullRawFloatExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new FloatFieldExtendedModel()); + FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); + assertEquals(0F, model.getRawFloat(), 0.0000000001F); + } + + @Test + public void nullFloatAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new FloatFieldAnnotatedModel()); + FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); + assertNull(model.getFloat()); + } + + @Test + public void nullRawFloatAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new FloatFieldAnnotatedModel()); + FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); + assertEquals(0F, model.getRawFloat(), 0.0000000001F); + } + + @Test + public void objectFloatExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + Float objectFloat = 25F; + save(new FloatFieldExtendedModel(objectFloat)); + FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); + assertEquals(objectFloat, model.getFloat()); + } + + @Test + public void rawFloatExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new FloatFieldExtendedModel(25F)); + FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); + assertEquals(25F, model.getRawFloat(), 0.0000000001F); + } + + @Test + public void objectFloatAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + Float objectFloat = 25F; + save(new FloatFieldAnnotatedModel(objectFloat)); + FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); + assertEquals(objectFloat, model.getFloat()); + } + + @Test + public void rawFloatAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new FloatFieldAnnotatedModel(25F)); + FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); + assertEquals(25F, model.getRawFloat(), 0.0000000001F); + } +} diff --git a/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java b/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java new file mode 100644 index 00000000..21898584 --- /dev/null +++ b/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java @@ -0,0 +1,34 @@ +package com.orm.record; + +import android.database.sqlite.SQLiteException; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.models.IncompleteAnnotatedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import static com.orm.SugarRecord.delete; +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertFalse; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class IncompleteAnnotatedModelTests { + + @Test(expected=SQLiteException.class) + public void saveNoIdFieldTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new IncompleteAnnotatedModel()); + } + + @Test + public void deleteNoIdFieldTest() { + SugarContext.init(RuntimeEnvironment.application); + assertFalse(delete(new IncompleteAnnotatedModel())); + } +} diff --git a/library/src/test/java/com/orm/record/IntegerFieldTests.java b/library/src/test/java/com/orm/record/IntegerFieldTests.java new file mode 100644 index 00000000..b666e6b6 --- /dev/null +++ b/library/src/test/java/com/orm/record/IntegerFieldTests.java @@ -0,0 +1,88 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.IntegerFieldAnnotatedModel; +import com.orm.models.IntegerFieldExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class IntegerFieldTests { + + @Test + public void nullIntegerExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new IntegerFieldExtendedModel()); + IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); + assertNull(model.getInteger()); + } + + @Test + public void nullIntExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new IntegerFieldExtendedModel()); + IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); + assertEquals(0, model.getInt()); + } + + @Test + public void nullIntegerAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new IntegerFieldAnnotatedModel()); + IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); + assertNull(model.getInteger()); + } + + @Test + public void nullIntAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new IntegerFieldAnnotatedModel()); + IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); + assertEquals(0, model.getInt()); + } + + @Test + public void integerExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + Integer integer = 25; + save(new IntegerFieldExtendedModel(integer)); + IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); + assertEquals(integer, model.getInteger()); + } + + @Test + public void intExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new IntegerFieldExtendedModel(25)); + IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); + assertEquals(25, model.getInt()); + } + + @Test + public void integerAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + Integer integer = 25; + save(new IntegerFieldAnnotatedModel(integer)); + IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); + assertEquals(integer, model.getInteger()); + } + + @Test + public void intAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new IntegerFieldAnnotatedModel(25)); + IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); + assertEquals(25, model.getInt()); + } +} diff --git a/library/src/test/java/com/orm/record/ListAllOrderByTests.java b/library/src/test/java/com/orm/record/ListAllOrderByTests.java new file mode 100644 index 00000000..46a07a91 --- /dev/null +++ b/library/src/test/java/com/orm/record/ListAllOrderByTests.java @@ -0,0 +1,59 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.IntegerFieldExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.List; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class ListAllOrderByTests { + + @Test + public void listAllOrderByEmptyTest() { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(0L, SugarRecord.listAll(IntegerFieldExtendedModel.class, "id").size()); + } + + @Test + public void listAllOrderByIdTest() { + SugarContext.init(RuntimeEnvironment.application); + for(int i = 1; i <= 100; i++) { + save(new IntegerFieldExtendedModel(i)); + } + List models = + SugarRecord.listAll(IntegerFieldExtendedModel.class, "id"); + assertEquals(100L, models.size()); + Long id = models.get(0).getId(); + for(int i = 1; i < 100; i++) { + assertTrue(id models = + SugarRecord.listAll(IntegerFieldExtendedModel.class, "raw_integer"); + assertEquals(100L, models.size()); + int raw = models.get(0).getInt(); + for(int i = 1; i < 100; i++) { + assertTrue(raw < models.get(i).getInt()); + } + } +} diff --git a/library/src/test/java/com/orm/record/LongFieldTests.java b/library/src/test/java/com/orm/record/LongFieldTests.java new file mode 100644 index 00000000..78307436 --- /dev/null +++ b/library/src/test/java/com/orm/record/LongFieldTests.java @@ -0,0 +1,88 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.LongFieldAnnotatedModel; +import com.orm.models.LongFieldExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class LongFieldTests { + + @Test + public void nullLongExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new LongFieldExtendedModel()); + LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); + assertNull(model.getLong()); + } + + @Test + public void nullRawLongExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new LongFieldExtendedModel()); + LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); + assertEquals(0L, model.getRawLong()); + } + + @Test + public void nullLongAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new LongFieldAnnotatedModel()); + LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); + assertNull(model.getLong()); + } + + @Test + public void nullRawLongAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new LongFieldAnnotatedModel()); + LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); + assertEquals(0L, model.getRawLong()); + } + + @Test + public void objectLongExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + Long objectLong = 25L; + save(new LongFieldExtendedModel(objectLong)); + LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); + assertEquals(objectLong, model.getLong()); + } + + @Test + public void rawLongExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new LongFieldExtendedModel(25L)); + LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); + assertEquals(25L, model.getRawLong()); + } + + @Test + public void objectLongAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + Long objectLong = 25L; + save(new LongFieldAnnotatedModel(objectLong)); + LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); + assertEquals(objectLong, model.getLong()); + } + + @Test + public void rawLongAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new LongFieldAnnotatedModel(25L)); + LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); + assertEquals(25L, model.getRawLong()); + } +} diff --git a/library/src/test/java/com/orm/record/MultipleSaveTests.java b/library/src/test/java/com/orm/record/MultipleSaveTests.java new file mode 100644 index 00000000..7ffc0855 --- /dev/null +++ b/library/src/test/java/com/orm/record/MultipleSaveTests.java @@ -0,0 +1,122 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.StringFieldAnnotatedModel; +import com.orm.models.StringFieldAnnotatedNoIdModel; +import com.orm.models.StringFieldExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class MultipleSaveTests { + + @Test + public void stringMultipleSaveOriginalExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + String string = "Test String"; + StringFieldExtendedModel model = new StringFieldExtendedModel(string); + long id = save(model); + StringFieldExtendedModel query = SugarRecord.findById(StringFieldExtendedModel.class, id); + + if (null != query) { + assertEquals(string, query.getString()); + } + + model.setString("Another test"); + assertEquals(id, save(model)); + assertNull(SugarRecord.findById(StringFieldExtendedModel.class, 2)); + } + + @Test + public void stringMultipleSaveQueriedExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + String string = "Test String"; + StringFieldExtendedModel model = new StringFieldExtendedModel(string); + long id = save(model); + StringFieldExtendedModel query = SugarRecord.findById(StringFieldExtendedModel.class, id); + + if (null != query) { + assertEquals(string, query.getString()); + query.setString("Another test"); + assertEquals(id, save(query)); + assertNull(SugarRecord.findById(StringFieldExtendedModel.class, 2)); + } + } + + @Test + public void stringMultipleSaveOriginalAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + String string = "Test String"; + StringFieldAnnotatedModel model = new StringFieldAnnotatedModel(string); + long id = save(model); + StringFieldAnnotatedModel query = SugarRecord.findById(StringFieldAnnotatedModel.class, id); + + if (null != query) { + assertEquals(string, query.getString()); + model.setString("Another test"); + assertEquals(id, save(model)); + assertNull(SugarRecord.findById(StringFieldAnnotatedModel.class, 2)); + } + } + + @Test + public void stringMultipleSaveQueriedAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + String string = "Test String"; + StringFieldAnnotatedModel model = new StringFieldAnnotatedModel(string); + long id = save(model); + StringFieldAnnotatedModel query = SugarRecord.findById(StringFieldAnnotatedModel.class, id); + + if (null != query) { + assertEquals(string, query.getString()); + query.setString("Another test"); + assertEquals(id, save(query)); + assertNull(SugarRecord.findById(StringFieldAnnotatedModel.class, 2)); + } + } + + @Test + public void stringMultipleSaveOriginalAnnotatedNoIdTest() { + SugarContext.init(RuntimeEnvironment.application); + String string = "Test String"; + StringFieldAnnotatedNoIdModel model = new StringFieldAnnotatedNoIdModel(string); + long id = save(model); + StringFieldAnnotatedNoIdModel query = + SugarRecord.findById(StringFieldAnnotatedNoIdModel.class, id); + + if (null != query) { + assertEquals(string, query.getString()); + model.setString("Another test"); + assertEquals(id, save(model)); + assertNull(SugarRecord.findById(StringFieldAnnotatedNoIdModel.class, 2)); + } + } + + @Test + public void stringMultipleSaveQueriedAnnotatedNoIdTest() { + SugarContext.init(RuntimeEnvironment.application); + String string = "Test String"; + StringFieldAnnotatedNoIdModel model = new StringFieldAnnotatedNoIdModel(string); + long id = save(model); + StringFieldAnnotatedNoIdModel query = + SugarRecord.findById(StringFieldAnnotatedNoIdModel.class, id); + + if (null != query) { + assertEquals(string, query.getString()); + query.setString("Another test"); + assertEquals(id, save(query)); + assertNull(SugarRecord.findById(StringFieldAnnotatedNoIdModel.class, 2)); + } + } +} diff --git a/library/src/test/java/com/orm/record/NestedAnnotatedTests.java b/library/src/test/java/com/orm/record/NestedAnnotatedTests.java new file mode 100644 index 00000000..a777f6de --- /dev/null +++ b/library/src/test/java/com/orm/record/NestedAnnotatedTests.java @@ -0,0 +1,143 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.NestedAnnotatedModel; +import com.orm.models.RelationshipAnnotatedModel; +import com.orm.models.SimpleAnnotatedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.List; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class NestedAnnotatedTests { + + @Test + public void emptyDatabaseTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(0L, SugarRecord.count(NestedAnnotatedModel.class)); + assertEquals(0L, SugarRecord.count(RelationshipAnnotatedModel.class)); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); + save(nested); + save(new NestedAnnotatedModel(nested)); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipAnnotatedModel.class)); + assertEquals(1L, SugarRecord.count(NestedAnnotatedModel.class)); + } + + @Test + public void twoSameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); + save(nested); + save(new NestedAnnotatedModel(nested)); + save(new NestedAnnotatedModel(nested)); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipAnnotatedModel.class)); + assertEquals(2L, SugarRecord.count(NestedAnnotatedModel.class)); + } + + @Test + public void twoDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); + save(another_simple); + RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); + save(nested); + RelationshipAnnotatedModel another_nested = new RelationshipAnnotatedModel(another_simple); + save(another_nested); + save(new NestedAnnotatedModel(nested)); + save(new NestedAnnotatedModel(another_nested)); + assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(2L, SugarRecord.count(RelationshipAnnotatedModel.class)); + assertEquals(2L, SugarRecord.count(NestedAnnotatedModel.class)); + } + + @Test + public void manySameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); + save(nested); + for (int i = 1; i <= 100; i++) { + save(new NestedAnnotatedModel(nested)); + } + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipAnnotatedModel.class)); + assertEquals(100L, SugarRecord.count(NestedAnnotatedModel.class)); + } + + @Test + public void manyDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); + save(nested); + save(new NestedAnnotatedModel(nested)); + } + assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(100L, SugarRecord.count(RelationshipAnnotatedModel.class)); + assertEquals(100L, SugarRecord.count(NestedAnnotatedModel.class)); + } + + @Test + public void listAllSameTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); + save(nested); + for (int i = 1; i <= 100; i++) { + save(new NestedAnnotatedModel(nested)); + } + List models = SugarRecord.listAll(NestedAnnotatedModel.class); + assertEquals(100, models.size()); + for (NestedAnnotatedModel model : models) { + assertEquals(nested.getId(), model.getNested().getId()); + assertEquals(simple.getId(), model.getNested().getSimple().getId()); + } + } + + @Test + public void listAllDifferentTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); + save(nested); + save(new NestedAnnotatedModel(nested)); + } + List models = SugarRecord.listAll(NestedAnnotatedModel.class); + assertEquals(100, models.size()); + for (NestedAnnotatedModel model : models) { + assertEquals(model.getId(), model.getNested().getId()); + assertEquals(model.getId(), model.getNested().getSimple().getId()); + } + } +} diff --git a/library/src/test/java/com/orm/record/NestedExtendedTests.java b/library/src/test/java/com/orm/record/NestedExtendedTests.java new file mode 100644 index 00000000..803aa8d1 --- /dev/null +++ b/library/src/test/java/com/orm/record/NestedExtendedTests.java @@ -0,0 +1,143 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.NestedExtendedModel; +import com.orm.models.RelationshipExtendedModel; +import com.orm.models.SimpleExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.List; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class NestedExtendedTests { + + @Test + public void emptyDatabaseTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(0L, SugarRecord.count(NestedExtendedModel.class)); + assertEquals(0L, SugarRecord.count(RelationshipExtendedModel.class)); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); + save(nested); + save(new NestedExtendedModel(nested)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipExtendedModel.class)); + assertEquals(1L, SugarRecord.count(NestedExtendedModel.class)); + } + + @Test + public void twoSameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); + save(nested); + save(new NestedExtendedModel(nested)); + save(new NestedExtendedModel(nested)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipExtendedModel.class)); + assertEquals(2L, SugarRecord.count(NestedExtendedModel.class)); + } + + @Test + public void twoDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + SimpleExtendedModel another_simple = new SimpleExtendedModel(); + save(another_simple); + RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); + save(nested); + RelationshipExtendedModel another_nested = new RelationshipExtendedModel(another_simple); + save(another_nested); + save(new NestedExtendedModel(nested)); + save(new NestedExtendedModel(another_nested)); + assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(2L, SugarRecord.count(RelationshipExtendedModel.class)); + assertEquals(2L, SugarRecord.count(NestedExtendedModel.class)); + } + + @Test + public void manySameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); + save(nested); + for (int i = 1; i <= 100; i++) { + save(new NestedExtendedModel(nested)); + } + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipExtendedModel.class)); + assertEquals(100L, SugarRecord.count(NestedExtendedModel.class)); + } + + @Test + public void manyDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); + save(nested); + save(new NestedExtendedModel(nested)); + } + assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(100L, SugarRecord.count(RelationshipExtendedModel.class)); + assertEquals(100L, SugarRecord.count(NestedExtendedModel.class)); + } + + @Test + public void listAllSameTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); + save(nested); + for (int i = 1; i <= 100; i++) { + save(new NestedExtendedModel(nested)); + } + List models = SugarRecord.listAll(NestedExtendedModel.class); + assertEquals(100, models.size()); + for (NestedExtendedModel model : models) { + assertEquals(nested.getId(), model.getNested().getId()); + assertEquals(simple.getId(), model.getNested().getSimple().getId()); + } + } + + @Test + public void listAllDifferentTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); + save(nested); + save(new NestedExtendedModel(nested)); + } + List models = SugarRecord.listAll(NestedExtendedModel.class); + assertEquals(100, models.size()); + for (NestedExtendedModel model : models) { + assertEquals(model.getId(), model.getNested().getId()); + assertEquals(model.getId(), model.getNested().getSimple().getId()); + } + } +} diff --git a/library/src/test/java/com/orm/record/NestedMixedAATests.java b/library/src/test/java/com/orm/record/NestedMixedAATests.java new file mode 100644 index 00000000..f3acacd2 --- /dev/null +++ b/library/src/test/java/com/orm/record/NestedMixedAATests.java @@ -0,0 +1,143 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.NestedMixedAAModel; +import com.orm.models.RelationshipMixedAModel; +import com.orm.models.SimpleAnnotatedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.List; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class NestedMixedAATests { + + @Test + public void emptyDatabaseTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(0L, SugarRecord.count(NestedMixedAAModel.class)); + assertEquals(0L, SugarRecord.count(RelationshipMixedAModel.class)); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); + save(new NestedMixedAAModel(nested)); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); + assertEquals(1L, SugarRecord.count(NestedMixedAAModel.class)); + } + + @Test + public void twoSameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); + save(new NestedMixedAAModel(nested)); + save(new NestedMixedAAModel(nested)); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); + assertEquals(2L, SugarRecord.count(NestedMixedAAModel.class)); + } + + @Test + public void twoDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); + save(another_simple); + RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); + RelationshipMixedAModel another_nested = new RelationshipMixedAModel(another_simple); + save(another_nested); + save(new NestedMixedAAModel(nested)); + save(new NestedMixedAAModel(another_nested)); + assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(2L, SugarRecord.count(RelationshipMixedAModel.class)); + assertEquals(2L, SugarRecord.count(NestedMixedAAModel.class)); + } + + @Test + public void manySameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); + for (int i = 1; i <= 100; i++) { + save(new NestedMixedAAModel(nested)); + } + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); + assertEquals(100L, SugarRecord.count(NestedMixedAAModel.class)); + } + + @Test + public void manyDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); + save(new NestedMixedAAModel(nested)); + } + assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(100L, SugarRecord.count(RelationshipMixedAModel.class)); + assertEquals(100L, SugarRecord.count(NestedMixedAAModel.class)); + } + + @Test + public void listAllSameTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); + for (int i = 1; i <= 100; i++) { + save(new NestedMixedAAModel(nested)); + } + List models = SugarRecord.listAll(NestedMixedAAModel.class); + assertEquals(100, models.size()); + for (NestedMixedAAModel model : models) { + assertEquals(nested.getId(), model.getNested().getId()); + assertEquals(simple.getId(), model.getNested().getSimple().getId()); + } + } + + @Test + public void listAllDifferentTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); + save(new NestedMixedAAModel(nested)); + } + List models = SugarRecord.listAll(NestedMixedAAModel.class); + assertEquals(100, models.size()); + for (NestedMixedAAModel model : models) { + assertEquals(model.getId(), model.getNested().getId()); + assertEquals(model.getId(), model.getNested().getSimple().getId()); + } + } +} diff --git a/library/src/test/java/com/orm/record/NestedMixedABTests.java b/library/src/test/java/com/orm/record/NestedMixedABTests.java new file mode 100644 index 00000000..5d4799d5 --- /dev/null +++ b/library/src/test/java/com/orm/record/NestedMixedABTests.java @@ -0,0 +1,143 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.NestedMixedABModel; +import com.orm.models.RelationshipMixedBModel; +import com.orm.models.SimpleExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.List; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class NestedMixedABTests { + + @Test + public void emptyDatabaseTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(0L, SugarRecord.count(NestedMixedABModel.class)); + assertEquals(0L, SugarRecord.count(RelationshipMixedBModel.class)); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); + save(new NestedMixedABModel(nested)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); + assertEquals(1L, SugarRecord.count(NestedMixedABModel.class)); + } + + @Test + public void twoSameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); + save(new NestedMixedABModel(nested)); + save(new NestedMixedABModel(nested)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); + assertEquals(2L, SugarRecord.count(NestedMixedABModel.class)); + } + + @Test + public void twoDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + SimpleExtendedModel another_simple = new SimpleExtendedModel(); + save(another_simple); + RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); + RelationshipMixedBModel another_nested = new RelationshipMixedBModel(another_simple); + save(another_nested); + save(new NestedMixedABModel(nested)); + save(new NestedMixedABModel(another_nested)); + assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(2L, SugarRecord.count(RelationshipMixedBModel.class)); + assertEquals(2L, SugarRecord.count(NestedMixedABModel.class)); + } + + @Test + public void manySameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); + for (int i = 1; i <= 100; i++) { + save(new NestedMixedABModel(nested)); + } + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); + assertEquals(100L, SugarRecord.count(NestedMixedABModel.class)); + } + + @Test + public void manyDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); + save(new NestedMixedABModel(nested)); + } + assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(100L, SugarRecord.count(RelationshipMixedBModel.class)); + assertEquals(100L, SugarRecord.count(NestedMixedABModel.class)); + } + + @Test + public void listAllSameTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); + for (int i = 1; i <= 100; i++) { + save(new NestedMixedABModel(nested)); + } + List models = SugarRecord.listAll(NestedMixedABModel.class); + assertEquals(100, models.size()); + for (NestedMixedABModel model : models) { + assertEquals(nested.getId(), model.getNested().getId()); + assertEquals(simple.getId(), model.getNested().getSimple().getId()); + } + } + + @Test + public void listAllDifferentTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); + save(new NestedMixedABModel(nested)); + } + List models = SugarRecord.listAll(NestedMixedABModel.class); + assertEquals(100, models.size()); + for (NestedMixedABModel model : models) { + assertEquals(model.getId(), model.getNested().getId()); + assertEquals(model.getId(), model.getNested().getSimple().getId()); + } + } +} diff --git a/library/src/test/java/com/orm/record/NestedMixedBATests.java b/library/src/test/java/com/orm/record/NestedMixedBATests.java new file mode 100644 index 00000000..f7575770 --- /dev/null +++ b/library/src/test/java/com/orm/record/NestedMixedBATests.java @@ -0,0 +1,143 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.NestedMixedBAModel; +import com.orm.models.RelationshipMixedAModel; +import com.orm.models.SimpleAnnotatedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.List; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class NestedMixedBATests { + + @Test + public void emptyDatabaseTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(0L, SugarRecord.count(NestedMixedBAModel.class)); + assertEquals(0L, SugarRecord.count(RelationshipMixedAModel.class)); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); + save(new NestedMixedBAModel(nested)); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); + assertEquals(1L, SugarRecord.count(NestedMixedBAModel.class)); + } + + @Test + public void twoSameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); + save(new NestedMixedBAModel(nested)); + save(new NestedMixedBAModel(nested)); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); + assertEquals(2L, SugarRecord.count(NestedMixedBAModel.class)); + } + + @Test + public void twoDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); + save(another_simple); + RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); + RelationshipMixedAModel another_nested = new RelationshipMixedAModel(another_simple); + save(another_nested); + save(new NestedMixedBAModel(nested)); + save(new NestedMixedBAModel(another_nested)); + assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(2L, SugarRecord.count(RelationshipMixedAModel.class)); + assertEquals(2L, SugarRecord.count(NestedMixedBAModel.class)); + } + + @Test + public void manySameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); + for (int i = 1; i <= 100; i++) { + save(new NestedMixedBAModel(nested)); + } + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); + assertEquals(100L, SugarRecord.count(NestedMixedBAModel.class)); + } + + @Test + public void manyDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); + save(new NestedMixedBAModel(nested)); + } + assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(100L, SugarRecord.count(RelationshipMixedAModel.class)); + assertEquals(100L, SugarRecord.count(NestedMixedBAModel.class)); + } + + @Test + public void listAllSameTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); + for (int i = 1; i <= 100; i++) { + save(new NestedMixedBAModel(nested)); + } + List models = SugarRecord.listAll(NestedMixedBAModel.class); + assertEquals(100, models.size()); + for (NestedMixedBAModel model : models) { + assertEquals(nested.getId(), model.getNested().getId()); + assertEquals(simple.getId(), model.getNested().getSimple().getId()); + } + } + + @Test + public void listAllDifferentTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); + save(new NestedMixedBAModel(nested)); + } + List models = SugarRecord.listAll(NestedMixedBAModel.class); + assertEquals(100, models.size()); + for (NestedMixedBAModel model : models) { + assertEquals(model.getId(), model.getNested().getId()); + assertEquals(model.getId(), model.getNested().getSimple().getId()); + } + } +} diff --git a/library/src/test/java/com/orm/record/NestedMixedBBTests.java b/library/src/test/java/com/orm/record/NestedMixedBBTests.java new file mode 100644 index 00000000..af88af23 --- /dev/null +++ b/library/src/test/java/com/orm/record/NestedMixedBBTests.java @@ -0,0 +1,143 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.NestedMixedBBModel; +import com.orm.models.RelationshipMixedBModel; +import com.orm.models.SimpleExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.List; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class NestedMixedBBTests { + + @Test + public void emptyDatabaseTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(0L, SugarRecord.count(NestedMixedBBModel.class)); + assertEquals(0L, SugarRecord.count(RelationshipMixedBModel.class)); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); + save(new NestedMixedBBModel(nested)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); + assertEquals(1L, SugarRecord.count(NestedMixedBBModel.class)); + } + + @Test + public void twoSameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); + save(new NestedMixedBBModel(nested)); + save(new NestedMixedBBModel(nested)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); + assertEquals(2L, SugarRecord.count(NestedMixedBBModel.class)); + } + + @Test + public void twoDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + SimpleExtendedModel another_simple = new SimpleExtendedModel(); + save(another_simple); + RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); + RelationshipMixedBModel another_nested = new RelationshipMixedBModel(another_simple); + save(another_nested); + save(new NestedMixedBBModel(nested)); + save(new NestedMixedBBModel(another_nested)); + assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(2L, SugarRecord.count(RelationshipMixedBModel.class)); + assertEquals(2L, SugarRecord.count(NestedMixedBBModel.class)); + } + + @Test + public void manySameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); + for (int i = 1; i <= 100; i++) { + save(new NestedMixedBBModel(nested)); + } + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); + assertEquals(100L, SugarRecord.count(NestedMixedBBModel.class)); + } + + @Test + public void manyDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); + save(new NestedMixedBBModel(nested)); + } + assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(100L, SugarRecord.count(RelationshipMixedBModel.class)); + assertEquals(100L, SugarRecord.count(NestedMixedBBModel.class)); + } + + @Test + public void listAllSameTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); + for (int i = 1; i <= 100; i++) { + save(new NestedMixedBBModel(nested)); + } + List models = SugarRecord.listAll(NestedMixedBBModel.class); + assertEquals(100, models.size()); + for (NestedMixedBBModel model : models) { + assertEquals(nested.getId(), model.getNested().getId()); + assertEquals(simple.getId(), model.getNested().getSimple().getId()); + } + } + + @Test + public void listAllDifferentTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); + save(new NestedMixedBBModel(nested)); + } + List models = SugarRecord.listAll(NestedMixedBBModel.class); + assertEquals(100, models.size()); + for (NestedMixedBBModel model : models) { + assertEquals(model.getId(), model.getNested().getId()); + assertEquals(model.getId(), model.getNested().getSimple().getId()); + } + } +} diff --git a/library/src/test/java/com/orm/record/NoSugarModelTests.java b/library/src/test/java/com/orm/record/NoSugarModelTests.java new file mode 100644 index 00000000..5ebc40ca --- /dev/null +++ b/library/src/test/java/com/orm/record/NoSugarModelTests.java @@ -0,0 +1,34 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.NoSugarModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class NoSugarModelTests { + + @Test + public void deleteTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + NoSugarModel model = new NoSugarModel(); + assertFalse(SugarRecord.delete(model)); + } + + @Test + public void saveInTransactionTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SugarRecord.saveInTx(new NoSugarModel(), new NoSugarModel()); + assertEquals(-1L, SugarRecord.count(NoSugarModel.class)); + } +} diff --git a/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java b/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java new file mode 100644 index 00000000..0719bcd0 --- /dev/null +++ b/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java @@ -0,0 +1,120 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.RelationshipAnnotatedModel; +import com.orm.models.SimpleAnnotatedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.List; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class RelationshipAnnotatedTests { + + @Test + public void emptyDatabaseTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(0L, SugarRecord.count(RelationshipAnnotatedModel.class)); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + save(new RelationshipAnnotatedModel(simple)); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipAnnotatedModel.class)); + } + + @Test + public void twoSameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + save(new RelationshipAnnotatedModel(simple)); + save(new RelationshipAnnotatedModel(simple)); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(2L, SugarRecord.count(RelationshipAnnotatedModel.class)); + } + + @Test + public void twoDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); + save(another_simple); + save(new RelationshipAnnotatedModel(simple)); + save(new RelationshipAnnotatedModel(another_simple)); + assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(2L, SugarRecord.count(RelationshipAnnotatedModel.class)); + } + + @Test + public void manySameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + for (int i = 1; i <= 100; i++) { + save(new RelationshipAnnotatedModel(simple)); + } + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(100L, SugarRecord.count(RelationshipAnnotatedModel.class)); + } + + @Test + public void manyDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + save(new RelationshipAnnotatedModel(simple)); + } + assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(100L, SugarRecord.count(RelationshipAnnotatedModel.class)); + } + + @Test + public void listAllSameTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + for (int i = 1; i <= 100; i++) { + save(new RelationshipAnnotatedModel(simple)); + } + List models = + SugarRecord.listAll(RelationshipAnnotatedModel.class); + assertEquals(100, models.size()); + for (RelationshipAnnotatedModel model : models) { + assertEquals(simple.getId(), model.getSimple().getId()); + } + } + + @Test + public void listAllDifferentTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + save(new RelationshipAnnotatedModel(simple)); + } + List models = + SugarRecord.listAll(RelationshipAnnotatedModel.class); + assertEquals(100, models.size()); + for (RelationshipAnnotatedModel model : models) { + assertEquals(model.getId(), model.getSimple().getId()); + } + } +} diff --git a/library/src/test/java/com/orm/record/RelationshipExtendedTests.java b/library/src/test/java/com/orm/record/RelationshipExtendedTests.java new file mode 100644 index 00000000..dbd17fe6 --- /dev/null +++ b/library/src/test/java/com/orm/record/RelationshipExtendedTests.java @@ -0,0 +1,120 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.RelationshipExtendedModel; +import com.orm.models.SimpleExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.List; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class RelationshipExtendedTests { + + @Test + public void emptyDatabaseTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(0L, SugarRecord.count(RelationshipExtendedModel.class)); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new RelationshipExtendedModel(simple)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipExtendedModel.class)); + } + + @Test + public void twoSameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new RelationshipExtendedModel(simple)); + save(new RelationshipExtendedModel(simple)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(2L, SugarRecord.count(RelationshipExtendedModel.class)); + } + + @Test + public void twoDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + SimpleExtendedModel another_simple = new SimpleExtendedModel(); + save(another_simple); + save(new RelationshipExtendedModel(simple)); + save(new RelationshipExtendedModel(another_simple)); + assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(2L, SugarRecord.count(RelationshipExtendedModel.class)); + } + + @Test + public void manySameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + for (int i = 1; i <= 100; i++) { + save(new RelationshipExtendedModel(simple)); + } + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(100L, SugarRecord.count(RelationshipExtendedModel.class)); + } + + @Test + public void manyDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new RelationshipExtendedModel(simple)); + } + assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(100L, SugarRecord.count(RelationshipExtendedModel.class)); + } + + @Test + public void listAllSameTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + for (int i = 1; i <= 100; i++) { + save(new RelationshipExtendedModel(simple)); + } + List models = + SugarRecord.listAll(RelationshipExtendedModel.class); + assertEquals(100, models.size()); + for (RelationshipExtendedModel model : models) { + assertEquals(simple.getId(), model.getSimple().getId()); + } + } + + @Test + public void listAllDifferentTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new RelationshipExtendedModel(simple)); + } + List models = + SugarRecord.listAll(RelationshipExtendedModel.class); + assertEquals(100, models.size()); + for (RelationshipExtendedModel model : models) { + assertEquals(model.getId(), model.getSimple().getId()); + } + } +} diff --git a/library/src/test/java/com/orm/record/RelationshipMixedBTests.java b/library/src/test/java/com/orm/record/RelationshipMixedBTests.java new file mode 100644 index 00000000..190c030f --- /dev/null +++ b/library/src/test/java/com/orm/record/RelationshipMixedBTests.java @@ -0,0 +1,120 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.RelationshipMixedBModel; +import com.orm.models.SimpleExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.List; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class RelationshipMixedBTests { + + @Test + public void emptyDatabaseTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(0L, SugarRecord.count(RelationshipMixedBModel.class)); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new RelationshipMixedBModel(simple)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); + } + + @Test + public void twoSameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new RelationshipMixedBModel(simple)); + save(new RelationshipMixedBModel(simple)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(2L, SugarRecord.count(RelationshipMixedBModel.class)); + } + + @Test + public void twoDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + SimpleExtendedModel another_simple = new SimpleExtendedModel(); + save(another_simple); + save(new RelationshipMixedBModel(simple)); + save(new RelationshipMixedBModel(another_simple)); + assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(2L, SugarRecord.count(RelationshipMixedBModel.class)); + } + + @Test + public void manySameSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + for (int i = 1; i <= 100; i++) { + save(new RelationshipMixedBModel(simple)); + } + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(100L, SugarRecord.count(RelationshipMixedBModel.class)); + } + + @Test + public void manyDifferentSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new RelationshipMixedBModel(simple)); + } + assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(100L, SugarRecord.count(RelationshipMixedBModel.class)); + } + + @Test + public void listAllSameTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + for (int i = 1; i <= 100; i++) { + save(new RelationshipMixedBModel(simple)); + } + List models = + SugarRecord.listAll(RelationshipMixedBModel.class); + assertEquals(100, models.size()); + for (RelationshipMixedBModel model : models) { + assertEquals(simple.getId(), model.getSimple().getId()); + } + } + + @Test + public void listAllDifferentTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new RelationshipMixedBModel(simple)); + } + List models = + SugarRecord.listAll(RelationshipMixedBModel.class); + assertEquals(100, models.size()); + for (RelationshipMixedBModel model : models) { + assertEquals(model.getId(), model.getSimple().getId()); + } + } +} diff --git a/library/src/test/java/com/orm/record/ShortFieldTests.java b/library/src/test/java/com/orm/record/ShortFieldTests.java new file mode 100644 index 00000000..0de32144 --- /dev/null +++ b/library/src/test/java/com/orm/record/ShortFieldTests.java @@ -0,0 +1,88 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.ShortFieldAnnotatedModel; +import com.orm.models.ShortFieldExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class ShortFieldTests { + + @Test + public void nullShortExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new ShortFieldExtendedModel()); + ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); + assertNull(model.getShort()); + } + + @Test + public void nullRawShortExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new ShortFieldExtendedModel()); + ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); + assertEquals((short) 0, model.getRawShort()); + } + + @Test + public void nullShortAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new ShortFieldAnnotatedModel()); + ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); + assertNull(model.getShort()); + } + + @Test + public void nullRawShortAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new ShortFieldAnnotatedModel()); + ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); + assertEquals((short) 0, model.getRawShort()); + } + + @Test + public void objectShortExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + Short objectShort = 25; + save(new ShortFieldExtendedModel(objectShort)); + ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); + assertEquals(objectShort, model.getShort()); + } + + @Test + public void rawShortExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new ShortFieldExtendedModel((short) 25)); + ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); + assertEquals((short) 25, model.getRawShort()); + } + + @Test + public void objectShortAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + Short objectShort = 25; + save(new ShortFieldAnnotatedModel(objectShort)); + ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); + assertEquals(objectShort, model.getShort()); + } + + @Test + public void rawShortAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new ShortFieldAnnotatedModel((short) 25)); + ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); + assertEquals((short) 25, model.getRawShort()); + } +} diff --git a/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java b/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java new file mode 100644 index 00000000..d2f8508e --- /dev/null +++ b/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java @@ -0,0 +1,393 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.helper.NamingHelper; +import com.orm.models.SimpleAnnotatedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class SimpleAnnotatedModelTests { + + @Test + public void emptyDatabaseTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleAnnotatedModel()); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void twoSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleAnnotatedModel()); + save(new SimpleAnnotatedModel()); + assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void manySaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + save(new SimpleAnnotatedModel()); + } + assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void defaultIdTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(1L, save(new SimpleAnnotatedModel())); + } + + @Test + public void whereCountTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleAnnotatedModel()); + save(new SimpleAnnotatedModel()); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class, "id = ?", new String[]{"1"})); + } + + @Test + public void whereNoCountTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class, "id = ?", new String[]{"1"})); + save(new SimpleAnnotatedModel()); + save(new SimpleAnnotatedModel()); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class, "id = ?", new String[]{"3"})); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class, "id = ?", new String[]{"a"})); + } + + @Test + public void whereBrokenCountTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleAnnotatedModel()); + save(new SimpleAnnotatedModel()); + assertEquals(-1L, SugarRecord.count(SimpleAnnotatedModel.class, "di = ?", new String[]{"1"})); + } + + @Test + public void deleteTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel model = new SimpleAnnotatedModel(); + save(model); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertTrue(SugarRecord.delete(model)); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void deleteUnsavedTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel model = new SimpleAnnotatedModel(); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertFalse(SugarRecord.delete(model)); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void deleteWrongTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel model = new SimpleAnnotatedModel(); + save(model); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + Field idField = model.getClass().getDeclaredField("id"); + idField.setAccessible(true); + idField.set(model, Long.MAX_VALUE); + assertFalse(SugarRecord.delete(model)); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void deleteAllTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + int elementNumber = 100; + for (int i = 1; i <= elementNumber; i++) { + save(new SimpleAnnotatedModel()); + } + assertEquals(elementNumber, SugarRecord.deleteAll(SimpleAnnotatedModel.class)); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + @SuppressWarnings("all") + public void deleteAllWhereTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + int elementNumber = 100; + for (int i = 1; i <= elementNumber; i++) { + save(new SimpleAnnotatedModel()); + } + assertEquals(elementNumber - 1, SugarRecord.deleteAll(SimpleAnnotatedModel.class, "id > ?", new String[]{"1"})); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void deleteInTransactionFewTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleAnnotatedModel first = new SimpleAnnotatedModel(); + SimpleAnnotatedModel second = new SimpleAnnotatedModel(); + SimpleAnnotatedModel third = new SimpleAnnotatedModel(); + save(first); + save(second); + // Not saving last model + assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(2, SugarRecord.deleteInTx(first, second, third)); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void deleteInTransactionManyTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + long elementNumber = 100; + List models = new ArrayList<>(); + for (int i = 1; i <= elementNumber; i++) { + SimpleAnnotatedModel model = new SimpleAnnotatedModel(); + models.add(model); + // Not saving last model + if (i < elementNumber) { + save(model); + } + } + assertEquals(elementNumber - 1, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(elementNumber - 1, SugarRecord.deleteInTx(models)); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void saveInTransactionTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SugarRecord.saveInTx(new SimpleAnnotatedModel(), new SimpleAnnotatedModel()); + assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void listAllTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + save(new SimpleAnnotatedModel()); + } + List models = SugarRecord.listAll(SimpleAnnotatedModel.class); + assertEquals(100, models.size()); + for (long i = 1; i <= 100; i++) { + assertEquals(Long.valueOf(i), models.get((int) i - 1).getId()); + } + } + + @Test + public void findTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleAnnotatedModel()); + save(new SimpleAnnotatedModel()); + List models = + SugarRecord.find(SimpleAnnotatedModel.class, "id = ?", "2"); + assertEquals(1, models.size()); + assertEquals(2L, models.get(0).getId().longValue()); + } + + @Test + public void findWithQueryTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + save(new SimpleAnnotatedModel()); + } + List models = + SugarRecord.findWithQuery(SimpleAnnotatedModel.class, "Select * from " + + NamingHelper.toSQLName(SimpleAnnotatedModel.class) + + " where id >= ? ", "50"); + for (SimpleAnnotatedModel model : models) { + assertEquals(75L, model.getId(), 25L); + } + } + + @Test + @SuppressWarnings("all") + public void findByIdTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleAnnotatedModel()); + assertEquals(1L, SugarRecord.findById(SimpleAnnotatedModel.class, 1L).getId().longValue()); + } + + @Test + public void findByIdIntegerTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleAnnotatedModel()); + assertEquals(1L, SugarRecord.findById(SimpleAnnotatedModel.class, 1).getId().longValue()); + } + + @Test + public void findByIdStringsNullTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleAnnotatedModel()); + assertEquals(0, SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{""}).size()); + } + + @Test + public void findByIdStringsOneTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleAnnotatedModel()); + List models = + SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{"1"}); + assertEquals(1, models.size()); + assertEquals(1L, models.get(0).getId().longValue()); + } + + @Test + public void findByIdStringsTwoTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleAnnotatedModel()); + save(new SimpleAnnotatedModel()); + save(new SimpleAnnotatedModel()); + List models = + SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{"1", "3"}); + assertEquals(2, models.size()); + assertEquals(Long.valueOf(1L), models.get(0).getId()); + assertEquals(Long.valueOf(3L), models.get(1).getId()); + } + + @Test + public void findByIdStringsManyTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 10; i++) { + save(new SimpleAnnotatedModel()); + } + List models = + SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{"1", "3", "6", "10"}); + assertEquals(4, models.size()); + assertEquals(Long.valueOf(1L), models.get(0).getId()); + assertEquals(Long.valueOf(3L), models.get(1).getId()); + assertEquals(Long.valueOf(6L), models.get(2).getId()); + assertEquals(Long.valueOf(10L), models.get(3).getId()); + } + + @Test + public void findByIdStringsOrderTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 10; i++) { + save(new SimpleAnnotatedModel()); + } + List models = + SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{"10", "6", "3", "1"}); + assertEquals(4, models.size()); + // The order of the query doesn't matter + assertEquals(Long.valueOf(1L), models.get(0).getId()); + assertEquals(Long.valueOf(3L), models.get(1).getId()); + assertEquals(Long.valueOf(6L), models.get(2).getId()); + assertEquals(Long.valueOf(10L), models.get(3).getId()); + } + + @Test + public void findByIdNullTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleAnnotatedModel()); + assertNull(SugarRecord.findById(SimpleAnnotatedModel.class, 2L)); + } + + @Test + public void findAllTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + save(new SimpleAnnotatedModel()); + } + Iterator cursor = SugarRecord.findAll(SimpleAnnotatedModel.class); + for (int i = 1; i <= 100; i++) { + assertTrue(cursor.hasNext()); + SimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(Long.valueOf(i), model.getId()); + } + } + + @Test + public void findAsIteratorTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + save(new SimpleAnnotatedModel()); + } + Iterator cursor = SugarRecord.findAsIterator(SimpleAnnotatedModel.class, + "id >= ?", "50"); + for (int i = 50; i <= 100; i++) { + assertTrue(cursor.hasNext()); + SimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(Long.valueOf(i), model.getId()); + } + } + + @Test + public void findWithQueryAsIteratorTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + save(new SimpleAnnotatedModel()); + } + Iterator cursor = + SugarRecord.findWithQueryAsIterator(SimpleAnnotatedModel.class, + "Select * from " + + NamingHelper.toSQLName(SimpleAnnotatedModel.class) + + " where id >= ? ", "50"); + for (int i = 50; i <= 100; i++) { + assertTrue(cursor.hasNext()); + SimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(Long.valueOf(i), model.getId()); + } + } + + @Test(expected=NoSuchElementException.class) + public void findAsIteratorOutOfBoundsTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleAnnotatedModel()); + Iterator cursor = SugarRecord.findAsIterator(SimpleAnnotatedModel.class, + "id = ?", "1"); + assertTrue(cursor.hasNext()); + SimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(Long.valueOf(1), model.getId()); + // This should throw a NoSuchElementException + cursor.next(); + } + + @Test(expected=UnsupportedOperationException.class) + public void disallowRemoveCursorTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleAnnotatedModel()); + Iterator cursor = SugarRecord.findAsIterator(SimpleAnnotatedModel.class, + "id = ?", "1"); + assertTrue(cursor.hasNext()); + SimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(Long.valueOf(1), model.getId()); + // This should throw a UnsupportedOperationException + cursor.remove(); + } + + @Test + public void vacuumTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SugarRecord.executeQuery("Vacuum"); + } +} \ No newline at end of file diff --git a/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java b/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java new file mode 100644 index 00000000..c6f4d37f --- /dev/null +++ b/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java @@ -0,0 +1,403 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.helper.NamingHelper; +import com.orm.models.SimpleExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class SimpleExtendedModelTests { + + @Test + public void emptyDatabaseTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleExtendedModel()); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void twoSaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleExtendedModel()); + save(new SimpleExtendedModel()); + assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void manySaveTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + save(new SimpleExtendedModel()); + } + assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void defaultIdTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(1L, save(new SimpleExtendedModel())); + } + + @Test + public void whereCountTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleExtendedModel()); + save(new SimpleExtendedModel()); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class, "id = ?", new String[]{"1"})); + } + + @Test + public void whereNoCountTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class, "id = ?", new String[]{"1"})); + save(new SimpleExtendedModel()); + save(new SimpleExtendedModel()); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class, "id = ?", new String[]{"3"})); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class, "id = ?", new String[]{"a"})); + } + + @Test + public void whereBrokenCountTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleExtendedModel()); + save(new SimpleExtendedModel()); + assertEquals(-1L, SugarRecord.count(SimpleExtendedModel.class, "di = ?", new String[]{"1"})); + } + + @Test + public void saveMethodTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel model = new SimpleExtendedModel(); + model.save(); + assertEquals(-1L, SugarRecord.count(SimpleExtendedModel.class, "di = ?", new String[]{"1"})); + } + + @Test + public void deleteTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel model = new SimpleExtendedModel(); + save(model); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertTrue(SugarRecord.delete(model)); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void deleteUnsavedTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel model = new SimpleExtendedModel(); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + assertFalse(SugarRecord.delete(model)); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void deleteWrongTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel model = new SimpleExtendedModel(); + save(model); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + Field idField = model.getClass().getSuperclass().getDeclaredField("id"); + idField.setAccessible(true); + idField.set(model, Long.MAX_VALUE); + assertFalse(SugarRecord.delete(model)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void deleteAllTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + int elementNumber = 100; + for (int i = 1; i <= elementNumber; i++) { + save(new SimpleExtendedModel()); + } + assertEquals(elementNumber, SugarRecord.deleteAll(SimpleExtendedModel.class)); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + @SuppressWarnings("all") + public void deleteAllWhereTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + int elementNumber = 100; + for (int i = 1; i <= elementNumber; i++) { + save(new SimpleExtendedModel()); + } + assertEquals(elementNumber - 1, SugarRecord.deleteAll(SimpleExtendedModel.class, + "id > ?", + new String[]{"1"})); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void deleteInTransactionFewTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SimpleExtendedModel first = new SimpleExtendedModel(); + SimpleExtendedModel second = new SimpleExtendedModel(); + SimpleExtendedModel third = new SimpleExtendedModel(); + save(first); + save(second); + // Not saving last model + assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(2, SugarRecord.deleteInTx(first, second, third)); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void deleteInTransactionManyTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + long elementNumber = 100; + List models = new ArrayList<>(); + for (int i = 1; i <= elementNumber; i++) { + SimpleExtendedModel model = new SimpleExtendedModel(); + models.add(model); + // Not saving last model + if (i < elementNumber) { + save(model); + } + } + assertEquals(elementNumber - 1, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(elementNumber - 1, SugarRecord.deleteInTx(models)); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void saveInTransactionTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SugarRecord.saveInTx(new SimpleExtendedModel(), new SimpleExtendedModel()); + assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void listAllTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + save(new SimpleExtendedModel()); + } + List models = SugarRecord.listAll(SimpleExtendedModel.class); + assertEquals(100, models.size()); + for (long i = 1; i <= 100; i++) { + assertEquals(Long.valueOf(i), models.get((int) i - 1).getId()); + } + } + + @Test + public void findTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleExtendedModel()); + save(new SimpleExtendedModel()); + List models = + SugarRecord.find(SimpleExtendedModel.class, "id = ?", "2"); + assertEquals(1, models.size()); + assertEquals(Long.valueOf(2L), models.get(0).getId()); + } + + @Test + public void findWithQueryTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + save(new SimpleExtendedModel()); + } + List models = + SugarRecord.findWithQuery(SimpleExtendedModel.class, "Select * from " + + NamingHelper.toSQLName(SimpleExtendedModel.class) + + " where id >= ? ", "50"); + for (SimpleExtendedModel model : models) { + assertEquals(75, model.getId(), 25L); + } + } + + @Test + @SuppressWarnings("all") + public void findByIdTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleExtendedModel()); + assertEquals(Long.valueOf(1L), SugarRecord.findById(SimpleExtendedModel.class, 1L).getId()); + } + + @Test + public void findByIdIntegerTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleExtendedModel()); + assertEquals(Long.valueOf(1L), SugarRecord.findById(SimpleExtendedModel.class, 1).getId()); + } + + @Test + public void findByIdStringsNullTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleExtendedModel()); + assertEquals(0, SugarRecord.findById(SimpleExtendedModel.class, new String[]{""}).size()); + } + + @Test + public void findByIdStringsOneTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleExtendedModel()); + List models = + SugarRecord.findById(SimpleExtendedModel.class, new String[]{"1"}); + assertEquals(1, models.size()); + assertEquals(Long.valueOf(1L), models.get(0).getId()); + } + + @Test + public void findByIdStringsTwoTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleExtendedModel()); + save(new SimpleExtendedModel()); + save(new SimpleExtendedModel()); + List models = + SugarRecord.findById(SimpleExtendedModel.class, new String[]{"1", "3"}); + assertEquals(2, models.size()); + assertEquals(Long.valueOf(1L), models.get(0).getId()); + assertEquals(Long.valueOf(3L), models.get(1).getId()); + } + + @Test + public void findByIdStringsManyTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 10; i++) { + save(new SimpleExtendedModel()); + } + List models = + SugarRecord.findById(SimpleExtendedModel.class, new String[]{"1", "3", "6", "10"}); + assertEquals(4, models.size()); + assertEquals(Long.valueOf(1L), models.get(0).getId()); + assertEquals(Long.valueOf(3L), models.get(1).getId()); + assertEquals(Long.valueOf(6L), models.get(2).getId()); + assertEquals(Long.valueOf(10L), models.get(3).getId()); + } + + @Test + public void findByIdStringsOrderTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 10; i++) { + save(new SimpleExtendedModel()); + } + List models = + SugarRecord.findById(SimpleExtendedModel.class, new String[]{"10", "6", "3", "1"}); + assertEquals(4, models.size()); + // The order of the query doesn't matter + assertEquals(Long.valueOf(1L), models.get(0).getId()); + assertEquals(Long.valueOf(3L), models.get(1).getId()); + assertEquals(Long.valueOf(6L), models.get(2).getId()); + assertEquals(Long.valueOf(10L), models.get(3).getId()); + } + + @Test + public void findByIdNullTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleExtendedModel()); + assertNull(SugarRecord.findById(SimpleExtendedModel.class, 2L)); + } + + @Test + public void findAllTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + save(new SimpleExtendedModel()); + } + Iterator cursor = SugarRecord.findAll(SimpleExtendedModel.class); + for (int i = 1; i <= 100; i++) { + assertTrue(cursor.hasNext()); + SimpleExtendedModel model = cursor.next(); + assertNotNull(model); + assertEquals(Long.valueOf(i), model.getId()); + } + } + + @Test + public void findAsIteratorTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + save(new SimpleExtendedModel()); + } + Iterator cursor = SugarRecord.findAsIterator(SimpleExtendedModel.class, + "id >= ?", "50"); + for (int i = 50; i <= 100; i++) { + assertTrue(cursor.hasNext()); + SimpleExtendedModel model = cursor.next(); + assertNotNull(model); + assertEquals(Long.valueOf(i), model.getId()); + } + } + + @Test + public void findWithQueryAsIteratorTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + for (int i = 1; i <= 100; i++) { + save(new SimpleExtendedModel()); + } + Iterator cursor = + SugarRecord.findWithQueryAsIterator(SimpleExtendedModel.class, + "Select * from " + + NamingHelper.toSQLName(SimpleExtendedModel.class) + + " where id >= ? ", "50"); + for (int i = 50; i <= 100; i++) { + assertTrue(cursor.hasNext()); + SimpleExtendedModel model = cursor.next(); + assertNotNull(model); + assertEquals(Long.valueOf(i), model.getId()); + } + } + + @Test(expected=NoSuchElementException.class) + public void findAsIteratorOutOfBoundsTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleExtendedModel()); + Iterator cursor = SugarRecord.findAsIterator(SimpleExtendedModel.class, + "id = ?", "1"); + assertTrue(cursor.hasNext()); + SimpleExtendedModel model = cursor.next(); + assertNotNull(model); + assertEquals(Long.valueOf(1), model.getId()); + // This should throw a NoSuchElementException + cursor.next(); + } + + @Test(expected=UnsupportedOperationException.class) + public void disallowRemoveCursorTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + save(new SimpleExtendedModel()); + Iterator cursor = SugarRecord.findAsIterator(SimpleExtendedModel.class, + "id = ?", "1"); + assertTrue(cursor.hasNext()); + SimpleExtendedModel model = cursor.next(); + assertNotNull(model); + assertEquals(Long.valueOf(1), model.getId()); + // This should throw a UnsupportedOperationException + cursor.remove(); + } + + @Test + public void vacuumTest() throws Exception { + SugarContext.init(RuntimeEnvironment.application); + SugarRecord.executeQuery("Vacuum"); + } +} \ No newline at end of file diff --git a/library/src/test/java/com/orm/record/StringFieldTests.java b/library/src/test/java/com/orm/record/StringFieldTests.java new file mode 100644 index 00000000..9c69e062 --- /dev/null +++ b/library/src/test/java/com/orm/record/StringFieldTests.java @@ -0,0 +1,56 @@ +package com.orm.record; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.SugarRecord; +import com.orm.models.StringFieldAnnotatedModel; +import com.orm.models.StringFieldExtendedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18, application = ClientApp.class) +public class StringFieldTests { + + @Test + public void nullStringExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new StringFieldExtendedModel()); + StringFieldExtendedModel model = SugarRecord.findById(StringFieldExtendedModel.class, 1); + assertNull(model.getString()); + } + + @Test + public void nullStringAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + save(new StringFieldAnnotatedModel()); + StringFieldAnnotatedModel model = SugarRecord.findById(StringFieldAnnotatedModel.class, 1); + assertNull(model.getString()); + } + + @Test + public void stringExtendedTest() { + SugarContext.init(RuntimeEnvironment.application); + String string = "Test String"; + save(new StringFieldExtendedModel(string)); + StringFieldExtendedModel model = SugarRecord.findById(StringFieldExtendedModel.class, 1); + assertEquals(string, model.getString()); + } + + @Test + public void stringAnnotatedTest() { + SugarContext.init(RuntimeEnvironment.application); + String string = "Test String"; + save(new StringFieldAnnotatedModel(string)); + StringFieldAnnotatedModel model = SugarRecord.findById(StringFieldAnnotatedModel.class, 1); + assertEquals(string, model.getString()); + } +} From 602e61f5faaee89aa8f2739e52f25412479e4488 Mon Sep 17 00:00:00 2001 From: jonatan Date: Thu, 7 Apr 2016 23:09:48 -0300 Subject: [PATCH 079/139] Start migration of test to library in order to increment coverage. --- library/src/test/java/com/orm/SchemaGeneratorTest.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index 60b74ae0..84720214 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -40,16 +40,14 @@ public void testSimpleColumnTableCreation() throws Exception { String createSQL2 = schemaGenerator.createTableSQL(StringFieldAnnotatedModel.class); - assertEquals( - "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(StringFieldAnnotatedModel.class) + + assertEquals("CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(StringFieldAnnotatedModel.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + "STRING TEXT ) ", createSQL2); String createSQL3 = schemaGenerator.createTableSQL(StringFieldExtendedModelAnnotatedColumn.class); - assertEquals( - "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(StringFieldExtendedModelAnnotatedColumn.class) + + assertEquals("CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(StringFieldExtendedModelAnnotatedColumn.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + "anyName TEXT ) ", createSQL3); From dbcc2729889758d177153ba69ef7d7a84d64011e Mon Sep 17 00:00:00 2001 From: jonisaa Date: Fri, 8 Apr 2016 14:29:13 -0300 Subject: [PATCH 080/139] Deleted DummyContext class Extracted all call to SugarContext.init() to run in an @Before annotated method. --- .../test/java/com/orm/NamingHelperTest.java | 17 +- .../java/com/orm/SchemaGeneratorTest.java | 17 +- .../com/orm/SugarDbConfigurationTest.java | 20 +- .../com/orm/{query => models}/TestRecord.java | 2 +- .../test/java/com/orm/query/DummyContext.java | 484 ------------------ .../test/java/com/orm/query/SelectTest.java | 2 + .../com/orm/record/BigDecimalFieldTests.java | 10 +- .../com/orm/record/BooleanFieldTests.java | 23 +- .../com/orm/record/ByteArrayFieldTests.java | 11 +- .../test/java/com/orm/record/CursorTests.java | 10 +- .../java/com/orm/record/DoubleFieldTests.java | 14 +- .../java/com/orm/record/EnumFieldTests.java | 14 +- .../com/orm/record/FirstAndLastTests.java | 20 +- .../java/com/orm/record/FloatFieldTests.java | 14 +- .../record/IncompleteAnnotatedModelTests.java | 8 +- .../com/orm/record/IntegerFieldTests.java | 14 +- .../com/orm/record/ListAllOrderByTests.java | 10 +- .../java/com/orm/record/LongFieldTests.java | 14 +- .../com/orm/record/MultipleSaveTests.java | 12 +- .../com/orm/record/NestedAnnotatedTests.java | 14 +- .../com/orm/record/NestedExtendedTests.java | 14 +- .../com/orm/record/NestedMixedAATests.java | 14 +- .../com/orm/record/NestedMixedABTests.java | 14 +- .../com/orm/record/NestedMixedBATests.java | 14 +- .../com/orm/record/NestedMixedBBTests.java | 14 +- .../com/orm/record/NoSugarModelTests.java | 8 +- .../record/RelationshipAnnotatedTests.java | 14 +- .../orm/record/RelationshipExtendedTests.java | 14 +- .../orm/record/RelationshipMixedBTests.java | 13 +- .../java/com/orm/record/ShortFieldTests.java | 14 +- .../orm/record/SimpleAnnotatedModelTests.java | 39 +- .../orm/record/SimpleExtendedModelTests.java | 41 +- .../java/com/orm/record/StringFieldTests.java | 11 +- .../java/com/orm/util/ContextUtilTest.java | 32 +- .../java/com/orm/util/ManifestHelperTest.java | 21 +- 35 files changed, 233 insertions(+), 774 deletions(-) rename library/src/test/java/com/orm/{query => models}/TestRecord.java (86%) delete mode 100644 library/src/test/java/com/orm/query/DummyContext.java diff --git a/library/src/test/java/com/orm/NamingHelperTest.java b/library/src/test/java/com/orm/NamingHelperTest.java index 263d816f..f4873c81 100644 --- a/library/src/test/java/com/orm/NamingHelperTest.java +++ b/library/src/test/java/com/orm/NamingHelperTest.java @@ -1,10 +1,9 @@ package com.orm; import com.orm.helper.NamingHelper; -import com.orm.query.TestRecord; +import com.orm.models.TestRecord; import com.orm.util.ReflectionUtil; -import org.junit.Assert; import org.junit.Test; import java.lang.reflect.Field; @@ -12,25 +11,27 @@ import java.util.List; import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; public class NamingHelperTest { @Test public void testToSQLNameFromField() { List fieldList = ReflectionUtil.getTableFields(TestRecord.class); - List columnList = new ArrayList<>(); if (null != fieldList && !fieldList.isEmpty()) { + List columnList = new ArrayList<>(); + for(Field field: fieldList) { columnList.add(NamingHelper.toSQLName(field)); } - } - boolean isIdInList = inList(columnList, "ID"); - boolean isNameInList = inList(columnList, "NAME"); + boolean isIdInList = inList(columnList, "ID"); + boolean isNameInList = inList(columnList, "NAME"); - Assert.assertTrue(isIdInList); - Assert.assertTrue(isNameInList); + assertTrue(isIdInList); + assertTrue(isNameInList); + } } private boolean inList(List list, String searchValue) { diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index 84720214..6e883caa 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -6,19 +6,27 @@ import com.orm.models.StringFieldAnnotatedModel; import com.orm.models.StringFieldExtendedModel; import com.orm.models.StringFieldExtendedModelAnnotatedColumn; -import com.orm.query.DummyContext; -import com.orm.util.ContextUtil; import com.orm.helper.NamingHelper; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; import static junit.framework.Assert.assertEquals; +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18, application = ClientApp.class, manifest = Config.NONE) public class SchemaGeneratorTest { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void testEmptyTableCreation() throws Exception { - ContextUtil.init(new DummyContext()); SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); String createSQL = schemaGenerator.createTableSQL(EmptyModel.class); assertEquals( @@ -29,7 +37,6 @@ public void testEmptyTableCreation() throws Exception { @Test public void testSimpleColumnTableCreation() throws Exception { - ContextUtil.init(new DummyContext()); SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); String createSQL = schemaGenerator.createTableSQL(StringFieldExtendedModel.class); assertEquals( @@ -55,7 +62,6 @@ public void testSimpleColumnTableCreation() throws Exception { @Test public void testUniqueTableCreation() { - ContextUtil.init(new DummyContext()); SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); String createSQL = schemaGenerator.createTableSQL(IntUniqueModel.class); assertEquals( @@ -67,7 +73,6 @@ public void testUniqueTableCreation() { @Test public void testMultiColumnUniqueTableCreation() { - ContextUtil.init(new DummyContext()); SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); String createSQL = schemaGenerator.createTableSQL(MultiColumnUniqueModel.class); assertEquals( diff --git a/library/src/test/java/com/orm/SugarDbConfigurationTest.java b/library/src/test/java/com/orm/SugarDbConfigurationTest.java index a55a00d6..06ea6ae9 100644 --- a/library/src/test/java/com/orm/SugarDbConfigurationTest.java +++ b/library/src/test/java/com/orm/SugarDbConfigurationTest.java @@ -1,10 +1,7 @@ package com.orm; -import android.database.sqlite.SQLiteDatabase; - import com.orm.dsl.BuildConfig; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricGradleTestRunner; @@ -13,6 +10,9 @@ import java.util.Locale; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + /** * @author jonatan.salas */ @@ -31,19 +31,17 @@ public void testNotNullConfiguration() { final SugarDbConfiguration config = SugarContext.getDbConfiguration(); - Assert.assertEquals(configuration.getDatabaseLocale(), config.getDatabaseLocale()); - Assert.assertEquals(configuration.getMaxSize(), config.getMaxSize()); - Assert.assertEquals(configuration.getPageSize(), config.getPageSize()); + assertEquals(configuration.getDatabaseLocale(), config.getDatabaseLocale()); + assertEquals(configuration.getMaxSize(), config.getMaxSize()); + assertEquals(configuration.getPageSize(), config.getPageSize()); } @Test public void testNullConfiguration() { SugarContext.init(RuntimeEnvironment.application); - - Assert.assertNull(SugarContext.getDbConfiguration()); + assertNull(SugarContext.getDbConfiguration()); } -//TODO: check this method // @Test // public void testNotNullConfigurationWithSugarDb() { // SugarDbConfiguration configuration = new SugarDbConfiguration() @@ -56,8 +54,8 @@ public void testNullConfiguration() { // SQLiteDatabase database = SugarContext.getSugarContext().getSugarDb().getDB(); // SQLiteDatabase sqLiteDatabase = SugarDb.getInstance().getDB(); // -// Assert.assertEquals(database.getMaximumSize(), sqLiteDatabase.getMaximumSize()); -// Assert.assertEquals(database.getPageSize(), sqLiteDatabase.getPageSize()); +// assertEquals(database.getMaximumSize(), sqLiteDatabase.getMaximumSize()); +// assertEquals(database.getPageSize(), sqLiteDatabase.getPageSize()); // // if (sqLiteDatabase.isOpen()) { // sqLiteDatabase.close(); diff --git a/library/src/test/java/com/orm/query/TestRecord.java b/library/src/test/java/com/orm/models/TestRecord.java similarity index 86% rename from library/src/test/java/com/orm/query/TestRecord.java rename to library/src/test/java/com/orm/models/TestRecord.java index 383a5234..f537f501 100644 --- a/library/src/test/java/com/orm/query/TestRecord.java +++ b/library/src/test/java/com/orm/models/TestRecord.java @@ -1,4 +1,4 @@ -package com.orm.query; +package com.orm.models; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/query/DummyContext.java b/library/src/test/java/com/orm/query/DummyContext.java deleted file mode 100644 index 77a897ff..00000000 --- a/library/src/test/java/com/orm/query/DummyContext.java +++ /dev/null @@ -1,484 +0,0 @@ -package com.orm.query; - -import android.content.*; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; -import android.content.res.AssetManager; -import android.content.res.Configuration; -import android.content.res.Resources; -import android.database.DatabaseErrorHandler; -import android.database.sqlite.SQLiteDatabase; -import android.graphics.Bitmap; -import android.graphics.drawable.Drawable; -import android.net.Uri; -import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.os.UserHandle; -import android.view.Display; - -import java.io.*; - -public class DummyContext extends Context { - @Override - public File getCodeCacheDir() { - return null; - } - - @Override - public File[] getExternalFilesDirs(String type) { - return null; - } - - @Override - public File[] getExternalCacheDirs() { - return null; - } - - @Override - public File getNoBackupFilesDir() { - return null; - } - - @Override - public File[] getObbDirs() { - return null; - } - - @Override - public File[] getExternalMediaDirs() { - return null; - } - - @Override - public AssetManager getAssets() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public Resources getResources() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public PackageManager getPackageManager() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public ContentResolver getContentResolver() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public Looper getMainLooper() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public Context getApplicationContext() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void setTheme(int i) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public Resources.Theme getTheme() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public ClassLoader getClassLoader() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public String getPackageName() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public ApplicationInfo getApplicationInfo() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public String getPackageResourcePath() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public String getPackageCodePath() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public SharedPreferences getSharedPreferences(String s, int i) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public FileInputStream openFileInput(String s) throws FileNotFoundException { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public FileOutputStream openFileOutput(String s, int i) throws FileNotFoundException { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public boolean deleteFile(String s) { - return false; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public File getFileStreamPath(String s) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public File getFilesDir() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public File getExternalFilesDir(String s) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public File getObbDir() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public File getCacheDir() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public File getExternalCacheDir() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public String[] fileList() { - return new String[0]; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public File getDir(String s, int i) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public SQLiteDatabase openOrCreateDatabase(String s, int i, SQLiteDatabase.CursorFactory cursorFactory) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public SQLiteDatabase openOrCreateDatabase(String s, int i, SQLiteDatabase.CursorFactory cursorFactory, DatabaseErrorHandler databaseErrorHandler) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public boolean deleteDatabase(String s) { - return false; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public File getDatabasePath(String s) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public String[] databaseList() { - return new String[0]; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public Drawable getWallpaper() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public Drawable peekWallpaper() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public int getWallpaperDesiredMinimumWidth() { - return 0; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public int getWallpaperDesiredMinimumHeight() { - return 0; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void setWallpaper(Bitmap bitmap) throws IOException { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void setWallpaper(InputStream inputStream) throws IOException { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void clearWallpaper() throws IOException { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void startActivity(Intent intent) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void startActivity(Intent intent, Bundle options) { - //To change body of implemented methods use File | Settings | File Templates. - } - - - @Override - public void startActivities(Intent[] intents) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void startActivities(Intent[] intents, Bundle options) { - //To change body of implemented methods use File | Settings | File Templates. - } - - - @Override - public void startIntentSender(IntentSender intentSender, Intent intent, int i, int i1, int i2) throws IntentSender.SendIntentException { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void startIntentSender(IntentSender intent, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) throws IntentSender.SendIntentException { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void sendBroadcast(Intent intent) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void sendBroadcast(Intent intent, String s) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void sendOrderedBroadcast(Intent intent, String s) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void sendOrderedBroadcast(Intent intent, String s, BroadcastReceiver broadcastReceiver, Handler handler, int i, String s1, Bundle bundle) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void sendBroadcastAsUser(Intent intent, UserHandle user) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void sendStickyBroadcast(Intent intent) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void sendStickyOrderedBroadcast(Intent intent, BroadcastReceiver broadcastReceiver, Handler handler, int i, String s, Bundle bundle) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void removeStickyBroadcast(Intent intent) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void sendStickyOrderedBroadcastAsUser(Intent intent, UserHandle user, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public Intent registerReceiver(BroadcastReceiver broadcastReceiver, IntentFilter intentFilter) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public Intent registerReceiver(BroadcastReceiver broadcastReceiver, IntentFilter intentFilter, String s, Handler handler) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void unregisterReceiver(BroadcastReceiver broadcastReceiver) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public ComponentName startService(Intent intent) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public boolean stopService(Intent intent) { - return false; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) { - return false; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void unbindService(ServiceConnection serviceConnection) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public boolean startInstrumentation(ComponentName componentName, String s, Bundle bundle) { - return false; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public Object getSystemService(String s) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public String getSystemServiceName(Class serviceClass) { - return null; - } - - @Override - public int checkPermission(String s, int i, int i1) { - return PackageManager.PERMISSION_GRANTED; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public int checkCallingPermission(String s) { - return PackageManager.PERMISSION_GRANTED; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public int checkCallingOrSelfPermission(String s) { - return PackageManager.PERMISSION_GRANTED; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public int checkSelfPermission(String permission) { - return PackageManager.PERMISSION_GRANTED; - } - - @Override - public void enforcePermission(String s, int i, int i1, String s1) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void enforceCallingPermission(String s, String s1) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void enforceCallingOrSelfPermission(String s, String s1) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void grantUriPermission(String s, Uri uri, int i) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void revokeUriPermission(Uri uri, int i) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public int checkUriPermission(Uri uri, int i, int i1, int i2) { - return 0; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public int checkCallingUriPermission(Uri uri, int i) { - return 0; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public int checkCallingOrSelfUriPermission(Uri uri, int i) { - return 0; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public int checkUriPermission(Uri uri, String s, String s1, int i, int i1, int i2) { - return 0; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void enforceUriPermission(Uri uri, int i, int i1, int i2, String s) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void enforceCallingUriPermission(Uri uri, int i, String s) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void enforceCallingOrSelfUriPermission(Uri uri, int i, String s) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public void enforceUriPermission(Uri uri, String s, String s1, int i, int i1, int i2, String s2) { - //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public Context createPackageContext(String s, int i) throws PackageManager.NameNotFoundException { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public Context createConfigurationContext(Configuration overrideConfiguration) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public Context createDisplayContext(Display display) { - return null; //To change body of implemented methods use File | Settings | File Templates. - } -} diff --git a/library/src/test/java/com/orm/query/SelectTest.java b/library/src/test/java/com/orm/query/SelectTest.java index 39508b77..734cca12 100644 --- a/library/src/test/java/com/orm/query/SelectTest.java +++ b/library/src/test/java/com/orm/query/SelectTest.java @@ -1,5 +1,7 @@ package com.orm.query; +import com.orm.models.TestRecord; + import org.junit.Test; import static junit.framework.Assert.assertEquals; diff --git a/library/src/test/java/com/orm/record/BigDecimalFieldTests.java b/library/src/test/java/com/orm/record/BigDecimalFieldTests.java index 38f8e595..ed2bddc9 100644 --- a/library/src/test/java/com/orm/record/BigDecimalFieldTests.java +++ b/library/src/test/java/com/orm/record/BigDecimalFieldTests.java @@ -7,6 +7,7 @@ import com.orm.models.BigDecimalFieldAnnotatedModel; import com.orm.models.BigDecimalFieldExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -22,9 +23,13 @@ @Config(sdk=18, application = ClientApp.class) public class BigDecimalFieldTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void nullBigDecimalExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new BigDecimalFieldExtendedModel()); BigDecimalFieldExtendedModel model = SugarRecord.findById(BigDecimalFieldExtendedModel.class, 1); @@ -33,7 +38,6 @@ public void nullBigDecimalExtendedTest() { @Test public void nullBigDecimalAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new BigDecimalFieldAnnotatedModel()); BigDecimalFieldAnnotatedModel model = SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); @@ -42,7 +46,6 @@ public void nullBigDecimalAnnotatedTest() { @Test public void bigDecimalExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); save(new BigDecimalFieldExtendedModel(decimal)); BigDecimalFieldExtendedModel model = SugarRecord.findById(BigDecimalFieldExtendedModel.class, 1); @@ -51,7 +54,6 @@ public void bigDecimalExtendedTest() { @Test public void bigDecimalAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); save(new BigDecimalFieldAnnotatedModel(decimal)); BigDecimalFieldAnnotatedModel model = diff --git a/library/src/test/java/com/orm/record/BooleanFieldTests.java b/library/src/test/java/com/orm/record/BooleanFieldTests.java index 146a0481..4038adf4 100644 --- a/library/src/test/java/com/orm/record/BooleanFieldTests.java +++ b/library/src/test/java/com/orm/record/BooleanFieldTests.java @@ -7,6 +7,7 @@ import com.orm.models.BooleanFieldAnnotatedModel; import com.orm.models.BooleanFieldExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -20,9 +21,13 @@ @Config(sdk=18, application = ClientApp.class) public class BooleanFieldTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void nullBooleanExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new BooleanFieldExtendedModel()); BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); assertNull(model.getBoolean()); @@ -30,7 +35,6 @@ public void nullBooleanExtendedTest() { @Test public void nullRawBooleanExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new BooleanFieldExtendedModel()); BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); assertEquals(false, model.getRawBoolean()); @@ -38,7 +42,6 @@ public void nullRawBooleanExtendedTest() { @Test public void nullBooleanAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new BooleanFieldAnnotatedModel()); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertNull(model.getBoolean()); @@ -46,16 +49,14 @@ public void nullBooleanAnnotatedTest() { @Test public void nullRawBooleanAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new BooleanFieldAnnotatedModel()); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(false, model.getRawBoolean()); } -//TODO check this method +////TODO check this method // @Test // public void objectBooleanExtendedTest() { -// SugarContext.init(RuntimeEnvironment.application); // save(new BooleanFieldExtendedModel(true)); // BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); // assertEquals(true, model.getBoolean()); @@ -63,24 +64,24 @@ public void nullRawBooleanAnnotatedTest() { @Test public void rawBooleanExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new BooleanFieldExtendedModel(true)); BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); assertEquals(true, model.getRawBoolean()); } -//TODO check this +// //TODO check this // @Test // public void objectBooleanAnnotatedTest() { -// SugarContext.init(RuntimeEnvironment.application); // save(new BooleanFieldAnnotatedModel(true)); // BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); -// assertEquals(true, model.getBoolean()); +// +// if (null != model) { +// assertEquals(true, model.getBoolean()); +// } // } @Test public void rawBooleanAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new BooleanFieldAnnotatedModel(true)); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(true, model.getRawBoolean()); diff --git a/library/src/test/java/com/orm/record/ByteArrayFieldTests.java b/library/src/test/java/com/orm/record/ByteArrayFieldTests.java index 50ef7662..fe3e7e92 100644 --- a/library/src/test/java/com/orm/record/ByteArrayFieldTests.java +++ b/library/src/test/java/com/orm/record/ByteArrayFieldTests.java @@ -7,6 +7,7 @@ import com.orm.models.ByteArrayAnnotatedModel; import com.orm.models.ByteArrayExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -20,9 +21,14 @@ @Config(sdk=18, application = ClientApp.class) public class ByteArrayFieldTests { + + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void nullByteArrayExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); byte[] array = "".getBytes(); save(new ByteArrayExtendedModel()); ByteArrayExtendedModel model = SugarRecord.findById(ByteArrayExtendedModel.class, 1); @@ -32,7 +38,6 @@ public void nullByteArrayExtendedTest() { @Test public void nullByteArrayAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); byte[] array = "".getBytes(); save(new ByteArrayAnnotatedModel()); ByteArrayAnnotatedModel model = SugarRecord.findById(ByteArrayAnnotatedModel.class, 1); @@ -42,7 +47,6 @@ public void nullByteArrayAnnotatedTest() { @Test public void byteArrayExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); byte[] array = "hello".getBytes(); save(new ByteArrayExtendedModel(array)); ByteArrayExtendedModel model = SugarRecord.findById(ByteArrayExtendedModel.class, 1); @@ -52,7 +56,6 @@ public void byteArrayExtendedTest() { @Test public void byteArrayAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); byte[] array = "hello".getBytes(); save(new ByteArrayAnnotatedModel(array)); ByteArrayAnnotatedModel model = SugarRecord.findById(ByteArrayAnnotatedModel.class, 1); diff --git a/library/src/test/java/com/orm/record/CursorTests.java b/library/src/test/java/com/orm/record/CursorTests.java index ef9ef849..a698b69b 100644 --- a/library/src/test/java/com/orm/record/CursorTests.java +++ b/library/src/test/java/com/orm/record/CursorTests.java @@ -16,6 +16,7 @@ import com.orm.query.Select; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -29,9 +30,13 @@ @Config(sdk=18, application = ClientApp.class) public class CursorTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void testColumnNames() { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleModel()); Cursor c = Select.from(SimpleModel.class).getCursor(); for (String col : new String[]{"STR", "INTEGER", "BOOL", "ID"}) { @@ -40,7 +45,6 @@ public void testColumnNames() { } @Test public void testSugarCursor() { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleModel()); Cursor cursor = Select.from(SimpleModel.class).getCursor(); assertNotSame("No _id", -1, cursor.getColumnIndex("_id")); @@ -49,7 +53,6 @@ public void testSugarCursor() { @Test public void testNoColumn() { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleModel()); Cursor cursor = Select.from(SimpleModel.class).getCursor(); assertSame(-1, cursor.getColumnIndex("nonexistent")); @@ -58,7 +61,6 @@ public void testNoColumn() { @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testMakeAdapter() { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleModel()); Cursor c = Select.from(SimpleModel.class).getCursor(); CursorAdapter adapter = new CursorAdapter(RuntimeEnvironment.application, c, true) { diff --git a/library/src/test/java/com/orm/record/DoubleFieldTests.java b/library/src/test/java/com/orm/record/DoubleFieldTests.java index e9418938..f5d9ce36 100644 --- a/library/src/test/java/com/orm/record/DoubleFieldTests.java +++ b/library/src/test/java/com/orm/record/DoubleFieldTests.java @@ -7,6 +7,7 @@ import com.orm.models.DoubleFieldAnnotatedModel; import com.orm.models.DoubleFieldExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -20,9 +21,13 @@ @Config(sdk=18, application = ClientApp.class, manifest = Config.NONE) public class DoubleFieldTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void nullDoubleExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new DoubleFieldExtendedModel()); DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); assertNull(model.getDouble()); @@ -30,7 +35,6 @@ public void nullDoubleExtendedTest() { @Test public void nullRawDoubleExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new DoubleFieldExtendedModel()); DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); assertEquals(0.0, model.getRawDouble(), 0.0000000001); @@ -38,7 +42,6 @@ public void nullRawDoubleExtendedTest() { @Test public void nullDoubleAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new DoubleFieldAnnotatedModel()); DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); assertNull(model.getDouble()); @@ -46,7 +49,6 @@ public void nullDoubleAnnotatedTest() { @Test public void nullRawDoubleAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new DoubleFieldAnnotatedModel()); DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); assertEquals(0.0, model.getRawDouble(), 0.0000000001); @@ -55,7 +57,6 @@ public void nullRawDoubleAnnotatedTest() { @Test @SuppressWarnings("all") public void objectDoubleExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); Double objectDouble = Double.valueOf(25.0); save(new DoubleFieldExtendedModel(objectDouble)); DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); @@ -64,7 +65,6 @@ public void objectDoubleExtendedTest() { @Test public void rawDoubleExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new DoubleFieldExtendedModel(25.0)); DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); assertEquals(25.0, model.getRawDouble(), 0.0000000001); @@ -73,7 +73,6 @@ public void rawDoubleExtendedTest() { @Test @SuppressWarnings("all") public void objectDoubleAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); Double objectDouble = Double.valueOf(25.0); save(new DoubleFieldAnnotatedModel(objectDouble)); DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); @@ -82,7 +81,6 @@ public void objectDoubleAnnotatedTest() { @Test public void rawDoubleAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new DoubleFieldAnnotatedModel(25.0)); DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); assertEquals(25.0, model.getRawDouble(), 0.0000000001); diff --git a/library/src/test/java/com/orm/record/EnumFieldTests.java b/library/src/test/java/com/orm/record/EnumFieldTests.java index a11f52a4..bbceb2c7 100644 --- a/library/src/test/java/com/orm/record/EnumFieldTests.java +++ b/library/src/test/java/com/orm/record/EnumFieldTests.java @@ -7,6 +7,7 @@ import com.orm.models.EnumFieldAnnotatedModel; import com.orm.models.EnumFieldExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -21,9 +22,13 @@ @Config(sdk = 18, application = ClientApp.class) public class EnumFieldTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void nullDefaultEnumExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new EnumFieldExtendedModel()); EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); assertNull(model.getDefaultEnum()); @@ -31,14 +36,12 @@ public void nullDefaultEnumExtendedTest() { @Test public void nullOverriddenEnumExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new EnumFieldExtendedModel()); EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); assertNull(model.getOverrideEnum()); } @Test public void nullDefaultEnumAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new EnumFieldAnnotatedModel()); EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); assertNull(model.getDefaultEnum()); @@ -46,7 +49,6 @@ public void nullDefaultEnumAnnotatedTest() { @Test public void nullOverriddenEnumAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new EnumFieldAnnotatedModel()); EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); assertNull(model.getOverrideEnum()); @@ -54,7 +56,6 @@ public void nullOverriddenEnumAnnotatedTest() { @Test public void defaultEnumExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new EnumFieldExtendedModel(EnumFieldExtendedModel.OverrideEnum.ONE, EnumFieldExtendedModel.DefaultEnum.TWO)); EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); @@ -64,7 +65,6 @@ public void defaultEnumExtendedTest() { @Test public void overriddenEnumExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new EnumFieldExtendedModel(EnumFieldExtendedModel.OverrideEnum.ONE, EnumFieldExtendedModel.DefaultEnum.TWO)); EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); @@ -74,7 +74,6 @@ public void overriddenEnumExtendedTest() { @Test public void defaultEnumAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new EnumFieldAnnotatedModel(EnumFieldAnnotatedModel.OverrideEnum.ONE, EnumFieldAnnotatedModel.DefaultEnum.TWO)); EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); @@ -84,7 +83,6 @@ public void defaultEnumAnnotatedTest() { @Test public void overriddenEnumAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new EnumFieldAnnotatedModel(EnumFieldAnnotatedModel.OverrideEnum.ONE, EnumFieldAnnotatedModel.DefaultEnum.TWO)); EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); diff --git a/library/src/test/java/com/orm/record/FirstAndLastTests.java b/library/src/test/java/com/orm/record/FirstAndLastTests.java index 61ec1d98..8c8e2d8d 100644 --- a/library/src/test/java/com/orm/record/FirstAndLastTests.java +++ b/library/src/test/java/com/orm/record/FirstAndLastTests.java @@ -7,6 +7,7 @@ import com.orm.models.FloatFieldAnnotatedModel; import com.orm.models.FloatFieldExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -20,10 +21,14 @@ @Config(sdk=18, application = ClientApp.class) public class FirstAndLastTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test @SuppressWarnings("all") public void firstExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); Float firstObjectFloat = 25F; Float lastObjectFloat = 50F; save(new FloatFieldExtendedModel(firstObjectFloat)); @@ -37,7 +42,6 @@ public void firstExtendedTest() { @Test public void firstDeletedRecordExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); Float firstObjectFloat = 15F; Float secondObjectFloat = 25F; Float thirdObjectFloat = 35F; @@ -57,7 +61,6 @@ public void firstDeletedRecordExtendedTest() { @Test public void lastExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); Float firstObjectFloat = 25F; Float lastObjectFloat = 50F; save(new FloatFieldExtendedModel(firstObjectFloat)); @@ -71,7 +74,6 @@ public void lastExtendedTest() { @Test public void lastDeletedRecordExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); Float firstObjectFloat = 15F; Float secondObjectFloat = 25F; Float thirdObjectFloat = 35F; @@ -91,19 +93,16 @@ public void lastDeletedRecordExtendedTest() { @Test public void nullFirstExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); assertNull(SugarRecord.first(FloatFieldExtendedModel.class)); } @Test public void nullLastExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); assertNull(SugarRecord.last(FloatFieldExtendedModel.class)); } @Test public void oneItemExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new FloatFieldExtendedModel(25F)); FloatFieldExtendedModel firstModel = SugarRecord.first(FloatFieldExtendedModel.class); FloatFieldExtendedModel lastModel = SugarRecord.last(FloatFieldExtendedModel.class); @@ -115,7 +114,6 @@ public void oneItemExtendedTest() { @Test public void firstAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); Float firstObjectFloat = 25F; Float lastObjectFloat = 50F; save(new FloatFieldAnnotatedModel(firstObjectFloat)); @@ -129,7 +127,6 @@ public void firstAnnotatedTest() { @Test public void firstDeletedRecordAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); Float firstObjectFloat = 15F; Float secondObjectFloat = 25F; Float thirdObjectFloat = 35F; @@ -149,7 +146,6 @@ public void firstDeletedRecordAnnotatedTest() { @Test public void lastAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); Float firstObjectFloat = 25F; Float lastObjectFloat = 50F; save(new FloatFieldAnnotatedModel(firstObjectFloat)); @@ -163,7 +159,6 @@ public void lastAnnotatedTest() { @Test public void lastDeletedRecordAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); Float firstObjectFloat = 15F; Float secondObjectFloat = 25F; Float thirdObjectFloat = 35F; @@ -183,19 +178,16 @@ public void lastDeletedRecordAnnotatedTest() { @Test public void nullFirstAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); assertNull(SugarRecord.first(FloatFieldAnnotatedModel.class)); } @Test public void nullLastAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); assertNull(SugarRecord.last(FloatFieldAnnotatedModel.class)); } @Test public void oneItemAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new FloatFieldAnnotatedModel(25F)); FloatFieldAnnotatedModel firstModel = SugarRecord.first(FloatFieldAnnotatedModel.class); FloatFieldAnnotatedModel lastModel = SugarRecord.last(FloatFieldAnnotatedModel.class); diff --git a/library/src/test/java/com/orm/record/FloatFieldTests.java b/library/src/test/java/com/orm/record/FloatFieldTests.java index 58c8dc80..f6016690 100644 --- a/library/src/test/java/com/orm/record/FloatFieldTests.java +++ b/library/src/test/java/com/orm/record/FloatFieldTests.java @@ -7,6 +7,7 @@ import com.orm.models.FloatFieldAnnotatedModel; import com.orm.models.FloatFieldExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -20,9 +21,13 @@ @Config(sdk=18, application = ClientApp.class) public class FloatFieldTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void nullFloatExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new FloatFieldExtendedModel()); FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); assertNull(model.getFloat()); @@ -30,7 +35,6 @@ public void nullFloatExtendedTest() { @Test public void nullRawFloatExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new FloatFieldExtendedModel()); FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); assertEquals(0F, model.getRawFloat(), 0.0000000001F); @@ -38,7 +42,6 @@ public void nullRawFloatExtendedTest() { @Test public void nullFloatAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new FloatFieldAnnotatedModel()); FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); assertNull(model.getFloat()); @@ -46,7 +49,6 @@ public void nullFloatAnnotatedTest() { @Test public void nullRawFloatAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new FloatFieldAnnotatedModel()); FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); assertEquals(0F, model.getRawFloat(), 0.0000000001F); @@ -54,7 +56,6 @@ public void nullRawFloatAnnotatedTest() { @Test public void objectFloatExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); Float objectFloat = 25F; save(new FloatFieldExtendedModel(objectFloat)); FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); @@ -63,7 +64,6 @@ public void objectFloatExtendedTest() { @Test public void rawFloatExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new FloatFieldExtendedModel(25F)); FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); assertEquals(25F, model.getRawFloat(), 0.0000000001F); @@ -71,7 +71,6 @@ public void rawFloatExtendedTest() { @Test public void objectFloatAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); Float objectFloat = 25F; save(new FloatFieldAnnotatedModel(objectFloat)); FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); @@ -80,7 +79,6 @@ public void objectFloatAnnotatedTest() { @Test public void rawFloatAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new FloatFieldAnnotatedModel(25F)); FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); assertEquals(25F, model.getRawFloat(), 0.0000000001F); diff --git a/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java b/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java index 21898584..d421f60a 100644 --- a/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java +++ b/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java @@ -7,6 +7,7 @@ import com.orm.SugarContext; import com.orm.models.IncompleteAnnotatedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -20,15 +21,18 @@ @Config(sdk=18, application = ClientApp.class) public class IncompleteAnnotatedModelTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test(expected=SQLiteException.class) public void saveNoIdFieldTest() { - SugarContext.init(RuntimeEnvironment.application); save(new IncompleteAnnotatedModel()); } @Test public void deleteNoIdFieldTest() { - SugarContext.init(RuntimeEnvironment.application); assertFalse(delete(new IncompleteAnnotatedModel())); } } diff --git a/library/src/test/java/com/orm/record/IntegerFieldTests.java b/library/src/test/java/com/orm/record/IntegerFieldTests.java index b666e6b6..fca422c0 100644 --- a/library/src/test/java/com/orm/record/IntegerFieldTests.java +++ b/library/src/test/java/com/orm/record/IntegerFieldTests.java @@ -7,6 +7,7 @@ import com.orm.models.IntegerFieldAnnotatedModel; import com.orm.models.IntegerFieldExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -20,9 +21,13 @@ @Config(sdk=18, application = ClientApp.class) public class IntegerFieldTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void nullIntegerExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new IntegerFieldExtendedModel()); IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); assertNull(model.getInteger()); @@ -30,7 +35,6 @@ public void nullIntegerExtendedTest() { @Test public void nullIntExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new IntegerFieldExtendedModel()); IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); assertEquals(0, model.getInt()); @@ -38,7 +42,6 @@ public void nullIntExtendedTest() { @Test public void nullIntegerAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new IntegerFieldAnnotatedModel()); IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); assertNull(model.getInteger()); @@ -46,7 +49,6 @@ public void nullIntegerAnnotatedTest() { @Test public void nullIntAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new IntegerFieldAnnotatedModel()); IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); assertEquals(0, model.getInt()); @@ -54,7 +56,6 @@ public void nullIntAnnotatedTest() { @Test public void integerExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); Integer integer = 25; save(new IntegerFieldExtendedModel(integer)); IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); @@ -63,7 +64,6 @@ public void integerExtendedTest() { @Test public void intExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new IntegerFieldExtendedModel(25)); IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); assertEquals(25, model.getInt()); @@ -71,7 +71,6 @@ public void intExtendedTest() { @Test public void integerAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); Integer integer = 25; save(new IntegerFieldAnnotatedModel(integer)); IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); @@ -80,7 +79,6 @@ public void integerAnnotatedTest() { @Test public void intAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new IntegerFieldAnnotatedModel(25)); IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); assertEquals(25, model.getInt()); diff --git a/library/src/test/java/com/orm/record/ListAllOrderByTests.java b/library/src/test/java/com/orm/record/ListAllOrderByTests.java index 46a07a91..e401bd6b 100644 --- a/library/src/test/java/com/orm/record/ListAllOrderByTests.java +++ b/library/src/test/java/com/orm/record/ListAllOrderByTests.java @@ -6,6 +6,7 @@ import com.orm.SugarRecord; import com.orm.models.IntegerFieldExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -21,15 +22,19 @@ @Config(sdk=18, application = ClientApp.class) public class ListAllOrderByTests { + + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void listAllOrderByEmptyTest() { - SugarContext.init(RuntimeEnvironment.application); assertEquals(0L, SugarRecord.listAll(IntegerFieldExtendedModel.class, "id").size()); } @Test public void listAllOrderByIdTest() { - SugarContext.init(RuntimeEnvironment.application); for(int i = 1; i <= 100; i++) { save(new IntegerFieldExtendedModel(i)); } @@ -44,7 +49,6 @@ public void listAllOrderByIdTest() { @Test public void listAllOrderByFieldTest() { - SugarContext.init(RuntimeEnvironment.application); for(int i = 1; i <= 100; i++) { save(new IntegerFieldExtendedModel(i)); } diff --git a/library/src/test/java/com/orm/record/LongFieldTests.java b/library/src/test/java/com/orm/record/LongFieldTests.java index 78307436..fe6001ea 100644 --- a/library/src/test/java/com/orm/record/LongFieldTests.java +++ b/library/src/test/java/com/orm/record/LongFieldTests.java @@ -7,6 +7,7 @@ import com.orm.models.LongFieldAnnotatedModel; import com.orm.models.LongFieldExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -20,9 +21,13 @@ @Config(sdk=18, application = ClientApp.class) public class LongFieldTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void nullLongExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new LongFieldExtendedModel()); LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); assertNull(model.getLong()); @@ -30,7 +35,6 @@ public void nullLongExtendedTest() { @Test public void nullRawLongExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new LongFieldExtendedModel()); LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); assertEquals(0L, model.getRawLong()); @@ -38,7 +42,6 @@ public void nullRawLongExtendedTest() { @Test public void nullLongAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new LongFieldAnnotatedModel()); LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); assertNull(model.getLong()); @@ -46,7 +49,6 @@ public void nullLongAnnotatedTest() { @Test public void nullRawLongAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new LongFieldAnnotatedModel()); LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); assertEquals(0L, model.getRawLong()); @@ -54,7 +56,6 @@ public void nullRawLongAnnotatedTest() { @Test public void objectLongExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); Long objectLong = 25L; save(new LongFieldExtendedModel(objectLong)); LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); @@ -63,7 +64,6 @@ public void objectLongExtendedTest() { @Test public void rawLongExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new LongFieldExtendedModel(25L)); LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); assertEquals(25L, model.getRawLong()); @@ -71,7 +71,6 @@ public void rawLongExtendedTest() { @Test public void objectLongAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); Long objectLong = 25L; save(new LongFieldAnnotatedModel(objectLong)); LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); @@ -80,7 +79,6 @@ public void objectLongAnnotatedTest() { @Test public void rawLongAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new LongFieldAnnotatedModel(25L)); LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); assertEquals(25L, model.getRawLong()); diff --git a/library/src/test/java/com/orm/record/MultipleSaveTests.java b/library/src/test/java/com/orm/record/MultipleSaveTests.java index 7ffc0855..5c1ec96e 100644 --- a/library/src/test/java/com/orm/record/MultipleSaveTests.java +++ b/library/src/test/java/com/orm/record/MultipleSaveTests.java @@ -8,6 +8,7 @@ import com.orm.models.StringFieldAnnotatedNoIdModel; import com.orm.models.StringFieldExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -21,9 +22,13 @@ @Config(sdk=18, application = ClientApp.class) public class MultipleSaveTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void stringMultipleSaveOriginalExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); String string = "Test String"; StringFieldExtendedModel model = new StringFieldExtendedModel(string); long id = save(model); @@ -40,7 +45,6 @@ public void stringMultipleSaveOriginalExtendedTest() { @Test public void stringMultipleSaveQueriedExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); String string = "Test String"; StringFieldExtendedModel model = new StringFieldExtendedModel(string); long id = save(model); @@ -56,7 +60,6 @@ public void stringMultipleSaveQueriedExtendedTest() { @Test public void stringMultipleSaveOriginalAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); String string = "Test String"; StringFieldAnnotatedModel model = new StringFieldAnnotatedModel(string); long id = save(model); @@ -72,7 +75,6 @@ public void stringMultipleSaveOriginalAnnotatedTest() { @Test public void stringMultipleSaveQueriedAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); String string = "Test String"; StringFieldAnnotatedModel model = new StringFieldAnnotatedModel(string); long id = save(model); @@ -88,7 +90,6 @@ public void stringMultipleSaveQueriedAnnotatedTest() { @Test public void stringMultipleSaveOriginalAnnotatedNoIdTest() { - SugarContext.init(RuntimeEnvironment.application); String string = "Test String"; StringFieldAnnotatedNoIdModel model = new StringFieldAnnotatedNoIdModel(string); long id = save(model); @@ -105,7 +106,6 @@ public void stringMultipleSaveOriginalAnnotatedNoIdTest() { @Test public void stringMultipleSaveQueriedAnnotatedNoIdTest() { - SugarContext.init(RuntimeEnvironment.application); String string = "Test String"; StringFieldAnnotatedNoIdModel model = new StringFieldAnnotatedNoIdModel(string); long id = save(model); diff --git a/library/src/test/java/com/orm/record/NestedAnnotatedTests.java b/library/src/test/java/com/orm/record/NestedAnnotatedTests.java index a777f6de..ede95593 100644 --- a/library/src/test/java/com/orm/record/NestedAnnotatedTests.java +++ b/library/src/test/java/com/orm/record/NestedAnnotatedTests.java @@ -8,6 +8,7 @@ import com.orm.models.RelationshipAnnotatedModel; import com.orm.models.SimpleAnnotatedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -22,9 +23,13 @@ @Config(sdk=18, application = ClientApp.class) public class NestedAnnotatedTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void emptyDatabaseTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(0L, SugarRecord.count(NestedAnnotatedModel.class)); assertEquals(0L, SugarRecord.count(RelationshipAnnotatedModel.class)); assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); @@ -32,7 +37,6 @@ public void emptyDatabaseTest() throws Exception { @Test public void oneSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); @@ -45,7 +49,6 @@ public void oneSaveTest() throws Exception { @Test public void twoSameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); @@ -59,7 +62,6 @@ public void twoSameSaveTest() throws Exception { @Test public void twoDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); @@ -77,7 +79,6 @@ public void twoDifferentSaveTest() throws Exception { @Test public void manySameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); @@ -92,7 +93,6 @@ public void manySameSaveTest() throws Exception { @Test public void manyDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); @@ -107,7 +107,6 @@ public void manyDifferentSaveTest() throws Exception { @Test public void listAllSameTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); @@ -125,7 +124,6 @@ public void listAllSameTest() throws Exception { @Test public void listAllDifferentTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); diff --git a/library/src/test/java/com/orm/record/NestedExtendedTests.java b/library/src/test/java/com/orm/record/NestedExtendedTests.java index 803aa8d1..5f737166 100644 --- a/library/src/test/java/com/orm/record/NestedExtendedTests.java +++ b/library/src/test/java/com/orm/record/NestedExtendedTests.java @@ -8,6 +8,7 @@ import com.orm.models.RelationshipExtendedModel; import com.orm.models.SimpleExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -22,9 +23,13 @@ @Config(sdk=18, application = ClientApp.class) public class NestedExtendedTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void emptyDatabaseTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(0L, SugarRecord.count(NestedExtendedModel.class)); assertEquals(0L, SugarRecord.count(RelationshipExtendedModel.class)); assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); @@ -32,7 +37,6 @@ public void emptyDatabaseTest() throws Exception { @Test public void oneSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); @@ -45,7 +49,6 @@ public void oneSaveTest() throws Exception { @Test public void twoSameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); @@ -59,7 +62,6 @@ public void twoSameSaveTest() throws Exception { @Test public void twoDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); SimpleExtendedModel another_simple = new SimpleExtendedModel(); @@ -77,7 +79,6 @@ public void twoDifferentSaveTest() throws Exception { @Test public void manySameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); @@ -92,7 +93,6 @@ public void manySameSaveTest() throws Exception { @Test public void manyDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); @@ -107,7 +107,6 @@ public void manyDifferentSaveTest() throws Exception { @Test public void listAllSameTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); @@ -125,7 +124,6 @@ public void listAllSameTest() throws Exception { @Test public void listAllDifferentTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); diff --git a/library/src/test/java/com/orm/record/NestedMixedAATests.java b/library/src/test/java/com/orm/record/NestedMixedAATests.java index f3acacd2..cb5335b4 100644 --- a/library/src/test/java/com/orm/record/NestedMixedAATests.java +++ b/library/src/test/java/com/orm/record/NestedMixedAATests.java @@ -8,6 +8,7 @@ import com.orm.models.RelationshipMixedAModel; import com.orm.models.SimpleAnnotatedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -22,9 +23,13 @@ @Config(sdk=18, application = ClientApp.class) public class NestedMixedAATests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void emptyDatabaseTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(0L, SugarRecord.count(NestedMixedAAModel.class)); assertEquals(0L, SugarRecord.count(RelationshipMixedAModel.class)); assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); @@ -32,7 +37,6 @@ public void emptyDatabaseTest() throws Exception { @Test public void oneSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); @@ -45,7 +49,6 @@ public void oneSaveTest() throws Exception { @Test public void twoSameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); @@ -59,7 +62,6 @@ public void twoSameSaveTest() throws Exception { @Test public void twoDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); @@ -77,7 +79,6 @@ public void twoDifferentSaveTest() throws Exception { @Test public void manySameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); @@ -92,7 +93,6 @@ public void manySameSaveTest() throws Exception { @Test public void manyDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); @@ -107,7 +107,6 @@ public void manyDifferentSaveTest() throws Exception { @Test public void listAllSameTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); @@ -125,7 +124,6 @@ public void listAllSameTest() throws Exception { @Test public void listAllDifferentTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); diff --git a/library/src/test/java/com/orm/record/NestedMixedABTests.java b/library/src/test/java/com/orm/record/NestedMixedABTests.java index 5d4799d5..3677a312 100644 --- a/library/src/test/java/com/orm/record/NestedMixedABTests.java +++ b/library/src/test/java/com/orm/record/NestedMixedABTests.java @@ -8,6 +8,7 @@ import com.orm.models.RelationshipMixedBModel; import com.orm.models.SimpleExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -22,9 +23,13 @@ @Config(sdk=18, application = ClientApp.class) public class NestedMixedABTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void emptyDatabaseTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(0L, SugarRecord.count(NestedMixedABModel.class)); assertEquals(0L, SugarRecord.count(RelationshipMixedBModel.class)); assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); @@ -32,7 +37,6 @@ public void emptyDatabaseTest() throws Exception { @Test public void oneSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); @@ -45,7 +49,6 @@ public void oneSaveTest() throws Exception { @Test public void twoSameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); @@ -59,7 +62,6 @@ public void twoSameSaveTest() throws Exception { @Test public void twoDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); SimpleExtendedModel another_simple = new SimpleExtendedModel(); @@ -77,7 +79,6 @@ public void twoDifferentSaveTest() throws Exception { @Test public void manySameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); @@ -92,7 +93,6 @@ public void manySameSaveTest() throws Exception { @Test public void manyDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); @@ -107,7 +107,6 @@ public void manyDifferentSaveTest() throws Exception { @Test public void listAllSameTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); @@ -125,7 +124,6 @@ public void listAllSameTest() throws Exception { @Test public void listAllDifferentTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); diff --git a/library/src/test/java/com/orm/record/NestedMixedBATests.java b/library/src/test/java/com/orm/record/NestedMixedBATests.java index f7575770..5dfd0757 100644 --- a/library/src/test/java/com/orm/record/NestedMixedBATests.java +++ b/library/src/test/java/com/orm/record/NestedMixedBATests.java @@ -8,6 +8,7 @@ import com.orm.models.RelationshipMixedAModel; import com.orm.models.SimpleAnnotatedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -22,9 +23,13 @@ @Config(sdk=18, application = ClientApp.class) public class NestedMixedBATests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void emptyDatabaseTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(0L, SugarRecord.count(NestedMixedBAModel.class)); assertEquals(0L, SugarRecord.count(RelationshipMixedAModel.class)); assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); @@ -32,7 +37,6 @@ public void emptyDatabaseTest() throws Exception { @Test public void oneSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); @@ -45,7 +49,6 @@ public void oneSaveTest() throws Exception { @Test public void twoSameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); @@ -59,7 +62,6 @@ public void twoSameSaveTest() throws Exception { @Test public void twoDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); @@ -77,7 +79,6 @@ public void twoDifferentSaveTest() throws Exception { @Test public void manySameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); @@ -92,7 +93,6 @@ public void manySameSaveTest() throws Exception { @Test public void manyDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); @@ -107,7 +107,6 @@ public void manyDifferentSaveTest() throws Exception { @Test public void listAllSameTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); @@ -125,7 +124,6 @@ public void listAllSameTest() throws Exception { @Test public void listAllDifferentTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); diff --git a/library/src/test/java/com/orm/record/NestedMixedBBTests.java b/library/src/test/java/com/orm/record/NestedMixedBBTests.java index af88af23..e2625eae 100644 --- a/library/src/test/java/com/orm/record/NestedMixedBBTests.java +++ b/library/src/test/java/com/orm/record/NestedMixedBBTests.java @@ -8,6 +8,7 @@ import com.orm.models.RelationshipMixedBModel; import com.orm.models.SimpleExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -22,9 +23,13 @@ @Config(sdk=18, application = ClientApp.class) public class NestedMixedBBTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void emptyDatabaseTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(0L, SugarRecord.count(NestedMixedBBModel.class)); assertEquals(0L, SugarRecord.count(RelationshipMixedBModel.class)); assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); @@ -32,7 +37,6 @@ public void emptyDatabaseTest() throws Exception { @Test public void oneSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); @@ -45,7 +49,6 @@ public void oneSaveTest() throws Exception { @Test public void twoSameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); @@ -59,7 +62,6 @@ public void twoSameSaveTest() throws Exception { @Test public void twoDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); SimpleExtendedModel another_simple = new SimpleExtendedModel(); @@ -77,7 +79,6 @@ public void twoDifferentSaveTest() throws Exception { @Test public void manySameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); @@ -92,7 +93,6 @@ public void manySameSaveTest() throws Exception { @Test public void manyDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); @@ -107,7 +107,6 @@ public void manyDifferentSaveTest() throws Exception { @Test public void listAllSameTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); @@ -125,7 +124,6 @@ public void listAllSameTest() throws Exception { @Test public void listAllDifferentTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); diff --git a/library/src/test/java/com/orm/record/NoSugarModelTests.java b/library/src/test/java/com/orm/record/NoSugarModelTests.java index 5ebc40ca..b77858f6 100644 --- a/library/src/test/java/com/orm/record/NoSugarModelTests.java +++ b/library/src/test/java/com/orm/record/NoSugarModelTests.java @@ -6,6 +6,7 @@ import com.orm.SugarRecord; import com.orm.models.NoSugarModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -18,16 +19,19 @@ @Config(sdk=18, application = ClientApp.class) public class NoSugarModelTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void deleteTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); NoSugarModel model = new NoSugarModel(); assertFalse(SugarRecord.delete(model)); } @Test public void saveInTransactionTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SugarRecord.saveInTx(new NoSugarModel(), new NoSugarModel()); assertEquals(-1L, SugarRecord.count(NoSugarModel.class)); } diff --git a/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java b/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java index 0719bcd0..df229b90 100644 --- a/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java +++ b/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java @@ -7,6 +7,7 @@ import com.orm.models.RelationshipAnnotatedModel; import com.orm.models.SimpleAnnotatedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -21,16 +22,19 @@ @Config(sdk=18, application = ClientApp.class) public class RelationshipAnnotatedTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void emptyDatabaseTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(0L, SugarRecord.count(RelationshipAnnotatedModel.class)); assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); } @Test public void oneSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); save(new RelationshipAnnotatedModel(simple)); @@ -40,7 +44,6 @@ public void oneSaveTest() throws Exception { @Test public void twoSameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); save(new RelationshipAnnotatedModel(simple)); @@ -51,7 +54,6 @@ public void twoSameSaveTest() throws Exception { @Test public void twoDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); @@ -64,7 +66,6 @@ public void twoDifferentSaveTest() throws Exception { @Test public void manySameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); for (int i = 1; i <= 100; i++) { @@ -76,7 +77,6 @@ public void manySameSaveTest() throws Exception { @Test public void manyDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); @@ -88,7 +88,6 @@ public void manyDifferentSaveTest() throws Exception { @Test public void listAllSameTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); for (int i = 1; i <= 100; i++) { @@ -104,7 +103,6 @@ public void listAllSameTest() throws Exception { @Test public void listAllDifferentTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); diff --git a/library/src/test/java/com/orm/record/RelationshipExtendedTests.java b/library/src/test/java/com/orm/record/RelationshipExtendedTests.java index dbd17fe6..6b3a7400 100644 --- a/library/src/test/java/com/orm/record/RelationshipExtendedTests.java +++ b/library/src/test/java/com/orm/record/RelationshipExtendedTests.java @@ -7,6 +7,7 @@ import com.orm.models.RelationshipExtendedModel; import com.orm.models.SimpleExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -21,16 +22,19 @@ @Config(sdk=18, application = ClientApp.class) public class RelationshipExtendedTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void emptyDatabaseTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(0L, SugarRecord.count(RelationshipExtendedModel.class)); assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); } @Test public void oneSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); save(new RelationshipExtendedModel(simple)); @@ -40,7 +44,6 @@ public void oneSaveTest() throws Exception { @Test public void twoSameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); save(new RelationshipExtendedModel(simple)); @@ -51,7 +54,6 @@ public void twoSameSaveTest() throws Exception { @Test public void twoDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); SimpleExtendedModel another_simple = new SimpleExtendedModel(); @@ -64,7 +66,6 @@ public void twoDifferentSaveTest() throws Exception { @Test public void manySameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); for (int i = 1; i <= 100; i++) { @@ -76,7 +77,6 @@ public void manySameSaveTest() throws Exception { @Test public void manyDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); @@ -88,7 +88,6 @@ public void manyDifferentSaveTest() throws Exception { @Test public void listAllSameTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); for (int i = 1; i <= 100; i++) { @@ -104,7 +103,6 @@ public void listAllSameTest() throws Exception { @Test public void listAllDifferentTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); diff --git a/library/src/test/java/com/orm/record/RelationshipMixedBTests.java b/library/src/test/java/com/orm/record/RelationshipMixedBTests.java index 190c030f..419ba98b 100644 --- a/library/src/test/java/com/orm/record/RelationshipMixedBTests.java +++ b/library/src/test/java/com/orm/record/RelationshipMixedBTests.java @@ -7,6 +7,7 @@ import com.orm.models.RelationshipMixedBModel; import com.orm.models.SimpleExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -21,16 +22,19 @@ @Config(sdk=18, application = ClientApp.class) public class RelationshipMixedBTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void emptyDatabaseTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(0L, SugarRecord.count(RelationshipMixedBModel.class)); assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); } @Test public void oneSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); save(new RelationshipMixedBModel(simple)); @@ -40,7 +44,6 @@ public void oneSaveTest() throws Exception { @Test public void twoSameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); save(new RelationshipMixedBModel(simple)); @@ -51,7 +54,6 @@ public void twoSameSaveTest() throws Exception { @Test public void twoDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); SimpleExtendedModel another_simple = new SimpleExtendedModel(); @@ -64,7 +66,6 @@ public void twoDifferentSaveTest() throws Exception { @Test public void manySameSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); for (int i = 1; i <= 100; i++) { @@ -76,7 +77,6 @@ public void manySameSaveTest() throws Exception { @Test public void manyDifferentSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); @@ -88,7 +88,6 @@ public void manyDifferentSaveTest() throws Exception { @Test public void listAllSameTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); for (int i = 1; i <= 100; i++) { diff --git a/library/src/test/java/com/orm/record/ShortFieldTests.java b/library/src/test/java/com/orm/record/ShortFieldTests.java index 0de32144..efe36cf0 100644 --- a/library/src/test/java/com/orm/record/ShortFieldTests.java +++ b/library/src/test/java/com/orm/record/ShortFieldTests.java @@ -7,6 +7,7 @@ import com.orm.models.ShortFieldAnnotatedModel; import com.orm.models.ShortFieldExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -20,9 +21,13 @@ @Config(sdk=18, application = ClientApp.class) public class ShortFieldTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void nullShortExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new ShortFieldExtendedModel()); ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); assertNull(model.getShort()); @@ -30,7 +35,6 @@ public void nullShortExtendedTest() { @Test public void nullRawShortExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new ShortFieldExtendedModel()); ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); assertEquals((short) 0, model.getRawShort()); @@ -38,7 +42,6 @@ public void nullRawShortExtendedTest() { @Test public void nullShortAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new ShortFieldAnnotatedModel()); ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); assertNull(model.getShort()); @@ -46,7 +49,6 @@ public void nullShortAnnotatedTest() { @Test public void nullRawShortAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new ShortFieldAnnotatedModel()); ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); assertEquals((short) 0, model.getRawShort()); @@ -54,7 +56,6 @@ public void nullRawShortAnnotatedTest() { @Test public void objectShortExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); Short objectShort = 25; save(new ShortFieldExtendedModel(objectShort)); ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); @@ -63,7 +64,6 @@ public void objectShortExtendedTest() { @Test public void rawShortExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new ShortFieldExtendedModel((short) 25)); ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); assertEquals((short) 25, model.getRawShort()); @@ -71,7 +71,6 @@ public void rawShortExtendedTest() { @Test public void objectShortAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); Short objectShort = 25; save(new ShortFieldAnnotatedModel(objectShort)); ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); @@ -80,7 +79,6 @@ public void objectShortAnnotatedTest() { @Test public void rawShortAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new ShortFieldAnnotatedModel((short) 25)); ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); assertEquals((short) 25, model.getRawShort()); diff --git a/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java b/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java index d2f8508e..f223a49c 100644 --- a/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java +++ b/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java @@ -7,6 +7,7 @@ import com.orm.helper.NamingHelper; import com.orm.models.SimpleAnnotatedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -29,22 +30,24 @@ @Config(sdk=18, application = ClientApp.class) public class SimpleAnnotatedModelTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void emptyDatabaseTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); } @Test public void oneSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleAnnotatedModel()); assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); } @Test public void twoSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleAnnotatedModel()); save(new SimpleAnnotatedModel()); assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); @@ -52,7 +55,6 @@ public void twoSaveTest() throws Exception { @Test public void manySaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { save(new SimpleAnnotatedModel()); } @@ -61,13 +63,11 @@ public void manySaveTest() throws Exception { @Test public void defaultIdTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(1L, save(new SimpleAnnotatedModel())); } @Test public void whereCountTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleAnnotatedModel()); save(new SimpleAnnotatedModel()); assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class, "id = ?", new String[]{"1"})); @@ -75,7 +75,6 @@ public void whereCountTest() throws Exception { @Test public void whereNoCountTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class, "id = ?", new String[]{"1"})); save(new SimpleAnnotatedModel()); save(new SimpleAnnotatedModel()); @@ -85,7 +84,6 @@ public void whereNoCountTest() throws Exception { @Test public void whereBrokenCountTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleAnnotatedModel()); save(new SimpleAnnotatedModel()); assertEquals(-1L, SugarRecord.count(SimpleAnnotatedModel.class, "di = ?", new String[]{"1"})); @@ -93,7 +91,6 @@ public void whereBrokenCountTest() throws Exception { @Test public void deleteTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel model = new SimpleAnnotatedModel(); save(model); assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); @@ -103,7 +100,6 @@ public void deleteTest() throws Exception { @Test public void deleteUnsavedTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel model = new SimpleAnnotatedModel(); assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); assertFalse(SugarRecord.delete(model)); @@ -112,7 +108,6 @@ public void deleteUnsavedTest() throws Exception { @Test public void deleteWrongTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel model = new SimpleAnnotatedModel(); save(model); assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); @@ -125,7 +120,6 @@ public void deleteWrongTest() throws Exception { @Test public void deleteAllTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); int elementNumber = 100; for (int i = 1; i <= elementNumber; i++) { save(new SimpleAnnotatedModel()); @@ -137,7 +131,6 @@ public void deleteAllTest() throws Exception { @Test @SuppressWarnings("all") public void deleteAllWhereTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); int elementNumber = 100; for (int i = 1; i <= elementNumber; i++) { save(new SimpleAnnotatedModel()); @@ -148,7 +141,6 @@ public void deleteAllWhereTest() throws Exception { @Test public void deleteInTransactionFewTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleAnnotatedModel first = new SimpleAnnotatedModel(); SimpleAnnotatedModel second = new SimpleAnnotatedModel(); SimpleAnnotatedModel third = new SimpleAnnotatedModel(); @@ -162,7 +154,6 @@ public void deleteInTransactionFewTest() throws Exception { @Test public void deleteInTransactionManyTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); long elementNumber = 100; List models = new ArrayList<>(); for (int i = 1; i <= elementNumber; i++) { @@ -180,14 +171,12 @@ public void deleteInTransactionManyTest() throws Exception { @Test public void saveInTransactionTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SugarRecord.saveInTx(new SimpleAnnotatedModel(), new SimpleAnnotatedModel()); assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); } @Test public void listAllTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { save(new SimpleAnnotatedModel()); } @@ -200,7 +189,6 @@ public void listAllTest() throws Exception { @Test public void findTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleAnnotatedModel()); save(new SimpleAnnotatedModel()); List models = @@ -211,7 +199,6 @@ public void findTest() throws Exception { @Test public void findWithQueryTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { save(new SimpleAnnotatedModel()); } @@ -227,28 +214,24 @@ public void findWithQueryTest() throws Exception { @Test @SuppressWarnings("all") public void findByIdTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleAnnotatedModel()); assertEquals(1L, SugarRecord.findById(SimpleAnnotatedModel.class, 1L).getId().longValue()); } @Test public void findByIdIntegerTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleAnnotatedModel()); assertEquals(1L, SugarRecord.findById(SimpleAnnotatedModel.class, 1).getId().longValue()); } @Test public void findByIdStringsNullTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleAnnotatedModel()); assertEquals(0, SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{""}).size()); } @Test public void findByIdStringsOneTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleAnnotatedModel()); List models = SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{"1"}); @@ -258,7 +241,6 @@ public void findByIdStringsOneTest() throws Exception { @Test public void findByIdStringsTwoTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleAnnotatedModel()); save(new SimpleAnnotatedModel()); save(new SimpleAnnotatedModel()); @@ -271,7 +253,6 @@ public void findByIdStringsTwoTest() throws Exception { @Test public void findByIdStringsManyTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 10; i++) { save(new SimpleAnnotatedModel()); } @@ -286,7 +267,6 @@ public void findByIdStringsManyTest() throws Exception { @Test public void findByIdStringsOrderTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 10; i++) { save(new SimpleAnnotatedModel()); } @@ -302,14 +282,12 @@ public void findByIdStringsOrderTest() throws Exception { @Test public void findByIdNullTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleAnnotatedModel()); assertNull(SugarRecord.findById(SimpleAnnotatedModel.class, 2L)); } @Test public void findAllTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { save(new SimpleAnnotatedModel()); } @@ -324,7 +302,6 @@ public void findAllTest() throws Exception { @Test public void findAsIteratorTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { save(new SimpleAnnotatedModel()); } @@ -340,7 +317,6 @@ public void findAsIteratorTest() throws Exception { @Test public void findWithQueryAsIteratorTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { save(new SimpleAnnotatedModel()); } @@ -359,7 +335,6 @@ public void findWithQueryAsIteratorTest() throws Exception { @Test(expected=NoSuchElementException.class) public void findAsIteratorOutOfBoundsTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleAnnotatedModel()); Iterator cursor = SugarRecord.findAsIterator(SimpleAnnotatedModel.class, "id = ?", "1"); @@ -373,7 +348,6 @@ public void findAsIteratorOutOfBoundsTest() throws Exception { @Test(expected=UnsupportedOperationException.class) public void disallowRemoveCursorTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleAnnotatedModel()); Iterator cursor = SugarRecord.findAsIterator(SimpleAnnotatedModel.class, "id = ?", "1"); @@ -387,7 +361,6 @@ public void disallowRemoveCursorTest() throws Exception { @Test public void vacuumTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SugarRecord.executeQuery("Vacuum"); } } \ No newline at end of file diff --git a/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java b/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java index c6f4d37f..9ac1d696 100644 --- a/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java +++ b/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java @@ -7,6 +7,7 @@ import com.orm.helper.NamingHelper; import com.orm.models.SimpleExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -29,22 +30,24 @@ @Config(sdk=18, application = ClientApp.class) public class SimpleExtendedModelTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + @Test public void emptyDatabaseTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); } @Test public void oneSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleExtendedModel()); assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); } @Test public void twoSaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleExtendedModel()); save(new SimpleExtendedModel()); assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); @@ -52,7 +55,6 @@ public void twoSaveTest() throws Exception { @Test public void manySaveTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { save(new SimpleExtendedModel()); } @@ -61,13 +63,11 @@ public void manySaveTest() throws Exception { @Test public void defaultIdTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(1L, save(new SimpleExtendedModel())); } @Test public void whereCountTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleExtendedModel()); save(new SimpleExtendedModel()); assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class, "id = ?", new String[]{"1"})); @@ -75,7 +75,6 @@ public void whereCountTest() throws Exception { @Test public void whereNoCountTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class, "id = ?", new String[]{"1"})); save(new SimpleExtendedModel()); save(new SimpleExtendedModel()); @@ -85,7 +84,6 @@ public void whereNoCountTest() throws Exception { @Test public void whereBrokenCountTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleExtendedModel()); save(new SimpleExtendedModel()); assertEquals(-1L, SugarRecord.count(SimpleExtendedModel.class, "di = ?", new String[]{"1"})); @@ -93,7 +91,6 @@ public void whereBrokenCountTest() throws Exception { @Test public void saveMethodTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel model = new SimpleExtendedModel(); model.save(); assertEquals(-1L, SugarRecord.count(SimpleExtendedModel.class, "di = ?", new String[]{"1"})); @@ -101,7 +98,6 @@ public void saveMethodTest() throws Exception { @Test public void deleteTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel model = new SimpleExtendedModel(); save(model); assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); @@ -111,7 +107,6 @@ public void deleteTest() throws Exception { @Test public void deleteUnsavedTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel model = new SimpleExtendedModel(); assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); assertFalse(SugarRecord.delete(model)); @@ -120,7 +115,6 @@ public void deleteUnsavedTest() throws Exception { @Test public void deleteWrongTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel model = new SimpleExtendedModel(); save(model); assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); @@ -133,7 +127,6 @@ public void deleteWrongTest() throws Exception { @Test public void deleteAllTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); int elementNumber = 100; for (int i = 1; i <= elementNumber; i++) { save(new SimpleExtendedModel()); @@ -145,7 +138,6 @@ public void deleteAllTest() throws Exception { @Test @SuppressWarnings("all") public void deleteAllWhereTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); int elementNumber = 100; for (int i = 1; i <= elementNumber; i++) { save(new SimpleExtendedModel()); @@ -158,7 +150,6 @@ public void deleteAllWhereTest() throws Exception { @Test public void deleteInTransactionFewTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SimpleExtendedModel first = new SimpleExtendedModel(); SimpleExtendedModel second = new SimpleExtendedModel(); SimpleExtendedModel third = new SimpleExtendedModel(); @@ -172,7 +163,6 @@ public void deleteInTransactionFewTest() throws Exception { @Test public void deleteInTransactionManyTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); long elementNumber = 100; List models = new ArrayList<>(); for (int i = 1; i <= elementNumber; i++) { @@ -190,14 +180,12 @@ public void deleteInTransactionManyTest() throws Exception { @Test public void saveInTransactionTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SugarRecord.saveInTx(new SimpleExtendedModel(), new SimpleExtendedModel()); assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); } @Test public void listAllTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { save(new SimpleExtendedModel()); } @@ -210,7 +198,6 @@ public void listAllTest() throws Exception { @Test public void findTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleExtendedModel()); save(new SimpleExtendedModel()); List models = @@ -221,7 +208,6 @@ public void findTest() throws Exception { @Test public void findWithQueryTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { save(new SimpleExtendedModel()); } @@ -237,28 +223,24 @@ public void findWithQueryTest() throws Exception { @Test @SuppressWarnings("all") public void findByIdTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleExtendedModel()); assertEquals(Long.valueOf(1L), SugarRecord.findById(SimpleExtendedModel.class, 1L).getId()); } @Test public void findByIdIntegerTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleExtendedModel()); assertEquals(Long.valueOf(1L), SugarRecord.findById(SimpleExtendedModel.class, 1).getId()); } @Test public void findByIdStringsNullTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleExtendedModel()); assertEquals(0, SugarRecord.findById(SimpleExtendedModel.class, new String[]{""}).size()); } @Test public void findByIdStringsOneTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleExtendedModel()); List models = SugarRecord.findById(SimpleExtendedModel.class, new String[]{"1"}); @@ -268,7 +250,6 @@ public void findByIdStringsOneTest() throws Exception { @Test public void findByIdStringsTwoTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleExtendedModel()); save(new SimpleExtendedModel()); save(new SimpleExtendedModel()); @@ -281,7 +262,6 @@ public void findByIdStringsTwoTest() throws Exception { @Test public void findByIdStringsManyTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 10; i++) { save(new SimpleExtendedModel()); } @@ -296,7 +276,6 @@ public void findByIdStringsManyTest() throws Exception { @Test public void findByIdStringsOrderTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 10; i++) { save(new SimpleExtendedModel()); } @@ -312,14 +291,12 @@ public void findByIdStringsOrderTest() throws Exception { @Test public void findByIdNullTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleExtendedModel()); assertNull(SugarRecord.findById(SimpleExtendedModel.class, 2L)); } @Test public void findAllTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { save(new SimpleExtendedModel()); } @@ -334,7 +311,6 @@ public void findAllTest() throws Exception { @Test public void findAsIteratorTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { save(new SimpleExtendedModel()); } @@ -350,7 +326,6 @@ public void findAsIteratorTest() throws Exception { @Test public void findWithQueryAsIteratorTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { save(new SimpleExtendedModel()); } @@ -359,6 +334,7 @@ public void findWithQueryAsIteratorTest() throws Exception { "Select * from " + NamingHelper.toSQLName(SimpleExtendedModel.class) + " where id >= ? ", "50"); + SugarContext.init(RuntimeEnvironment.application); for (int i = 50; i <= 100; i++) { assertTrue(cursor.hasNext()); SimpleExtendedModel model = cursor.next(); @@ -369,7 +345,6 @@ public void findWithQueryAsIteratorTest() throws Exception { @Test(expected=NoSuchElementException.class) public void findAsIteratorOutOfBoundsTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleExtendedModel()); Iterator cursor = SugarRecord.findAsIterator(SimpleExtendedModel.class, "id = ?", "1"); @@ -383,7 +358,6 @@ public void findAsIteratorOutOfBoundsTest() throws Exception { @Test(expected=UnsupportedOperationException.class) public void disallowRemoveCursorTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); save(new SimpleExtendedModel()); Iterator cursor = SugarRecord.findAsIterator(SimpleExtendedModel.class, "id = ?", "1"); @@ -397,7 +371,6 @@ public void disallowRemoveCursorTest() throws Exception { @Test public void vacuumTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); SugarRecord.executeQuery("Vacuum"); } } \ No newline at end of file diff --git a/library/src/test/java/com/orm/record/StringFieldTests.java b/library/src/test/java/com/orm/record/StringFieldTests.java index 9c69e062..6fd800cd 100644 --- a/library/src/test/java/com/orm/record/StringFieldTests.java +++ b/library/src/test/java/com/orm/record/StringFieldTests.java @@ -7,6 +7,7 @@ import com.orm.models.StringFieldAnnotatedModel; import com.orm.models.StringFieldExtendedModel; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RuntimeEnvironment; @@ -20,9 +21,14 @@ @Config(sdk=18, application = ClientApp.class) public class StringFieldTests { + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + + @Test public void nullStringExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new StringFieldExtendedModel()); StringFieldExtendedModel model = SugarRecord.findById(StringFieldExtendedModel.class, 1); assertNull(model.getString()); @@ -30,7 +36,6 @@ public void nullStringExtendedTest() { @Test public void nullStringAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); save(new StringFieldAnnotatedModel()); StringFieldAnnotatedModel model = SugarRecord.findById(StringFieldAnnotatedModel.class, 1); assertNull(model.getString()); @@ -38,7 +43,6 @@ public void nullStringAnnotatedTest() { @Test public void stringExtendedTest() { - SugarContext.init(RuntimeEnvironment.application); String string = "Test String"; save(new StringFieldExtendedModel(string)); StringFieldExtendedModel model = SugarRecord.findById(StringFieldExtendedModel.class, 1); @@ -47,7 +51,6 @@ public void stringExtendedTest() { @Test public void stringAnnotatedTest() { - SugarContext.init(RuntimeEnvironment.application); String string = "Test String"; save(new StringFieldAnnotatedModel(string)); StringFieldAnnotatedModel model = SugarRecord.findById(StringFieldAnnotatedModel.class, 1); diff --git a/library/src/test/java/com/orm/util/ContextUtilTest.java b/library/src/test/java/com/orm/util/ContextUtilTest.java index fc627076..b4bd9042 100644 --- a/library/src/test/java/com/orm/util/ContextUtilTest.java +++ b/library/src/test/java/com/orm/util/ContextUtilTest.java @@ -2,10 +2,15 @@ import android.content.Context; -import com.orm.query.DummyContext; +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import org.junit.Before; import org.junit.Test; - +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNull; @@ -14,46 +19,43 @@ /** * @author jonatan.salas */ +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18, application = ClientApp.class, manifest = Config.NONE) public class ContextUtilTest { - public void initContextUtil() { - init(new DummyContext()); + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application.getApplicationContext()); } @Test public void testInitContext() { - initContextUtil(); assertNotNull(getContext()); } @Test public void testGetAssets() { - initContextUtil(); - assertNull(getAssets()); + assertNotNull(getAssets()); } @Test public void testGetPackageManager() { - initContextUtil(); - assertNull(getPackageManager()); + assertNotNull(getPackageManager()); } @Test public void testGetPackageName() { - initContextUtil(); - assertNull(getPackageName()); + assertNotNull(getPackageName()); } @Test public void testGetPreferences() { - initContextUtil(); - assertNull(getSharedPreferences("lala", Context.MODE_PRIVATE)); + assertNotNull(getSharedPreferences("lala", Context.MODE_PRIVATE)); } @Test public void testTerminateContext() { - initContextUtil(); - terminate(); + SugarContext.terminate(); assertNull(getContext()); } } diff --git a/library/src/test/java/com/orm/util/ManifestHelperTest.java b/library/src/test/java/com/orm/util/ManifestHelperTest.java index 65340b0d..4340ac43 100644 --- a/library/src/test/java/com/orm/util/ManifestHelperTest.java +++ b/library/src/test/java/com/orm/util/ManifestHelperTest.java @@ -1,49 +1,52 @@ package com.orm.util; -import com.orm.query.DummyContext; +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; import static org.junit.Assert.*; import static com.orm.helper.ManifestHelper.*; -import static com.orm.util.ContextUtil.init; +import static com.orm.SugarContext.init; /** * @author jonatan.salas */ +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18, application = ClientApp.class, manifest = Config.NONE) public class ManifestHelperTest { - public void initContext() { - init(new DummyContext()); + @Before + public void setUp() { + init(RuntimeEnvironment.application); } @Test public void testGetDbName() { - initContext(); assertEquals(DATABASE_DEFAULT_NAME, getDatabaseName()); } @Test public void testGetDatabaseName() { - initContext(); assertEquals(DATABASE_DEFAULT_NAME, getDatabaseName()); } @Test public void testGetDatabaseVersion() { - initContext(); assertEquals(1, getDatabaseVersion()); } @Test public void testGetDomainPackageName() { - initContext(); assertNotNull(getDomainPackageName()); } @Test public void testGetDebugEnabled() { - initContext(); assertEquals(false, isDebugEnabled()); } } From ba572f93558856ea935ce6839f6cfee261600c38 Mon Sep 17 00:00:00 2001 From: jonatan Date: Fri, 8 Apr 2016 19:02:26 -0300 Subject: [PATCH 081/139] Added NumberComparatorTest Added ReflectionUtilTest Added SugarConfigTest Refactored SchemaGeneratorTest and NamingHelperTest Simplified NumberComparator Changed NamingHelper method to toTableName and toColumnName Reflection util is final and has a private constructor to prevent instantiation --- .../sugartest/SimpleAnnotatedModelTests.java | 4 +- .../sugartest/SimpleExtendedModelTests.java | 4 +- .../main/java/com/orm/SchemaGenerator.java | 12 ++--- .../src/main/java/com/orm/SugarRecord.java | 26 ++++----- .../java/com/orm/helper/NamingHelper.java | 4 +- .../src/main/java/com/orm/query/Select.java | 2 +- .../java/com/orm/util/NumberComparator.java | 19 ++++--- .../java/com/orm/util/ReflectionUtil.java | 14 ++--- .../main/java/com/orm/util/SugarConfig.java | 1 - .../test/java/com/orm/NamingHelperTest.java | 4 +- .../java/com/orm/SchemaGeneratorTest.java | 12 ++--- .../orm/record/SimpleAnnotatedModelTests.java | 4 +- .../orm/record/SimpleExtendedModelTests.java | 4 +- .../com/orm/util/NumberComparatorTest.java | 54 +++++++++++++++++++ .../java/com/orm/util/ReflectionUtilTest.java | 35 ++++++++++++ .../java/com/orm/util/SugarConfigTest.java | 15 ++++++ 16 files changed, 161 insertions(+), 53 deletions(-) create mode 100644 library/src/test/java/com/orm/util/NumberComparatorTest.java create mode 100644 library/src/test/java/com/orm/util/ReflectionUtilTest.java create mode 100644 library/src/test/java/com/orm/util/SugarConfigTest.java diff --git a/example/src/test/java/com/example/sugartest/SimpleAnnotatedModelTests.java b/example/src/test/java/com/example/sugartest/SimpleAnnotatedModelTests.java index bce99ffe..8436df41 100644 --- a/example/src/test/java/com/example/sugartest/SimpleAnnotatedModelTests.java +++ b/example/src/test/java/com/example/sugartest/SimpleAnnotatedModelTests.java @@ -195,7 +195,7 @@ public void findWithQueryTest() throws Exception { } List models = SugarRecord.findWithQuery(SimpleAnnotatedModel.class, "Select * from " + - NamingHelper.toSQLName(SimpleAnnotatedModel.class) + + NamingHelper.toTableName(SimpleAnnotatedModel.class) + " where id >= ? ", "50"); for (SimpleAnnotatedModel model : models) { assertEquals(new Long(75), model.getId(), 25L); @@ -313,7 +313,7 @@ public void findWithQueryAsIteratorTest() throws Exception { Iterator cursor = SugarRecord.findWithQueryAsIterator(SimpleAnnotatedModel.class, "Select * from " + - NamingHelper.toSQLName(SimpleAnnotatedModel.class) + + NamingHelper.toTableName(SimpleAnnotatedModel.class) + " where id >= ? ", "50"); for (int i = 50; i <= 100; i++) { assertTrue(cursor.hasNext()); diff --git a/example/src/test/java/com/example/sugartest/SimpleExtendedModelTests.java b/example/src/test/java/com/example/sugartest/SimpleExtendedModelTests.java index e8645fb4..7abaf58a 100644 --- a/example/src/test/java/com/example/sugartest/SimpleExtendedModelTests.java +++ b/example/src/test/java/com/example/sugartest/SimpleExtendedModelTests.java @@ -202,7 +202,7 @@ public void findWithQueryTest() throws Exception { } List models = SugarRecord.findWithQuery(SimpleExtendedModel.class, "Select * from " + - NamingHelper.toSQLName(SimpleExtendedModel.class) + + NamingHelper.toTableName(SimpleExtendedModel.class) + " where id >= ? ", "50"); for (SimpleExtendedModel model : models) { assertEquals(new Long(75), model.getId(), 25L); @@ -320,7 +320,7 @@ public void findWithQueryAsIteratorTest() throws Exception { Iterator cursor = SugarRecord.findWithQueryAsIterator(SimpleExtendedModel.class, "Select * from " + - NamingHelper.toSQLName(SimpleExtendedModel.class) + + NamingHelper.toTableName(SimpleExtendedModel.class) + " where id >= ? ", "50"); for (int i = 50; i <= 100; i++) { assertTrue(cursor.hasNext()); diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index 2f95b568..2ad6529f 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -54,7 +54,7 @@ public void doUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVers String sql = "select count(*) from sqlite_master where type='table' and name='%s';"; for (Class domain : domainClasses) { - String tableName = NamingHelper.toSQLName(domain); + String tableName = NamingHelper.toTableName(domain); Cursor c = sqLiteDatabase.rawQuery(String.format(sql, tableName), null); if (c.moveToFirst() && c.getInt(0) == 0) { createTable(domain, sqLiteDatabase); @@ -81,7 +81,7 @@ private ArrayList getColumnNames(SQLiteDatabase sqLiteDatabase, String t public void deleteTables(SQLiteDatabase sqLiteDatabase) { List tables = getDomainClasses(); for (Class table : tables) { - sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + NamingHelper.toSQLName(table)); + sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + NamingHelper.toTableName(table)); } } @@ -139,12 +139,12 @@ private void executeScript(SQLiteDatabase db, String file) { private void addColumns(Class table, SQLiteDatabase sqLiteDatabase) { List fields = ReflectionUtil.getTableFields(table); - String tableName = NamingHelper.toSQLName(table); + String tableName = NamingHelper.toTableName(table); ArrayList presentColumns = getColumnNames(sqLiteDatabase, tableName); ArrayList alterCommands = new ArrayList<>(); for (Field column : fields) { - String columnName = NamingHelper.toSQLName(column); + String columnName = NamingHelper.toColumnName(column); String columnType = QueryBuilder.getColumnType(column.getType()); if (column.isAnnotationPresent(Column.class)) { @@ -179,7 +179,7 @@ private void addColumns(Class table, SQLiteDatabase sqLiteDatabase) { protected String createTableSQL(Class table) { Log.i(SUGAR, "Create table if not exists"); List fields = ReflectionUtil.getTableFields(table); - String tableName = NamingHelper.toSQLName(table); + String tableName = NamingHelper.toTableName(table); if(KeyWordUtil.isKeyword(tableName)) { Log.i(SUGAR,"ERROR, SQLITE RESERVED WORD USED IN " + tableName); @@ -189,7 +189,7 @@ protected String createTableSQL(Class table) { sb.append(tableName).append(" ( ID INTEGER PRIMARY KEY AUTOINCREMENT "); for (Field column : fields) { - String columnName = NamingHelper.toSQLName(column); + String columnName = NamingHelper.toColumnName(column); String columnType = QueryBuilder.getColumnType(column.getType()); if (columnType != null) { diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 139f1aa3..a6d22f04 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -41,11 +41,11 @@ public static int deleteAll(Class type) { } public static int deleteAll(Class type, String whereClause, String... whereArgs) { - return getSugarDataBase().delete(NamingHelper.toSQLName(type), whereClause, whereArgs); + return getSugarDataBase().delete(NamingHelper.toTableName(type), whereClause, whereArgs); } public static Cursor getCursor(Class type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) { - Cursor raw = getSugarDataBase().query(NamingHelper.toSQLName(type), null, whereClause, whereArgs, + Cursor raw = getSugarDataBase().query(NamingHelper.toTableName(type), null, whereClause, whereArgs, groupBy, null, orderBy, limit); return new SugarCursor(raw); } @@ -149,7 +149,7 @@ public static List findById(Class type, String[] ids) { public static T first(Classtype) { List list = findWithQuery(type, - "SELECT * FROM " + NamingHelper.toSQLName(type) + " ORDER BY ID ASC LIMIT 1"); + "SELECT * FROM " + NamingHelper.toTableName(type) + " ORDER BY ID ASC LIMIT 1"); if (list.isEmpty()) { return null; } @@ -158,7 +158,7 @@ public static T first(Classtype) { public static T last(Classtype) { List list = findWithQuery(type, - "SELECT * FROM " + NamingHelper.toSQLName(type) + " ORDER BY ID DESC LIMIT 1"); + "SELECT * FROM " + NamingHelper.toTableName(type) + " ORDER BY ID DESC LIMIT 1"); if (list.isEmpty()) { return null; } @@ -179,7 +179,7 @@ public static Iterator findWithQueryAsIterator(Class type, String quer } public static Iterator findAsIterator(Class type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) { - Cursor cursor = getSugarDataBase().query(NamingHelper.toSQLName(type), null, whereClause, whereArgs, + Cursor cursor = getSugarDataBase().query(NamingHelper.toTableName(type), null, whereClause, whereArgs, groupBy, null, orderBy, limit); return new CursorIterator<>(type, cursor); } @@ -203,7 +203,7 @@ public static List find(Class type, String whereClause, String[] where String args[]; args = (whereArgs == null) ? null : replaceArgs(whereArgs); - Cursor cursor = getSugarDataBase().query(NamingHelper.toSQLName(type), null, whereClause, args, + Cursor cursor = getSugarDataBase().query(NamingHelper.toTableName(type), null, whereClause, args, groupBy, null, orderBy, limit); return getEntitiesFromCursor(cursor, type); @@ -240,7 +240,7 @@ public static long count(Class type, String whereClause, String[] whereAr String filter = (!TextUtils.isEmpty(whereClause)) ? " where " + whereClause : ""; SQLiteStatement sqliteStatement; try { - sqliteStatement = getSugarDataBase().compileStatement("SELECT count(*) FROM " + NamingHelper.toSQLName(type) + filter); + sqliteStatement = getSugarDataBase().compileStatement("SELECT count(*) FROM " + NamingHelper.toTableName(type) + filter); } catch (SQLiteException e) { e.printStackTrace(); return result; @@ -282,7 +282,7 @@ static long save(SQLiteDatabase db, Object object) { values.put("id", entitiesMap.get(object)); } - long id = db.insertWithOnConflict(NamingHelper.toSQLName(object.getClass()), null, values, + long id = db.insertWithOnConflict(NamingHelper.toTableName(object.getClass()), null, values, SQLiteDatabase.CONFLICT_REPLACE); if (object.getClass().isAnnotationPresent(Table.class)) { @@ -323,7 +323,7 @@ static long update(SQLiteDatabase db, Object object) { if(column.isAnnotationPresent(Unique.class)) { try { column.setAccessible(true); - String columnName = NamingHelper.toSQLName(column); + String columnName = NamingHelper.toColumnName(column); Object columnValue = column.get(object); whereClause.append(columnName).append(" = ?"); @@ -340,7 +340,7 @@ static long update(SQLiteDatabase db, Object object) { String[] whereArgsArray = whereArgs.toArray(new String[whereArgs.size()]); // Get SugarRecord based on Unique values - long rowsEffected = db.update(NamingHelper.toSQLName(object.getClass()), values, whereClause.toString(), whereArgsArray); + long rowsEffected = db.update(NamingHelper.toTableName(object.getClass()), values, whereClause.toString(), whereArgsArray); if (rowsEffected == 0) { return save(db, object); @@ -366,7 +366,7 @@ private static void inflate(Cursor cursor, Object object, Map enti Class fieldType = field.getType(); if (isSugarEntity(fieldType)) { try { - long id = cursor.getLong(cursor.getColumnIndex(NamingHelper.toSQLName(field))); + long id = cursor.getLong(cursor.getColumnIndex(NamingHelper.toColumnName(field))); field.set(object, (id > 0) ? findById(fieldType, id) : null); } catch (IllegalAccessException e) { e.printStackTrace(); @@ -382,7 +382,7 @@ public boolean delete() { Class type = getClass(); if (id != null && id > 0L) { Log.i(SUGAR, type.getSimpleName() + " deleted : " + id); - return getSugarDataBase().delete(NamingHelper.toSQLName(type), "Id=?", new String[]{id.toString()}) == 1; + return getSugarDataBase().delete(NamingHelper.toTableName(type), "Id=?", new String[]{id.toString()}) == 1; } else { Log.i(SUGAR, "Cannot delete object: " + type.getSimpleName() + " - object has not been saved"); return false; @@ -397,7 +397,7 @@ public static boolean delete(Object object) { field.setAccessible(true); Long id = (Long) field.get(object); if (id != null && id > 0L) { - boolean deleted = getSugarDataBase().delete(NamingHelper.toSQLName(type), "Id=?", new String[]{id.toString()}) == 1; + boolean deleted = getSugarDataBase().delete(NamingHelper.toTableName(type), "Id=?", new String[]{id.toString()}) == 1; Log.i(SUGAR, type.getSimpleName() + " deleted : " + id); return deleted; } else { diff --git a/library/src/main/java/com/orm/helper/NamingHelper.java b/library/src/main/java/com/orm/helper/NamingHelper.java index 7a107b5c..0ae3128b 100644 --- a/library/src/main/java/com/orm/helper/NamingHelper.java +++ b/library/src/main/java/com/orm/helper/NamingHelper.java @@ -60,7 +60,7 @@ public static String toSQLNameDefault(String camelCased) { * returned. Else, the Field's {@link java.lang.reflect.Field#getName()} will be * converted from CamelCase to UNDER_SCORE notation */ - public static String toSQLName(Field field) { + public static String toColumnName(Field field) { if (field.isAnnotationPresent(Column.class)) { Column annotation = field.getAnnotation(Column.class); return annotation.name(); @@ -77,7 +77,7 @@ public static String toSQLName(Field field) { * {@link com.orm.annotation.Table#name()} will be returned. Else, the class' simple name will * be converted from CamelCase to UNDER_SCORE notation */ - public static String toSQLName(Class table) { + public static String toTableName(Class table) { if (table.isAnnotationPresent(Table.class)) { Table annotation = table.getAnnotation(Table.class); if ("".equals(annotation.name())) { diff --git a/library/src/main/java/com/orm/query/Select.java b/library/src/main/java/com/orm/query/Select.java index f87e7be9..cc57199d 100644 --- a/library/src/main/java/com/orm/query/Select.java +++ b/library/src/main/java/com/orm/query/Select.java @@ -157,7 +157,7 @@ public T first() { String toSql() { StringBuilder sql = new StringBuilder(); - sql.append(SELECT_FROM).append(NamingHelper.toSQLName(this.record)).append(SPACE); + sql.append(SELECT_FROM).append(NamingHelper.toTableName(this.record)).append(SPACE); if (!whereClause.isEmpty()) { sql.append(WHERE).append(whereClause).append(SPACE); diff --git a/library/src/main/java/com/orm/util/NumberComparator.java b/library/src/main/java/com/orm/util/NumberComparator.java index 08a9f0b4..a6a08053 100644 --- a/library/src/main/java/com/orm/util/NumberComparator.java +++ b/library/src/main/java/com/orm/util/NumberComparator.java @@ -2,9 +2,12 @@ import java.util.Comparator; +import static java.lang.Character.isDigit; +import static java.lang.Character.isSpaceChar; + public class NumberComparator implements Comparator { - private static char charAt(String s, int i) { + protected static char charAt(String s, int i) { if (i >= s.length()) { return '\000'; } @@ -12,7 +15,7 @@ private static char charAt(String s, int i) { return s.charAt(i); } - private int compareRight(String a, String b) { + protected int compareRight(String a, String b) { int bias = 0; int ia = 0; int ib = 0; @@ -20,13 +23,13 @@ private int compareRight(String a, String b) { char ca = charAt(a, ia); char cb = charAt(b, ib); - if ((!Character.isDigit(ca)) && (!Character.isDigit(cb))) { + if ((!isDigit(ca)) && (!isDigit(cb))) { return bias; } - if (!Character.isDigit(ca)) { + if (!isDigit(ca)) { return -1; } - if (!Character.isDigit(cb)) { + if (!isDigit(cb)) { return 1; } if (ca < cb) { @@ -57,7 +60,7 @@ public int compare(Object o1, Object o2) { char ca = charAt(a, ia); char cb = charAt(b, ib); - while ((Character.isSpaceChar(ca)) || (ca == '0')) { + while ((isSpaceChar(ca)) || (ca == '0')) { if (ca == '0') { nza++; } else { @@ -67,7 +70,7 @@ public int compare(Object o1, Object o2) { ca = charAt(a, ++ia); } - while ((Character.isSpaceChar(cb)) || (cb == '0')) { + while ((isSpaceChar(cb)) || (cb == '0')) { if (cb == '0') { nzb++; } else { @@ -77,7 +80,7 @@ public int compare(Object o1, Object o2) { cb = charAt(b, ++ib); } int result; - if ((Character.isDigit(ca)) && (Character.isDigit(cb)) && + if ((isDigit(ca)) && (isDigit(cb)) && ((result = compareRight(a.substring(ia), b.substring(ib))) != 0)) { return result; } diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index 6fd1067f..05502bf6 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -1,7 +1,6 @@ package com.orm.util; import android.content.ContentValues; -import android.content.Context; import android.content.pm.PackageManager; import android.database.Cursor; import android.util.Log; @@ -29,7 +28,10 @@ import java.util.List; import java.util.Map; -public class ReflectionUtil { +public final class ReflectionUtil { + + //Prevent instantiation.. + private ReflectionUtil() { } public static List getTableFields(Class table) { List fieldList = SugarConfig.getFields(table); @@ -66,7 +68,7 @@ public static void addFieldValueToColumn(ContentValues values, Field column, Obj column.setAccessible(true); Class columnType = column.getType(); try { - String columnName = NamingHelper.toSQLName(column); + String columnName = NamingHelper.toColumnName(column); Object columnValue = column.get(object); if (columnType.isAnnotationPresent(Table.class)) { @@ -150,7 +152,7 @@ public static void setFieldValueFromCursor(Cursor cursor, Field field, Object ob field.setAccessible(true); try { Class fieldType = field.getType(); - String colName = NamingHelper.toSQLName(field); + String colName = NamingHelper.toColumnName(field); int columnIndex = cursor.getColumnIndex(colName); @@ -342,7 +344,7 @@ private static void populateFiles(File path, List fileNames, String pare } } - private static String getSourcePath(Context context) throws PackageManager.NameNotFoundException { - return context.getPackageManager().getApplicationInfo(context.getPackageName(), 0).sourceDir; + private static String getSourcePath() throws PackageManager.NameNotFoundException { + return ContextUtil.getPackageManager().getApplicationInfo(ContextUtil.getPackageName(), 0).sourceDir; } } diff --git a/library/src/main/java/com/orm/util/SugarConfig.java b/library/src/main/java/com/orm/util/SugarConfig.java index 9328c82f..46ebd8da 100644 --- a/library/src/main/java/com/orm/util/SugarConfig.java +++ b/library/src/main/java/com/orm/util/SugarConfig.java @@ -27,5 +27,4 @@ public static void clearCache() { fields.clear(); fields = new HashMap<>(); } - } diff --git a/library/src/test/java/com/orm/NamingHelperTest.java b/library/src/test/java/com/orm/NamingHelperTest.java index f4873c81..550c9e31 100644 --- a/library/src/test/java/com/orm/NamingHelperTest.java +++ b/library/src/test/java/com/orm/NamingHelperTest.java @@ -23,7 +23,7 @@ public void testToSQLNameFromField() { List columnList = new ArrayList<>(); for(Field field: fieldList) { - columnList.add(NamingHelper.toSQLName(field)); + columnList.add(NamingHelper.toColumnName(field)); } boolean isIdInList = inList(columnList, "ID"); @@ -46,7 +46,7 @@ private boolean inList(List list, String searchValue) { @Test public void testToSQLNameFromClass() { - assertEquals("TEST_RECORD", NamingHelper.toSQLName(TestRecord.class)); + assertEquals("TEST_RECORD", NamingHelper.toTableName(TestRecord.class)); } @Test diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index 6e883caa..289f132c 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -30,7 +30,7 @@ public void testEmptyTableCreation() throws Exception { SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); String createSQL = schemaGenerator.createTableSQL(EmptyModel.class); assertEquals( - "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(EmptyModel.class) + + "CREATE TABLE IF NOT EXISTS " + NamingHelper.toTableName(EmptyModel.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT ) ", createSQL); } @@ -40,21 +40,21 @@ public void testSimpleColumnTableCreation() throws Exception { SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); String createSQL = schemaGenerator.createTableSQL(StringFieldExtendedModel.class); assertEquals( - "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(StringFieldExtendedModel.class) + + "CREATE TABLE IF NOT EXISTS " + NamingHelper.toTableName(StringFieldExtendedModel.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + "STRING TEXT ) ", createSQL); String createSQL2 = schemaGenerator.createTableSQL(StringFieldAnnotatedModel.class); - assertEquals("CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(StringFieldAnnotatedModel.class) + + assertEquals("CREATE TABLE IF NOT EXISTS " + NamingHelper.toTableName(StringFieldAnnotatedModel.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + "STRING TEXT ) ", createSQL2); String createSQL3 = schemaGenerator.createTableSQL(StringFieldExtendedModelAnnotatedColumn.class); - assertEquals("CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(StringFieldExtendedModelAnnotatedColumn.class) + + assertEquals("CREATE TABLE IF NOT EXISTS " + NamingHelper.toTableName(StringFieldExtendedModelAnnotatedColumn.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + "anyName TEXT ) ", createSQL3); @@ -65,7 +65,7 @@ public void testUniqueTableCreation() { SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); String createSQL = schemaGenerator.createTableSQL(IntUniqueModel.class); assertEquals( - "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(IntUniqueModel.class) + + "CREATE TABLE IF NOT EXISTS " + NamingHelper.toTableName(IntUniqueModel.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + "VALUE INTEGER UNIQUE ) ", createSQL); @@ -76,7 +76,7 @@ public void testMultiColumnUniqueTableCreation() { SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); String createSQL = schemaGenerator.createTableSQL(MultiColumnUniqueModel.class); assertEquals( - "CREATE TABLE IF NOT EXISTS " + NamingHelper.toSQLName(MultiColumnUniqueModel.class) + + "CREATE TABLE IF NOT EXISTS " + NamingHelper.toTableName(MultiColumnUniqueModel.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + "A INTEGER, B INTEGER, " + "UNIQUE(A, B) ON CONFLICT REPLACE ) ", diff --git a/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java b/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java index f223a49c..837f4403 100644 --- a/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java +++ b/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java @@ -204,7 +204,7 @@ public void findWithQueryTest() throws Exception { } List models = SugarRecord.findWithQuery(SimpleAnnotatedModel.class, "Select * from " + - NamingHelper.toSQLName(SimpleAnnotatedModel.class) + + NamingHelper.toTableName(SimpleAnnotatedModel.class) + " where id >= ? ", "50"); for (SimpleAnnotatedModel model : models) { assertEquals(75L, model.getId(), 25L); @@ -323,7 +323,7 @@ public void findWithQueryAsIteratorTest() throws Exception { Iterator cursor = SugarRecord.findWithQueryAsIterator(SimpleAnnotatedModel.class, "Select * from " + - NamingHelper.toSQLName(SimpleAnnotatedModel.class) + + NamingHelper.toTableName(SimpleAnnotatedModel.class) + " where id >= ? ", "50"); for (int i = 50; i <= 100; i++) { assertTrue(cursor.hasNext()); diff --git a/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java b/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java index 9ac1d696..592a1511 100644 --- a/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java +++ b/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java @@ -213,7 +213,7 @@ public void findWithQueryTest() throws Exception { } List models = SugarRecord.findWithQuery(SimpleExtendedModel.class, "Select * from " + - NamingHelper.toSQLName(SimpleExtendedModel.class) + + NamingHelper.toTableName(SimpleExtendedModel.class) + " where id >= ? ", "50"); for (SimpleExtendedModel model : models) { assertEquals(75, model.getId(), 25L); @@ -332,7 +332,7 @@ public void findWithQueryAsIteratorTest() throws Exception { Iterator cursor = SugarRecord.findWithQueryAsIterator(SimpleExtendedModel.class, "Select * from " + - NamingHelper.toSQLName(SimpleExtendedModel.class) + + NamingHelper.toTableName(SimpleExtendedModel.class) + " where id >= ? ", "50"); SugarContext.init(RuntimeEnvironment.application); for (int i = 50; i <= 100; i++) { 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..2dd8458b --- /dev/null +++ b/library/src/test/java/com/orm/util/NumberComparatorTest.java @@ -0,0 +1,54 @@ +package com.orm.util; + +import com.orm.RobolectricGradleTestRunner; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +import static org.junit.Assert.assertEquals; + +/** + * @author jonatan.salas + */ +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18) +public class NumberComparatorTest { + private NumberComparator comparator; + + @Before + public void setUp() { + comparator = new NumberComparator(); + } + + @Test + public void testNumberComparatorWithoutNumbers() { + int result = comparator.compare("hola", "hola"); + assertEquals(0, result); + } + + @Test + public void testNumberComparatorWithNumbers() { + int result = comparator.compare("1", "2"); + assertEquals(-1, result); + } + + @Test + public void testComparatorWithNumbers() { + int result = comparator.compare("4", "2"); + assertEquals(1, result); + } + + @Test + public void testCompareRight() { + int result = comparator.compareRight("hola", "hola"); + assertEquals(0, result); + } + + @Test + public void testCharAt() { + Character c = NumberComparator.charAt("Hola", 0); + assertEquals("H", c.toString()); + } +} diff --git a/library/src/test/java/com/orm/util/ReflectionUtilTest.java b/library/src/test/java/com/orm/util/ReflectionUtilTest.java new file mode 100644 index 00000000..8efba281 --- /dev/null +++ b/library/src/test/java/com/orm/util/ReflectionUtilTest.java @@ -0,0 +1,35 @@ +package com.orm.util; + +import com.orm.RobolectricGradleTestRunner; +import com.orm.models.TestRecord; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author jonatan.salas + */ +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18) +public class ReflectionUtilTest { + + @Test + public void testGetTableFields() { + List fieldList = ReflectionUtil.getTableFields(TestRecord.class); + List strings = new ArrayList<>(); + + for (Field field: fieldList) { + strings.add(field.getName()); + } + + assertEquals(true, strings.contains("id")); + assertEquals(true, strings.contains("name")); + } +} 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..c694dee0 --- /dev/null +++ b/library/src/test/java/com/orm/util/SugarConfigTest.java @@ -0,0 +1,15 @@ +package com.orm.util; + +import com.orm.models.TestRecord; + +import org.junit.Test; + +import java.lang.reflect.Field; +import java.util.List; + +/** + * @author jonatan.salas + */ +public class SugarConfigTest { + +} From 1a600cea601a5b09f7ecba0eaa5113ede759d4ce Mon Sep 17 00:00:00 2001 From: jonatan Date: Fri, 8 Apr 2016 19:37:16 -0300 Subject: [PATCH 082/139] Solved bug with SugarTransactionHelper Modified TestRecord Added SugarTransactionHelperTest --- .../orm/helper/SugarTransactionHelper.java | 4 - .../test/java/com/orm/models/TestRecord.java | 9 +++ .../orm/util/SugarTransactionHelperTest.java | 76 +++++++++++++++++++ 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 library/src/test/java/com/orm/util/SugarTransactionHelperTest.java diff --git a/library/src/main/java/com/orm/helper/SugarTransactionHelper.java b/library/src/main/java/com/orm/helper/SugarTransactionHelper.java index 06fd8731..82077a4a 100644 --- a/library/src/main/java/com/orm/helper/SugarTransactionHelper.java +++ b/library/src/main/java/com/orm/helper/SugarTransactionHelper.java @@ -26,10 +26,6 @@ public static void doInTransaction(Callback callback) { Log.d(LOG_TAG, "Could execute callback within transaction", e); } finally { database.endTransaction(); - - if (database.isOpen()) { - database.close(); - } } } diff --git a/library/src/test/java/com/orm/models/TestRecord.java b/library/src/test/java/com/orm/models/TestRecord.java index f537f501..72ab8d24 100644 --- a/library/src/test/java/com/orm/models/TestRecord.java +++ b/library/src/test/java/com/orm/models/TestRecord.java @@ -9,4 +9,13 @@ public class TestRecord extends SugarRecord { public TestRecord() { super(); } + + public TestRecord setName(String name) { + this.name = name; + return this; + } + + public String getName() { + return name; + } } diff --git a/library/src/test/java/com/orm/util/SugarTransactionHelperTest.java b/library/src/test/java/com/orm/util/SugarTransactionHelperTest.java new file mode 100644 index 00000000..410be646 --- /dev/null +++ b/library/src/test/java/com/orm/util/SugarTransactionHelperTest.java @@ -0,0 +1,76 @@ +package com.orm.util; + +import com.orm.ClientApp; +import com.orm.RobolectricGradleTestRunner; +import com.orm.SugarContext; +import com.orm.helper.SugarTransactionHelper; +import com.orm.models.TestRecord; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.ArrayList; +import java.util.List; + +import static junit.framework.Assert.assertEquals; + +/** + * @author jonatan.salas + */ +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18, application = ClientApp.class, manifest = Config.NONE) +public class SugarTransactionHelperTest { + private List recordList = new ArrayList<>(); + private TestRecord record1 = new TestRecord(); + private TestRecord record2 = new TestRecord(); + private TestRecord record3 = new TestRecord(); + + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + + record1.setId(1L); + record1.setName("lala"); + + record2.setId(2L); + record2.setName("fefe"); + + record3.setId(3L); + record3.setName("jaja"); + + recordList.add(record1); + recordList.add(record2); + recordList.add(record3); + } + + @Test + public void testDoInTransaction() { + SugarTransactionHelper.doInTransaction(new SugarTransactionHelper.Callback() { + @Override + public void manipulateInTransaction() { + for (TestRecord record: recordList) { + TestRecord.save(record); + } + } + }); + + final List results = TestRecord.listAll(TestRecord.class); + + assertEquals(true, inList(results, record1)); + assertEquals(true, inList(results, record2)); + assertEquals(true, inList(results, record3)); + } + + private boolean inList(List list, TestRecord testRecord) { + for (TestRecord record: list) { + if (record.getId().equals(testRecord.getId()) && + record.getName().equals(testRecord.getName())) { + return true; + } + } + return false; + } +} From b6fdb8e18b842c63a5e0ce3feec813a33a48bb76 Mon Sep 17 00:00:00 2001 From: jonatan Date: Fri, 8 Apr 2016 20:11:22 -0300 Subject: [PATCH 083/139] Added SugarDbTest --- .../src/test/java/com/orm/SugarDbTest.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 library/src/test/java/com/orm/SugarDbTest.java diff --git a/library/src/test/java/com/orm/SugarDbTest.java b/library/src/test/java/com/orm/SugarDbTest.java new file mode 100644 index 00000000..482410b5 --- /dev/null +++ b/library/src/test/java/com/orm/SugarDbTest.java @@ -0,0 +1,43 @@ +package com.orm; + +import android.database.sqlite.SQLiteDatabase; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +/** + * @author jonatan.salas + */ +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18, application = ClientApp.class, manifest = Config.NONE) +public class SugarDbTest { + private final SugarDb sugarDb = SugarDb.getInstance(); + + @Before + public void setUp() { + SugarContext.init(RuntimeEnvironment.application); + } + + @Test + //TODO check this better! + public void testGetReadableDatabase() { + final SQLiteDatabase db = sugarDb.getReadableDatabase(); + Assert.assertEquals(false, db.isReadOnly()); + } + + @Test + public void testGetWritableDatabase() { + final SQLiteDatabase db = sugarDb.getWritableDatabase(); + Assert.assertEquals(false, db.isReadOnly()); + } + + @Test + public void testGetDB() { + final SQLiteDatabase db = sugarDb.getDB(); + Assert.assertEquals(false, db.isReadOnly()); + } +} From e4a29d122fc8403e21319e5b7434c0bbab6fba34 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 9 Apr 2016 13:42:34 -0300 Subject: [PATCH 084/139] Deleted all test and classes from sample app Deleted unused classes in library test Generated helper package to add all helper classes test Generated app package to put ClientApp class Refactored class models in test to model --- .../models/BigDecimalFieldAnnotatedModel.java | 25 -- .../models/BigDecimalFieldExtendedModel.java | 19 - .../models/BooleanFieldAnnotatedModel.java | 28 -- .../models/BooleanFieldExtendedModel.java | 26 -- .../models/ByteArrayAnnotatedModel.java | 23 -- .../models/ByteArrayExtendedModel.java | 17 - .../models/DoubleFieldAnnotatedModel.java | 28 -- .../models/DoubleFieldExtendedModel.java | 26 -- .../models/EnumFieldAnnotatedModel.java | 52 --- .../models/EnumFieldExtendedModel.java | 46 --- .../models/FloatFieldAnnotatedModel.java | 28 -- .../models/FloatFieldExtendedModel.java | 26 -- .../models/IncompleteAnnotatedModel.java | 9 - .../models/IntegerFieldAnnotatedModel.java | 28 -- .../models/IntegerFieldExtendedModel.java | 26 -- .../models/LongFieldAnnotatedModel.java | 28 -- .../models/LongFieldExtendedModel.java | 26 -- .../example/models/NestedAnnotatedModel.java | 23 -- .../example/models/NestedExtendedModel.java | 18 - .../example/models/NestedMixedAAModel.java | 18 - .../example/models/NestedMixedABModel.java | 18 - .../example/models/NestedMixedBAModel.java | 24 -- .../example/models/NestedMixedBBModel.java | 24 -- .../java/com/example/models/NoSugarModel.java | 7 - .../models/RelationshipAnnotatedModel.java | 23 -- .../models/RelationshipExtendedModel.java | 18 - .../models/RelationshipMixedAModel.java | 18 - .../models/RelationshipMixedBModel.java | 24 -- .../models/ShortFieldAnnotatedModel.java | 28 -- .../models/ShortFieldExtendedModel.java | 26 -- .../example/models/SimpleAnnotatedModel.java | 15 - .../example/models/SimpleExtendedModel.java | 8 - .../java/com/example/models/SimpleModel.java | 33 -- .../models/StringFieldAnnotatedModel.java | 23 -- .../models/StringFieldAnnotatedNoIdModel.java | 22 -- .../models/StringFieldExtendedModel.java | 21 - .../sugartest/BigDecimalFieldTests.java | 53 --- .../example/sugartest/BooleanFieldTests.java | 76 ---- .../sugartest/ByteArrayFieldTests.java | 54 --- .../com/example/sugartest/CursorTests.java | 72 ---- .../example/sugartest/DoubleFieldTests.java | 76 ---- .../com/example/sugartest/EnumFieldTests.java | 81 ---- .../example/sugartest/FirstAndLastTests.java | 158 -------- .../example/sugartest/FloatFieldTests.java | 76 ---- .../IncompleteAnnotatedModelTests.java | 28 -- .../example/sugartest/IntegerFieldTests.java | 76 ---- .../sugartest/ListAllOrderByTests.java | 53 --- .../com/example/sugartest/LongFieldTests.java | 76 ---- .../example/sugartest/MultipleSaveTests.java | 93 ----- .../sugartest/NestedAnnotatedTests.java | 131 ------- .../sugartest/NestedExtendedTests.java | 131 ------- .../example/sugartest/NestedMixedAATests.java | 131 ------- .../example/sugartest/NestedMixedABTests.java | 131 ------- .../example/sugartest/NestedMixedBATests.java | 131 ------- .../example/sugartest/NestedMixedBBTests.java | 131 ------- .../example/sugartest/NoSugarModelTests.java | 28 -- .../sugartest/RelationshipAnnotatedTests.java | 108 ------ .../sugartest/RelationshipExtendedTests.java | 108 ------ .../sugartest/RelationshipMixedATests.java | 108 ------ .../sugartest/RelationshipMixedBTests.java | 108 ------ .../RobolectricGradleTestRunner.java | 25 -- .../example/sugartest/ShortFieldTests.java | 76 ---- .../sugartest/SimpleAnnotatedModelTests.java | 356 ----------------- .../sugartest/SimpleExtendedModelTests.java | 363 ------------------ .../example/sugartest/StringFieldTests.java | 48 --- library/src/test/java/com/orm/ClientApp.java | 18 - .../com/orm/RobolectricGradleTestRunner.java | 25 -- .../java/com/orm/SchemaGeneratorTest.java | 26 +- .../com/orm/SugarDbConfigurationTest.java | 2 +- .../src/test/java/com/orm/SugarDbTest.java | 24 +- .../src/test/java/com/orm/app}/ClientApp.java | 2 +- .../{util => helper}/ManifestHelperTest.java | 29 +- .../orm/{ => helper}/NamingHelperTest.java | 17 +- .../SugarTransactionHelperTest.java | 13 +- .../BigDecimalFieldAnnotatedModel.java | 2 +- .../BigDecimalFieldExtendedModel.java | 2 +- .../BooleanFieldAnnotatedModel.java | 2 +- .../BooleanFieldExtendedModel.java | 2 +- .../ByteArrayAnnotatedModel.java | 2 +- .../ByteArrayExtendedModel.java | 2 +- .../DoubleFieldAnnotatedModel.java | 2 +- .../DoubleFieldExtendedModel.java | 2 +- .../com/orm/{models => model}/EmptyModel.java | 2 +- .../EnumFieldAnnotatedModel.java | 2 +- .../EnumFieldExtendedModel.java | 2 +- .../FloatFieldAnnotatedModel.java | 2 +- .../FloatFieldExtendedModel.java | 2 +- .../IncompleteAnnotatedModel.java | 2 +- .../orm/{models => model}/IntUniqueModel.java | 2 +- .../IntegerFieldAnnotatedModel.java | 2 +- .../IntegerFieldExtendedModel.java | 2 +- .../LongFieldAnnotatedModel.java | 2 +- .../LongFieldExtendedModel.java | 2 +- .../MultiColumnUniqueModel.java | 2 +- .../NestedAnnotatedModel.java | 2 +- .../NestedExtendedModel.java | 2 +- .../{models => model}/NestedMixedAAModel.java | 2 +- .../{models => model}/NestedMixedABModel.java | 2 +- .../{models => model}/NestedMixedBAModel.java | 2 +- .../{models => model}/NestedMixedBBModel.java | 2 +- .../orm/{models => model}/NoSugarModel.java | 2 +- .../RelationshipAnnotatedModel.java | 2 +- .../RelationshipExtendedModel.java | 2 +- .../RelationshipMixedAModel.java | 2 +- .../RelationshipMixedBModel.java | 2 +- .../ShortFieldAnnotatedModel.java | 2 +- .../ShortFieldExtendedModel.java | 2 +- .../SimpleAnnotatedModel.java | 2 +- .../SimpleExtendedModel.java | 2 +- .../orm/{models => model}/SimpleModel.java | 2 +- .../StringFieldAnnotatedModel.java | 2 +- .../StringFieldAnnotatedNoIdModel.java | 2 +- .../StringFieldExtendedModel.java | 2 +- ...ringFieldExtendedModelAnnotatedColumn.java | 2 +- .../com/orm/{models => model}/TestRecord.java | 2 +- .../java/com/orm/query/QueryBuilderTests.java | 2 +- .../test/java/com/orm/query/SelectTest.java | 12 +- .../com/orm/record/BigDecimalFieldTests.java | 46 +-- .../com/orm/record/BooleanFieldTests.java | 21 +- .../com/orm/record/ByteArrayFieldTests.java | 22 +- .../test/java/com/orm/record/CursorTests.java | 18 +- .../java/com/orm/record/DoubleFieldTests.java | 21 +- .../java/com/orm/record/EnumFieldTests.java | 21 +- .../com/orm/record/FirstAndLastTests.java | 19 +- .../java/com/orm/record/FloatFieldTests.java | 21 +- .../record/IncompleteAnnotatedModelTests.java | 21 +- .../com/orm/record/IntegerFieldTests.java | 19 +- .../com/orm/record/ListAllOrderByTests.java | 20 +- .../java/com/orm/record/LongFieldTests.java | 19 +- .../com/orm/record/MultipleSaveTests.java | 21 +- .../com/orm/record/NestedAnnotatedTests.java | 23 +- .../com/orm/record/NestedExtendedTests.java | 23 +- .../com/orm/record/NestedMixedAATests.java | 23 +- .../com/orm/record/NestedMixedABTests.java | 21 +- .../com/orm/record/NestedMixedBATests.java | 23 +- .../com/orm/record/NestedMixedBBTests.java | 21 +- .../com/orm/record/NoSugarModelTests.java | 19 +- .../record/RelationshipAnnotatedTests.java | 21 +- .../orm/record/RelationshipExtendedTests.java | 19 +- .../orm/record/RelationshipMixedATests.java | 141 +++++++ .../orm/record/RelationshipMixedBTests.java | 20 +- .../java/com/orm/record/ShortFieldTests.java | 21 +- .../orm/record/SimpleAnnotatedModelTests.java | 19 +- .../orm/record/SimpleExtendedModelTests.java | 20 +- .../java/com/orm/record/StringFieldTests.java | 22 +- .../java/com/orm/util/ContextUtilTest.java | 16 +- .../java/com/orm/util/KeyWordUtilTest.java | 2 +- .../com/orm/util/MigrationFileParserTest.java | 2 +- .../com/orm/util/NumberComparatorTest.java | 8 +- .../java/com/orm/util/ReflectionUtilTest.java | 11 +- .../java/com/orm/util/SugarConfigTest.java | 7 - 151 files changed, 456 insertions(+), 4505 deletions(-) delete mode 100644 example/src/main/java/com/example/models/BigDecimalFieldAnnotatedModel.java delete mode 100644 example/src/main/java/com/example/models/BigDecimalFieldExtendedModel.java delete mode 100644 example/src/main/java/com/example/models/BooleanFieldAnnotatedModel.java delete mode 100644 example/src/main/java/com/example/models/BooleanFieldExtendedModel.java delete mode 100644 example/src/main/java/com/example/models/ByteArrayAnnotatedModel.java delete mode 100644 example/src/main/java/com/example/models/ByteArrayExtendedModel.java delete mode 100644 example/src/main/java/com/example/models/DoubleFieldAnnotatedModel.java delete mode 100644 example/src/main/java/com/example/models/DoubleFieldExtendedModel.java delete mode 100644 example/src/main/java/com/example/models/EnumFieldAnnotatedModel.java delete mode 100644 example/src/main/java/com/example/models/EnumFieldExtendedModel.java delete mode 100644 example/src/main/java/com/example/models/FloatFieldAnnotatedModel.java delete mode 100644 example/src/main/java/com/example/models/FloatFieldExtendedModel.java delete mode 100644 example/src/main/java/com/example/models/IncompleteAnnotatedModel.java delete mode 100644 example/src/main/java/com/example/models/IntegerFieldAnnotatedModel.java delete mode 100644 example/src/main/java/com/example/models/IntegerFieldExtendedModel.java delete mode 100644 example/src/main/java/com/example/models/LongFieldAnnotatedModel.java delete mode 100644 example/src/main/java/com/example/models/LongFieldExtendedModel.java delete mode 100644 example/src/main/java/com/example/models/NestedAnnotatedModel.java delete mode 100644 example/src/main/java/com/example/models/NestedExtendedModel.java delete mode 100644 example/src/main/java/com/example/models/NestedMixedAAModel.java delete mode 100644 example/src/main/java/com/example/models/NestedMixedABModel.java delete mode 100644 example/src/main/java/com/example/models/NestedMixedBAModel.java delete mode 100644 example/src/main/java/com/example/models/NestedMixedBBModel.java delete mode 100644 example/src/main/java/com/example/models/NoSugarModel.java delete mode 100644 example/src/main/java/com/example/models/RelationshipAnnotatedModel.java delete mode 100644 example/src/main/java/com/example/models/RelationshipExtendedModel.java delete mode 100644 example/src/main/java/com/example/models/RelationshipMixedAModel.java delete mode 100644 example/src/main/java/com/example/models/RelationshipMixedBModel.java delete mode 100644 example/src/main/java/com/example/models/ShortFieldAnnotatedModel.java delete mode 100644 example/src/main/java/com/example/models/ShortFieldExtendedModel.java delete mode 100644 example/src/main/java/com/example/models/SimpleAnnotatedModel.java delete mode 100644 example/src/main/java/com/example/models/SimpleExtendedModel.java delete mode 100644 example/src/main/java/com/example/models/SimpleModel.java delete mode 100644 example/src/main/java/com/example/models/StringFieldAnnotatedModel.java delete mode 100644 example/src/main/java/com/example/models/StringFieldAnnotatedNoIdModel.java delete mode 100644 example/src/main/java/com/example/models/StringFieldExtendedModel.java delete mode 100644 example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java delete mode 100644 example/src/test/java/com/example/sugartest/BooleanFieldTests.java delete mode 100644 example/src/test/java/com/example/sugartest/ByteArrayFieldTests.java delete mode 100644 example/src/test/java/com/example/sugartest/CursorTests.java delete mode 100644 example/src/test/java/com/example/sugartest/DoubleFieldTests.java delete mode 100644 example/src/test/java/com/example/sugartest/EnumFieldTests.java delete mode 100644 example/src/test/java/com/example/sugartest/FirstAndLastTests.java delete mode 100644 example/src/test/java/com/example/sugartest/FloatFieldTests.java delete mode 100644 example/src/test/java/com/example/sugartest/IncompleteAnnotatedModelTests.java delete mode 100644 example/src/test/java/com/example/sugartest/IntegerFieldTests.java delete mode 100644 example/src/test/java/com/example/sugartest/ListAllOrderByTests.java delete mode 100644 example/src/test/java/com/example/sugartest/LongFieldTests.java delete mode 100644 example/src/test/java/com/example/sugartest/MultipleSaveTests.java delete mode 100644 example/src/test/java/com/example/sugartest/NestedAnnotatedTests.java delete mode 100644 example/src/test/java/com/example/sugartest/NestedExtendedTests.java delete mode 100644 example/src/test/java/com/example/sugartest/NestedMixedAATests.java delete mode 100644 example/src/test/java/com/example/sugartest/NestedMixedABTests.java delete mode 100644 example/src/test/java/com/example/sugartest/NestedMixedBATests.java delete mode 100644 example/src/test/java/com/example/sugartest/NestedMixedBBTests.java delete mode 100644 example/src/test/java/com/example/sugartest/NoSugarModelTests.java delete mode 100644 example/src/test/java/com/example/sugartest/RelationshipAnnotatedTests.java delete mode 100644 example/src/test/java/com/example/sugartest/RelationshipExtendedTests.java delete mode 100644 example/src/test/java/com/example/sugartest/RelationshipMixedATests.java delete mode 100644 example/src/test/java/com/example/sugartest/RelationshipMixedBTests.java delete mode 100644 example/src/test/java/com/example/sugartest/RobolectricGradleTestRunner.java delete mode 100644 example/src/test/java/com/example/sugartest/ShortFieldTests.java delete mode 100644 example/src/test/java/com/example/sugartest/SimpleAnnotatedModelTests.java delete mode 100644 example/src/test/java/com/example/sugartest/SimpleExtendedModelTests.java delete mode 100644 example/src/test/java/com/example/sugartest/StringFieldTests.java delete mode 100644 library/src/test/java/com/orm/ClientApp.java delete mode 100644 library/src/test/java/com/orm/RobolectricGradleTestRunner.java rename {example/src/main/java/com/example => library/src/test/java/com/orm/app}/ClientApp.java (94%) rename library/src/test/java/com/orm/{util => helper}/ManifestHelperTest.java (50%) rename library/src/test/java/com/orm/{ => helper}/NamingHelperTest.java (81%) rename library/src/test/java/com/orm/{util => helper}/SugarTransactionHelperTest.java (84%) rename library/src/test/java/com/orm/{models => model}/BigDecimalFieldAnnotatedModel.java (94%) rename library/src/test/java/com/orm/{models => model}/BigDecimalFieldExtendedModel.java (94%) rename library/src/test/java/com/orm/{models => model}/BooleanFieldAnnotatedModel.java (96%) rename library/src/test/java/com/orm/{models => model}/BooleanFieldExtendedModel.java (95%) rename library/src/test/java/com/orm/{models => model}/ByteArrayAnnotatedModel.java (94%) rename library/src/test/java/com/orm/{models => model}/ByteArrayExtendedModel.java (93%) rename library/src/test/java/com/orm/{models => model}/DoubleFieldAnnotatedModel.java (95%) rename library/src/test/java/com/orm/{models => model}/DoubleFieldExtendedModel.java (95%) rename library/src/test/java/com/orm/{models => model}/EmptyModel.java (86%) rename library/src/test/java/com/orm/{models => model}/EnumFieldAnnotatedModel.java (97%) rename library/src/test/java/com/orm/{models => model}/EnumFieldExtendedModel.java (97%) rename library/src/test/java/com/orm/{models => model}/FloatFieldAnnotatedModel.java (95%) rename library/src/test/java/com/orm/{models => model}/FloatFieldExtendedModel.java (95%) rename library/src/test/java/com/orm/{models => model}/IncompleteAnnotatedModel.java (89%) rename library/src/test/java/com/orm/{models => model}/IntUniqueModel.java (92%) rename library/src/test/java/com/orm/{models => model}/IntegerFieldAnnotatedModel.java (95%) rename library/src/test/java/com/orm/{models => model}/IntegerFieldExtendedModel.java (95%) rename library/src/test/java/com/orm/{models => model}/LongFieldAnnotatedModel.java (95%) rename library/src/test/java/com/orm/{models => model}/LongFieldExtendedModel.java (95%) rename library/src/test/java/com/orm/{models => model}/MultiColumnUniqueModel.java (93%) rename library/src/test/java/com/orm/{models => model}/NestedAnnotatedModel.java (94%) rename library/src/test/java/com/orm/{models => model}/NestedExtendedModel.java (93%) rename library/src/test/java/com/orm/{models => model}/NestedMixedAAModel.java (93%) rename library/src/test/java/com/orm/{models => model}/NestedMixedABModel.java (93%) rename library/src/test/java/com/orm/{models => model}/NestedMixedBAModel.java (94%) rename library/src/test/java/com/orm/{models => model}/NestedMixedBBModel.java (94%) rename library/src/test/java/com/orm/{models => model}/NoSugarModel.java (73%) rename library/src/test/java/com/orm/{models => model}/RelationshipAnnotatedModel.java (94%) rename library/src/test/java/com/orm/{models => model}/RelationshipExtendedModel.java (93%) rename library/src/test/java/com/orm/{models => model}/RelationshipMixedAModel.java (93%) rename library/src/test/java/com/orm/{models => model}/RelationshipMixedBModel.java (94%) rename library/src/test/java/com/orm/{models => model}/ShortFieldAnnotatedModel.java (95%) rename library/src/test/java/com/orm/{models => model}/ShortFieldExtendedModel.java (95%) rename library/src/test/java/com/orm/{models => model}/SimpleAnnotatedModel.java (88%) rename library/src/test/java/com/orm/{models => model}/SimpleExtendedModel.java (83%) rename library/src/test/java/com/orm/{models => model}/SimpleModel.java (95%) rename library/src/test/java/com/orm/{models => model}/StringFieldAnnotatedModel.java (94%) rename library/src/test/java/com/orm/{models => model}/StringFieldAnnotatedNoIdModel.java (94%) rename library/src/test/java/com/orm/{models => model}/StringFieldExtendedModel.java (94%) rename library/src/test/java/com/orm/{models => model}/StringFieldExtendedModelAnnotatedColumn.java (94%) rename library/src/test/java/com/orm/{models => model}/TestRecord.java (92%) create mode 100644 library/src/test/java/com/orm/record/RelationshipMixedATests.java diff --git a/example/src/main/java/com/example/models/BigDecimalFieldAnnotatedModel.java b/example/src/main/java/com/example/models/BigDecimalFieldAnnotatedModel.java deleted file mode 100644 index bd4fd846..00000000 --- a/example/src/main/java/com/example/models/BigDecimalFieldAnnotatedModel.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - -import java.math.BigDecimal; - -@Table -public class BigDecimalFieldAnnotatedModel { - private BigDecimal decimal; - private Long id; - - public BigDecimalFieldAnnotatedModel() {} - - public BigDecimalFieldAnnotatedModel(BigDecimal decimal) { - this.decimal = decimal; - } - - public BigDecimal getBigDecimal() { - return decimal; - } - - public Long getId() { - return id; - } -} diff --git a/example/src/main/java/com/example/models/BigDecimalFieldExtendedModel.java b/example/src/main/java/com/example/models/BigDecimalFieldExtendedModel.java deleted file mode 100644 index e258cf32..00000000 --- a/example/src/main/java/com/example/models/BigDecimalFieldExtendedModel.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.example.models; - -import com.orm.SugarRecord; - -import java.math.BigDecimal; - -public class BigDecimalFieldExtendedModel extends SugarRecord { - private BigDecimal decimal; - - public BigDecimalFieldExtendedModel() {} - - public BigDecimalFieldExtendedModel(BigDecimal decimal) { - this.decimal = decimal; - } - - public BigDecimal getBigDecimal() { - return decimal; - } -} diff --git a/example/src/main/java/com/example/models/BooleanFieldAnnotatedModel.java b/example/src/main/java/com/example/models/BooleanFieldAnnotatedModel.java deleted file mode 100644 index d46455f4..00000000 --- a/example/src/main/java/com/example/models/BooleanFieldAnnotatedModel.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - -@Table -public class BooleanFieldAnnotatedModel { - private Boolean objectBoolean; - private boolean rawBoolean; - private Long id; - - public BooleanFieldAnnotatedModel() {} - - public BooleanFieldAnnotatedModel(Boolean objectBoolean) { - this.objectBoolean = objectBoolean; - } - - public BooleanFieldAnnotatedModel(boolean rawBoolean) { - this.rawBoolean = rawBoolean; - } - - public Boolean getBoolean() { - return objectBoolean; - } - - public boolean getRawBoolean() { - return rawBoolean; - } -} diff --git a/example/src/main/java/com/example/models/BooleanFieldExtendedModel.java b/example/src/main/java/com/example/models/BooleanFieldExtendedModel.java deleted file mode 100644 index 972855cd..00000000 --- a/example/src/main/java/com/example/models/BooleanFieldExtendedModel.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.example.models; - -import com.orm.SugarRecord; - -public class BooleanFieldExtendedModel extends SugarRecord { - private Boolean objectBoolean; - private boolean rawBoolean; - - public BooleanFieldExtendedModel() {} - - public BooleanFieldExtendedModel(Boolean objectBoolean) { - this.objectBoolean = objectBoolean; - } - - public BooleanFieldExtendedModel(boolean rawBoolean) { - this.rawBoolean = rawBoolean; - } - - public Boolean getBoolean() { - return objectBoolean; - } - - public boolean getRawBoolean() { - return rawBoolean; - } -} diff --git a/example/src/main/java/com/example/models/ByteArrayAnnotatedModel.java b/example/src/main/java/com/example/models/ByteArrayAnnotatedModel.java deleted file mode 100644 index f92c277a..00000000 --- a/example/src/main/java/com/example/models/ByteArrayAnnotatedModel.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - -@Table -public class ByteArrayAnnotatedModel { - private byte[] byteArray; - private Long id; - - public ByteArrayAnnotatedModel() {} - - public ByteArrayAnnotatedModel(byte[] byteArray) { - this.byteArray = byteArray; - } - - public byte[] getByteArray() { - return byteArray; - } - - public Long getId() { - return id; - } -} diff --git a/example/src/main/java/com/example/models/ByteArrayExtendedModel.java b/example/src/main/java/com/example/models/ByteArrayExtendedModel.java deleted file mode 100644 index 7808c514..00000000 --- a/example/src/main/java/com/example/models/ByteArrayExtendedModel.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.example.models; - -import com.orm.SugarRecord; - -public class ByteArrayExtendedModel extends SugarRecord { - private byte[] byteArray; - - public ByteArrayExtendedModel() {} - - public ByteArrayExtendedModel(byte[] byteArray) { - this.byteArray = byteArray; - } - - public byte[] getByteArray() { - return byteArray; - } -} diff --git a/example/src/main/java/com/example/models/DoubleFieldAnnotatedModel.java b/example/src/main/java/com/example/models/DoubleFieldAnnotatedModel.java deleted file mode 100644 index 1657d23e..00000000 --- a/example/src/main/java/com/example/models/DoubleFieldAnnotatedModel.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - -@Table -public class DoubleFieldAnnotatedModel { - private Double objectDouble; - private double rawDouble; - private Long id; - - public DoubleFieldAnnotatedModel() {} - - public DoubleFieldAnnotatedModel(Double objectDouble) { - this.objectDouble = objectDouble; - } - - public DoubleFieldAnnotatedModel(double rawDouble) { - this.rawDouble = rawDouble; - } - - public Double getDouble() { - return objectDouble; - } - - public double getRawDouble() { - return rawDouble; - } -} diff --git a/example/src/main/java/com/example/models/DoubleFieldExtendedModel.java b/example/src/main/java/com/example/models/DoubleFieldExtendedModel.java deleted file mode 100644 index b0708b7c..00000000 --- a/example/src/main/java/com/example/models/DoubleFieldExtendedModel.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.example.models; - -import com.orm.SugarRecord; - -public class DoubleFieldExtendedModel extends SugarRecord { - private Double objectDouble; - private double rawDouble; - - public DoubleFieldExtendedModel() {} - - public DoubleFieldExtendedModel(Double objectDouble) { - this.objectDouble = objectDouble; - } - - public DoubleFieldExtendedModel(double rawDouble) { - this.rawDouble = rawDouble; - } - - public Double getDouble() { - return objectDouble; - } - - public double getRawDouble() { - return rawDouble; - } -} diff --git a/example/src/main/java/com/example/models/EnumFieldAnnotatedModel.java b/example/src/main/java/com/example/models/EnumFieldAnnotatedModel.java deleted file mode 100644 index 8303c787..00000000 --- a/example/src/main/java/com/example/models/EnumFieldAnnotatedModel.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - -@Table -public class EnumFieldAnnotatedModel { - public static enum DefaultEnum { - ONE, TWO - } - - public static enum OverrideEnum { - ONE, TWO; - - @Override - public String toString() { - return super.toString().toLowerCase(); - } - } - - private OverrideEnum overrideEnum; - private DefaultEnum defaultEnum; - private Long id; - - public EnumFieldAnnotatedModel() { - - } - - public EnumFieldAnnotatedModel(OverrideEnum e1, DefaultEnum d1) { - overrideEnum = e1; - defaultEnum = d1; - } - - public DefaultEnum getDefaultEnum() { - return defaultEnum; - } - - public void setDefaultEnum(DefaultEnum defaultEnum) { - this.defaultEnum = defaultEnum; - } - - public void setOverrideEnum(OverrideEnum overrideEnum) { - this.overrideEnum = overrideEnum; - } - - public OverrideEnum getOverrideEnum() { - return overrideEnum; - } - - public Long getId() { - return id; - } -} diff --git a/example/src/main/java/com/example/models/EnumFieldExtendedModel.java b/example/src/main/java/com/example/models/EnumFieldExtendedModel.java deleted file mode 100644 index df3a093b..00000000 --- a/example/src/main/java/com/example/models/EnumFieldExtendedModel.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.example.models; - -import com.orm.SugarRecord; - -public class EnumFieldExtendedModel extends SugarRecord { - public static enum DefaultEnum { - ONE, TWO - } - - public static enum OverrideEnum { - ONE, TWO; - - @Override - public String toString() { - return super.toString().toLowerCase(); - } - } - - private OverrideEnum overrideEnum; - private DefaultEnum defaultEnum; - - public EnumFieldExtendedModel() { - - } - - public EnumFieldExtendedModel(OverrideEnum e1, DefaultEnum d1) { - overrideEnum = e1; - defaultEnum = d1; - } - - public DefaultEnum getDefaultEnum() { - return defaultEnum; - } - - public void setDefaultEnum(DefaultEnum defaultEnum) { - this.defaultEnum = defaultEnum; - } - - public void setOverrideEnum(OverrideEnum overrideEnum) { - this.overrideEnum = overrideEnum; - } - - public OverrideEnum getOverrideEnum() { - return overrideEnum; - } -} diff --git a/example/src/main/java/com/example/models/FloatFieldAnnotatedModel.java b/example/src/main/java/com/example/models/FloatFieldAnnotatedModel.java deleted file mode 100644 index 6da3fcc5..00000000 --- a/example/src/main/java/com/example/models/FloatFieldAnnotatedModel.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - -@Table -public class FloatFieldAnnotatedModel { - private Float objectFloat; - private float rawFloat; - private Long id; - - public FloatFieldAnnotatedModel() {} - - public FloatFieldAnnotatedModel(Float objectFloat) { - this.objectFloat = objectFloat; - } - - public FloatFieldAnnotatedModel(float rawFloat) { - this.rawFloat = rawFloat; - } - - public Float getFloat() { - return objectFloat; - } - - public float getRawFloat() { - return rawFloat; - } -} diff --git a/example/src/main/java/com/example/models/FloatFieldExtendedModel.java b/example/src/main/java/com/example/models/FloatFieldExtendedModel.java deleted file mode 100644 index 0abf904b..00000000 --- a/example/src/main/java/com/example/models/FloatFieldExtendedModel.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.example.models; - -import com.orm.SugarRecord; - -public class FloatFieldExtendedModel extends SugarRecord { - private Float objectFloat; - private float rawFloat; - - public FloatFieldExtendedModel() {} - - public FloatFieldExtendedModel(Float objectFloat) { - this.objectFloat = objectFloat; - } - - public FloatFieldExtendedModel(float rawFloat) { - this.rawFloat = rawFloat; - } - - public Float getFloat() { - return objectFloat; - } - - public float getRawFloat() { - return rawFloat; - } -} diff --git a/example/src/main/java/com/example/models/IncompleteAnnotatedModel.java b/example/src/main/java/com/example/models/IncompleteAnnotatedModel.java deleted file mode 100644 index ccb47fa8..00000000 --- a/example/src/main/java/com/example/models/IncompleteAnnotatedModel.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - -@Table -public class IncompleteAnnotatedModel { - // An annotated model must provide a Long id field. A setter or getter is optional - public IncompleteAnnotatedModel() {} -} diff --git a/example/src/main/java/com/example/models/IntegerFieldAnnotatedModel.java b/example/src/main/java/com/example/models/IntegerFieldAnnotatedModel.java deleted file mode 100644 index b4bcd03a..00000000 --- a/example/src/main/java/com/example/models/IntegerFieldAnnotatedModel.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - -@Table -public class IntegerFieldAnnotatedModel { - private Integer integer; - private int rawInteger; - public Long id; - - public IntegerFieldAnnotatedModel() {} - - public IntegerFieldAnnotatedModel(Integer integer) { - this.integer = integer; - } - - public IntegerFieldAnnotatedModel(int rawInteger) { - this.rawInteger = rawInteger; - } - - public Integer getInteger() { - return integer; - } - - public int getInt() { - return rawInteger; - } -} diff --git a/example/src/main/java/com/example/models/IntegerFieldExtendedModel.java b/example/src/main/java/com/example/models/IntegerFieldExtendedModel.java deleted file mode 100644 index b05df191..00000000 --- a/example/src/main/java/com/example/models/IntegerFieldExtendedModel.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.example.models; - -import com.orm.SugarRecord; - -public class IntegerFieldExtendedModel extends SugarRecord { - private Integer integer; - private int rawInteger; - - public IntegerFieldExtendedModel() {} - - public IntegerFieldExtendedModel(Integer integer) { - this.integer = integer; - } - - public IntegerFieldExtendedModel(int rawInteger) { - this.rawInteger = rawInteger; - } - - public Integer getInteger() { - return integer; - } - - public int getInt() { - return rawInteger; - } -} diff --git a/example/src/main/java/com/example/models/LongFieldAnnotatedModel.java b/example/src/main/java/com/example/models/LongFieldAnnotatedModel.java deleted file mode 100644 index 9900bf8c..00000000 --- a/example/src/main/java/com/example/models/LongFieldAnnotatedModel.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - -@Table -public class LongFieldAnnotatedModel { - private Long objectLong; - private long rawLong; - private Long id; - - public LongFieldAnnotatedModel() {} - - public LongFieldAnnotatedModel(Long objectLong) { - this.objectLong = objectLong; - } - - public LongFieldAnnotatedModel(long rawLong) { - this.rawLong = rawLong; - } - - public Long getLong() { - return objectLong; - } - - public long getRawLong() { - return rawLong; - } -} diff --git a/example/src/main/java/com/example/models/LongFieldExtendedModel.java b/example/src/main/java/com/example/models/LongFieldExtendedModel.java deleted file mode 100644 index 19ccdaa5..00000000 --- a/example/src/main/java/com/example/models/LongFieldExtendedModel.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.example.models; - -import com.orm.SugarRecord; - -public class LongFieldExtendedModel extends SugarRecord { - private Long objectLong; - private long rawLong; - - public LongFieldExtendedModel() {} - - public LongFieldExtendedModel(Long objectLong) { - this.objectLong = objectLong; - } - - public LongFieldExtendedModel(long rawLong) { - this.rawLong = rawLong; - } - - public Long getLong() { - return objectLong; - } - - public long getRawLong() { - return rawLong; - } -} diff --git a/example/src/main/java/com/example/models/NestedAnnotatedModel.java b/example/src/main/java/com/example/models/NestedAnnotatedModel.java deleted file mode 100644 index c7ec80fd..00000000 --- a/example/src/main/java/com/example/models/NestedAnnotatedModel.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - -@Table -public class NestedAnnotatedModel { - private RelationshipAnnotatedModel nested; - private Long id; - - public NestedAnnotatedModel() {} - - public NestedAnnotatedModel(RelationshipAnnotatedModel nested) { - this.nested = nested; - } - - public RelationshipAnnotatedModel getNested() { - return nested; - } - - public Long getId() { - return id; - } -} diff --git a/example/src/main/java/com/example/models/NestedExtendedModel.java b/example/src/main/java/com/example/models/NestedExtendedModel.java deleted file mode 100644 index 369107c5..00000000 --- a/example/src/main/java/com/example/models/NestedExtendedModel.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.example.models; - - -import com.orm.SugarRecord; - -public class NestedExtendedModel extends SugarRecord { - private RelationshipExtendedModel nested; - - public NestedExtendedModel() {} - - public NestedExtendedModel(RelationshipExtendedModel nested) { - this.nested = nested; - } - - public RelationshipExtendedModel getNested() { - return nested; - } -} diff --git a/example/src/main/java/com/example/models/NestedMixedAAModel.java b/example/src/main/java/com/example/models/NestedMixedAAModel.java deleted file mode 100644 index f5f70753..00000000 --- a/example/src/main/java/com/example/models/NestedMixedAAModel.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.example.models; - - -import com.orm.SugarRecord; - -public class NestedMixedAAModel extends SugarRecord { - private RelationshipMixedAModel nested; - - public NestedMixedAAModel() {} - - public NestedMixedAAModel(RelationshipMixedAModel nested) { - this.nested = nested; - } - - public RelationshipMixedAModel getNested() { - return nested; - } -} diff --git a/example/src/main/java/com/example/models/NestedMixedABModel.java b/example/src/main/java/com/example/models/NestedMixedABModel.java deleted file mode 100644 index 045de69f..00000000 --- a/example/src/main/java/com/example/models/NestedMixedABModel.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.example.models; - - -import com.orm.SugarRecord; - -public class NestedMixedABModel extends SugarRecord { - private RelationshipMixedBModel nested; - - public NestedMixedABModel() {} - - public NestedMixedABModel(RelationshipMixedBModel nested) { - this.nested = nested; - } - - public RelationshipMixedBModel getNested() { - return nested; - } -} diff --git a/example/src/main/java/com/example/models/NestedMixedBAModel.java b/example/src/main/java/com/example/models/NestedMixedBAModel.java deleted file mode 100644 index 338f748e..00000000 --- a/example/src/main/java/com/example/models/NestedMixedBAModel.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.example.models; - - -import com.orm.annotation.Table; - -@Table -public class NestedMixedBAModel { - private RelationshipMixedAModel nested; - private Long id; - - public NestedMixedBAModel() {} - - public NestedMixedBAModel(RelationshipMixedAModel nested) { - this.nested = nested; - } - - public RelationshipMixedAModel getNested() { - return nested; - } - - public Long getId() { - return id; - } -} diff --git a/example/src/main/java/com/example/models/NestedMixedBBModel.java b/example/src/main/java/com/example/models/NestedMixedBBModel.java deleted file mode 100644 index 439eb5d9..00000000 --- a/example/src/main/java/com/example/models/NestedMixedBBModel.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.example.models; - - -import com.orm.annotation.Table; - -@Table -public class NestedMixedBBModel { - private RelationshipMixedBModel nested; - private Long id; - - public NestedMixedBBModel() {} - - public NestedMixedBBModel(RelationshipMixedBModel nested) { - this.nested = nested; - } - - public RelationshipMixedBModel getNested() { - return nested; - } - - public Long getId() { - return id; - } -} diff --git a/example/src/main/java/com/example/models/NoSugarModel.java b/example/src/main/java/com/example/models/NoSugarModel.java deleted file mode 100644 index 0e2d55e8..00000000 --- a/example/src/main/java/com/example/models/NoSugarModel.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.example.models; - - -public class NoSugarModel { - public NoSugarModel() { - } -} \ No newline at end of file diff --git a/example/src/main/java/com/example/models/RelationshipAnnotatedModel.java b/example/src/main/java/com/example/models/RelationshipAnnotatedModel.java deleted file mode 100644 index 44faeed0..00000000 --- a/example/src/main/java/com/example/models/RelationshipAnnotatedModel.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - -@Table -public class RelationshipAnnotatedModel { - private SimpleAnnotatedModel simple; - private Long id; - - public RelationshipAnnotatedModel() {} - - public RelationshipAnnotatedModel(SimpleAnnotatedModel simple) { - this.simple = simple; - } - - public SimpleAnnotatedModel getSimple() { - return simple; - } - - public Long getId() { - return id; - } -} diff --git a/example/src/main/java/com/example/models/RelationshipExtendedModel.java b/example/src/main/java/com/example/models/RelationshipExtendedModel.java deleted file mode 100644 index afa2d105..00000000 --- a/example/src/main/java/com/example/models/RelationshipExtendedModel.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.example.models; - - -import com.orm.SugarRecord; - -public class RelationshipExtendedModel extends SugarRecord { - private SimpleExtendedModel simple; - - public RelationshipExtendedModel() {} - - public RelationshipExtendedModel(SimpleExtendedModel simple) { - this.simple = simple; - } - - public SimpleExtendedModel getSimple() { - return simple; - } -} diff --git a/example/src/main/java/com/example/models/RelationshipMixedAModel.java b/example/src/main/java/com/example/models/RelationshipMixedAModel.java deleted file mode 100644 index f09dd5c0..00000000 --- a/example/src/main/java/com/example/models/RelationshipMixedAModel.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.example.models; - - -import com.orm.SugarRecord; - -public class RelationshipMixedAModel extends SugarRecord { - private SimpleAnnotatedModel simple; - - public RelationshipMixedAModel() {} - - public RelationshipMixedAModel(SimpleAnnotatedModel simple) { - this.simple = simple; - } - - public SimpleAnnotatedModel getSimple() { - return simple; - } -} diff --git a/example/src/main/java/com/example/models/RelationshipMixedBModel.java b/example/src/main/java/com/example/models/RelationshipMixedBModel.java deleted file mode 100644 index 525ad2e0..00000000 --- a/example/src/main/java/com/example/models/RelationshipMixedBModel.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.example.models; - - -import com.orm.annotation.Table; - -@Table -public class RelationshipMixedBModel { - private SimpleExtendedModel simple; - private Long id; - - public RelationshipMixedBModel() {} - - public RelationshipMixedBModel(SimpleExtendedModel simple) { - this.simple = simple; - } - - public SimpleExtendedModel getSimple() { - return simple; - } - - public Long getId() { - return id; - } -} diff --git a/example/src/main/java/com/example/models/ShortFieldAnnotatedModel.java b/example/src/main/java/com/example/models/ShortFieldAnnotatedModel.java deleted file mode 100644 index 0d732b7f..00000000 --- a/example/src/main/java/com/example/models/ShortFieldAnnotatedModel.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - -@Table -public class ShortFieldAnnotatedModel { - private Short objectShort; - private short rawShort; - private Long id; - - public ShortFieldAnnotatedModel() {} - - public ShortFieldAnnotatedModel(Short objectShort) { - this.objectShort = objectShort; - } - - public ShortFieldAnnotatedModel(short rawShort) { - this.rawShort = rawShort; - } - - public Short getShort() { - return objectShort; - } - - public short getRawShort() { - return rawShort; - } -} diff --git a/example/src/main/java/com/example/models/ShortFieldExtendedModel.java b/example/src/main/java/com/example/models/ShortFieldExtendedModel.java deleted file mode 100644 index 5373bb23..00000000 --- a/example/src/main/java/com/example/models/ShortFieldExtendedModel.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.example.models; - -import com.orm.SugarRecord; - -public class ShortFieldExtendedModel extends SugarRecord { - private Short objectShort; - private short rawShort; - - public ShortFieldExtendedModel() {} - - public ShortFieldExtendedModel(Short objectShort) { - this.objectShort = objectShort; - } - - public ShortFieldExtendedModel(short rawShort) { - this.rawShort = rawShort; - } - - public Short getShort() { - return objectShort; - } - - public short getRawShort() { - return rawShort; - } -} diff --git a/example/src/main/java/com/example/models/SimpleAnnotatedModel.java b/example/src/main/java/com/example/models/SimpleAnnotatedModel.java deleted file mode 100644 index 7e9e7db4..00000000 --- a/example/src/main/java/com/example/models/SimpleAnnotatedModel.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - - -@Table -public class SimpleAnnotatedModel { - private Long id; - - public SimpleAnnotatedModel() {} - - public Long getId() { - return id; - } -} diff --git a/example/src/main/java/com/example/models/SimpleExtendedModel.java b/example/src/main/java/com/example/models/SimpleExtendedModel.java deleted file mode 100644 index 77f75b63..00000000 --- a/example/src/main/java/com/example/models/SimpleExtendedModel.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.example.models; - -import com.orm.SugarRecord; - - -public class SimpleExtendedModel extends SugarRecord { - public SimpleExtendedModel() {} -} diff --git a/example/src/main/java/com/example/models/SimpleModel.java b/example/src/main/java/com/example/models/SimpleModel.java deleted file mode 100644 index dd482ee5..00000000 --- a/example/src/main/java/com/example/models/SimpleModel.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.example.models; - -import com.orm.SugarRecord; - -public class SimpleModel extends SugarRecord { - private String str; - private int integer; - private boolean bool; - - public String getStr() { - return str; - } - - public void setStr(String str) { - this.str = str; - } - - public int getInteger() { - return integer; - } - - public void setInteger(int integer) { - this.integer = integer; - } - - public boolean isBool() { - return bool; - } - - public void setBool(boolean bool) { - this.bool = bool; - } -} diff --git a/example/src/main/java/com/example/models/StringFieldAnnotatedModel.java b/example/src/main/java/com/example/models/StringFieldAnnotatedModel.java deleted file mode 100644 index d4e86aa4..00000000 --- a/example/src/main/java/com/example/models/StringFieldAnnotatedModel.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - -@Table -public class StringFieldAnnotatedModel { - private String string; - private Long id; - - public StringFieldAnnotatedModel() {} - - public StringFieldAnnotatedModel(String string) { - this.string = string; - } - - public String getString() { - return string; - } - - public void setString(String string) { - this.string = string; - } -} diff --git a/example/src/main/java/com/example/models/StringFieldAnnotatedNoIdModel.java b/example/src/main/java/com/example/models/StringFieldAnnotatedNoIdModel.java deleted file mode 100644 index 54737775..00000000 --- a/example/src/main/java/com/example/models/StringFieldAnnotatedNoIdModel.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.example.models; - -import com.orm.annotation.Table; - -@Table -public class StringFieldAnnotatedNoIdModel { - private String string; - - public StringFieldAnnotatedNoIdModel() {} - - public StringFieldAnnotatedNoIdModel(String string) { - this.string = string; - } - - public String getString() { - return string; - } - - public void setString(String string) { - this.string = string; - } -} diff --git a/example/src/main/java/com/example/models/StringFieldExtendedModel.java b/example/src/main/java/com/example/models/StringFieldExtendedModel.java deleted file mode 100644 index 33bab4e9..00000000 --- a/example/src/main/java/com/example/models/StringFieldExtendedModel.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.example.models; - -import com.orm.SugarRecord; - -public class StringFieldExtendedModel extends SugarRecord { - private String string; - - public StringFieldExtendedModel() {} - - public StringFieldExtendedModel(String string) { - this.string = string; - } - - public String getString() { - return string; - } - - public void setString(String string) { - this.string = string; - } -} diff --git a/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java b/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java deleted file mode 100644 index 9e4b36bb..00000000 --- a/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.example.sugartest; - - -import com.example.models.BigDecimalFieldAnnotatedModel; -import com.example.models.BigDecimalFieldExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import java.math.BigDecimal; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class BigDecimalFieldTests { - @Test - public void nullBigDecimalExtendedTest() { - save(new BigDecimalFieldExtendedModel()); - BigDecimalFieldExtendedModel model = - SugarRecord.findById(BigDecimalFieldExtendedModel.class, 1); - assertNull(model.getBigDecimal()); - } - - @Test - public void nullBigDecimalAnnotatedTest() { - save(new BigDecimalFieldAnnotatedModel()); - BigDecimalFieldAnnotatedModel model = - SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); - assertNull(model.getBigDecimal()); - } - - @Test - public void bigDecimalExtendedTest() { - BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); - save(new BigDecimalFieldExtendedModel(decimal)); - BigDecimalFieldExtendedModel model = SugarRecord.findById(BigDecimalFieldExtendedModel.class, 1); - assertEquals(decimal, model.getBigDecimal()); - } - - @Test - public void bigDecimalAnnotatedTest() { - BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); - save(new BigDecimalFieldAnnotatedModel(decimal)); - BigDecimalFieldAnnotatedModel model = - SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); - assertEquals(decimal, model.getBigDecimal()); - } -} diff --git a/example/src/test/java/com/example/sugartest/BooleanFieldTests.java b/example/src/test/java/com/example/sugartest/BooleanFieldTests.java deleted file mode 100644 index a117af6c..00000000 --- a/example/src/test/java/com/example/sugartest/BooleanFieldTests.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.example.sugartest; - - -import com.example.models.BooleanFieldAnnotatedModel; -import com.example.models.BooleanFieldExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class BooleanFieldTests { - @Test - public void nullBooleanExtendedTest() { - save(new BooleanFieldExtendedModel()); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); - assertNull(model.getBoolean()); - } - - @Test - public void nullRawBooleanExtendedTest() { - save(new BooleanFieldExtendedModel()); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); - assertEquals(false, model.getRawBoolean()); - } - - @Test - public void nullBooleanAnnotatedTest() { - save(new BooleanFieldAnnotatedModel()); - BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); - assertNull(model.getBoolean()); - } - - @Test - public void nullRawBooleanAnnotatedTest() { - save(new BooleanFieldAnnotatedModel()); - BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); - assertEquals(false, model.getRawBoolean()); - } - - @Test - public void objectBooleanExtendedTest() { - Boolean objectBoolean = new Boolean(true); - save(new BooleanFieldExtendedModel(objectBoolean)); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); - assertEquals(objectBoolean, model.getBoolean()); - } - - @Test - public void rawBooleanExtendedTest() { - save(new BooleanFieldExtendedModel(true)); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); - assertEquals(true, model.getRawBoolean()); - } - - @Test - public void objectBooleanAnnotatedTest() { - Boolean objectBoolean = new Boolean(true); - save(new BooleanFieldAnnotatedModel(objectBoolean)); - BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); - assertEquals(objectBoolean, model.getBoolean()); - } - - @Test - public void rawBooleanAnnotatedTest() { - save(new BooleanFieldAnnotatedModel(true)); - BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); - assertEquals(true, model.getRawBoolean()); - } -} diff --git a/example/src/test/java/com/example/sugartest/ByteArrayFieldTests.java b/example/src/test/java/com/example/sugartest/ByteArrayFieldTests.java deleted file mode 100644 index 6f830960..00000000 --- a/example/src/test/java/com/example/sugartest/ByteArrayFieldTests.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.example.sugartest; - - -import com.example.models.ByteArrayAnnotatedModel; -import com.example.models.ByteArrayExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class ByteArrayFieldTests { - @Test - public void nullByteArrayExtendedTest() { - byte[] array = "".getBytes(); - save(new ByteArrayExtendedModel()); - ByteArrayExtendedModel model = SugarRecord.findById(ByteArrayExtendedModel.class, 1); - assertEquals(new String(array), new String(model.getByteArray())); - assertArrayEquals(array, model.getByteArray()); - } - - @Test - public void nullByteArrayAnnotatedTest() { - byte[] array = "".getBytes(); - save(new ByteArrayAnnotatedModel()); - ByteArrayAnnotatedModel model = SugarRecord.findById(ByteArrayAnnotatedModel.class, 1); - assertEquals(new String(array), new String(model.getByteArray())); - assertArrayEquals(array, model.getByteArray()); - } - - @Test - public void byteArrayExtendedTest() { - byte[] array = "hello".getBytes(); - save(new ByteArrayExtendedModel(array)); - ByteArrayExtendedModel model = SugarRecord.findById(ByteArrayExtendedModel.class, 1); - assertEquals(new String(array), new String(model.getByteArray())); - assertArrayEquals(array, model.getByteArray()); - } - - @Test - public void byteArrayAnnotatedTest() { - byte[] array = "hello".getBytes(); - save(new ByteArrayAnnotatedModel(array)); - ByteArrayAnnotatedModel model = SugarRecord.findById(ByteArrayAnnotatedModel.class, 1); - assertEquals(new String(array), new String(model.getByteArray())); - assertArrayEquals(array, model.getByteArray()); - } -} diff --git a/example/src/test/java/com/example/sugartest/CursorTests.java b/example/src/test/java/com/example/sugartest/CursorTests.java deleted file mode 100644 index 78c3d71b..00000000 --- a/example/src/test/java/com/example/sugartest/CursorTests.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.example.sugartest; - -import android.annotation.TargetApi; -import android.content.Context; -import android.database.Cursor; -import android.os.Build; -import android.view.View; -import android.view.ViewGroup; -import android.widget.CursorAdapter; -import android.widget.TextView; - -import com.example.models.SimpleModel; -import com.orm.query.Select; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.Robolectric; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.annotation.Config; - -import static com.orm.SugarRecord.save; -import static junit.framework.Assert.assertNotSame; -import static junit.framework.Assert.assertSame; - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class CursorTests { - @Test - public void testColumnNames() { - save(new SimpleModel()); - Cursor c = Select.from(SimpleModel.class).getCursor(); - for (String col : new String[]{"STR", "INTEGER", "BOOL", "ID"}) { - assertNotSame("Missing column for field: " + col, -1, c.getColumnIndex(col)); - } - } - @Test - public void testSugarCursor(){ - save(new SimpleModel()); - Cursor cursor = Select.from(SimpleModel.class).getCursor(); - assertNotSame("No _id", -1, cursor.getColumnIndex("_id")); - assertSame("_id != ID", cursor.getColumnIndex("_id"), cursor.getColumnIndex("ID")); - } - - @Test - public void testNoColumn(){ - save(new SimpleModel()); - Cursor cursor = Select.from(SimpleModel.class).getCursor(); - assertSame(-1, cursor.getColumnIndex("nonexistent")); - } - - @TargetApi(Build.VERSION_CODES.HONEYCOMB) - @Test - public void testMakeAdapter() { - save(new SimpleModel()); - Cursor c = Select.from(SimpleModel.class).getCursor(); - CursorAdapter adapter = new CursorAdapter(RuntimeEnvironment.application, c, true) { - @Override - public View newView(Context context, Cursor cursor, ViewGroup parent) { - TextView tv = new TextView(context); - String s = cursor.getString(cursor.getColumnIndex("STR")); - tv.setText(s); - return null; - } - - @Override - public void bindView(View view, Context context, Cursor cursor) { - String s = cursor.getString(cursor.getColumnIndex("STR")); - ((TextView) view).setText(s); - } - }; - } -} diff --git a/example/src/test/java/com/example/sugartest/DoubleFieldTests.java b/example/src/test/java/com/example/sugartest/DoubleFieldTests.java deleted file mode 100644 index 89dbd00a..00000000 --- a/example/src/test/java/com/example/sugartest/DoubleFieldTests.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.example.sugartest; - - -import com.example.models.DoubleFieldAnnotatedModel; -import com.example.models.DoubleFieldExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class DoubleFieldTests { - @Test - public void nullDoubleExtendedTest() { - save(new DoubleFieldExtendedModel()); - DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); - assertNull(model.getDouble()); - } - - @Test - public void nullRawDoubleExtendedTest() { - save(new DoubleFieldExtendedModel()); - DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); - assertEquals(0.0, model.getRawDouble(), 0.0000000001); - } - - @Test - public void nullDoubleAnnotatedTest() { - save(new DoubleFieldAnnotatedModel()); - DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); - assertNull(model.getDouble()); - } - - @Test - public void nullRawDoubleAnnotatedTest() { - save(new DoubleFieldAnnotatedModel()); - DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); - assertEquals(0.0, model.getRawDouble(), 0.0000000001); - } - - @Test - public void objectDoubleExtendedTest() { - Double objectDouble = new Double(25.0); - save(new DoubleFieldExtendedModel(objectDouble)); - DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); - assertEquals(objectDouble, model.getDouble()); - } - - @Test - public void rawDoubleExtendedTest() { - save(new DoubleFieldExtendedModel(25.0)); - DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); - assertEquals(25.0, model.getRawDouble(), 0.0000000001); - } - - @Test - public void objectDoubleAnnotatedTest() { - Double objectDouble = new Double(25.0); - save(new DoubleFieldAnnotatedModel(objectDouble)); - DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); - assertEquals(objectDouble, model.getDouble()); - } - - @Test - public void rawDoubleAnnotatedTest() { - save(new DoubleFieldAnnotatedModel(25.0)); - DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); - assertEquals(25.0, model.getRawDouble(), 0.0000000001); - } -} diff --git a/example/src/test/java/com/example/sugartest/EnumFieldTests.java b/example/src/test/java/com/example/sugartest/EnumFieldTests.java deleted file mode 100644 index 4eeaed71..00000000 --- a/example/src/test/java/com/example/sugartest/EnumFieldTests.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.example.sugartest; - -import com.example.models.EnumFieldAnnotatedModel; -import com.example.models.EnumFieldExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk = 18) -public class EnumFieldTests { - @Test - public void nullDefaultEnumExtendedTest() { - save(new EnumFieldExtendedModel()); - EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); - assertNull(model.getDefaultEnum()); - } - - @Test - public void nullOverriddenEnumExtendedTest() { - save(new EnumFieldExtendedModel()); - EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); - assertNull(model.getOverrideEnum()); - } - @Test - public void nullDefaultEnumAnnotatedTest() { - save(new EnumFieldAnnotatedModel()); - EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); - assertNull(model.getDefaultEnum()); - } - - @Test - public void nullOverriddenEnumAnnotatedTest() { - save(new EnumFieldAnnotatedModel()); - EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); - assertNull(model.getOverrideEnum()); - } - - @Test - public void defaultEnumExtendedTest() { - save(new EnumFieldExtendedModel(EnumFieldExtendedModel.OverrideEnum.ONE, - EnumFieldExtendedModel.DefaultEnum.TWO)); - EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); - assertNotNull(model); - assertEquals(model.getDefaultEnum(), EnumFieldExtendedModel.DefaultEnum.TWO); - } - - @Test - public void overriddenEnumExtendedTest() { - save(new EnumFieldExtendedModel(EnumFieldExtendedModel.OverrideEnum.ONE, - EnumFieldExtendedModel.DefaultEnum.TWO)); - EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); - assertNotNull(model); - assertEquals(model.getOverrideEnum(), EnumFieldExtendedModel.OverrideEnum.ONE); - } - - @Test - public void defaultEnumAnnotatedTest() { - save(new EnumFieldAnnotatedModel(EnumFieldAnnotatedModel.OverrideEnum.ONE, - EnumFieldAnnotatedModel.DefaultEnum.TWO)); - EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); - assertNotNull(model); - assertEquals(model.getDefaultEnum(), EnumFieldAnnotatedModel.DefaultEnum.TWO); - } - - @Test - public void overriddenEnumAnnotatedTest() { - save(new EnumFieldAnnotatedModel(EnumFieldAnnotatedModel.OverrideEnum.ONE, - EnumFieldAnnotatedModel.DefaultEnum.TWO)); - EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); - assertNotNull(model); - assertEquals(model.getOverrideEnum(), EnumFieldAnnotatedModel.OverrideEnum.ONE); - } -} diff --git a/example/src/test/java/com/example/sugartest/FirstAndLastTests.java b/example/src/test/java/com/example/sugartest/FirstAndLastTests.java deleted file mode 100644 index d095578f..00000000 --- a/example/src/test/java/com/example/sugartest/FirstAndLastTests.java +++ /dev/null @@ -1,158 +0,0 @@ -package com.example.sugartest; - - -import com.example.models.FloatFieldAnnotatedModel; -import com.example.models.FloatFieldExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class FirstAndLastTests { - @Test - public void firstExtendedTest() { - Float firstObjectFloat = new Float(25F); - Float lastObjectFloat = new Float(50F); - save(new FloatFieldExtendedModel(firstObjectFloat)); - save(new FloatFieldExtendedModel(lastObjectFloat)); - FloatFieldExtendedModel model = SugarRecord.first(FloatFieldExtendedModel.class); - assertEquals(firstObjectFloat, model.getFloat()); - } - - @Test - public void firstDeletedRecordExtendedTest() { - Float firstObjectFloat = new Float(15F); - Float secondObjectFloat = new Float(25F); - Float thirdObjectFloat = new Float(35F); - Float fourthObjectFloat = new Float(45F); - save(new FloatFieldExtendedModel(firstObjectFloat)); - save(new FloatFieldExtendedModel(secondObjectFloat)); - save(new FloatFieldExtendedModel(thirdObjectFloat)); - save(new FloatFieldExtendedModel(fourthObjectFloat)); - FloatFieldExtendedModel firstRecord = SugarRecord.findById(FloatFieldExtendedModel.class, 1); - firstRecord.delete(); - FloatFieldExtendedModel model = SugarRecord.first(FloatFieldExtendedModel.class); - assertEquals(secondObjectFloat, model.getFloat()); - } - - @Test - public void lastExtendedTest() { - Float firstObjectFloat = new Float(25F); - Float lastObjectFloat = new Float(50F); - save(new FloatFieldExtendedModel(firstObjectFloat)); - save(new FloatFieldExtendedModel(lastObjectFloat)); - FloatFieldExtendedModel model = SugarRecord.last(FloatFieldExtendedModel.class); - assertEquals(lastObjectFloat, model.getFloat()); - } - - @Test - public void lastDeletedRecordExtendedTest() { - Float firstObjectFloat = new Float(15F); - Float secondObjectFloat = new Float(25F); - Float thirdObjectFloat = new Float(35F); - Float fourthObjectFloat = new Float(45F); - save(new FloatFieldExtendedModel(firstObjectFloat)); - save(new FloatFieldExtendedModel(secondObjectFloat)); - save(new FloatFieldExtendedModel(thirdObjectFloat)); - save(new FloatFieldExtendedModel(fourthObjectFloat)); - FloatFieldExtendedModel lastRecord = SugarRecord.findById(FloatFieldExtendedModel.class, 4); - lastRecord.delete(); - FloatFieldExtendedModel model = SugarRecord.last(FloatFieldExtendedModel.class); - assertEquals(thirdObjectFloat, model.getFloat()); - } - - @Test - public void nullFirstExtendedTest() { - assertNull(SugarRecord.first(FloatFieldExtendedModel.class)); - } - - @Test - public void nullLastExtendedTest() { - assertNull(SugarRecord.last(FloatFieldExtendedModel.class)); - } - - @Test - public void oneItemExtendedTest() { - save(new FloatFieldExtendedModel(new Float(25F))); - FloatFieldExtendedModel firstModel = SugarRecord.first(FloatFieldExtendedModel.class); - FloatFieldExtendedModel lastModel = SugarRecord.last(FloatFieldExtendedModel.class); - assertEquals(firstModel.getFloat(), lastModel.getFloat()); - } - - @Test - public void firstAnnotatedTest() { - Float firstObjectFloat = new Float(25F); - Float lastObjectFloat = new Float(50F); - save(new FloatFieldAnnotatedModel(firstObjectFloat)); - save(new FloatFieldAnnotatedModel(lastObjectFloat)); - FloatFieldAnnotatedModel model = SugarRecord.first(FloatFieldAnnotatedModel.class); - assertEquals(firstObjectFloat, model.getFloat()); - } - - @Test - public void firstDeletedRecordAnnotatedTest() { - Float firstObjectFloat = new Float(15F); - Float secondObjectFloat = new Float(25F); - Float thirdObjectFloat = new Float(35F); - Float fourthObjectFloat = new Float(45F); - save(new FloatFieldAnnotatedModel(firstObjectFloat)); - save(new FloatFieldAnnotatedModel(secondObjectFloat)); - save(new FloatFieldAnnotatedModel(thirdObjectFloat)); - save(new FloatFieldAnnotatedModel(fourthObjectFloat)); - FloatFieldAnnotatedModel firstRecord = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); - SugarRecord.delete(firstRecord); - FloatFieldAnnotatedModel model = SugarRecord.first(FloatFieldAnnotatedModel.class); - assertEquals(secondObjectFloat, model.getFloat()); - } - - @Test - public void lastAnnotatedTest() { - Float firstObjectFloat = new Float(25F); - Float lastObjectFloat = new Float(50F); - save(new FloatFieldAnnotatedModel(firstObjectFloat)); - save(new FloatFieldAnnotatedModel(lastObjectFloat)); - FloatFieldAnnotatedModel model = SugarRecord.last(FloatFieldAnnotatedModel.class); - assertEquals(lastObjectFloat, model.getFloat()); - } - - @Test - public void lastDeletedRecordAnnotatedTest() { - Float firstObjectFloat = new Float(15F); - Float secondObjectFloat = new Float(25F); - Float thirdObjectFloat = new Float(35F); - Float fourthObjectFloat = new Float(45F); - save(new FloatFieldAnnotatedModel(firstObjectFloat)); - save(new FloatFieldAnnotatedModel(secondObjectFloat)); - save(new FloatFieldAnnotatedModel(thirdObjectFloat)); - save(new FloatFieldAnnotatedModel(fourthObjectFloat)); - FloatFieldAnnotatedModel lastRecord = SugarRecord.findById(FloatFieldAnnotatedModel.class, 4); - SugarRecord.delete(lastRecord); - FloatFieldAnnotatedModel model = SugarRecord.last(FloatFieldAnnotatedModel.class); - assertEquals(thirdObjectFloat, model.getFloat()); - } - - @Test - public void nullFirstAnnotatedTest() { - assertNull(SugarRecord.first(FloatFieldAnnotatedModel.class)); - } - - @Test - public void nullLastAnnotatedTest() { - assertNull(SugarRecord.last(FloatFieldAnnotatedModel.class)); - } - - @Test - public void oneItemAnnotatedTest() { - save(new FloatFieldAnnotatedModel(new Float(25F))); - FloatFieldAnnotatedModel firstModel = SugarRecord.first(FloatFieldAnnotatedModel.class); - FloatFieldAnnotatedModel lastModel = SugarRecord.last(FloatFieldAnnotatedModel.class); - assertEquals(firstModel.getFloat(), lastModel.getFloat()); - } -} diff --git a/example/src/test/java/com/example/sugartest/FloatFieldTests.java b/example/src/test/java/com/example/sugartest/FloatFieldTests.java deleted file mode 100644 index 16f52bbc..00000000 --- a/example/src/test/java/com/example/sugartest/FloatFieldTests.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.example.sugartest; - - -import com.example.models.FloatFieldAnnotatedModel; -import com.example.models.FloatFieldExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class FloatFieldTests { - @Test - public void nullFloatExtendedTest() { - save(new FloatFieldExtendedModel()); - FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); - assertNull(model.getFloat()); - } - - @Test - public void nullRawFloatExtendedTest() { - save(new FloatFieldExtendedModel()); - FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); - assertEquals(0F, model.getRawFloat(), 0.0000000001F); - } - - @Test - public void nullFloatAnnotatedTest() { - save(new FloatFieldAnnotatedModel()); - FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); - assertNull(model.getFloat()); - } - - @Test - public void nullRawFloatAnnotatedTest() { - save(new FloatFieldAnnotatedModel()); - FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); - assertEquals(0F, model.getRawFloat(), 0.0000000001F); - } - - @Test - public void objectFloatExtendedTest() { - Float objectFloat = new Float(25F); - save(new FloatFieldExtendedModel(objectFloat)); - FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); - assertEquals(objectFloat, model.getFloat()); - } - - @Test - public void rawFloatExtendedTest() { - save(new FloatFieldExtendedModel(25F)); - FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); - assertEquals(25F, model.getRawFloat(), 0.0000000001F); - } - - @Test - public void objectFloatAnnotatedTest() { - Float objectFloat = new Float(25F); - save(new FloatFieldAnnotatedModel(objectFloat)); - FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); - assertEquals(objectFloat, model.getFloat()); - } - - @Test - public void rawFloatAnnotatedTest() { - save(new FloatFieldAnnotatedModel(25F)); - FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); - assertEquals(25F, model.getRawFloat(), 0.0000000001F); - } -} diff --git a/example/src/test/java/com/example/sugartest/IncompleteAnnotatedModelTests.java b/example/src/test/java/com/example/sugartest/IncompleteAnnotatedModelTests.java deleted file mode 100644 index 25f544ae..00000000 --- a/example/src/test/java/com/example/sugartest/IncompleteAnnotatedModelTests.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.sugartest; - - -import android.database.sqlite.SQLiteException; - -import com.example.models.IncompleteAnnotatedModel; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import static com.orm.SugarRecord.delete; -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertFalse; - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class IncompleteAnnotatedModelTests { - @Test(expected=SQLiteException.class) - public void saveNoIdFieldTest() { - save(new IncompleteAnnotatedModel()); - } - - @Test - public void deleteNoIdFieldTest() { - assertFalse(delete(new IncompleteAnnotatedModel())); - } -} diff --git a/example/src/test/java/com/example/sugartest/IntegerFieldTests.java b/example/src/test/java/com/example/sugartest/IntegerFieldTests.java deleted file mode 100644 index 25dbd0bd..00000000 --- a/example/src/test/java/com/example/sugartest/IntegerFieldTests.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.example.sugartest; - - -import com.example.models.IntegerFieldAnnotatedModel; -import com.example.models.IntegerFieldExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class IntegerFieldTests { - @Test - public void nullIntegerExtendedTest() { - save(new IntegerFieldExtendedModel()); - IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); - assertNull(model.getInteger()); - } - - @Test - public void nullIntExtendedTest() { - save(new IntegerFieldExtendedModel()); - IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); - assertEquals(0, model.getInt()); - } - - @Test - public void nullIntegerAnnotatedTest() { - save(new IntegerFieldAnnotatedModel()); - IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); - assertNull(model.getInteger()); - } - - @Test - public void nullIntAnnotatedTest() { - save(new IntegerFieldAnnotatedModel()); - IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); - assertEquals(0, model.getInt()); - } - - @Test - public void integerExtendedTest() { - Integer integer = new Integer(25); - save(new IntegerFieldExtendedModel(integer)); - IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); - assertEquals(integer, model.getInteger()); - } - - @Test - public void intExtendedTest() { - save(new IntegerFieldExtendedModel(25)); - IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); - assertEquals(25, model.getInt()); - } - - @Test - public void integerAnnotatedTest() { - Integer integer = new Integer(25); - save(new IntegerFieldAnnotatedModel(integer)); - IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); - assertEquals(integer, model.getInteger()); - } - - @Test - public void intAnnotatedTest() { - save(new IntegerFieldAnnotatedModel(25)); - IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); - assertEquals(25, model.getInt()); - } -} diff --git a/example/src/test/java/com/example/sugartest/ListAllOrderByTests.java b/example/src/test/java/com/example/sugartest/ListAllOrderByTests.java deleted file mode 100644 index 014c989d..00000000 --- a/example/src/test/java/com/example/sugartest/ListAllOrderByTests.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.example.sugartest; - - -import com.example.models.IntegerFieldExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import java.util.List; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class ListAllOrderByTests { - @Test - public void listAllOrderByEmptyTest() { - assertEquals(0L, SugarRecord.listAll(IntegerFieldExtendedModel.class, "id").size()); - } - - @Test - public void listAllOrderByIdTest() { - for(int i = 1; i <= 100; i++) { - save(new IntegerFieldExtendedModel(i)); - } - List models = - SugarRecord.listAll(IntegerFieldExtendedModel.class, "id"); - assertEquals(100L, models.size()); - Long id = models.get(0).getId(); - for(int i = 1; i < 100; i++) { - assertTrue(id models = - SugarRecord.listAll(IntegerFieldExtendedModel.class, "raw_integer"); - assertEquals(100L, models.size()); - int raw = models.get(0).getInt(); - for(int i = 1; i < 100; i++) { - assertTrue(raw < models.get(i).getInt()); - } - } -} diff --git a/example/src/test/java/com/example/sugartest/LongFieldTests.java b/example/src/test/java/com/example/sugartest/LongFieldTests.java deleted file mode 100644 index b99e7537..00000000 --- a/example/src/test/java/com/example/sugartest/LongFieldTests.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.example.sugartest; - - -import com.example.models.LongFieldAnnotatedModel; -import com.example.models.LongFieldExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class LongFieldTests { - @Test - public void nullLongExtendedTest() { - save(new LongFieldExtendedModel()); - LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); - assertNull(model.getLong()); - } - - @Test - public void nullRawLongExtendedTest() { - save(new LongFieldExtendedModel()); - LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); - assertEquals(0L, model.getRawLong()); - } - - @Test - public void nullLongAnnotatedTest() { - save(new LongFieldAnnotatedModel()); - LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); - assertNull(model.getLong()); - } - - @Test - public void nullRawLongAnnotatedTest() { - save(new LongFieldAnnotatedModel()); - LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); - assertEquals(0L, model.getRawLong()); - } - - @Test - public void objectLongExtendedTest() { - Long objectLong = new Long(25L); - save(new LongFieldExtendedModel(objectLong)); - LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); - assertEquals(objectLong, model.getLong()); - } - - @Test - public void rawLongExtendedTest() { - save(new LongFieldExtendedModel(25L)); - LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); - assertEquals(25L, model.getRawLong()); - } - - @Test - public void objectLongAnnotatedTest() { - Long objectLong = new Long(25L); - save(new LongFieldAnnotatedModel(objectLong)); - LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); - assertEquals(objectLong, model.getLong()); - } - - @Test - public void rawLongAnnotatedTest() { - save(new LongFieldAnnotatedModel(25L)); - LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); - assertEquals(25L, model.getRawLong()); - } -} diff --git a/example/src/test/java/com/example/sugartest/MultipleSaveTests.java b/example/src/test/java/com/example/sugartest/MultipleSaveTests.java deleted file mode 100644 index 58c669c5..00000000 --- a/example/src/test/java/com/example/sugartest/MultipleSaveTests.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.example.sugartest; - - -import com.example.models.StringFieldAnnotatedModel; -import com.example.models.StringFieldAnnotatedNoIdModel; -import com.example.models.StringFieldExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class MultipleSaveTests { - @Test - public void stringMultipleSaveOriginalExtendedTest() { - String string = "Test String"; - StringFieldExtendedModel model = new StringFieldExtendedModel(string); - long id = save(model); - StringFieldExtendedModel query = SugarRecord.findById(StringFieldExtendedModel.class, id); - assertEquals(string, query.getString()); - model.setString("Another test"); - assertEquals(id, save(model)); - assertNull(SugarRecord.findById(StringFieldExtendedModel.class, 2)); - } - - @Test - public void stringMultipleSaveQueriedExtendedTest() { - String string = "Test String"; - StringFieldExtendedModel model = new StringFieldExtendedModel(string); - long id = save(model); - StringFieldExtendedModel query = SugarRecord.findById(StringFieldExtendedModel.class, id); - assertEquals(string, query.getString()); - query.setString("Another test"); - assertEquals(id, save(query)); - assertNull(SugarRecord.findById(StringFieldExtendedModel.class, 2)); - } - - @Test - public void stringMultipleSaveOriginalAnnotatedTest() { - String string = "Test String"; - StringFieldAnnotatedModel model = new StringFieldAnnotatedModel(string); - long id = save(model); - StringFieldAnnotatedModel query = SugarRecord.findById(StringFieldAnnotatedModel.class, id); - assertEquals(string, query.getString()); - model.setString("Another test"); - assertEquals(id, save(model)); - assertNull(SugarRecord.findById(StringFieldAnnotatedModel.class, 2)); - } - - @Test - public void stringMultipleSaveQueriedAnnotatedTest() { - String string = "Test String"; - StringFieldAnnotatedModel model = new StringFieldAnnotatedModel(string); - long id = save(model); - StringFieldAnnotatedModel query = SugarRecord.findById(StringFieldAnnotatedModel.class, id); - assertEquals(string, query.getString()); - query.setString("Another test"); - assertEquals(id, save(query)); - assertNull(SugarRecord.findById(StringFieldAnnotatedModel.class, 2)); - } - - @Test - public void stringMultipleSaveOriginalAnnotatedNoIdTest() { - String string = "Test String"; - StringFieldAnnotatedNoIdModel model = new StringFieldAnnotatedNoIdModel(string); - long id = save(model); - StringFieldAnnotatedNoIdModel query = - SugarRecord.findById(StringFieldAnnotatedNoIdModel.class, id); - assertEquals(string, query.getString()); - model.setString("Another test"); - assertEquals(id, save(model)); - assertNull(SugarRecord.findById(StringFieldAnnotatedNoIdModel.class, 2)); - } - - @Test - public void stringMultipleSaveQueriedAnnotatedNoIdTest() { - String string = "Test String"; - StringFieldAnnotatedNoIdModel model = new StringFieldAnnotatedNoIdModel(string); - long id = save(model); - StringFieldAnnotatedNoIdModel query = - SugarRecord.findById(StringFieldAnnotatedNoIdModel.class, id); - assertEquals(string, query.getString()); - query.setString("Another test"); - assertEquals(id, save(query)); - assertNull(SugarRecord.findById(StringFieldAnnotatedNoIdModel.class, 2)); - } -} diff --git a/example/src/test/java/com/example/sugartest/NestedAnnotatedTests.java b/example/src/test/java/com/example/sugartest/NestedAnnotatedTests.java deleted file mode 100644 index bf3f0e46..00000000 --- a/example/src/test/java/com/example/sugartest/NestedAnnotatedTests.java +++ /dev/null @@ -1,131 +0,0 @@ -package com.example.sugartest; - -import com.example.models.NestedAnnotatedModel; -import com.example.models.RelationshipAnnotatedModel; -import com.example.models.SimpleAnnotatedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import java.util.List; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class NestedAnnotatedTests { - @Test - public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(NestedAnnotatedModel.class)); - assertEquals(0L, SugarRecord.count(RelationshipAnnotatedModel.class)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void oneSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); - save(nested); - save(new NestedAnnotatedModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(NestedAnnotatedModel.class)); - } - - @Test - public void twoSameSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); - save(nested); - save(new NestedAnnotatedModel(nested)); - save(new NestedAnnotatedModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(NestedAnnotatedModel.class)); - } - - @Test - public void twoDifferentSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); - save(another_simple); - RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); - save(nested); - RelationshipAnnotatedModel another_nested = new RelationshipAnnotatedModel(another_simple); - save(another_nested); - save(new NestedAnnotatedModel(nested)); - save(new NestedAnnotatedModel(another_nested)); - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(NestedAnnotatedModel.class)); - } - - @Test - public void manySameSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); - save(nested); - for (int i = 1; i <= 100; i++) { - save(new NestedAnnotatedModel(nested)); - } - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(NestedAnnotatedModel.class)); - } - - @Test - public void manyDifferentSaveTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); - save(nested); - save(new NestedAnnotatedModel(nested)); - } - assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(NestedAnnotatedModel.class)); - } - - @Test - public void listAllSameTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); - save(nested); - for (int i = 1; i <= 100; i++) { - save(new NestedAnnotatedModel(nested)); - } - List models = SugarRecord.listAll(NestedAnnotatedModel.class); - assertEquals(100, models.size()); - for (NestedAnnotatedModel model : models) { - assertEquals(nested.getId(), model.getNested().getId()); - assertEquals(simple.getId(), model.getNested().getSimple().getId()); - } - } - - @Test - public void listAllDifferentTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); - save(nested); - save(new NestedAnnotatedModel(nested)); - } - List models = SugarRecord.listAll(NestedAnnotatedModel.class); - assertEquals(100, models.size()); - for (NestedAnnotatedModel model : models) { - assertEquals(model.getId(), model.getNested().getId()); - assertEquals(model.getId(), model.getNested().getSimple().getId()); - } - } -} diff --git a/example/src/test/java/com/example/sugartest/NestedExtendedTests.java b/example/src/test/java/com/example/sugartest/NestedExtendedTests.java deleted file mode 100644 index f403784b..00000000 --- a/example/src/test/java/com/example/sugartest/NestedExtendedTests.java +++ /dev/null @@ -1,131 +0,0 @@ -package com.example.sugartest; - -import com.example.models.NestedExtendedModel; -import com.example.models.RelationshipExtendedModel; -import com.example.models.SimpleExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import java.util.List; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class NestedExtendedTests { - @Test - public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(NestedExtendedModel.class)); - assertEquals(0L, SugarRecord.count(RelationshipExtendedModel.class)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void oneSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); - save(nested); - save(new NestedExtendedModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipExtendedModel.class)); - assertEquals(1L, SugarRecord.count(NestedExtendedModel.class)); - } - - @Test - public void twoSameSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); - save(nested); - save(new NestedExtendedModel(nested)); - save(new NestedExtendedModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipExtendedModel.class)); - assertEquals(2L, SugarRecord.count(NestedExtendedModel.class)); - } - - @Test - public void twoDifferentSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - SimpleExtendedModel another_simple = new SimpleExtendedModel(); - save(another_simple); - RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); - save(nested); - RelationshipExtendedModel another_nested = new RelationshipExtendedModel(another_simple); - save(another_nested); - save(new NestedExtendedModel(nested)); - save(new NestedExtendedModel(another_nested)); - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipExtendedModel.class)); - assertEquals(2L, SugarRecord.count(NestedExtendedModel.class)); - } - - @Test - public void manySameSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); - save(nested); - for (int i = 1; i <= 100; i++) { - save(new NestedExtendedModel(nested)); - } - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipExtendedModel.class)); - assertEquals(100L, SugarRecord.count(NestedExtendedModel.class)); - } - - @Test - public void manyDifferentSaveTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); - save(nested); - save(new NestedExtendedModel(nested)); - } - assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipExtendedModel.class)); - assertEquals(100L, SugarRecord.count(NestedExtendedModel.class)); - } - - @Test - public void listAllSameTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); - save(nested); - for (int i = 1; i <= 100; i++) { - save(new NestedExtendedModel(nested)); - } - List models = SugarRecord.listAll(NestedExtendedModel.class); - assertEquals(100, models.size()); - for (NestedExtendedModel model : models) { - assertEquals(nested.getId(), model.getNested().getId()); - assertEquals(simple.getId(), model.getNested().getSimple().getId()); - } - } - - @Test - public void listAllDifferentTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); - save(nested); - save(new NestedExtendedModel(nested)); - } - List models = SugarRecord.listAll(NestedExtendedModel.class); - assertEquals(100, models.size()); - for (NestedExtendedModel model : models) { - assertEquals(model.getId(), model.getNested().getId()); - assertEquals(model.getId(), model.getNested().getSimple().getId()); - } - } -} diff --git a/example/src/test/java/com/example/sugartest/NestedMixedAATests.java b/example/src/test/java/com/example/sugartest/NestedMixedAATests.java deleted file mode 100644 index daf19272..00000000 --- a/example/src/test/java/com/example/sugartest/NestedMixedAATests.java +++ /dev/null @@ -1,131 +0,0 @@ -package com.example.sugartest; - -import com.example.models.NestedMixedAAModel; -import com.example.models.RelationshipMixedAModel; -import com.example.models.SimpleAnnotatedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import java.util.List; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class NestedMixedAATests { - @Test - public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(NestedMixedAAModel.class)); - assertEquals(0L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void oneSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); - save(nested); - save(new NestedMixedAAModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(1L, SugarRecord.count(NestedMixedAAModel.class)); - } - - @Test - public void twoSameSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); - save(nested); - save(new NestedMixedAAModel(nested)); - save(new NestedMixedAAModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedAAModel.class)); - } - - @Test - public void twoDifferentSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); - save(another_simple); - RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); - save(nested); - RelationshipMixedAModel another_nested = new RelationshipMixedAModel(another_simple); - save(another_nested); - save(new NestedMixedAAModel(nested)); - save(new NestedMixedAAModel(another_nested)); - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedAAModel.class)); - } - - @Test - public void manySameSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); - save(nested); - for (int i = 1; i <= 100; i++) { - save(new NestedMixedAAModel(nested)); - } - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedAAModel.class)); - } - - @Test - public void manyDifferentSaveTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); - save(nested); - save(new NestedMixedAAModel(nested)); - } - assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedAAModel.class)); - } - - @Test - public void listAllSameTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); - save(nested); - for (int i = 1; i <= 100; i++) { - save(new NestedMixedAAModel(nested)); - } - List models = SugarRecord.listAll(NestedMixedAAModel.class); - assertEquals(100, models.size()); - for (NestedMixedAAModel model : models) { - assertEquals(nested.getId(), model.getNested().getId()); - assertEquals(simple.getId(), model.getNested().getSimple().getId()); - } - } - - @Test - public void listAllDifferentTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); - save(nested); - save(new NestedMixedAAModel(nested)); - } - List models = SugarRecord.listAll(NestedMixedAAModel.class); - assertEquals(100, models.size()); - for (NestedMixedAAModel model : models) { - assertEquals(model.getId(), model.getNested().getId()); - assertEquals(model.getId(), model.getNested().getSimple().getId()); - } - } -} diff --git a/example/src/test/java/com/example/sugartest/NestedMixedABTests.java b/example/src/test/java/com/example/sugartest/NestedMixedABTests.java deleted file mode 100644 index 5b9a70dc..00000000 --- a/example/src/test/java/com/example/sugartest/NestedMixedABTests.java +++ /dev/null @@ -1,131 +0,0 @@ -package com.example.sugartest; - -import com.example.models.NestedMixedABModel; -import com.example.models.RelationshipMixedBModel; -import com.example.models.SimpleExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import java.util.List; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class NestedMixedABTests { - @Test - public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(NestedMixedABModel.class)); - assertEquals(0L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void oneSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); - save(nested); - save(new NestedMixedABModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(1L, SugarRecord.count(NestedMixedABModel.class)); - } - - @Test - public void twoSameSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); - save(nested); - save(new NestedMixedABModel(nested)); - save(new NestedMixedABModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedABModel.class)); - } - - @Test - public void twoDifferentSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - SimpleExtendedModel another_simple = new SimpleExtendedModel(); - save(another_simple); - RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); - save(nested); - RelationshipMixedBModel another_nested = new RelationshipMixedBModel(another_simple); - save(another_nested); - save(new NestedMixedABModel(nested)); - save(new NestedMixedABModel(another_nested)); - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedABModel.class)); - } - - @Test - public void manySameSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); - save(nested); - for (int i = 1; i <= 100; i++) { - save(new NestedMixedABModel(nested)); - } - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedABModel.class)); - } - - @Test - public void manyDifferentSaveTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); - save(nested); - save(new NestedMixedABModel(nested)); - } - assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedABModel.class)); - } - - @Test - public void listAllSameTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); - save(nested); - for (int i = 1; i <= 100; i++) { - save(new NestedMixedABModel(nested)); - } - List models = SugarRecord.listAll(NestedMixedABModel.class); - assertEquals(100, models.size()); - for (NestedMixedABModel model : models) { - assertEquals(nested.getId(), model.getNested().getId()); - assertEquals(simple.getId(), model.getNested().getSimple().getId()); - } - } - - @Test - public void listAllDifferentTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); - save(nested); - save(new NestedMixedABModel(nested)); - } - List models = SugarRecord.listAll(NestedMixedABModel.class); - assertEquals(100, models.size()); - for (NestedMixedABModel model : models) { - assertEquals(model.getId(), model.getNested().getId()); - assertEquals(model.getId(), model.getNested().getSimple().getId()); - } - } -} diff --git a/example/src/test/java/com/example/sugartest/NestedMixedBATests.java b/example/src/test/java/com/example/sugartest/NestedMixedBATests.java deleted file mode 100644 index 7d6e4886..00000000 --- a/example/src/test/java/com/example/sugartest/NestedMixedBATests.java +++ /dev/null @@ -1,131 +0,0 @@ -package com.example.sugartest; - -import com.example.models.NestedMixedBAModel; -import com.example.models.RelationshipMixedAModel; -import com.example.models.SimpleAnnotatedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import java.util.List; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class NestedMixedBATests { - @Test - public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(NestedMixedBAModel.class)); - assertEquals(0L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void oneSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); - save(nested); - save(new NestedMixedBAModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(1L, SugarRecord.count(NestedMixedBAModel.class)); - } - - @Test - public void twoSameSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); - save(nested); - save(new NestedMixedBAModel(nested)); - save(new NestedMixedBAModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedBAModel.class)); - } - - @Test - public void twoDifferentSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); - save(another_simple); - RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); - save(nested); - RelationshipMixedAModel another_nested = new RelationshipMixedAModel(another_simple); - save(another_nested); - save(new NestedMixedBAModel(nested)); - save(new NestedMixedBAModel(another_nested)); - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedBAModel.class)); - } - - @Test - public void manySameSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); - save(nested); - for (int i = 1; i <= 100; i++) { - save(new NestedMixedBAModel(nested)); - } - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedBAModel.class)); - } - - @Test - public void manyDifferentSaveTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); - save(nested); - save(new NestedMixedBAModel(nested)); - } - assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedBAModel.class)); - } - - @Test - public void listAllSameTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); - save(nested); - for (int i = 1; i <= 100; i++) { - save(new NestedMixedBAModel(nested)); - } - List models = SugarRecord.listAll(NestedMixedBAModel.class); - assertEquals(100, models.size()); - for (NestedMixedBAModel model : models) { - assertEquals(nested.getId(), model.getNested().getId()); - assertEquals(simple.getId(), model.getNested().getSimple().getId()); - } - } - - @Test - public void listAllDifferentTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); - save(nested); - save(new NestedMixedBAModel(nested)); - } - List models = SugarRecord.listAll(NestedMixedBAModel.class); - assertEquals(100, models.size()); - for (NestedMixedBAModel model : models) { - assertEquals(model.getId(), model.getNested().getId()); - assertEquals(model.getId(), model.getNested().getSimple().getId()); - } - } -} diff --git a/example/src/test/java/com/example/sugartest/NestedMixedBBTests.java b/example/src/test/java/com/example/sugartest/NestedMixedBBTests.java deleted file mode 100644 index c9e32182..00000000 --- a/example/src/test/java/com/example/sugartest/NestedMixedBBTests.java +++ /dev/null @@ -1,131 +0,0 @@ -package com.example.sugartest; - -import com.example.models.NestedMixedBBModel; -import com.example.models.RelationshipMixedBModel; -import com.example.models.SimpleExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import java.util.List; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class NestedMixedBBTests { - @Test - public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(NestedMixedBBModel.class)); - assertEquals(0L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void oneSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); - save(nested); - save(new NestedMixedBBModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(1L, SugarRecord.count(NestedMixedBBModel.class)); - } - - @Test - public void twoSameSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); - save(nested); - save(new NestedMixedBBModel(nested)); - save(new NestedMixedBBModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedBBModel.class)); - } - - @Test - public void twoDifferentSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - SimpleExtendedModel another_simple = new SimpleExtendedModel(); - save(another_simple); - RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); - save(nested); - RelationshipMixedBModel another_nested = new RelationshipMixedBModel(another_simple); - save(another_nested); - save(new NestedMixedBBModel(nested)); - save(new NestedMixedBBModel(another_nested)); - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedBBModel.class)); - } - - @Test - public void manySameSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); - save(nested); - for (int i = 1; i <= 100; i++) { - save(new NestedMixedBBModel(nested)); - } - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedBBModel.class)); - } - - @Test - public void manyDifferentSaveTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); - save(nested); - save(new NestedMixedBBModel(nested)); - } - assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedBBModel.class)); - } - - @Test - public void listAllSameTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); - save(nested); - for (int i = 1; i <= 100; i++) { - save(new NestedMixedBBModel(nested)); - } - List models = SugarRecord.listAll(NestedMixedBBModel.class); - assertEquals(100, models.size()); - for (NestedMixedBBModel model : models) { - assertEquals(nested.getId(), model.getNested().getId()); - assertEquals(simple.getId(), model.getNested().getSimple().getId()); - } - } - - @Test - public void listAllDifferentTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); - save(nested); - save(new NestedMixedBBModel(nested)); - } - List models = SugarRecord.listAll(NestedMixedBBModel.class); - assertEquals(100, models.size()); - for (NestedMixedBBModel model : models) { - assertEquals(model.getId(), model.getNested().getId()); - assertEquals(model.getId(), model.getNested().getSimple().getId()); - } - } -} diff --git a/example/src/test/java/com/example/sugartest/NoSugarModelTests.java b/example/src/test/java/com/example/sugartest/NoSugarModelTests.java deleted file mode 100644 index dbab8103..00000000 --- a/example/src/test/java/com/example/sugartest/NoSugarModelTests.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.sugartest; - -import com.example.models.NoSugarModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class NoSugarModelTests { - @Test - public void deleteTest() throws Exception { - NoSugarModel model = new NoSugarModel(); - assertFalse(SugarRecord.delete(model)); - } - - @Test - public void saveInTransactionTest() throws Exception { - SugarRecord.saveInTx(new NoSugarModel(), new NoSugarModel()); - assertEquals(-1L, SugarRecord.count(NoSugarModel.class)); - } -} diff --git a/example/src/test/java/com/example/sugartest/RelationshipAnnotatedTests.java b/example/src/test/java/com/example/sugartest/RelationshipAnnotatedTests.java deleted file mode 100644 index 503926e1..00000000 --- a/example/src/test/java/com/example/sugartest/RelationshipAnnotatedTests.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.example.sugartest; - -import com.example.models.RelationshipAnnotatedModel; -import com.example.models.SimpleAnnotatedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import java.util.List; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class RelationshipAnnotatedTests { - @Test - public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(RelationshipAnnotatedModel.class)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void oneSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - save(new RelationshipAnnotatedModel(simple)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipAnnotatedModel.class)); - } - - @Test - public void twoSameSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - save(new RelationshipAnnotatedModel(simple)); - save(new RelationshipAnnotatedModel(simple)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipAnnotatedModel.class)); - } - - @Test - public void twoDifferentSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); - save(another_simple); - save(new RelationshipAnnotatedModel(simple)); - save(new RelationshipAnnotatedModel(another_simple)); - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipAnnotatedModel.class)); - } - - @Test - public void manySameSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - for (int i = 1; i <= 100; i++) { - save(new RelationshipAnnotatedModel(simple)); - } - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipAnnotatedModel.class)); - } - - @Test - public void manyDifferentSaveTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - save(new RelationshipAnnotatedModel(simple)); - } - assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipAnnotatedModel.class)); - } - - @Test - public void listAllSameTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - for (int i = 1; i <= 100; i++) { - save(new RelationshipAnnotatedModel(simple)); - } - List models = - SugarRecord.listAll(RelationshipAnnotatedModel.class); - assertEquals(100, models.size()); - for (RelationshipAnnotatedModel model : models) { - assertEquals(simple.getId(), model.getSimple().getId()); - } - } - - @Test - public void listAllDifferentTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - save(new RelationshipAnnotatedModel(simple)); - } - List models = - SugarRecord.listAll(RelationshipAnnotatedModel.class); - assertEquals(100, models.size()); - for (RelationshipAnnotatedModel model : models) { - assertEquals(model.getId(), model.getSimple().getId()); - } - } -} diff --git a/example/src/test/java/com/example/sugartest/RelationshipExtendedTests.java b/example/src/test/java/com/example/sugartest/RelationshipExtendedTests.java deleted file mode 100644 index 3c3e04c7..00000000 --- a/example/src/test/java/com/example/sugartest/RelationshipExtendedTests.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.example.sugartest; - -import com.example.models.RelationshipExtendedModel; -import com.example.models.SimpleExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import java.util.List; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class RelationshipExtendedTests { - @Test - public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(RelationshipExtendedModel.class)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void oneSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - save(new RelationshipExtendedModel(simple)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipExtendedModel.class)); - } - - @Test - public void twoSameSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - save(new RelationshipExtendedModel(simple)); - save(new RelationshipExtendedModel(simple)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipExtendedModel.class)); - } - - @Test - public void twoDifferentSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - SimpleExtendedModel another_simple = new SimpleExtendedModel(); - save(another_simple); - save(new RelationshipExtendedModel(simple)); - save(new RelationshipExtendedModel(another_simple)); - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipExtendedModel.class)); - } - - @Test - public void manySameSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - for (int i = 1; i <= 100; i++) { - save(new RelationshipExtendedModel(simple)); - } - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipExtendedModel.class)); - } - - @Test - public void manyDifferentSaveTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - save(new RelationshipExtendedModel(simple)); - } - assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipExtendedModel.class)); - } - - @Test - public void listAllSameTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - for (int i = 1; i <= 100; i++) { - save(new RelationshipExtendedModel(simple)); - } - List models = - SugarRecord.listAll(RelationshipExtendedModel.class); - assertEquals(100, models.size()); - for (RelationshipExtendedModel model : models) { - assertEquals(simple.getId(), model.getSimple().getId()); - } - } - - @Test - public void listAllDifferentTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - save(new RelationshipExtendedModel(simple)); - } - List models = - SugarRecord.listAll(RelationshipExtendedModel.class); - assertEquals(100, models.size()); - for (RelationshipExtendedModel model : models) { - assertEquals(model.getId(), model.getSimple().getId()); - } - } -} diff --git a/example/src/test/java/com/example/sugartest/RelationshipMixedATests.java b/example/src/test/java/com/example/sugartest/RelationshipMixedATests.java deleted file mode 100644 index 7ecb6751..00000000 --- a/example/src/test/java/com/example/sugartest/RelationshipMixedATests.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.example.sugartest; - -import com.example.models.RelationshipMixedAModel; -import com.example.models.SimpleAnnotatedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import java.util.List; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class RelationshipMixedATests { - @Test - public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void oneSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - save(new RelationshipMixedAModel(simple)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); - } - - @Test - public void twoSameSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - save(new RelationshipMixedAModel(simple)); - save(new RelationshipMixedAModel(simple)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipMixedAModel.class)); - } - - @Test - public void twoDifferentSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); - save(another_simple); - save(new RelationshipMixedAModel(simple)); - save(new RelationshipMixedAModel(another_simple)); - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipMixedAModel.class)); - } - - @Test - public void manySameSaveTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - for (int i = 1; i <= 100; i++) { - save(new RelationshipMixedAModel(simple)); - } - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipMixedAModel.class)); - } - - @Test - public void manyDifferentSaveTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - save(new RelationshipMixedAModel(simple)); - } - assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipMixedAModel.class)); - } - - @Test - public void listAllSameTest() throws Exception { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - for (int i = 1; i <= 100; i++) { - save(new RelationshipMixedAModel(simple)); - } - List models = - SugarRecord.listAll(RelationshipMixedAModel.class); - assertEquals(100, models.size()); - for (RelationshipMixedAModel model : models) { - assertEquals(simple.getId(), model.getSimple().getId()); - } - } - - @Test - public void listAllDifferentTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); - save(simple); - save(new RelationshipMixedAModel(simple)); - } - List models = - SugarRecord.listAll(RelationshipMixedAModel.class); - assertEquals(100, models.size()); - for (RelationshipMixedAModel model : models) { - assertEquals(model.getId(), model.getSimple().getId()); - } - } -} diff --git a/example/src/test/java/com/example/sugartest/RelationshipMixedBTests.java b/example/src/test/java/com/example/sugartest/RelationshipMixedBTests.java deleted file mode 100644 index 1d31ecd2..00000000 --- a/example/src/test/java/com/example/sugartest/RelationshipMixedBTests.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.example.sugartest; - -import com.example.models.RelationshipMixedBModel; -import com.example.models.SimpleExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import java.util.List; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class RelationshipMixedBTests { - @Test - public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void oneSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - save(new RelationshipMixedBModel(simple)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); - } - - @Test - public void twoSameSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - save(new RelationshipMixedBModel(simple)); - save(new RelationshipMixedBModel(simple)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipMixedBModel.class)); - } - - @Test - public void twoDifferentSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - SimpleExtendedModel another_simple = new SimpleExtendedModel(); - save(another_simple); - save(new RelationshipMixedBModel(simple)); - save(new RelationshipMixedBModel(another_simple)); - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipMixedBModel.class)); - } - - @Test - public void manySameSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - for (int i = 1; i <= 100; i++) { - save(new RelationshipMixedBModel(simple)); - } - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipMixedBModel.class)); - } - - @Test - public void manyDifferentSaveTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - save(new RelationshipMixedBModel(simple)); - } - assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipMixedBModel.class)); - } - - @Test - public void listAllSameTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - for (int i = 1; i <= 100; i++) { - save(new RelationshipMixedBModel(simple)); - } - List models = - SugarRecord.listAll(RelationshipMixedBModel.class); - assertEquals(100, models.size()); - for (RelationshipMixedBModel model : models) { - assertEquals(simple.getId(), model.getSimple().getId()); - } - } - - @Test - public void listAllDifferentTest() throws Exception { - for (int i = 1; i <= 100; i++) { - SimpleExtendedModel simple = new SimpleExtendedModel(); - save(simple); - save(new RelationshipMixedBModel(simple)); - } - List models = - SugarRecord.listAll(RelationshipMixedBModel.class); - assertEquals(100, models.size()); - for (RelationshipMixedBModel model : models) { - assertEquals(model.getId(), model.getSimple().getId()); - } - } -} diff --git a/example/src/test/java/com/example/sugartest/RobolectricGradleTestRunner.java b/example/src/test/java/com/example/sugartest/RobolectricGradleTestRunner.java deleted file mode 100644 index 7a94cc01..00000000 --- a/example/src/test/java/com/example/sugartest/RobolectricGradleTestRunner.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.example.sugartest; - -import com.example.ClientApp; - -import org.junit.runners.model.InitializationError; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.annotation.Config; -import org.robolectric.manifest.AndroidManifest; -import org.robolectric.res.Fs; - -public class RobolectricGradleTestRunner extends RobolectricTestRunner { - public RobolectricGradleTestRunner(Class testClass) throws InitializationError { - super(testClass); - } - - @Override - protected AndroidManifest getAppManifest(Config config) { - String myAppPath = ClientApp.class.getProtectionDomain().getCodeSource().getLocation().getPath(); - String manifestPath = myAppPath + "../../../../src/main/AndroidManifest.xml"; - String resPath = myAppPath + "../../../../src/main/res"; - String assetPath = myAppPath + "../../../../src/main/assets"; - String packageName = "com.example"; - return createAppManifest(Fs.fileFromPath(manifestPath), Fs.fileFromPath(resPath), Fs.fileFromPath(assetPath), packageName); - } -} \ No newline at end of file diff --git a/example/src/test/java/com/example/sugartest/ShortFieldTests.java b/example/src/test/java/com/example/sugartest/ShortFieldTests.java deleted file mode 100644 index 3c428752..00000000 --- a/example/src/test/java/com/example/sugartest/ShortFieldTests.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.example.sugartest; - - -import com.example.models.ShortFieldAnnotatedModel; -import com.example.models.ShortFieldExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class ShortFieldTests { - @Test - public void nullShortExtendedTest() { - save(new ShortFieldExtendedModel()); - ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); - assertNull(model.getShort()); - } - - @Test - public void nullRawShortExtendedTest() { - save(new ShortFieldExtendedModel()); - ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); - assertEquals((short) 0, model.getRawShort()); - } - - @Test - public void nullShortAnnotatedTest() { - save(new ShortFieldAnnotatedModel()); - ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); - assertNull(model.getShort()); - } - - @Test - public void nullRawShortAnnotatedTest() { - save(new ShortFieldAnnotatedModel()); - ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); - assertEquals((short) 0, model.getRawShort()); - } - - @Test - public void objectShortExtendedTest() { - Short objectShort = new Short((short) 25); - save(new ShortFieldExtendedModel(objectShort)); - ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); - assertEquals(objectShort, model.getShort()); - } - - @Test - public void rawShortExtendedTest() { - save(new ShortFieldExtendedModel((short) 25)); - ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); - assertEquals((short) 25, model.getRawShort()); - } - - @Test - public void objectShortAnnotatedTest() { - Short objectShort = new Short((short) 25); - save(new ShortFieldAnnotatedModel(objectShort)); - ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); - assertEquals(objectShort, model.getShort()); - } - - @Test - public void rawShortAnnotatedTest() { - save(new ShortFieldAnnotatedModel((short) 25)); - ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); - assertEquals((short) 25, model.getRawShort()); - } -} diff --git a/example/src/test/java/com/example/sugartest/SimpleAnnotatedModelTests.java b/example/src/test/java/com/example/sugartest/SimpleAnnotatedModelTests.java deleted file mode 100644 index 8436df41..00000000 --- a/example/src/test/java/com/example/sugartest/SimpleAnnotatedModelTests.java +++ /dev/null @@ -1,356 +0,0 @@ -package com.example.sugartest; - -import com.example.models.SimpleAnnotatedModel; -import com.orm.SugarRecord; -import com.orm.helper.NamingHelper; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.NoSuchElementException; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class SimpleAnnotatedModelTests { - @Test - public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void oneSaveTest() throws Exception { - save(new SimpleAnnotatedModel()); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void twoSaveTest() throws Exception { - save(new SimpleAnnotatedModel()); - save(new SimpleAnnotatedModel()); - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void manySaveTest() throws Exception { - for (int i = 1; i <= 100; i++) { - save(new SimpleAnnotatedModel()); - } - assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void defaultIdTest() throws Exception { - assertEquals(1L, save(new SimpleAnnotatedModel())); - } - - @Test - public void whereCountTest() throws Exception { - save(new SimpleAnnotatedModel()); - save(new SimpleAnnotatedModel()); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class, "id = ?", new String[]{"1"})); - } - - @Test - public void whereNoCountTest() throws Exception { - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class, "id = ?", new String[]{"1"})); - save(new SimpleAnnotatedModel()); - save(new SimpleAnnotatedModel()); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class, "id = ?", new String[]{"3"})); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class, "id = ?", new String[]{"a"})); - } - - @Test - public void whereBrokenCountTest() throws Exception { - save(new SimpleAnnotatedModel()); - save(new SimpleAnnotatedModel()); - assertEquals(-1L, SugarRecord.count(SimpleAnnotatedModel.class, "di = ?", new String[]{"1"})); - } - - @Test - public void deleteTest() throws Exception { - SimpleAnnotatedModel model = new SimpleAnnotatedModel(); - save(model); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertTrue(SugarRecord.delete(model)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void deleteUnsavedTest() throws Exception { - SimpleAnnotatedModel model = new SimpleAnnotatedModel(); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertFalse(SugarRecord.delete(model)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void deleteWrongTest() throws Exception { - SimpleAnnotatedModel model = new SimpleAnnotatedModel(); - save(model); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - Field idField = model.getClass().getDeclaredField("id"); - idField.setAccessible(true); - idField.set(model, Long.MAX_VALUE); - assertFalse(SugarRecord.delete(model)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void deleteAllTest() throws Exception { - int elementNumber = 100; - for (int i = 1; i <= elementNumber; i++) { - save(new SimpleAnnotatedModel()); - } - assertEquals(elementNumber, SugarRecord.deleteAll(SimpleAnnotatedModel.class)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void deleteAllWhereTest() throws Exception { - int elementNumber = 100; - for (int i = 1; i <= elementNumber; i++) { - save(new SimpleAnnotatedModel()); - } - assertEquals(elementNumber - 1, SugarRecord.deleteAll(SimpleAnnotatedModel.class, - "id > ?", - new String[]{"1"})); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void deleteInTransactionFewTest() throws Exception { - SimpleAnnotatedModel first = new SimpleAnnotatedModel(); - SimpleAnnotatedModel second = new SimpleAnnotatedModel(); - SimpleAnnotatedModel third = new SimpleAnnotatedModel(); - save(first); - save(second); - // Not saving last model - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(2, SugarRecord.deleteInTx(first, second, third)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void deleteInTransactionManyTest() throws Exception { - long elementNumber = 100; - List models = new ArrayList<>(); - for (int i = 1; i <= elementNumber; i++) { - SimpleAnnotatedModel model = new SimpleAnnotatedModel(); - models.add(model); - // Not saving last model - if (i < elementNumber) { - save(model); - } - } - assertEquals(elementNumber - 1, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(elementNumber - 1, SugarRecord.deleteInTx(models)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void saveInTransactionTest() throws Exception { - SugarRecord.saveInTx(new SimpleAnnotatedModel(), new SimpleAnnotatedModel()); - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); - } - - @Test - public void listAllTest() throws Exception { - for (int i = 1; i <= 100; i++) { - save(new SimpleAnnotatedModel()); - } - List models = SugarRecord.listAll(SimpleAnnotatedModel.class); - assertEquals(100, models.size()); - for (long i = 1; i <= 100; i++) { - assertEquals(new Long(i), models.get((int) i - 1).getId()); - } - } - - @Test - public void findTest() throws Exception { - save(new SimpleAnnotatedModel()); - save(new SimpleAnnotatedModel()); - List models = - SugarRecord.find(SimpleAnnotatedModel.class, "id = ?", "2"); - assertEquals(1, models.size()); - assertEquals(new Long(2L), models.get(0).getId()); - } - - @Test - public void findWithQueryTest() throws Exception { - for (int i = 1; i <= 100; i++) { - save(new SimpleAnnotatedModel()); - } - List models = - SugarRecord.findWithQuery(SimpleAnnotatedModel.class, "Select * from " + - NamingHelper.toTableName(SimpleAnnotatedModel.class) + - " where id >= ? ", "50"); - for (SimpleAnnotatedModel model : models) { - assertEquals(new Long(75), model.getId(), 25L); - } - } - - @Test - public void findByIdTest() throws Exception { - save(new SimpleAnnotatedModel()); - assertEquals(new Long(1L), SugarRecord.findById(SimpleAnnotatedModel.class, 1L).getId()); - } - - @Test - public void findByIdIntegerTest() throws Exception { - save(new SimpleAnnotatedModel()); - assertEquals(new Long(1L), SugarRecord.findById(SimpleAnnotatedModel.class, 1).getId()); - } - - @Test - public void findByIdStringsNullTest() throws Exception { - save(new SimpleAnnotatedModel()); - assertEquals(0, SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{""}).size()); - } - - @Test - public void findByIdStringsOneTest() throws Exception { - save(new SimpleAnnotatedModel()); - List models = - SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{"1"}); - assertEquals(1, models.size()); - assertEquals(new Long(1L), models.get(0).getId()); - } - - @Test - public void findByIdStringsTwoTest() throws Exception { - save(new SimpleAnnotatedModel()); - save(new SimpleAnnotatedModel()); - save(new SimpleAnnotatedModel()); - List models = - SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{"1", "3"}); - assertEquals(2, models.size()); - assertEquals(new Long(1L), models.get(0).getId()); - assertEquals(new Long(3L), models.get(1).getId()); - } - - @Test - public void findByIdStringsManyTest() throws Exception { - for (int i = 1; i <= 10; i++) { - save(new SimpleAnnotatedModel()); - } - List models = - SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{"1", "3", "6", "10"}); - assertEquals(4, models.size()); - assertEquals(new Long(1L), models.get(0).getId()); - assertEquals(new Long(3L), models.get(1).getId()); - assertEquals(new Long(6L), models.get(2).getId()); - assertEquals(new Long(10L), models.get(3).getId()); - } - - @Test - public void findByIdStringsOrderTest() throws Exception { - for (int i = 1; i <= 10; i++) { - save(new SimpleAnnotatedModel()); - } - List models = - SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{"10", "6", "3", "1"}); - assertEquals(4, models.size()); - // The order of the query doesn't matter - assertEquals(new Long(1L), models.get(0).getId()); - assertEquals(new Long(3L), models.get(1).getId()); - assertEquals(new Long(6L), models.get(2).getId()); - assertEquals(new Long(10L), models.get(3).getId()); - } - - @Test - public void findByIdNullTest() throws Exception { - save(new SimpleAnnotatedModel()); - assertNull(SugarRecord.findById(SimpleAnnotatedModel.class, 2L)); - } - - @Test - public void findAllTest() throws Exception { - for (int i = 1; i <= 100; i++) { - save(new SimpleAnnotatedModel()); - } - Iterator cursor = SugarRecord.findAll(SimpleAnnotatedModel.class); - for (int i = 1; i <= 100; i++) { - assertTrue(cursor.hasNext()); - SimpleAnnotatedModel model = cursor.next(); - assertNotNull(model); - assertEquals(new Long(i), model.getId()); - } - } - - @Test - public void findAsIteratorTest() throws Exception { - for (int i = 1; i <= 100; i++) { - save(new SimpleAnnotatedModel()); - } - Iterator cursor = SugarRecord.findAsIterator(SimpleAnnotatedModel.class, - "id >= ?", "50"); - for (int i = 50; i <= 100; i++) { - assertTrue(cursor.hasNext()); - SimpleAnnotatedModel model = cursor.next(); - assertNotNull(model); - assertEquals(new Long(i), model.getId()); - } - } - - @Test - public void findWithQueryAsIteratorTest() throws Exception { - for (int i = 1; i <= 100; i++) { - save(new SimpleAnnotatedModel()); - } - Iterator cursor = - SugarRecord.findWithQueryAsIterator(SimpleAnnotatedModel.class, - "Select * from " + - NamingHelper.toTableName(SimpleAnnotatedModel.class) + - " where id >= ? ", "50"); - for (int i = 50; i <= 100; i++) { - assertTrue(cursor.hasNext()); - SimpleAnnotatedModel model = cursor.next(); - assertNotNull(model); - assertEquals(new Long(i), model.getId()); - } - } - - @Test(expected=NoSuchElementException.class) - public void findAsIteratorOutOfBoundsTest() throws Exception { - save(new SimpleAnnotatedModel()); - Iterator cursor = SugarRecord.findAsIterator(SimpleAnnotatedModel.class, - "id = ?", "1"); - assertTrue(cursor.hasNext()); - SimpleAnnotatedModel model = cursor.next(); - assertNotNull(model); - assertEquals(new Long(1), model.getId()); - // This should throw a NoSuchElementException - cursor.next(); - } - - @Test(expected=UnsupportedOperationException.class) - public void disallowRemoveCursorTest() throws Exception { - save(new SimpleAnnotatedModel()); - Iterator cursor = SugarRecord.findAsIterator(SimpleAnnotatedModel.class, - "id = ?", "1"); - assertTrue(cursor.hasNext()); - SimpleAnnotatedModel model = cursor.next(); - assertNotNull(model); - assertEquals(new Long(1), model.getId()); - // This should throw a UnsupportedOperationException - cursor.remove(); - } - - @Test - public void vacuumTest() throws Exception { - SugarRecord.executeQuery("Vacuum"); - } -} \ No newline at end of file diff --git a/example/src/test/java/com/example/sugartest/SimpleExtendedModelTests.java b/example/src/test/java/com/example/sugartest/SimpleExtendedModelTests.java deleted file mode 100644 index 7abaf58a..00000000 --- a/example/src/test/java/com/example/sugartest/SimpleExtendedModelTests.java +++ /dev/null @@ -1,363 +0,0 @@ -package com.example.sugartest; - -import com.example.models.SimpleExtendedModel; -import com.orm.SugarRecord; -import com.orm.helper.NamingHelper; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.NoSuchElementException; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class SimpleExtendedModelTests { - @Test - public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void oneSaveTest() throws Exception { - save(new SimpleExtendedModel()); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void twoSaveTest() throws Exception { - save(new SimpleExtendedModel()); - save(new SimpleExtendedModel()); - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void manySaveTest() throws Exception { - for (int i = 1; i <= 100; i++) { - save(new SimpleExtendedModel()); - } - assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void defaultIdTest() throws Exception { - assertEquals(1L, save(new SimpleExtendedModel())); - } - - @Test - public void whereCountTest() throws Exception { - save(new SimpleExtendedModel()); - save(new SimpleExtendedModel()); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class, "id = ?", new String[]{"1"})); - } - - @Test - public void whereNoCountTest() throws Exception { - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class, "id = ?", new String[]{"1"})); - save(new SimpleExtendedModel()); - save(new SimpleExtendedModel()); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class, "id = ?", new String[]{"3"})); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class, "id = ?", new String[]{"a"})); - } - - @Test - public void whereBrokenCountTest() throws Exception { - save(new SimpleExtendedModel()); - save(new SimpleExtendedModel()); - assertEquals(-1L, SugarRecord.count(SimpleExtendedModel.class, "di = ?", new String[]{"1"})); - } - - @Test - public void saveMethodTest() throws Exception { - SimpleExtendedModel model = new SimpleExtendedModel(); - model.save(); - assertEquals(-1L, SugarRecord.count(SimpleExtendedModel.class, "di = ?", new String[]{"1"})); - } - - @Test - public void deleteTest() throws Exception { - SimpleExtendedModel model = new SimpleExtendedModel(); - save(model); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertTrue(SugarRecord.delete(model)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void deleteUnsavedTest() throws Exception { - SimpleExtendedModel model = new SimpleExtendedModel(); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); - assertFalse(SugarRecord.delete(model)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void deleteWrongTest() throws Exception { - SimpleExtendedModel model = new SimpleExtendedModel(); - save(model); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - Field idField = model.getClass().getSuperclass().getDeclaredField("id"); - idField.setAccessible(true); - idField.set(model, Long.MAX_VALUE); - assertFalse(SugarRecord.delete(model)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void deleteAllTest() throws Exception { - int elementNumber = 100; - for (int i = 1; i <= elementNumber; i++) { - save(new SimpleExtendedModel()); - } - assertEquals(elementNumber, SugarRecord.deleteAll(SimpleExtendedModel.class)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void deleteAllWhereTest() throws Exception { - int elementNumber = 100; - for (int i = 1; i <= elementNumber; i++) { - save(new SimpleExtendedModel()); - } - assertEquals(elementNumber - 1, SugarRecord.deleteAll(SimpleExtendedModel.class, - "id > ?", - new String[]{"1"})); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void deleteInTransactionFewTest() throws Exception { - SimpleExtendedModel first = new SimpleExtendedModel(); - SimpleExtendedModel second = new SimpleExtendedModel(); - SimpleExtendedModel third = new SimpleExtendedModel(); - save(first); - save(second); - // Not saving last model - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2, SugarRecord.deleteInTx(first, second, third)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void deleteInTransactionManyTest() throws Exception { - long elementNumber = 100; - List models = new ArrayList<>(); - for (int i = 1; i <= elementNumber; i++) { - SimpleExtendedModel model = new SimpleExtendedModel(); - models.add(model); - // Not saving last model - if (i < elementNumber) { - save(model); - } - } - assertEquals(elementNumber - 1, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(elementNumber - 1, SugarRecord.deleteInTx(models)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void saveInTransactionTest() throws Exception { - SugarRecord.saveInTx(new SimpleExtendedModel(), new SimpleExtendedModel()); - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); - } - - @Test - public void listAllTest() throws Exception { - for (int i = 1; i <= 100; i++) { - save(new SimpleExtendedModel()); - } - List models = SugarRecord.listAll(SimpleExtendedModel.class); - assertEquals(100, models.size()); - for (long i = 1; i <= 100; i++) { - assertEquals(new Long(i), models.get((int) i - 1).getId()); - } - } - - @Test - public void findTest() throws Exception { - save(new SimpleExtendedModel()); - save(new SimpleExtendedModel()); - List models = - SugarRecord.find(SimpleExtendedModel.class, "id = ?", "2"); - assertEquals(1, models.size()); - assertEquals(new Long(2L), models.get(0).getId()); - } - - @Test - public void findWithQueryTest() throws Exception { - for (int i = 1; i <= 100; i++) { - save(new SimpleExtendedModel()); - } - List models = - SugarRecord.findWithQuery(SimpleExtendedModel.class, "Select * from " + - NamingHelper.toTableName(SimpleExtendedModel.class) + - " where id >= ? ", "50"); - for (SimpleExtendedModel model : models) { - assertEquals(new Long(75), model.getId(), 25L); - } - } - - @Test - public void findByIdTest() throws Exception { - save(new SimpleExtendedModel()); - assertEquals(new Long(1L), SugarRecord.findById(SimpleExtendedModel.class, 1L).getId()); - } - - @Test - public void findByIdIntegerTest() throws Exception { - save(new SimpleExtendedModel()); - assertEquals(new Long(1L), SugarRecord.findById(SimpleExtendedModel.class, 1).getId()); - } - - @Test - public void findByIdStringsNullTest() throws Exception { - save(new SimpleExtendedModel()); - assertEquals(0, SugarRecord.findById(SimpleExtendedModel.class, new String[]{""}).size()); - } - - @Test - public void findByIdStringsOneTest() throws Exception { - save(new SimpleExtendedModel()); - List models = - SugarRecord.findById(SimpleExtendedModel.class, new String[]{"1"}); - assertEquals(1, models.size()); - assertEquals(new Long(1L), models.get(0).getId()); - } - - @Test - public void findByIdStringsTwoTest() throws Exception { - save(new SimpleExtendedModel()); - save(new SimpleExtendedModel()); - save(new SimpleExtendedModel()); - List models = - SugarRecord.findById(SimpleExtendedModel.class, new String[]{"1", "3"}); - assertEquals(2, models.size()); - assertEquals(new Long(1L), models.get(0).getId()); - assertEquals(new Long(3L), models.get(1).getId()); - } - - @Test - public void findByIdStringsManyTest() throws Exception { - for (int i = 1; i <= 10; i++) { - save(new SimpleExtendedModel()); - } - List models = - SugarRecord.findById(SimpleExtendedModel.class, new String[]{"1", "3", "6", "10"}); - assertEquals(4, models.size()); - assertEquals(new Long(1L), models.get(0).getId()); - assertEquals(new Long(3L), models.get(1).getId()); - assertEquals(new Long(6L), models.get(2).getId()); - assertEquals(new Long(10L), models.get(3).getId()); - } - - @Test - public void findByIdStringsOrderTest() throws Exception { - for (int i = 1; i <= 10; i++) { - save(new SimpleExtendedModel()); - } - List models = - SugarRecord.findById(SimpleExtendedModel.class, new String[]{"10", "6", "3", "1"}); - assertEquals(4, models.size()); - // The order of the query doesn't matter - assertEquals(new Long(1L), models.get(0).getId()); - assertEquals(new Long(3L), models.get(1).getId()); - assertEquals(new Long(6L), models.get(2).getId()); - assertEquals(new Long(10L), models.get(3).getId()); - } - - @Test - public void findByIdNullTest() throws Exception { - save(new SimpleExtendedModel()); - assertNull(SugarRecord.findById(SimpleExtendedModel.class, 2L)); - } - - @Test - public void findAllTest() throws Exception { - for (int i = 1; i <= 100; i++) { - save(new SimpleExtendedModel()); - } - Iterator cursor = SugarRecord.findAll(SimpleExtendedModel.class); - for (int i = 1; i <= 100; i++) { - assertTrue(cursor.hasNext()); - SimpleExtendedModel model = cursor.next(); - assertNotNull(model); - assertEquals(new Long(i), model.getId()); - } - } - - @Test - public void findAsIteratorTest() throws Exception { - for (int i = 1; i <= 100; i++) { - save(new SimpleExtendedModel()); - } - Iterator cursor = SugarRecord.findAsIterator(SimpleExtendedModel.class, - "id >= ?", "50"); - for (int i = 50; i <= 100; i++) { - assertTrue(cursor.hasNext()); - SimpleExtendedModel model = cursor.next(); - assertNotNull(model); - assertEquals(new Long(i), model.getId()); - } - } - - @Test - public void findWithQueryAsIteratorTest() throws Exception { - for (int i = 1; i <= 100; i++) { - save(new SimpleExtendedModel()); - } - Iterator cursor = - SugarRecord.findWithQueryAsIterator(SimpleExtendedModel.class, - "Select * from " + - NamingHelper.toTableName(SimpleExtendedModel.class) + - " where id >= ? ", "50"); - for (int i = 50; i <= 100; i++) { - assertTrue(cursor.hasNext()); - SimpleExtendedModel model = cursor.next(); - assertNotNull(model); - assertEquals(new Long(i), model.getId()); - } - } - - @Test(expected=NoSuchElementException.class) - public void findAsIteratorOutOfBoundsTest() throws Exception { - save(new SimpleExtendedModel()); - Iterator cursor = SugarRecord.findAsIterator(SimpleExtendedModel.class, - "id = ?", "1"); - assertTrue(cursor.hasNext()); - SimpleExtendedModel model = cursor.next(); - assertNotNull(model); - assertEquals(new Long(1), model.getId()); - // This should throw a NoSuchElementException - cursor.next(); - } - - @Test(expected=UnsupportedOperationException.class) - public void disallowRemoveCursorTest() throws Exception { - save(new SimpleExtendedModel()); - Iterator cursor = SugarRecord.findAsIterator(SimpleExtendedModel.class, - "id = ?", "1"); - assertTrue(cursor.hasNext()); - SimpleExtendedModel model = cursor.next(); - assertNotNull(model); - assertEquals(new Long(1), model.getId()); - // This should throw a UnsupportedOperationException - cursor.remove(); - } - - @Test - public void vacuumTest() throws Exception { - SugarRecord.executeQuery("Vacuum"); - } -} \ No newline at end of file diff --git a/example/src/test/java/com/example/sugartest/StringFieldTests.java b/example/src/test/java/com/example/sugartest/StringFieldTests.java deleted file mode 100644 index afecdf3d..00000000 --- a/example/src/test/java/com/example/sugartest/StringFieldTests.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.example.sugartest; - - -import com.example.models.StringFieldAnnotatedModel; -import com.example.models.StringFieldExtendedModel; -import com.orm.SugarRecord; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - -import static com.orm.SugarRecord.save; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class StringFieldTests { - @Test - public void nullStringExtendedTest() { - save(new StringFieldExtendedModel()); - StringFieldExtendedModel model = SugarRecord.findById(StringFieldExtendedModel.class, 1); - assertNull(model.getString()); - } - - @Test - public void nullStringAnnotatedTest() { - save(new StringFieldAnnotatedModel()); - StringFieldAnnotatedModel model = SugarRecord.findById(StringFieldAnnotatedModel.class, 1); - assertNull(model.getString()); - } - - @Test - public void stringExtendedTest() { - String string = "Test String"; - save(new StringFieldExtendedModel(string)); - StringFieldExtendedModel model = SugarRecord.findById(StringFieldExtendedModel.class, 1); - assertEquals(string, model.getString()); - } - - @Test - public void stringAnnotatedTest() { - String string = "Test String"; - save(new StringFieldAnnotatedModel(string)); - StringFieldAnnotatedModel model = SugarRecord.findById(StringFieldAnnotatedModel.class, 1); - assertEquals(string, model.getString()); - } -} diff --git a/library/src/test/java/com/orm/ClientApp.java b/library/src/test/java/com/orm/ClientApp.java deleted file mode 100644 index d9cd56bf..00000000 --- a/library/src/test/java/com/orm/ClientApp.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.orm; - -import android.app.Application; - -public class ClientApp extends Application { - - @Override - public void onCreate() { - super.onCreate(); - SugarContext.init(this); - } - - @Override - public void onTerminate() { - super.onTerminate(); - SugarContext.terminate(); - } -} diff --git a/library/src/test/java/com/orm/RobolectricGradleTestRunner.java b/library/src/test/java/com/orm/RobolectricGradleTestRunner.java deleted file mode 100644 index 6f2134b2..00000000 --- a/library/src/test/java/com/orm/RobolectricGradleTestRunner.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.orm; - -import org.junit.runners.model.InitializationError; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.annotation.Config; -import org.robolectric.manifest.AndroidManifest; -import org.robolectric.res.Fs; - -public class RobolectricGradleTestRunner extends RobolectricTestRunner { - - public RobolectricGradleTestRunner(Class testClass) throws InitializationError { - super(testClass); - } - - @Override - protected AndroidManifest getAppManifest(Config config) { - String myAppPath = ClientApp.class.getProtectionDomain().getCodeSource().getLocation().getPath(); - String manifestPath = myAppPath + "../../../../src/main/AndroidManifest.xml"; - String resPath = myAppPath + "../../../../src/main/res"; - String assetPath = myAppPath + "../../../../src/main/assets"; - String packageName = "com.orm.models"; - - return createAppManifest(Fs.fileFromPath(manifestPath), Fs.fileFromPath(resPath), Fs.fileFromPath(assetPath), packageName); - } -} \ No newline at end of file diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index 289f132c..9ef72cbc 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -1,29 +1,25 @@ package com.orm; -import com.orm.models.EmptyModel; -import com.orm.models.IntUniqueModel; -import com.orm.models.MultiColumnUniqueModel; -import com.orm.models.StringFieldAnnotatedModel; -import com.orm.models.StringFieldExtendedModel; -import com.orm.models.StringFieldExtendedModelAnnotatedColumn; +import com.orm.app.ClientApp; +import com.orm.dsl.BuildConfig; +import com.orm.model.EmptyModel; +import com.orm.model.IntUniqueModel; +import com.orm.model.MultiColumnUniqueModel; +import com.orm.model.StringFieldAnnotatedModel; +import com.orm.model.StringFieldExtendedModel; +import com.orm.model.StringFieldExtendedModelAnnotatedColumn; import com.orm.helper.NamingHelper; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static junit.framework.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk = 18, application = ClientApp.class, manifest = Config.NONE) -public class SchemaGeneratorTest { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class SchemaGeneratorTest { @Test public void testEmptyTableCreation() throws Exception { diff --git a/library/src/test/java/com/orm/SugarDbConfigurationTest.java b/library/src/test/java/com/orm/SugarDbConfigurationTest.java index 06ea6ae9..64d50df6 100644 --- a/library/src/test/java/com/orm/SugarDbConfigurationTest.java +++ b/library/src/test/java/com/orm/SugarDbConfigurationTest.java @@ -18,7 +18,7 @@ */ @RunWith(RobolectricGradleTestRunner.class) @Config(sdk = 16, constants = BuildConfig.class) -public class SugarDbConfigurationTest { +public final class SugarDbConfigurationTest { @Test public void testNotNullConfiguration() { diff --git a/library/src/test/java/com/orm/SugarDbTest.java b/library/src/test/java/com/orm/SugarDbTest.java index 482410b5..7ff5914a 100644 --- a/library/src/test/java/com/orm/SugarDbTest.java +++ b/library/src/test/java/com/orm/SugarDbTest.java @@ -2,42 +2,40 @@ import android.database.sqlite.SQLiteDatabase; -import org.junit.Assert; -import org.junit.Before; +import com.orm.app.ClientApp; +import com.orm.dsl.BuildConfig; + import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; +import static org.junit.Assert.assertEquals; + /** * @author jonatan.salas */ @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk = 18, application = ClientApp.class, manifest = Config.NONE) -public class SugarDbTest { +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class SugarDbTest { private final SugarDb sugarDb = SugarDb.getInstance(); - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } - @Test //TODO check this better! public void testGetReadableDatabase() { final SQLiteDatabase db = sugarDb.getReadableDatabase(); - Assert.assertEquals(false, db.isReadOnly()); + assertEquals(false, db.isReadOnly()); } @Test public void testGetWritableDatabase() { final SQLiteDatabase db = sugarDb.getWritableDatabase(); - Assert.assertEquals(false, db.isReadOnly()); + assertEquals(false, db.isReadOnly()); } @Test public void testGetDB() { final SQLiteDatabase db = sugarDb.getDB(); - Assert.assertEquals(false, db.isReadOnly()); + assertEquals(false, db.isReadOnly()); } } diff --git a/example/src/main/java/com/example/ClientApp.java b/library/src/test/java/com/orm/app/ClientApp.java similarity index 94% rename from example/src/main/java/com/example/ClientApp.java rename to library/src/test/java/com/orm/app/ClientApp.java index 6637ea26..e9943aea 100644 --- a/example/src/main/java/com/example/ClientApp.java +++ b/library/src/test/java/com/orm/app/ClientApp.java @@ -1,4 +1,4 @@ -package com.example; +package com.orm.app; import android.app.Application; diff --git a/library/src/test/java/com/orm/util/ManifestHelperTest.java b/library/src/test/java/com/orm/helper/ManifestHelperTest.java similarity index 50% rename from library/src/test/java/com/orm/util/ManifestHelperTest.java rename to library/src/test/java/com/orm/helper/ManifestHelperTest.java index 4340ac43..b8bfbbf9 100644 --- a/library/src/test/java/com/orm/util/ManifestHelperTest.java +++ b/library/src/test/java/com/orm/helper/ManifestHelperTest.java @@ -1,29 +1,28 @@ -package com.orm.util; +package com.orm.helper; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; +import com.orm.app.ClientApp; +import com.orm.dsl.BuildConfig; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; -import static org.junit.Assert.*; -import static com.orm.helper.ManifestHelper.*; -import static com.orm.SugarContext.init; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import static com.orm.helper.ManifestHelper.getDatabaseName; +import static com.orm.helper.ManifestHelper.getDatabaseVersion; +import static com.orm.helper.ManifestHelper.getDomainPackageName; +import static com.orm.helper.ManifestHelper.isDebugEnabled; +import static com.orm.helper.ManifestHelper.DATABASE_DEFAULT_NAME; /** * @author jonatan.salas */ @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk = 18, application = ClientApp.class, manifest = Config.NONE) -public class ManifestHelperTest { - - @Before - public void setUp() { - init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class ManifestHelperTest { @Test public void testGetDbName() { diff --git a/library/src/test/java/com/orm/NamingHelperTest.java b/library/src/test/java/com/orm/helper/NamingHelperTest.java similarity index 81% rename from library/src/test/java/com/orm/NamingHelperTest.java rename to library/src/test/java/com/orm/helper/NamingHelperTest.java index 550c9e31..54c3684c 100644 --- a/library/src/test/java/com/orm/NamingHelperTest.java +++ b/library/src/test/java/com/orm/helper/NamingHelperTest.java @@ -1,7 +1,6 @@ -package com.orm; +package com.orm.helper; -import com.orm.helper.NamingHelper; -import com.orm.models.TestRecord; +import com.orm.model.TestRecord; import com.orm.util.ReflectionUtil; import org.junit.Test; @@ -13,7 +12,11 @@ import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; -public class NamingHelperTest { +import static com.orm.helper.NamingHelper.toColumnName; +import static com.orm.helper.NamingHelper.toTableName; +import static com.orm.helper.NamingHelper.toSQLNameDefault; + +public final class NamingHelperTest { @Test public void testToSQLNameFromField() { @@ -23,7 +26,7 @@ public void testToSQLNameFromField() { List columnList = new ArrayList<>(); for(Field field: fieldList) { - columnList.add(NamingHelper.toColumnName(field)); + columnList.add(toColumnName(field)); } boolean isIdInList = inList(columnList, "ID"); @@ -46,7 +49,7 @@ private boolean inList(List list, String searchValue) { @Test public void testToSQLNameFromClass() { - assertEquals("TEST_RECORD", NamingHelper.toTableName(TestRecord.class)); + assertEquals("TEST_RECORD", toTableName(TestRecord.class)); } @Test @@ -73,7 +76,7 @@ public void testToSQLNameUnderscore() { * @param actual the expected UPPER_CASE_UNDER_SCORE string */ private static void assertToSqlNameEquals(String expected, String actual) { - assertEquals(expected, NamingHelper.toSQLNameDefault(actual)); + assertEquals(expected, toSQLNameDefault(actual)); } } diff --git a/library/src/test/java/com/orm/util/SugarTransactionHelperTest.java b/library/src/test/java/com/orm/helper/SugarTransactionHelperTest.java similarity index 84% rename from library/src/test/java/com/orm/util/SugarTransactionHelperTest.java rename to library/src/test/java/com/orm/helper/SugarTransactionHelperTest.java index 410be646..396b3887 100644 --- a/library/src/test/java/com/orm/util/SugarTransactionHelperTest.java +++ b/library/src/test/java/com/orm/helper/SugarTransactionHelperTest.java @@ -1,14 +1,15 @@ -package com.orm.util; +package com.orm.helper; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; +import com.orm.app.ClientApp; import com.orm.SugarContext; +import com.orm.dsl.BuildConfig; import com.orm.helper.SugarTransactionHelper; -import com.orm.models.TestRecord; +import com.orm.model.TestRecord; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; @@ -21,8 +22,8 @@ * @author jonatan.salas */ @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk = 18, application = ClientApp.class, manifest = Config.NONE) -public class SugarTransactionHelperTest { +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class SugarTransactionHelperTest { private List recordList = new ArrayList<>(); private TestRecord record1 = new TestRecord(); private TestRecord record2 = new TestRecord(); diff --git a/library/src/test/java/com/orm/models/BigDecimalFieldAnnotatedModel.java b/library/src/test/java/com/orm/model/BigDecimalFieldAnnotatedModel.java similarity index 94% rename from library/src/test/java/com/orm/models/BigDecimalFieldAnnotatedModel.java rename to library/src/test/java/com/orm/model/BigDecimalFieldAnnotatedModel.java index 8bab8c6d..f5529512 100644 --- a/library/src/test/java/com/orm/models/BigDecimalFieldAnnotatedModel.java +++ b/library/src/test/java/com/orm/model/BigDecimalFieldAnnotatedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/BigDecimalFieldExtendedModel.java b/library/src/test/java/com/orm/model/BigDecimalFieldExtendedModel.java similarity index 94% rename from library/src/test/java/com/orm/models/BigDecimalFieldExtendedModel.java rename to library/src/test/java/com/orm/model/BigDecimalFieldExtendedModel.java index 958ce330..cbcdc3c9 100644 --- a/library/src/test/java/com/orm/models/BigDecimalFieldExtendedModel.java +++ b/library/src/test/java/com/orm/model/BigDecimalFieldExtendedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/BooleanFieldAnnotatedModel.java b/library/src/test/java/com/orm/model/BooleanFieldAnnotatedModel.java similarity index 96% rename from library/src/test/java/com/orm/models/BooleanFieldAnnotatedModel.java rename to library/src/test/java/com/orm/model/BooleanFieldAnnotatedModel.java index 11bc661b..31a8668b 100644 --- a/library/src/test/java/com/orm/models/BooleanFieldAnnotatedModel.java +++ b/library/src/test/java/com/orm/model/BooleanFieldAnnotatedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/BooleanFieldExtendedModel.java b/library/src/test/java/com/orm/model/BooleanFieldExtendedModel.java similarity index 95% rename from library/src/test/java/com/orm/models/BooleanFieldExtendedModel.java rename to library/src/test/java/com/orm/model/BooleanFieldExtendedModel.java index 25ce58b9..0c78c82a 100644 --- a/library/src/test/java/com/orm/models/BooleanFieldExtendedModel.java +++ b/library/src/test/java/com/orm/model/BooleanFieldExtendedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/ByteArrayAnnotatedModel.java b/library/src/test/java/com/orm/model/ByteArrayAnnotatedModel.java similarity index 94% rename from library/src/test/java/com/orm/models/ByteArrayAnnotatedModel.java rename to library/src/test/java/com/orm/model/ByteArrayAnnotatedModel.java index 99256e95..4a9f809c 100644 --- a/library/src/test/java/com/orm/models/ByteArrayAnnotatedModel.java +++ b/library/src/test/java/com/orm/model/ByteArrayAnnotatedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/ByteArrayExtendedModel.java b/library/src/test/java/com/orm/model/ByteArrayExtendedModel.java similarity index 93% rename from library/src/test/java/com/orm/models/ByteArrayExtendedModel.java rename to library/src/test/java/com/orm/model/ByteArrayExtendedModel.java index 0cb63964..7e40208d 100644 --- a/library/src/test/java/com/orm/models/ByteArrayExtendedModel.java +++ b/library/src/test/java/com/orm/model/ByteArrayExtendedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/DoubleFieldAnnotatedModel.java b/library/src/test/java/com/orm/model/DoubleFieldAnnotatedModel.java similarity index 95% rename from library/src/test/java/com/orm/models/DoubleFieldAnnotatedModel.java rename to library/src/test/java/com/orm/model/DoubleFieldAnnotatedModel.java index 1c933cbf..2c13e075 100644 --- a/library/src/test/java/com/orm/models/DoubleFieldAnnotatedModel.java +++ b/library/src/test/java/com/orm/model/DoubleFieldAnnotatedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/DoubleFieldExtendedModel.java b/library/src/test/java/com/orm/model/DoubleFieldExtendedModel.java similarity index 95% rename from library/src/test/java/com/orm/models/DoubleFieldExtendedModel.java rename to library/src/test/java/com/orm/model/DoubleFieldExtendedModel.java index 1a1834b3..1a000c3c 100644 --- a/library/src/test/java/com/orm/models/DoubleFieldExtendedModel.java +++ b/library/src/test/java/com/orm/model/DoubleFieldExtendedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/EmptyModel.java b/library/src/test/java/com/orm/model/EmptyModel.java similarity index 86% rename from library/src/test/java/com/orm/models/EmptyModel.java rename to library/src/test/java/com/orm/model/EmptyModel.java index 73459b51..a98ab5e2 100644 --- a/library/src/test/java/com/orm/models/EmptyModel.java +++ b/library/src/test/java/com/orm/model/EmptyModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/EnumFieldAnnotatedModel.java b/library/src/test/java/com/orm/model/EnumFieldAnnotatedModel.java similarity index 97% rename from library/src/test/java/com/orm/models/EnumFieldAnnotatedModel.java rename to library/src/test/java/com/orm/model/EnumFieldAnnotatedModel.java index 5ea90026..abb3a63f 100644 --- a/library/src/test/java/com/orm/models/EnumFieldAnnotatedModel.java +++ b/library/src/test/java/com/orm/model/EnumFieldAnnotatedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/EnumFieldExtendedModel.java b/library/src/test/java/com/orm/model/EnumFieldExtendedModel.java similarity index 97% rename from library/src/test/java/com/orm/models/EnumFieldExtendedModel.java rename to library/src/test/java/com/orm/model/EnumFieldExtendedModel.java index 52cce942..24ec7725 100644 --- a/library/src/test/java/com/orm/models/EnumFieldExtendedModel.java +++ b/library/src/test/java/com/orm/model/EnumFieldExtendedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/FloatFieldAnnotatedModel.java b/library/src/test/java/com/orm/model/FloatFieldAnnotatedModel.java similarity index 95% rename from library/src/test/java/com/orm/models/FloatFieldAnnotatedModel.java rename to library/src/test/java/com/orm/model/FloatFieldAnnotatedModel.java index 34a55794..be2f01f5 100644 --- a/library/src/test/java/com/orm/models/FloatFieldAnnotatedModel.java +++ b/library/src/test/java/com/orm/model/FloatFieldAnnotatedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/FloatFieldExtendedModel.java b/library/src/test/java/com/orm/model/FloatFieldExtendedModel.java similarity index 95% rename from library/src/test/java/com/orm/models/FloatFieldExtendedModel.java rename to library/src/test/java/com/orm/model/FloatFieldExtendedModel.java index 2f3311d2..e79f1a4b 100644 --- a/library/src/test/java/com/orm/models/FloatFieldExtendedModel.java +++ b/library/src/test/java/com/orm/model/FloatFieldExtendedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/IncompleteAnnotatedModel.java b/library/src/test/java/com/orm/model/IncompleteAnnotatedModel.java similarity index 89% rename from library/src/test/java/com/orm/models/IncompleteAnnotatedModel.java rename to library/src/test/java/com/orm/model/IncompleteAnnotatedModel.java index 55b5c52f..64f8436b 100644 --- a/library/src/test/java/com/orm/models/IncompleteAnnotatedModel.java +++ b/library/src/test/java/com/orm/model/IncompleteAnnotatedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/IntUniqueModel.java b/library/src/test/java/com/orm/model/IntUniqueModel.java similarity index 92% rename from library/src/test/java/com/orm/models/IntUniqueModel.java rename to library/src/test/java/com/orm/model/IntUniqueModel.java index 6c9cdfdd..7a03e3d0 100644 --- a/library/src/test/java/com/orm/models/IntUniqueModel.java +++ b/library/src/test/java/com/orm/model/IntUniqueModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; import com.orm.annotation.Unique; diff --git a/library/src/test/java/com/orm/models/IntegerFieldAnnotatedModel.java b/library/src/test/java/com/orm/model/IntegerFieldAnnotatedModel.java similarity index 95% rename from library/src/test/java/com/orm/models/IntegerFieldAnnotatedModel.java rename to library/src/test/java/com/orm/model/IntegerFieldAnnotatedModel.java index c9598436..e486d5db 100644 --- a/library/src/test/java/com/orm/models/IntegerFieldAnnotatedModel.java +++ b/library/src/test/java/com/orm/model/IntegerFieldAnnotatedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/IntegerFieldExtendedModel.java b/library/src/test/java/com/orm/model/IntegerFieldExtendedModel.java similarity index 95% rename from library/src/test/java/com/orm/models/IntegerFieldExtendedModel.java rename to library/src/test/java/com/orm/model/IntegerFieldExtendedModel.java index d6f30553..407979a3 100644 --- a/library/src/test/java/com/orm/models/IntegerFieldExtendedModel.java +++ b/library/src/test/java/com/orm/model/IntegerFieldExtendedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/LongFieldAnnotatedModel.java b/library/src/test/java/com/orm/model/LongFieldAnnotatedModel.java similarity index 95% rename from library/src/test/java/com/orm/models/LongFieldAnnotatedModel.java rename to library/src/test/java/com/orm/model/LongFieldAnnotatedModel.java index b6bb0104..7f2cd539 100644 --- a/library/src/test/java/com/orm/models/LongFieldAnnotatedModel.java +++ b/library/src/test/java/com/orm/model/LongFieldAnnotatedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/LongFieldExtendedModel.java b/library/src/test/java/com/orm/model/LongFieldExtendedModel.java similarity index 95% rename from library/src/test/java/com/orm/models/LongFieldExtendedModel.java rename to library/src/test/java/com/orm/model/LongFieldExtendedModel.java index 4769ac3b..66bc7d6d 100644 --- a/library/src/test/java/com/orm/models/LongFieldExtendedModel.java +++ b/library/src/test/java/com/orm/model/LongFieldExtendedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/MultiColumnUniqueModel.java b/library/src/test/java/com/orm/model/MultiColumnUniqueModel.java similarity index 93% rename from library/src/test/java/com/orm/models/MultiColumnUniqueModel.java rename to library/src/test/java/com/orm/model/MultiColumnUniqueModel.java index 7057b2d6..cea9cd92 100644 --- a/library/src/test/java/com/orm/models/MultiColumnUniqueModel.java +++ b/library/src/test/java/com/orm/model/MultiColumnUniqueModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; import com.orm.annotation.MultiUnique; diff --git a/library/src/test/java/com/orm/models/NestedAnnotatedModel.java b/library/src/test/java/com/orm/model/NestedAnnotatedModel.java similarity index 94% rename from library/src/test/java/com/orm/models/NestedAnnotatedModel.java rename to library/src/test/java/com/orm/model/NestedAnnotatedModel.java index f39f20c5..f57b2d7a 100644 --- a/library/src/test/java/com/orm/models/NestedAnnotatedModel.java +++ b/library/src/test/java/com/orm/model/NestedAnnotatedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/NestedExtendedModel.java b/library/src/test/java/com/orm/model/NestedExtendedModel.java similarity index 93% rename from library/src/test/java/com/orm/models/NestedExtendedModel.java rename to library/src/test/java/com/orm/model/NestedExtendedModel.java index 66ae8900..0b583d95 100644 --- a/library/src/test/java/com/orm/models/NestedExtendedModel.java +++ b/library/src/test/java/com/orm/model/NestedExtendedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/NestedMixedAAModel.java b/library/src/test/java/com/orm/model/NestedMixedAAModel.java similarity index 93% rename from library/src/test/java/com/orm/models/NestedMixedAAModel.java rename to library/src/test/java/com/orm/model/NestedMixedAAModel.java index 53884b0e..a96500f5 100644 --- a/library/src/test/java/com/orm/models/NestedMixedAAModel.java +++ b/library/src/test/java/com/orm/model/NestedMixedAAModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/NestedMixedABModel.java b/library/src/test/java/com/orm/model/NestedMixedABModel.java similarity index 93% rename from library/src/test/java/com/orm/models/NestedMixedABModel.java rename to library/src/test/java/com/orm/model/NestedMixedABModel.java index dc75faab..2cbae3db 100644 --- a/library/src/test/java/com/orm/models/NestedMixedABModel.java +++ b/library/src/test/java/com/orm/model/NestedMixedABModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/NestedMixedBAModel.java b/library/src/test/java/com/orm/model/NestedMixedBAModel.java similarity index 94% rename from library/src/test/java/com/orm/models/NestedMixedBAModel.java rename to library/src/test/java/com/orm/model/NestedMixedBAModel.java index 43628978..82de9717 100644 --- a/library/src/test/java/com/orm/models/NestedMixedBAModel.java +++ b/library/src/test/java/com/orm/model/NestedMixedBAModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/NestedMixedBBModel.java b/library/src/test/java/com/orm/model/NestedMixedBBModel.java similarity index 94% rename from library/src/test/java/com/orm/models/NestedMixedBBModel.java rename to library/src/test/java/com/orm/model/NestedMixedBBModel.java index 6c32109e..6a208b5b 100644 --- a/library/src/test/java/com/orm/models/NestedMixedBBModel.java +++ b/library/src/test/java/com/orm/model/NestedMixedBBModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/NoSugarModel.java b/library/src/test/java/com/orm/model/NoSugarModel.java similarity index 73% rename from library/src/test/java/com/orm/models/NoSugarModel.java rename to library/src/test/java/com/orm/model/NoSugarModel.java index 6641126c..617a99c4 100644 --- a/library/src/test/java/com/orm/models/NoSugarModel.java +++ b/library/src/test/java/com/orm/model/NoSugarModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; public class NoSugarModel { diff --git a/library/src/test/java/com/orm/models/RelationshipAnnotatedModel.java b/library/src/test/java/com/orm/model/RelationshipAnnotatedModel.java similarity index 94% rename from library/src/test/java/com/orm/models/RelationshipAnnotatedModel.java rename to library/src/test/java/com/orm/model/RelationshipAnnotatedModel.java index d4875a98..42b9a4d9 100644 --- a/library/src/test/java/com/orm/models/RelationshipAnnotatedModel.java +++ b/library/src/test/java/com/orm/model/RelationshipAnnotatedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/RelationshipExtendedModel.java b/library/src/test/java/com/orm/model/RelationshipExtendedModel.java similarity index 93% rename from library/src/test/java/com/orm/models/RelationshipExtendedModel.java rename to library/src/test/java/com/orm/model/RelationshipExtendedModel.java index 491dd823..1e4e99bc 100644 --- a/library/src/test/java/com/orm/models/RelationshipExtendedModel.java +++ b/library/src/test/java/com/orm/model/RelationshipExtendedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/RelationshipMixedAModel.java b/library/src/test/java/com/orm/model/RelationshipMixedAModel.java similarity index 93% rename from library/src/test/java/com/orm/models/RelationshipMixedAModel.java rename to library/src/test/java/com/orm/model/RelationshipMixedAModel.java index fbbbc103..1afa4a82 100644 --- a/library/src/test/java/com/orm/models/RelationshipMixedAModel.java +++ b/library/src/test/java/com/orm/model/RelationshipMixedAModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/RelationshipMixedBModel.java b/library/src/test/java/com/orm/model/RelationshipMixedBModel.java similarity index 94% rename from library/src/test/java/com/orm/models/RelationshipMixedBModel.java rename to library/src/test/java/com/orm/model/RelationshipMixedBModel.java index c302f85a..2197e62f 100644 --- a/library/src/test/java/com/orm/models/RelationshipMixedBModel.java +++ b/library/src/test/java/com/orm/model/RelationshipMixedBModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/ShortFieldAnnotatedModel.java b/library/src/test/java/com/orm/model/ShortFieldAnnotatedModel.java similarity index 95% rename from library/src/test/java/com/orm/models/ShortFieldAnnotatedModel.java rename to library/src/test/java/com/orm/model/ShortFieldAnnotatedModel.java index f6a0cc45..fea8e58e 100644 --- a/library/src/test/java/com/orm/models/ShortFieldAnnotatedModel.java +++ b/library/src/test/java/com/orm/model/ShortFieldAnnotatedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/ShortFieldExtendedModel.java b/library/src/test/java/com/orm/model/ShortFieldExtendedModel.java similarity index 95% rename from library/src/test/java/com/orm/models/ShortFieldExtendedModel.java rename to library/src/test/java/com/orm/model/ShortFieldExtendedModel.java index c7df3a59..5e90b9f6 100644 --- a/library/src/test/java/com/orm/models/ShortFieldExtendedModel.java +++ b/library/src/test/java/com/orm/model/ShortFieldExtendedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/SimpleAnnotatedModel.java b/library/src/test/java/com/orm/model/SimpleAnnotatedModel.java similarity index 88% rename from library/src/test/java/com/orm/models/SimpleAnnotatedModel.java rename to library/src/test/java/com/orm/model/SimpleAnnotatedModel.java index 8a55e0bb..918840ed 100644 --- a/library/src/test/java/com/orm/models/SimpleAnnotatedModel.java +++ b/library/src/test/java/com/orm/model/SimpleAnnotatedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/SimpleExtendedModel.java b/library/src/test/java/com/orm/model/SimpleExtendedModel.java similarity index 83% rename from library/src/test/java/com/orm/models/SimpleExtendedModel.java rename to library/src/test/java/com/orm/model/SimpleExtendedModel.java index 65edf2c5..9040145d 100644 --- a/library/src/test/java/com/orm/models/SimpleExtendedModel.java +++ b/library/src/test/java/com/orm/model/SimpleExtendedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/SimpleModel.java b/library/src/test/java/com/orm/model/SimpleModel.java similarity index 95% rename from library/src/test/java/com/orm/models/SimpleModel.java rename to library/src/test/java/com/orm/model/SimpleModel.java index bdf26167..b2659084 100644 --- a/library/src/test/java/com/orm/models/SimpleModel.java +++ b/library/src/test/java/com/orm/model/SimpleModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java b/library/src/test/java/com/orm/model/StringFieldAnnotatedModel.java similarity index 94% rename from library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java rename to library/src/test/java/com/orm/model/StringFieldAnnotatedModel.java index 9526a515..e3220278 100644 --- a/library/src/test/java/com/orm/models/StringFieldAnnotatedModel.java +++ b/library/src/test/java/com/orm/model/StringFieldAnnotatedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/StringFieldAnnotatedNoIdModel.java b/library/src/test/java/com/orm/model/StringFieldAnnotatedNoIdModel.java similarity index 94% rename from library/src/test/java/com/orm/models/StringFieldAnnotatedNoIdModel.java rename to library/src/test/java/com/orm/model/StringFieldAnnotatedNoIdModel.java index feb91920..4b458960 100644 --- a/library/src/test/java/com/orm/models/StringFieldAnnotatedNoIdModel.java +++ b/library/src/test/java/com/orm/model/StringFieldAnnotatedNoIdModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.annotation.Table; diff --git a/library/src/test/java/com/orm/models/StringFieldExtendedModel.java b/library/src/test/java/com/orm/model/StringFieldExtendedModel.java similarity index 94% rename from library/src/test/java/com/orm/models/StringFieldExtendedModel.java rename to library/src/test/java/com/orm/model/StringFieldExtendedModel.java index 309c67b9..9b79bc61 100644 --- a/library/src/test/java/com/orm/models/StringFieldExtendedModel.java +++ b/library/src/test/java/com/orm/model/StringFieldExtendedModel.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java b/library/src/test/java/com/orm/model/StringFieldExtendedModelAnnotatedColumn.java similarity index 94% rename from library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java rename to library/src/test/java/com/orm/model/StringFieldExtendedModelAnnotatedColumn.java index 16c25e22..674d53ba 100644 --- a/library/src/test/java/com/orm/models/StringFieldExtendedModelAnnotatedColumn.java +++ b/library/src/test/java/com/orm/model/StringFieldExtendedModelAnnotatedColumn.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; import com.orm.annotation.Column; diff --git a/library/src/test/java/com/orm/models/TestRecord.java b/library/src/test/java/com/orm/model/TestRecord.java similarity index 92% rename from library/src/test/java/com/orm/models/TestRecord.java rename to library/src/test/java/com/orm/model/TestRecord.java index 72ab8d24..64fe7dba 100644 --- a/library/src/test/java/com/orm/models/TestRecord.java +++ b/library/src/test/java/com/orm/model/TestRecord.java @@ -1,4 +1,4 @@ -package com.orm.models; +package com.orm.model; import com.orm.SugarRecord; diff --git a/library/src/test/java/com/orm/query/QueryBuilderTests.java b/library/src/test/java/com/orm/query/QueryBuilderTests.java index d43ca5ee..30b220df 100644 --- a/library/src/test/java/com/orm/query/QueryBuilderTests.java +++ b/library/src/test/java/com/orm/query/QueryBuilderTests.java @@ -6,7 +6,7 @@ import static junit.framework.Assert.assertEquals; -public class QueryBuilderTests { +public final class QueryBuilderTests { @Test(expected=RuntimeException.class) public void noArgumentsTest() { diff --git a/library/src/test/java/com/orm/query/SelectTest.java b/library/src/test/java/com/orm/query/SelectTest.java index 734cca12..ad0393bd 100644 --- a/library/src/test/java/com/orm/query/SelectTest.java +++ b/library/src/test/java/com/orm/query/SelectTest.java @@ -1,12 +1,19 @@ package com.orm.query; -import com.orm.models.TestRecord; +import com.orm.app.ClientApp; +import com.orm.dsl.BuildConfig; +import com.orm.model.TestRecord; import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.annotation.Config; import static junit.framework.Assert.assertEquals; -public class SelectTest { +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class SelectTest { @Test public void testMergeCondition(){ @@ -22,7 +29,6 @@ public void testMergeCondition(){ assertEquals("2", where.getArgs()[1]); } - @Test public void testWhere(){ Select where = Select.from(TestRecord.class).where(Condition.prop("test").eq("satya")); diff --git a/library/src/test/java/com/orm/record/BigDecimalFieldTests.java b/library/src/test/java/com/orm/record/BigDecimalFieldTests.java index ed2bddc9..a877a6af 100644 --- a/library/src/test/java/com/orm/record/BigDecimalFieldTests.java +++ b/library/src/test/java/com/orm/record/BigDecimalFieldTests.java @@ -1,63 +1,59 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; -import com.orm.SugarRecord; -import com.orm.models.BigDecimalFieldAnnotatedModel; -import com.orm.models.BigDecimalFieldExtendedModel; - -import org.junit.Before; +import com.orm.app.ClientApp; +import com.orm.dsl.BuildConfig; +import com.orm.model.BigDecimalFieldAnnotatedModel; +import com.orm.model.BigDecimalFieldExtendedModel; + +import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import java.math.BigDecimal; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.findById; + import static org.junit.Assert.assertNull; import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class BigDecimalFieldTests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class BigDecimalFieldTests { + private BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); @Test public void nullBigDecimalExtendedTest() { save(new BigDecimalFieldExtendedModel()); - BigDecimalFieldExtendedModel model = - SugarRecord.findById(BigDecimalFieldExtendedModel.class, 1); + BigDecimalFieldExtendedModel model = findById(BigDecimalFieldExtendedModel.class, 1); assertNull(model.getBigDecimal()); } @Test public void nullBigDecimalAnnotatedTest() { save(new BigDecimalFieldAnnotatedModel()); - BigDecimalFieldAnnotatedModel model = - SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); + BigDecimalFieldAnnotatedModel model = findById(BigDecimalFieldAnnotatedModel.class, 1); assertNull(model.getBigDecimal()); } @Test public void bigDecimalExtendedTest() { - BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); save(new BigDecimalFieldExtendedModel(decimal)); - BigDecimalFieldExtendedModel model = SugarRecord.findById(BigDecimalFieldExtendedModel.class, 1); + BigDecimalFieldExtendedModel model = findById(BigDecimalFieldExtendedModel.class, 1); assertEquals(decimal, model.getBigDecimal()); } @Test public void bigDecimalAnnotatedTest() { - BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); save(new BigDecimalFieldAnnotatedModel(decimal)); - BigDecimalFieldAnnotatedModel model = - SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); + BigDecimalFieldAnnotatedModel model = findById(BigDecimalFieldAnnotatedModel.class, 1); assertEquals(decimal, model.getBigDecimal()); } + + @After + public void after() { + decimal = null; + } } diff --git a/library/src/test/java/com/orm/record/BooleanFieldTests.java b/library/src/test/java/com/orm/record/BooleanFieldTests.java index 4038adf4..2966aedd 100644 --- a/library/src/test/java/com/orm/record/BooleanFieldTests.java +++ b/library/src/test/java/com/orm/record/BooleanFieldTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.BooleanFieldAnnotatedModel; -import com.orm.models.BooleanFieldExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.BooleanFieldAnnotatedModel; +import com.orm.model.BooleanFieldExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; @@ -18,13 +16,8 @@ import static org.junit.Assert.assertNull; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class BooleanFieldTests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class BooleanFieldTests { @Test public void nullBooleanExtendedTest() { diff --git a/library/src/test/java/com/orm/record/ByteArrayFieldTests.java b/library/src/test/java/com/orm/record/ByteArrayFieldTests.java index fe3e7e92..73fdd6da 100644 --- a/library/src/test/java/com/orm/record/ByteArrayFieldTests.java +++ b/library/src/test/java/com/orm/record/ByteArrayFieldTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.ByteArrayAnnotatedModel; -import com.orm.models.ByteArrayExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.ByteArrayAnnotatedModel; +import com.orm.model.ByteArrayExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; @@ -18,14 +16,8 @@ import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class ByteArrayFieldTests { - - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class ByteArrayFieldTests { @Test public void nullByteArrayExtendedTest() { diff --git a/library/src/test/java/com/orm/record/CursorTests.java b/library/src/test/java/com/orm/record/CursorTests.java index a698b69b..a6036a15 100644 --- a/library/src/test/java/com/orm/record/CursorTests.java +++ b/library/src/test/java/com/orm/record/CursorTests.java @@ -9,16 +9,15 @@ import android.widget.CursorAdapter; import android.widget.TextView; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; -import com.orm.models.SimpleModel; +import com.orm.app.ClientApp; +import com.orm.dsl.BuildConfig; +import com.orm.model.SimpleModel; import com.orm.query.Select; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; @@ -27,13 +26,8 @@ import static junit.framework.Assert.assertSame; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class CursorTests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class CursorTests { @Test public void testColumnNames() { diff --git a/library/src/test/java/com/orm/record/DoubleFieldTests.java b/library/src/test/java/com/orm/record/DoubleFieldTests.java index f5d9ce36..96ea7f48 100644 --- a/library/src/test/java/com/orm/record/DoubleFieldTests.java +++ b/library/src/test/java/com/orm/record/DoubleFieldTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.DoubleFieldAnnotatedModel; -import com.orm.models.DoubleFieldExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.DoubleFieldAnnotatedModel; +import com.orm.model.DoubleFieldExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; @@ -18,13 +16,8 @@ import static org.junit.Assert.assertNull; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class, manifest = Config.NONE) -public class DoubleFieldTests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class DoubleFieldTests { @Test public void nullDoubleExtendedTest() { diff --git a/library/src/test/java/com/orm/record/EnumFieldTests.java b/library/src/test/java/com/orm/record/EnumFieldTests.java index bbceb2c7..d41d4025 100644 --- a/library/src/test/java/com/orm/record/EnumFieldTests.java +++ b/library/src/test/java/com/orm/record/EnumFieldTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.EnumFieldAnnotatedModel; -import com.orm.models.EnumFieldExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.EnumFieldAnnotatedModel; +import com.orm.model.EnumFieldExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; @@ -19,13 +17,8 @@ import static org.junit.Assert.assertNull; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk = 18, application = ClientApp.class) -public class EnumFieldTests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class EnumFieldTests { @Test public void nullDefaultEnumExtendedTest() { diff --git a/library/src/test/java/com/orm/record/FirstAndLastTests.java b/library/src/test/java/com/orm/record/FirstAndLastTests.java index 8c8e2d8d..278b82f0 100644 --- a/library/src/test/java/com/orm/record/FirstAndLastTests.java +++ b/library/src/test/java/com/orm/record/FirstAndLastTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.FloatFieldAnnotatedModel; -import com.orm.models.FloatFieldExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.FloatFieldAnnotatedModel; +import com.orm.model.FloatFieldExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; @@ -18,14 +16,9 @@ import static org.junit.Assert.assertNull; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public class FirstAndLastTests { - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } - @Test @SuppressWarnings("all") public void firstExtendedTest() { diff --git a/library/src/test/java/com/orm/record/FloatFieldTests.java b/library/src/test/java/com/orm/record/FloatFieldTests.java index f6016690..48317fcf 100644 --- a/library/src/test/java/com/orm/record/FloatFieldTests.java +++ b/library/src/test/java/com/orm/record/FloatFieldTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.FloatFieldAnnotatedModel; -import com.orm.models.FloatFieldExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.FloatFieldAnnotatedModel; +import com.orm.model.FloatFieldExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; @@ -18,13 +16,8 @@ import static org.junit.Assert.assertNull; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class FloatFieldTests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class FloatFieldTests { @Test public void nullFloatExtendedTest() { diff --git a/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java b/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java index d421f60a..9834829f 100644 --- a/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java +++ b/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java @@ -2,15 +2,13 @@ import android.database.sqlite.SQLiteException; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; -import com.orm.models.IncompleteAnnotatedModel; +import com.orm.app.ClientApp; +import com.orm.dsl.BuildConfig; +import com.orm.model.IncompleteAnnotatedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static com.orm.SugarRecord.delete; @@ -18,15 +16,10 @@ import static org.junit.Assert.assertFalse; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class IncompleteAnnotatedModelTests { +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class IncompleteAnnotatedModelTests { - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } - - @Test(expected=SQLiteException.class) + @Test(expected = SQLiteException.class) public void saveNoIdFieldTest() { save(new IncompleteAnnotatedModel()); } diff --git a/library/src/test/java/com/orm/record/IntegerFieldTests.java b/library/src/test/java/com/orm/record/IntegerFieldTests.java index fca422c0..b9ebf11d 100644 --- a/library/src/test/java/com/orm/record/IntegerFieldTests.java +++ b/library/src/test/java/com/orm/record/IntegerFieldTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.IntegerFieldAnnotatedModel; -import com.orm.models.IntegerFieldExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.IntegerFieldAnnotatedModel; +import com.orm.model.IntegerFieldExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; @@ -18,14 +16,9 @@ import static org.junit.Assert.assertNull; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public class IntegerFieldTests { - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } - @Test public void nullIntegerExtendedTest() { save(new IntegerFieldExtendedModel()); diff --git a/library/src/test/java/com/orm/record/ListAllOrderByTests.java b/library/src/test/java/com/orm/record/ListAllOrderByTests.java index e401bd6b..c5f34916 100644 --- a/library/src/test/java/com/orm/record/ListAllOrderByTests.java +++ b/library/src/test/java/com/orm/record/ListAllOrderByTests.java @@ -1,15 +1,13 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.IntegerFieldExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.IntegerFieldExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import java.util.List; @@ -19,14 +17,8 @@ import static org.junit.Assert.assertTrue; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class ListAllOrderByTests { - - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class ListAllOrderByTests { @Test public void listAllOrderByEmptyTest() { diff --git a/library/src/test/java/com/orm/record/LongFieldTests.java b/library/src/test/java/com/orm/record/LongFieldTests.java index fe6001ea..bcb95c24 100644 --- a/library/src/test/java/com/orm/record/LongFieldTests.java +++ b/library/src/test/java/com/orm/record/LongFieldTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.LongFieldAnnotatedModel; -import com.orm.models.LongFieldExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.LongFieldAnnotatedModel; +import com.orm.model.LongFieldExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; @@ -18,14 +16,9 @@ import static org.junit.Assert.assertNull; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public class LongFieldTests { - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } - @Test public void nullLongExtendedTest() { save(new LongFieldExtendedModel()); diff --git a/library/src/test/java/com/orm/record/MultipleSaveTests.java b/library/src/test/java/com/orm/record/MultipleSaveTests.java index 5c1ec96e..78406380 100644 --- a/library/src/test/java/com/orm/record/MultipleSaveTests.java +++ b/library/src/test/java/com/orm/record/MultipleSaveTests.java @@ -1,17 +1,15 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.StringFieldAnnotatedModel; -import com.orm.models.StringFieldAnnotatedNoIdModel; -import com.orm.models.StringFieldExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.StringFieldAnnotatedModel; +import com.orm.model.StringFieldAnnotatedNoIdModel; +import com.orm.model.StringFieldExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; @@ -19,14 +17,9 @@ import static org.junit.Assert.assertNull; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public class MultipleSaveTests { - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } - @Test public void stringMultipleSaveOriginalExtendedTest() { String string = "Test String"; diff --git a/library/src/test/java/com/orm/record/NestedAnnotatedTests.java b/library/src/test/java/com/orm/record/NestedAnnotatedTests.java index ede95593..66fc2c79 100644 --- a/library/src/test/java/com/orm/record/NestedAnnotatedTests.java +++ b/library/src/test/java/com/orm/record/NestedAnnotatedTests.java @@ -1,17 +1,15 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.NestedAnnotatedModel; -import com.orm.models.RelationshipAnnotatedModel; -import com.orm.models.SimpleAnnotatedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.NestedAnnotatedModel; +import com.orm.model.RelationshipAnnotatedModel; +import com.orm.model.SimpleAnnotatedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import java.util.List; @@ -20,13 +18,8 @@ import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class NestedAnnotatedTests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class NestedAnnotatedTests { @Test public void emptyDatabaseTest() throws Exception { diff --git a/library/src/test/java/com/orm/record/NestedExtendedTests.java b/library/src/test/java/com/orm/record/NestedExtendedTests.java index 5f737166..33823754 100644 --- a/library/src/test/java/com/orm/record/NestedExtendedTests.java +++ b/library/src/test/java/com/orm/record/NestedExtendedTests.java @@ -1,17 +1,15 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.NestedExtendedModel; -import com.orm.models.RelationshipExtendedModel; -import com.orm.models.SimpleExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.NestedExtendedModel; +import com.orm.model.RelationshipExtendedModel; +import com.orm.model.SimpleExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import java.util.List; @@ -20,13 +18,8 @@ import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class NestedExtendedTests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class NestedExtendedTests { @Test public void emptyDatabaseTest() throws Exception { diff --git a/library/src/test/java/com/orm/record/NestedMixedAATests.java b/library/src/test/java/com/orm/record/NestedMixedAATests.java index cb5335b4..1a8f1b43 100644 --- a/library/src/test/java/com/orm/record/NestedMixedAATests.java +++ b/library/src/test/java/com/orm/record/NestedMixedAATests.java @@ -1,17 +1,15 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.NestedMixedAAModel; -import com.orm.models.RelationshipMixedAModel; -import com.orm.models.SimpleAnnotatedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.NestedMixedAAModel; +import com.orm.model.RelationshipMixedAModel; +import com.orm.model.SimpleAnnotatedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import java.util.List; @@ -20,13 +18,8 @@ import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class NestedMixedAATests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class NestedMixedAATests { @Test public void emptyDatabaseTest() throws Exception { diff --git a/library/src/test/java/com/orm/record/NestedMixedABTests.java b/library/src/test/java/com/orm/record/NestedMixedABTests.java index 3677a312..110d82c0 100644 --- a/library/src/test/java/com/orm/record/NestedMixedABTests.java +++ b/library/src/test/java/com/orm/record/NestedMixedABTests.java @@ -1,17 +1,15 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.NestedMixedABModel; -import com.orm.models.RelationshipMixedBModel; -import com.orm.models.SimpleExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.NestedMixedABModel; +import com.orm.model.RelationshipMixedBModel; +import com.orm.model.SimpleExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import java.util.List; @@ -20,14 +18,9 @@ import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public class NestedMixedABTests { - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } - @Test public void emptyDatabaseTest() throws Exception { assertEquals(0L, SugarRecord.count(NestedMixedABModel.class)); diff --git a/library/src/test/java/com/orm/record/NestedMixedBATests.java b/library/src/test/java/com/orm/record/NestedMixedBATests.java index 5dfd0757..edc1c575 100644 --- a/library/src/test/java/com/orm/record/NestedMixedBATests.java +++ b/library/src/test/java/com/orm/record/NestedMixedBATests.java @@ -1,17 +1,15 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.NestedMixedBAModel; -import com.orm.models.RelationshipMixedAModel; -import com.orm.models.SimpleAnnotatedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.NestedMixedBAModel; +import com.orm.model.RelationshipMixedAModel; +import com.orm.model.SimpleAnnotatedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import java.util.List; @@ -20,13 +18,8 @@ import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class NestedMixedBATests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class NestedMixedBATests { @Test public void emptyDatabaseTest() throws Exception { diff --git a/library/src/test/java/com/orm/record/NestedMixedBBTests.java b/library/src/test/java/com/orm/record/NestedMixedBBTests.java index e2625eae..c5ebd4ca 100644 --- a/library/src/test/java/com/orm/record/NestedMixedBBTests.java +++ b/library/src/test/java/com/orm/record/NestedMixedBBTests.java @@ -1,17 +1,15 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.NestedMixedBBModel; -import com.orm.models.RelationshipMixedBModel; -import com.orm.models.SimpleExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.NestedMixedBBModel; +import com.orm.model.RelationshipMixedBModel; +import com.orm.model.SimpleExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import java.util.List; @@ -20,14 +18,9 @@ import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public class NestedMixedBBTests { - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } - @Test public void emptyDatabaseTest() throws Exception { assertEquals(0L, SugarRecord.count(NestedMixedBBModel.class)); diff --git a/library/src/test/java/com/orm/record/NoSugarModelTests.java b/library/src/test/java/com/orm/record/NoSugarModelTests.java index b77858f6..14b16493 100644 --- a/library/src/test/java/com/orm/record/NoSugarModelTests.java +++ b/library/src/test/java/com/orm/record/NoSugarModelTests.java @@ -1,28 +1,21 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.NoSugarModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.NoSugarModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class NoSugarModelTests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class NoSugarModelTests { @Test public void deleteTest() throws Exception { diff --git a/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java b/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java index df229b90..fec134db 100644 --- a/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java +++ b/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.RelationshipAnnotatedModel; -import com.orm.models.SimpleAnnotatedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.RelationshipAnnotatedModel; +import com.orm.model.SimpleAnnotatedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import java.util.List; @@ -19,13 +17,8 @@ import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class RelationshipAnnotatedTests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class RelationshipAnnotatedTests { @Test public void emptyDatabaseTest() throws Exception { diff --git a/library/src/test/java/com/orm/record/RelationshipExtendedTests.java b/library/src/test/java/com/orm/record/RelationshipExtendedTests.java index 6b3a7400..1ff689ee 100644 --- a/library/src/test/java/com/orm/record/RelationshipExtendedTests.java +++ b/library/src/test/java/com/orm/record/RelationshipExtendedTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.RelationshipExtendedModel; -import com.orm.models.SimpleExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.RelationshipExtendedModel; +import com.orm.model.SimpleExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import java.util.List; @@ -19,14 +17,9 @@ import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public class RelationshipExtendedTests { - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } - @Test public void emptyDatabaseTest() throws Exception { assertEquals(0L, SugarRecord.count(RelationshipExtendedModel.class)); diff --git a/library/src/test/java/com/orm/record/RelationshipMixedATests.java b/library/src/test/java/com/orm/record/RelationshipMixedATests.java new file mode 100644 index 00000000..35aaa16d --- /dev/null +++ b/library/src/test/java/com/orm/record/RelationshipMixedATests.java @@ -0,0 +1,141 @@ +package com.orm.record; + +import com.orm.app.ClientApp; +import com.orm.dsl.BuildConfig; +import com.orm.model.RelationshipMixedAModel; +import com.orm.model.SimpleAnnotatedModel; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.annotation.Config; + +import java.util.List; + +import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.count; +import static com.orm.SugarRecord.listAll; + +import static org.junit.Assert.assertEquals; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class RelationshipMixedATests { + + @Test + public void emptyDatabaseTest() throws Exception { + assertEquals(0L, count(RelationshipMixedAModel.class)); + assertEquals(0L, count(SimpleAnnotatedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception { + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + RelationshipMixedAModel mixedAModel = new RelationshipMixedAModel(simple); + + save(simple); + save(mixedAModel); + + assertEquals(1L, count(simple.getClass())); + assertEquals(1L, count(mixedAModel.getClass())); + } + + @Test + public void twoSameSaveTest() throws Exception { + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + RelationshipMixedAModel mixedAModel1 = new RelationshipMixedAModel(simple); + RelationshipMixedAModel mixedAModel2 = new RelationshipMixedAModel(simple); + + + save(simple); + save(mixedAModel1); + save(mixedAModel2); + + assertEquals(1L, count(simple.getClass())); + assertEquals(2L, count(mixedAModel1.getClass())); + } + + @Test + public void twoDifferentSaveTest() throws Exception { + SimpleAnnotatedModel anotherSimple = new SimpleAnnotatedModel(); + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + RelationshipMixedAModel mixedAModel = new RelationshipMixedAModel(simple); + RelationshipMixedAModel anotherMixedAModel = new RelationshipMixedAModel(anotherSimple); + + save(simple); + save(anotherSimple); + save(mixedAModel); + save(anotherMixedAModel); + + assertEquals(2L, count(simple.getClass())); + assertEquals(2L, count(mixedAModel.getClass())); + } + + @Test + public void manySameSaveTest() throws Exception { + final SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + RelationshipMixedAModel mixedAModel = null; + save(simple); + + for (int i = 1; i <= 100; i++) { + mixedAModel = new RelationshipMixedAModel(simple); + save(mixedAModel); + } + + assertEquals(1L, count(simple.getClass())); + assertEquals(100L, count(mixedAModel.getClass())); + } + + @Test + public void manyDifferentSaveTest() throws Exception { + SimpleAnnotatedModel simple = null; + RelationshipMixedAModel mixedAModel = null; + + for (int i = 1; i <= 100; i++) { + simple = new SimpleAnnotatedModel(); + mixedAModel = new RelationshipMixedAModel(simple); + + save(simple); + save(mixedAModel); + } + + assertEquals(100L, count(simple.getClass())); + assertEquals(100L, count(mixedAModel.getClass())); + } + + @Test + public void listAllSameTest() throws Exception { + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + + for (int i = 1; i <= 100; i++) { + RelationshipMixedAModel mixedAModel = new RelationshipMixedAModel(simple); + + save(simple); + save(mixedAModel); + } + + List models = listAll(RelationshipMixedAModel.class); + assertEquals(100, models.size()); + + for (RelationshipMixedAModel model : models) { + assertEquals(simple.getId(), model.getSimple().getId()); + } + } + + @Test + public void listAllDifferentTest() throws Exception { + for (int i = 1; i <= 100; i++) { + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + + save(simple); + save(new RelationshipMixedAModel(simple)); + } + + List models = listAll(RelationshipMixedAModel.class); + assertEquals(100, models.size()); + + for (RelationshipMixedAModel model : models) { + assertEquals(model.getId(), model.getSimple().getId()); + } + } +} diff --git a/library/src/test/java/com/orm/record/RelationshipMixedBTests.java b/library/src/test/java/com/orm/record/RelationshipMixedBTests.java index 419ba98b..112cdd50 100644 --- a/library/src/test/java/com/orm/record/RelationshipMixedBTests.java +++ b/library/src/test/java/com/orm/record/RelationshipMixedBTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.RelationshipMixedBModel; -import com.orm.models.SimpleExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.RelationshipMixedBModel; +import com.orm.model.SimpleExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import java.util.List; @@ -19,14 +17,9 @@ import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public class RelationshipMixedBTests { - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } - @Test public void emptyDatabaseTest() throws Exception { assertEquals(0L, SugarRecord.count(RelationshipMixedBModel.class)); @@ -103,7 +96,6 @@ public void listAllSameTest() throws Exception { @Test public void listAllDifferentTest() throws Exception { - SugarContext.init(RuntimeEnvironment.application); for (int i = 1; i <= 100; i++) { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); diff --git a/library/src/test/java/com/orm/record/ShortFieldTests.java b/library/src/test/java/com/orm/record/ShortFieldTests.java index efe36cf0..489325fc 100644 --- a/library/src/test/java/com/orm/record/ShortFieldTests.java +++ b/library/src/test/java/com/orm/record/ShortFieldTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.ShortFieldAnnotatedModel; -import com.orm.models.ShortFieldExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.ShortFieldAnnotatedModel; +import com.orm.model.ShortFieldExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; @@ -18,13 +16,8 @@ import static org.junit.Assert.assertNull; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class ShortFieldTests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class ShortFieldTests { @Test public void nullShortExtendedTest() { diff --git a/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java b/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java index 837f4403..6e4b9a7a 100644 --- a/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java +++ b/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; +import com.orm.dsl.BuildConfig; import com.orm.helper.NamingHelper; -import com.orm.models.SimpleAnnotatedModel; +import com.orm.model.SimpleAnnotatedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import java.lang.reflect.Field; @@ -27,13 +25,8 @@ import static org.junit.Assert.assertTrue; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class SimpleAnnotatedModelTests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class SimpleAnnotatedModelTests { @Test public void emptyDatabaseTest() throws Exception { diff --git a/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java b/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java index 592a1511..4cd7bdbe 100644 --- a/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java +++ b/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; +import com.orm.dsl.BuildConfig; import com.orm.helper.NamingHelper; -import com.orm.models.SimpleExtendedModel; +import com.orm.model.SimpleExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import java.lang.reflect.Field; @@ -27,13 +25,8 @@ import static org.junit.Assert.assertTrue; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class SimpleExtendedModelTests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class SimpleExtendedModelTests { @Test public void emptyDatabaseTest() throws Exception { @@ -334,7 +327,6 @@ public void findWithQueryAsIteratorTest() throws Exception { "Select * from " + NamingHelper.toTableName(SimpleExtendedModel.class) + " where id >= ? ", "50"); - SugarContext.init(RuntimeEnvironment.application); for (int i = 50; i <= 100; i++) { assertTrue(cursor.hasNext()); SimpleExtendedModel model = cursor.next(); diff --git a/library/src/test/java/com/orm/record/StringFieldTests.java b/library/src/test/java/com/orm/record/StringFieldTests.java index 6fd800cd..8b6af73a 100644 --- a/library/src/test/java/com/orm/record/StringFieldTests.java +++ b/library/src/test/java/com/orm/record/StringFieldTests.java @@ -1,16 +1,14 @@ package com.orm.record; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; -import com.orm.SugarContext; +import com.orm.app.ClientApp; import com.orm.SugarRecord; -import com.orm.models.StringFieldAnnotatedModel; -import com.orm.models.StringFieldExtendedModel; +import com.orm.dsl.BuildConfig; +import com.orm.model.StringFieldAnnotatedModel; +import com.orm.model.StringFieldExtendedModel; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; @@ -18,14 +16,8 @@ import static org.junit.Assert.assertNull; @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18, application = ClientApp.class) -public class StringFieldTests { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application); - } - +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class StringFieldTests { @Test public void nullStringExtendedTest() { diff --git a/library/src/test/java/com/orm/util/ContextUtilTest.java b/library/src/test/java/com/orm/util/ContextUtilTest.java index b4bd9042..46372db9 100644 --- a/library/src/test/java/com/orm/util/ContextUtilTest.java +++ b/library/src/test/java/com/orm/util/ContextUtilTest.java @@ -2,14 +2,13 @@ import android.content.Context; -import com.orm.ClientApp; -import com.orm.RobolectricGradleTestRunner; +import com.orm.app.ClientApp; import com.orm.SugarContext; +import com.orm.dsl.BuildConfig; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static junit.framework.Assert.assertNotNull; @@ -20,13 +19,8 @@ * @author jonatan.salas */ @RunWith(RobolectricGradleTestRunner.class) -@Config(sdk = 18, application = ClientApp.class, manifest = Config.NONE) -public class ContextUtilTest { - - @Before - public void setUp() { - SugarContext.init(RuntimeEnvironment.application.getApplicationContext()); - } +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class ContextUtilTest { @Test public void testInitContext() { diff --git a/library/src/test/java/com/orm/util/KeyWordUtilTest.java b/library/src/test/java/com/orm/util/KeyWordUtilTest.java index 0f132e3a..d2d44b09 100644 --- a/library/src/test/java/com/orm/util/KeyWordUtilTest.java +++ b/library/src/test/java/com/orm/util/KeyWordUtilTest.java @@ -7,7 +7,7 @@ /** * @author jonatan.salas */ -public class KeyWordUtilTest { +public final class KeyWordUtilTest { @Test public void testKeyWord() { diff --git a/library/src/test/java/com/orm/util/MigrationFileParserTest.java b/library/src/test/java/com/orm/util/MigrationFileParserTest.java index 72b9f9ca..9b2d87ce 100644 --- a/library/src/test/java/com/orm/util/MigrationFileParserTest.java +++ b/library/src/test/java/com/orm/util/MigrationFileParserTest.java @@ -6,7 +6,7 @@ import static junit.framework.Assert.assertEquals; -public class MigrationFileParserTest { +public final class MigrationFileParserTest { @Test public void testSingleLineStatement() { diff --git a/library/src/test/java/com/orm/util/NumberComparatorTest.java b/library/src/test/java/com/orm/util/NumberComparatorTest.java index 2dd8458b..aaabf453 100644 --- a/library/src/test/java/com/orm/util/NumberComparatorTest.java +++ b/library/src/test/java/com/orm/util/NumberComparatorTest.java @@ -1,20 +1,14 @@ package com.orm.util; -import com.orm.RobolectricGradleTestRunner; - import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; import static org.junit.Assert.assertEquals; /** * @author jonatan.salas */ -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk = 18) -public class NumberComparatorTest { +public final class NumberComparatorTest { private NumberComparator comparator; @Before diff --git a/library/src/test/java/com/orm/util/ReflectionUtilTest.java b/library/src/test/java/com/orm/util/ReflectionUtilTest.java index 8efba281..eac94abc 100644 --- a/library/src/test/java/com/orm/util/ReflectionUtilTest.java +++ b/library/src/test/java/com/orm/util/ReflectionUtilTest.java @@ -1,11 +1,10 @@ package com.orm.util; -import com.orm.RobolectricGradleTestRunner; -import com.orm.models.TestRecord; +import com.orm.app.ClientApp; +import com.orm.dsl.BuildConfig; +import com.orm.model.TestRecord; import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; import java.lang.reflect.Field; import java.util.ArrayList; @@ -16,9 +15,7 @@ /** * @author jonatan.salas */ -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk = 18) -public class ReflectionUtilTest { +public final class ReflectionUtilTest { @Test public void testGetTableFields() { diff --git a/library/src/test/java/com/orm/util/SugarConfigTest.java b/library/src/test/java/com/orm/util/SugarConfigTest.java index c694dee0..d27eb85d 100644 --- a/library/src/test/java/com/orm/util/SugarConfigTest.java +++ b/library/src/test/java/com/orm/util/SugarConfigTest.java @@ -1,12 +1,5 @@ package com.orm.util; -import com.orm.models.TestRecord; - -import org.junit.Test; - -import java.lang.reflect.Field; -import java.util.List; - /** * @author jonatan.salas */ From 445c63b19504f080ab761247e590d803756e1c28 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 9 Apr 2016 17:01:26 -0300 Subject: [PATCH 085/139] Added static import to replace SugarRecord class import --- .../com/orm/record/BooleanFieldTests.java | 15 +- .../com/orm/record/ByteArrayFieldTests.java | 11 +- .../java/com/orm/record/DoubleFieldTests.java | 18 +- .../java/com/orm/record/EnumFieldTests.java | 31 ++-- .../com/orm/record/FirstAndLastTests.java | 163 +++++++++--------- .../java/com/orm/record/FloatFieldTests.java | 39 +++-- .../record/IncompleteAnnotatedModelTests.java | 1 + .../com/orm/record/IntegerFieldTests.java | 32 ++-- .../com/orm/record/ListAllOrderByTests.java | 29 ++-- .../java/com/orm/record/LongFieldTests.java | 41 ++--- .../com/orm/record/MultipleSaveTests.java | 76 ++++---- .../com/orm/record/NestedAnnotatedTests.java | 60 ++++--- .../com/orm/record/NestedExtendedTests.java | 58 ++++--- .../com/orm/record/NestedMixedAATests.java | 59 ++++--- .../com/orm/record/NestedMixedABTests.java | 68 +++++--- .../com/orm/record/NestedMixedBATests.java | 67 ++++--- .../com/orm/record/NestedMixedBBTests.java | 70 +++++--- .../com/orm/record/NoSugarModelTests.java | 11 +- .../record/RelationshipAnnotatedTests.java | 54 +++--- .../orm/record/RelationshipExtendedTests.java | 54 +++--- .../orm/record/RelationshipMixedBTests.java | 49 +++--- .../java/com/orm/record/ShortFieldTests.java | 39 +++-- .../orm/record/SimpleAnnotatedModelTests.java | 140 ++++++++------- .../orm/record/SimpleExtendedModelTests.java | 136 ++++++++------- .../java/com/orm/record/StringFieldTests.java | 14 +- 25 files changed, 746 insertions(+), 589 deletions(-) diff --git a/library/src/test/java/com/orm/record/BooleanFieldTests.java b/library/src/test/java/com/orm/record/BooleanFieldTests.java index 2966aedd..3a026a48 100644 --- a/library/src/test/java/com/orm/record/BooleanFieldTests.java +++ b/library/src/test/java/com/orm/record/BooleanFieldTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.BooleanFieldAnnotatedModel; import com.orm.model.BooleanFieldExtendedModel; @@ -12,6 +11,8 @@ import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.findById; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -22,28 +23,28 @@ public final class BooleanFieldTests { @Test public void nullBooleanExtendedTest() { save(new BooleanFieldExtendedModel()); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); + BooleanFieldExtendedModel model = findById(BooleanFieldExtendedModel.class, 1); assertNull(model.getBoolean()); } @Test public void nullRawBooleanExtendedTest() { save(new BooleanFieldExtendedModel()); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); + BooleanFieldExtendedModel model = findById(BooleanFieldExtendedModel.class, 1); assertEquals(false, model.getRawBoolean()); } @Test public void nullBooleanAnnotatedTest() { save(new BooleanFieldAnnotatedModel()); - BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); + BooleanFieldAnnotatedModel model = findById(BooleanFieldAnnotatedModel.class, 1); assertNull(model.getBoolean()); } @Test public void nullRawBooleanAnnotatedTest() { save(new BooleanFieldAnnotatedModel()); - BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); + BooleanFieldAnnotatedModel model = findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(false, model.getRawBoolean()); } @@ -58,7 +59,7 @@ public void nullRawBooleanAnnotatedTest() { @Test public void rawBooleanExtendedTest() { save(new BooleanFieldExtendedModel(true)); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); + BooleanFieldExtendedModel model = findById(BooleanFieldExtendedModel.class, 1); assertEquals(true, model.getRawBoolean()); } @@ -76,7 +77,7 @@ public void rawBooleanExtendedTest() { @Test public void rawBooleanAnnotatedTest() { save(new BooleanFieldAnnotatedModel(true)); - BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); + BooleanFieldAnnotatedModel model = findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(true, model.getRawBoolean()); } } diff --git a/library/src/test/java/com/orm/record/ByteArrayFieldTests.java b/library/src/test/java/com/orm/record/ByteArrayFieldTests.java index 73fdd6da..450823f2 100644 --- a/library/src/test/java/com/orm/record/ByteArrayFieldTests.java +++ b/library/src/test/java/com/orm/record/ByteArrayFieldTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.ByteArrayAnnotatedModel; import com.orm.model.ByteArrayExtendedModel; @@ -12,6 +11,8 @@ import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.findById; + import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; @@ -23,7 +24,7 @@ public final class ByteArrayFieldTests { public void nullByteArrayExtendedTest() { byte[] array = "".getBytes(); save(new ByteArrayExtendedModel()); - ByteArrayExtendedModel model = SugarRecord.findById(ByteArrayExtendedModel.class, 1); + ByteArrayExtendedModel model = findById(ByteArrayExtendedModel.class, 1); assertEquals(new String(array), new String(model.getByteArray())); assertArrayEquals(array, model.getByteArray()); } @@ -32,7 +33,7 @@ public void nullByteArrayExtendedTest() { public void nullByteArrayAnnotatedTest() { byte[] array = "".getBytes(); save(new ByteArrayAnnotatedModel()); - ByteArrayAnnotatedModel model = SugarRecord.findById(ByteArrayAnnotatedModel.class, 1); + ByteArrayAnnotatedModel model = findById(ByteArrayAnnotatedModel.class, 1); assertEquals(new String(array), new String(model.getByteArray())); assertArrayEquals(array, model.getByteArray()); } @@ -41,7 +42,7 @@ public void nullByteArrayAnnotatedTest() { public void byteArrayExtendedTest() { byte[] array = "hello".getBytes(); save(new ByteArrayExtendedModel(array)); - ByteArrayExtendedModel model = SugarRecord.findById(ByteArrayExtendedModel.class, 1); + ByteArrayExtendedModel model = findById(ByteArrayExtendedModel.class, 1); assertEquals(new String(array), new String(model.getByteArray())); assertArrayEquals(array, model.getByteArray()); } @@ -50,7 +51,7 @@ public void byteArrayExtendedTest() { public void byteArrayAnnotatedTest() { byte[] array = "hello".getBytes(); save(new ByteArrayAnnotatedModel(array)); - ByteArrayAnnotatedModel model = SugarRecord.findById(ByteArrayAnnotatedModel.class, 1); + ByteArrayAnnotatedModel model = findById(ByteArrayAnnotatedModel.class, 1); assertEquals(new String(array), new String(model.getByteArray())); assertArrayEquals(array, model.getByteArray()); } diff --git a/library/src/test/java/com/orm/record/DoubleFieldTests.java b/library/src/test/java/com/orm/record/DoubleFieldTests.java index 96ea7f48..a05fa3e0 100644 --- a/library/src/test/java/com/orm/record/DoubleFieldTests.java +++ b/library/src/test/java/com/orm/record/DoubleFieldTests.java @@ -12,6 +12,8 @@ import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.findById; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -22,28 +24,28 @@ public final class DoubleFieldTests { @Test public void nullDoubleExtendedTest() { save(new DoubleFieldExtendedModel()); - DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); + DoubleFieldExtendedModel model = findById(DoubleFieldExtendedModel.class, 1); assertNull(model.getDouble()); } @Test public void nullRawDoubleExtendedTest() { save(new DoubleFieldExtendedModel()); - DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); + DoubleFieldExtendedModel model = findById(DoubleFieldExtendedModel.class, 1); assertEquals(0.0, model.getRawDouble(), 0.0000000001); } @Test public void nullDoubleAnnotatedTest() { save(new DoubleFieldAnnotatedModel()); - DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); + DoubleFieldAnnotatedModel model = findById(DoubleFieldAnnotatedModel.class, 1); assertNull(model.getDouble()); } @Test public void nullRawDoubleAnnotatedTest() { save(new DoubleFieldAnnotatedModel()); - DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); + DoubleFieldAnnotatedModel model = findById(DoubleFieldAnnotatedModel.class, 1); assertEquals(0.0, model.getRawDouble(), 0.0000000001); } @@ -52,14 +54,14 @@ public void nullRawDoubleAnnotatedTest() { public void objectDoubleExtendedTest() { Double objectDouble = Double.valueOf(25.0); save(new DoubleFieldExtendedModel(objectDouble)); - DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); + DoubleFieldExtendedModel model = findById(DoubleFieldExtendedModel.class, 1); assertEquals(objectDouble, model.getDouble()); } @Test public void rawDoubleExtendedTest() { save(new DoubleFieldExtendedModel(25.0)); - DoubleFieldExtendedModel model = SugarRecord.findById(DoubleFieldExtendedModel.class, 1); + DoubleFieldExtendedModel model = findById(DoubleFieldExtendedModel.class, 1); assertEquals(25.0, model.getRawDouble(), 0.0000000001); } @@ -68,14 +70,14 @@ public void rawDoubleExtendedTest() { public void objectDoubleAnnotatedTest() { Double objectDouble = Double.valueOf(25.0); save(new DoubleFieldAnnotatedModel(objectDouble)); - DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); + DoubleFieldAnnotatedModel model = findById(DoubleFieldAnnotatedModel.class, 1); assertEquals(objectDouble, model.getDouble()); } @Test public void rawDoubleAnnotatedTest() { save(new DoubleFieldAnnotatedModel(25.0)); - DoubleFieldAnnotatedModel model = SugarRecord.findById(DoubleFieldAnnotatedModel.class, 1); + DoubleFieldAnnotatedModel model = findById(DoubleFieldAnnotatedModel.class, 1); assertEquals(25.0, model.getRawDouble(), 0.0000000001); } } diff --git a/library/src/test/java/com/orm/record/EnumFieldTests.java b/library/src/test/java/com/orm/record/EnumFieldTests.java index d41d4025..42647b00 100644 --- a/library/src/test/java/com/orm/record/EnumFieldTests.java +++ b/library/src/test/java/com/orm/record/EnumFieldTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.EnumFieldAnnotatedModel; import com.orm.model.EnumFieldExtendedModel; @@ -12,6 +11,10 @@ import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.findById; +import static com.orm.model.EnumFieldExtendedModel.DefaultEnum; +import static com.orm.model.EnumFieldExtendedModel.OverrideEnum; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -23,53 +26,51 @@ public final class EnumFieldTests { @Test public void nullDefaultEnumExtendedTest() { save(new EnumFieldExtendedModel()); - EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); + EnumFieldExtendedModel model = findById(EnumFieldExtendedModel.class, 1); assertNull(model.getDefaultEnum()); } @Test public void nullOverriddenEnumExtendedTest() { save(new EnumFieldExtendedModel()); - EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); + EnumFieldExtendedModel model = findById(EnumFieldExtendedModel.class, 1); assertNull(model.getOverrideEnum()); } @Test public void nullDefaultEnumAnnotatedTest() { save(new EnumFieldAnnotatedModel()); - EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); + EnumFieldAnnotatedModel model = findById(EnumFieldAnnotatedModel.class, 1); assertNull(model.getDefaultEnum()); } @Test public void nullOverriddenEnumAnnotatedTest() { save(new EnumFieldAnnotatedModel()); - EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); + EnumFieldAnnotatedModel model = findById(EnumFieldAnnotatedModel.class, 1); assertNull(model.getOverrideEnum()); } @Test public void defaultEnumExtendedTest() { - save(new EnumFieldExtendedModel(EnumFieldExtendedModel.OverrideEnum.ONE, - EnumFieldExtendedModel.DefaultEnum.TWO)); - EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); + save(new EnumFieldExtendedModel(OverrideEnum.ONE, DefaultEnum.TWO)); + EnumFieldExtendedModel model = findById(EnumFieldExtendedModel.class, 1); assertNotNull(model); - assertEquals(model.getDefaultEnum(), EnumFieldExtendedModel.DefaultEnum.TWO); + assertEquals(model.getDefaultEnum(), DefaultEnum.TWO); } @Test public void overriddenEnumExtendedTest() { - save(new EnumFieldExtendedModel(EnumFieldExtendedModel.OverrideEnum.ONE, - EnumFieldExtendedModel.DefaultEnum.TWO)); - EnumFieldExtendedModel model = SugarRecord.findById(EnumFieldExtendedModel.class, 1); + save(new EnumFieldExtendedModel(OverrideEnum.ONE, DefaultEnum.TWO)); + EnumFieldExtendedModel model = findById(EnumFieldExtendedModel.class, 1); assertNotNull(model); - assertEquals(model.getOverrideEnum(), EnumFieldExtendedModel.OverrideEnum.ONE); + assertEquals(model.getOverrideEnum(), OverrideEnum.ONE); } @Test public void defaultEnumAnnotatedTest() { save(new EnumFieldAnnotatedModel(EnumFieldAnnotatedModel.OverrideEnum.ONE, EnumFieldAnnotatedModel.DefaultEnum.TWO)); - EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); + EnumFieldAnnotatedModel model = findById(EnumFieldAnnotatedModel.class, 1); assertNotNull(model); assertEquals(model.getDefaultEnum(), EnumFieldAnnotatedModel.DefaultEnum.TWO); } @@ -78,7 +79,7 @@ public void defaultEnumAnnotatedTest() { public void overriddenEnumAnnotatedTest() { save(new EnumFieldAnnotatedModel(EnumFieldAnnotatedModel.OverrideEnum.ONE, EnumFieldAnnotatedModel.DefaultEnum.TWO)); - EnumFieldAnnotatedModel model = SugarRecord.findById(EnumFieldAnnotatedModel.class, 1); + EnumFieldAnnotatedModel model = findById(EnumFieldAnnotatedModel.class, 1); assertNotNull(model); assertEquals(model.getOverrideEnum(), EnumFieldAnnotatedModel.OverrideEnum.ONE); } diff --git a/library/src/test/java/com/orm/record/FirstAndLastTests.java b/library/src/test/java/com/orm/record/FirstAndLastTests.java index 278b82f0..1b6db799 100644 --- a/library/src/test/java/com/orm/record/FirstAndLastTests.java +++ b/library/src/test/java/com/orm/record/FirstAndLastTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.FloatFieldAnnotatedModel; import com.orm.model.FloatFieldExtendedModel; @@ -12,6 +11,11 @@ import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.first; +import static com.orm.SugarRecord.delete; +import static com.orm.SugarRecord.findById; +import static com.orm.SugarRecord.last; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -26,7 +30,7 @@ public void firstExtendedTest() { Float lastObjectFloat = 50F; save(new FloatFieldExtendedModel(firstObjectFloat)); save(new FloatFieldExtendedModel(lastObjectFloat)); - FloatFieldExtendedModel model = SugarRecord.first(FloatFieldExtendedModel.class); + FloatFieldExtendedModel model = first(FloatFieldExtendedModel.class); if (null != model) { assertEquals(firstObjectFloat, model.getFloat()); @@ -35,70 +39,70 @@ public void firstExtendedTest() { @Test public void firstDeletedRecordExtendedTest() { - Float firstObjectFloat = 15F; - Float secondObjectFloat = 25F; - Float thirdObjectFloat = 35F; - Float fourthObjectFloat = 45F; - save(new FloatFieldExtendedModel(firstObjectFloat)); - save(new FloatFieldExtendedModel(secondObjectFloat)); - save(new FloatFieldExtendedModel(thirdObjectFloat)); - save(new FloatFieldExtendedModel(fourthObjectFloat)); - FloatFieldExtendedModel firstRecord = SugarRecord.findById(FloatFieldExtendedModel.class, 1); - firstRecord.delete(); - FloatFieldExtendedModel model = SugarRecord.first(FloatFieldExtendedModel.class); + Float second = 25F; + + save(new FloatFieldExtendedModel(15F)); + save(new FloatFieldExtendedModel(second)); + save(new FloatFieldExtendedModel(35F)); + save(new FloatFieldExtendedModel(45F)); + + FloatFieldExtendedModel firstRecord = findById(FloatFieldExtendedModel.class, 1); + delete(firstRecord); + FloatFieldExtendedModel model = first(FloatFieldExtendedModel.class); if (null != model) { - assertEquals(secondObjectFloat, model.getFloat()); + assertEquals(second, model.getFloat()); } } @Test public void lastExtendedTest() { - Float firstObjectFloat = 25F; - Float lastObjectFloat = 50F; - save(new FloatFieldExtendedModel(firstObjectFloat)); - save(new FloatFieldExtendedModel(lastObjectFloat)); - FloatFieldExtendedModel model = SugarRecord.last(FloatFieldExtendedModel.class); + Float last = 50F; + + save(new FloatFieldExtendedModel(25F)); + save(new FloatFieldExtendedModel(last)); + + FloatFieldExtendedModel model = last(FloatFieldExtendedModel.class); if (null != model) { - assertEquals(lastObjectFloat, model.getFloat()); + assertEquals(last, model.getFloat()); } } @Test public void lastDeletedRecordExtendedTest() { - Float firstObjectFloat = 15F; - Float secondObjectFloat = 25F; - Float thirdObjectFloat = 35F; - Float fourthObjectFloat = 45F; - save(new FloatFieldExtendedModel(firstObjectFloat)); - save(new FloatFieldExtendedModel(secondObjectFloat)); - save(new FloatFieldExtendedModel(thirdObjectFloat)); - save(new FloatFieldExtendedModel(fourthObjectFloat)); - FloatFieldExtendedModel lastRecord = SugarRecord.findById(FloatFieldExtendedModel.class, 4); - lastRecord.delete(); - FloatFieldExtendedModel model = SugarRecord.last(FloatFieldExtendedModel.class); + Float third = 35F; + + save(new FloatFieldExtendedModel(15F)); + save(new FloatFieldExtendedModel(25F)); + save(new FloatFieldExtendedModel(third)); + save(new FloatFieldExtendedModel(45F)); + + FloatFieldExtendedModel lastRecord = findById(FloatFieldExtendedModel.class, 4); + delete(lastRecord); + FloatFieldExtendedModel model = last(FloatFieldExtendedModel.class); if (null != model) { - assertEquals(thirdObjectFloat, model.getFloat()); + assertEquals(third, model.getFloat()); } } @Test public void nullFirstExtendedTest() { - assertNull(SugarRecord.first(FloatFieldExtendedModel.class)); + assertNull(first(FloatFieldExtendedModel.class)); } @Test public void nullLastExtendedTest() { - assertNull(SugarRecord.last(FloatFieldExtendedModel.class)); + assertNull(last(FloatFieldExtendedModel.class)); } @Test public void oneItemExtendedTest() { save(new FloatFieldExtendedModel(25F)); - FloatFieldExtendedModel firstModel = SugarRecord.first(FloatFieldExtendedModel.class); - FloatFieldExtendedModel lastModel = SugarRecord.last(FloatFieldExtendedModel.class); + + FloatFieldExtendedModel firstModel = first(FloatFieldExtendedModel.class); + FloatFieldExtendedModel lastModel = last(FloatFieldExtendedModel.class); if (null != firstModel && null != lastModel) { assertEquals(firstModel.getFloat(), lastModel.getFloat()); @@ -107,86 +111,89 @@ public void oneItemExtendedTest() { @Test public void firstAnnotatedTest() { - Float firstObjectFloat = 25F; - Float lastObjectFloat = 50F; - save(new FloatFieldAnnotatedModel(firstObjectFloat)); - save(new FloatFieldAnnotatedModel(lastObjectFloat)); - FloatFieldAnnotatedModel model = SugarRecord.first(FloatFieldAnnotatedModel.class); + Float first = 25F; + + save(new FloatFieldAnnotatedModel(first)); + save(new FloatFieldAnnotatedModel(50F)); + + FloatFieldAnnotatedModel model = first(FloatFieldAnnotatedModel.class); if (null != model) { - assertEquals(firstObjectFloat, model.getFloat()); + assertEquals(first, model.getFloat()); } } @Test public void firstDeletedRecordAnnotatedTest() { - Float firstObjectFloat = 15F; - Float secondObjectFloat = 25F; - Float thirdObjectFloat = 35F; - Float fourthObjectFloat = 45F; - save(new FloatFieldAnnotatedModel(firstObjectFloat)); - save(new FloatFieldAnnotatedModel(secondObjectFloat)); - save(new FloatFieldAnnotatedModel(thirdObjectFloat)); - save(new FloatFieldAnnotatedModel(fourthObjectFloat)); - FloatFieldAnnotatedModel firstRecord = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); - SugarRecord.delete(firstRecord); - FloatFieldAnnotatedModel model = SugarRecord.first(FloatFieldAnnotatedModel.class); + Float second = 25F; + + save(new FloatFieldAnnotatedModel(15F)); + save(new FloatFieldAnnotatedModel(second)); + save(new FloatFieldAnnotatedModel(35F)); + save(new FloatFieldAnnotatedModel(45F)); + + FloatFieldAnnotatedModel firstRecord = findById(FloatFieldAnnotatedModel.class, 1); + + delete(firstRecord); + + FloatFieldAnnotatedModel model = first(FloatFieldAnnotatedModel.class); if (null != model) { - assertEquals(secondObjectFloat, model.getFloat()); + assertEquals(second, model.getFloat()); } } @Test public void lastAnnotatedTest() { - Float firstObjectFloat = 25F; - Float lastObjectFloat = 50F; - save(new FloatFieldAnnotatedModel(firstObjectFloat)); - save(new FloatFieldAnnotatedModel(lastObjectFloat)); - FloatFieldAnnotatedModel model = SugarRecord.last(FloatFieldAnnotatedModel.class); + Float last = 50F; + + save(new FloatFieldAnnotatedModel(25F)); + save(new FloatFieldAnnotatedModel(last)); + + FloatFieldAnnotatedModel model = last(FloatFieldAnnotatedModel.class); if (null != model) { - assertEquals(lastObjectFloat, model.getFloat()); + assertEquals(last, model.getFloat()); } } @Test public void lastDeletedRecordAnnotatedTest() { - Float firstObjectFloat = 15F; - Float secondObjectFloat = 25F; - Float thirdObjectFloat = 35F; - Float fourthObjectFloat = 45F; - save(new FloatFieldAnnotatedModel(firstObjectFloat)); - save(new FloatFieldAnnotatedModel(secondObjectFloat)); - save(new FloatFieldAnnotatedModel(thirdObjectFloat)); - save(new FloatFieldAnnotatedModel(fourthObjectFloat)); - FloatFieldAnnotatedModel lastRecord = SugarRecord.findById(FloatFieldAnnotatedModel.class, 4); - SugarRecord.delete(lastRecord); - FloatFieldAnnotatedModel model = SugarRecord.last(FloatFieldAnnotatedModel.class); + Float third = 35F; + + save(new FloatFieldAnnotatedModel(15F)); + save(new FloatFieldAnnotatedModel(25F)); + save(new FloatFieldAnnotatedModel(third)); + save(new FloatFieldAnnotatedModel(45F)); + + FloatFieldAnnotatedModel lastRecord = findById(FloatFieldAnnotatedModel.class, 4); + delete(lastRecord); + FloatFieldAnnotatedModel model = last(FloatFieldAnnotatedModel.class); if (null != model) { - assertEquals(thirdObjectFloat, model.getFloat()); + assertEquals(third, model.getFloat()); } } @Test public void nullFirstAnnotatedTest() { - assertNull(SugarRecord.first(FloatFieldAnnotatedModel.class)); + assertNull(first(FloatFieldAnnotatedModel.class)); } @Test public void nullLastAnnotatedTest() { - assertNull(SugarRecord.last(FloatFieldAnnotatedModel.class)); + assertNull(last(FloatFieldAnnotatedModel.class)); } @Test public void oneItemAnnotatedTest() { save(new FloatFieldAnnotatedModel(25F)); - FloatFieldAnnotatedModel firstModel = SugarRecord.first(FloatFieldAnnotatedModel.class); - FloatFieldAnnotatedModel lastModel = SugarRecord.last(FloatFieldAnnotatedModel.class); - if (null != firstModel && null != lastModel) { - assertEquals(firstModel.getFloat(), lastModel.getFloat()); + FloatFieldAnnotatedModel first = first(FloatFieldAnnotatedModel.class); + FloatFieldAnnotatedModel last = last(FloatFieldAnnotatedModel.class); + + if (null != first && null != last) { + assertEquals(first.getFloat(), last.getFloat()); } } } diff --git a/library/src/test/java/com/orm/record/FloatFieldTests.java b/library/src/test/java/com/orm/record/FloatFieldTests.java index 48317fcf..d76516b9 100644 --- a/library/src/test/java/com/orm/record/FloatFieldTests.java +++ b/library/src/test/java/com/orm/record/FloatFieldTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.FloatFieldAnnotatedModel; import com.orm.model.FloatFieldExtendedModel; @@ -12,68 +11,70 @@ import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.findById; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +@SuppressWarnings("all") @RunWith(RobolectricGradleTestRunner.class) @Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public final class FloatFieldTests { + Float aFloat = Float.valueOf(25F); @Test public void nullFloatExtendedTest() { save(new FloatFieldExtendedModel()); - FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); + FloatFieldExtendedModel model = findById(FloatFieldExtendedModel.class, 1); assertNull(model.getFloat()); } @Test public void nullRawFloatExtendedTest() { save(new FloatFieldExtendedModel()); - FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); + FloatFieldExtendedModel model = findById(FloatFieldExtendedModel.class, 1); assertEquals(0F, model.getRawFloat(), 0.0000000001F); } @Test public void nullFloatAnnotatedTest() { save(new FloatFieldAnnotatedModel()); - FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); + FloatFieldAnnotatedModel model = findById(FloatFieldAnnotatedModel.class, 1); assertNull(model.getFloat()); } @Test public void nullRawFloatAnnotatedTest() { save(new FloatFieldAnnotatedModel()); - FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); + FloatFieldAnnotatedModel model = findById(FloatFieldAnnotatedModel.class, 1); assertEquals(0F, model.getRawFloat(), 0.0000000001F); } @Test public void objectFloatExtendedTest() { - Float objectFloat = 25F; - save(new FloatFieldExtendedModel(objectFloat)); - FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); - assertEquals(objectFloat, model.getFloat()); + save(new FloatFieldExtendedModel(aFloat)); + FloatFieldExtendedModel model = findById(FloatFieldExtendedModel.class, 1); + assertEquals(aFloat, model.getFloat()); } @Test public void rawFloatExtendedTest() { - save(new FloatFieldExtendedModel(25F)); - FloatFieldExtendedModel model = SugarRecord.findById(FloatFieldExtendedModel.class, 1); - assertEquals(25F, model.getRawFloat(), 0.0000000001F); + save(new FloatFieldExtendedModel(aFloat.floatValue())); + FloatFieldExtendedModel model = findById(FloatFieldExtendedModel.class, 1); + assertEquals(aFloat.floatValue(), model.getRawFloat(), 0.0000000001F); } @Test public void objectFloatAnnotatedTest() { - Float objectFloat = 25F; - save(new FloatFieldAnnotatedModel(objectFloat)); - FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); - assertEquals(objectFloat, model.getFloat()); + save(new FloatFieldAnnotatedModel(aFloat)); + FloatFieldAnnotatedModel model = findById(FloatFieldAnnotatedModel.class, 1); + assertEquals(aFloat, model.getFloat()); } @Test public void rawFloatAnnotatedTest() { - save(new FloatFieldAnnotatedModel(25F)); - FloatFieldAnnotatedModel model = SugarRecord.findById(FloatFieldAnnotatedModel.class, 1); - assertEquals(25F, model.getRawFloat(), 0.0000000001F); + save(new FloatFieldAnnotatedModel(aFloat.floatValue())); + FloatFieldAnnotatedModel model = findById(FloatFieldAnnotatedModel.class, 1); + assertEquals(aFloat.floatValue(), model.getRawFloat(), 0.0000000001F); } } diff --git a/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java b/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java index 9834829f..0053ed96 100644 --- a/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java +++ b/library/src/test/java/com/orm/record/IncompleteAnnotatedModelTests.java @@ -13,6 +13,7 @@ import static com.orm.SugarRecord.delete; import static com.orm.SugarRecord.save; + import static org.junit.Assert.assertFalse; @RunWith(RobolectricGradleTestRunner.class) diff --git a/library/src/test/java/com/orm/record/IntegerFieldTests.java b/library/src/test/java/com/orm/record/IntegerFieldTests.java index b9ebf11d..5bb78a47 100644 --- a/library/src/test/java/com/orm/record/IntegerFieldTests.java +++ b/library/src/test/java/com/orm/record/IntegerFieldTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.IntegerFieldAnnotatedModel; import com.orm.model.IntegerFieldExtendedModel; @@ -12,68 +11,69 @@ import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.findById; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @RunWith(RobolectricGradleTestRunner.class) @Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) -public class IntegerFieldTests { +public final class IntegerFieldTests { + private Integer integer = 25; @Test public void nullIntegerExtendedTest() { save(new IntegerFieldExtendedModel()); - IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); + IntegerFieldExtendedModel model = findById(IntegerFieldExtendedModel.class, 1); assertNull(model.getInteger()); } @Test public void nullIntExtendedTest() { save(new IntegerFieldExtendedModel()); - IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); + IntegerFieldExtendedModel model = findById(IntegerFieldExtendedModel.class, 1); assertEquals(0, model.getInt()); } @Test public void nullIntegerAnnotatedTest() { save(new IntegerFieldAnnotatedModel()); - IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); + IntegerFieldAnnotatedModel model = findById(IntegerFieldAnnotatedModel.class, 1); assertNull(model.getInteger()); } @Test public void nullIntAnnotatedTest() { save(new IntegerFieldAnnotatedModel()); - IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); + IntegerFieldAnnotatedModel model = findById(IntegerFieldAnnotatedModel.class, 1); assertEquals(0, model.getInt()); } @Test public void integerExtendedTest() { - Integer integer = 25; save(new IntegerFieldExtendedModel(integer)); - IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); + IntegerFieldExtendedModel model = findById(IntegerFieldExtendedModel.class, 1); assertEquals(integer, model.getInteger()); } @Test public void intExtendedTest() { - save(new IntegerFieldExtendedModel(25)); - IntegerFieldExtendedModel model = SugarRecord.findById(IntegerFieldExtendedModel.class, 1); - assertEquals(25, model.getInt()); + save(new IntegerFieldExtendedModel(integer.intValue())); + IntegerFieldExtendedModel model = findById(IntegerFieldExtendedModel.class, 1); + assertEquals(integer.intValue(), model.getInt()); } @Test public void integerAnnotatedTest() { - Integer integer = 25; save(new IntegerFieldAnnotatedModel(integer)); - IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); + IntegerFieldAnnotatedModel model = findById(IntegerFieldAnnotatedModel.class, 1); assertEquals(integer, model.getInteger()); } @Test public void intAnnotatedTest() { - save(new IntegerFieldAnnotatedModel(25)); - IntegerFieldAnnotatedModel model = SugarRecord.findById(IntegerFieldAnnotatedModel.class, 1); - assertEquals(25, model.getInt()); + save(new IntegerFieldAnnotatedModel(integer.intValue())); + IntegerFieldAnnotatedModel model = findById(IntegerFieldAnnotatedModel.class, 1); + assertEquals(integer.intValue(), model.getInt()); } } diff --git a/library/src/test/java/com/orm/record/ListAllOrderByTests.java b/library/src/test/java/com/orm/record/ListAllOrderByTests.java index c5f34916..548ead23 100644 --- a/library/src/test/java/com/orm/record/ListAllOrderByTests.java +++ b/library/src/test/java/com/orm/record/ListAllOrderByTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.IntegerFieldExtendedModel; @@ -13,6 +12,8 @@ import java.util.List; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.listAll; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -22,33 +23,39 @@ public final class ListAllOrderByTests { @Test public void listAllOrderByEmptyTest() { - assertEquals(0L, SugarRecord.listAll(IntegerFieldExtendedModel.class, "id").size()); + final List list = listAll(IntegerFieldExtendedModel.class, "id"); + assertEquals(0L, list.size()); } @Test public void listAllOrderByIdTest() { - for(int i = 1; i <= 100; i++) { + for (int i = 1; i <= 100; i++) { save(new IntegerFieldExtendedModel(i)); } - List models = - SugarRecord.listAll(IntegerFieldExtendedModel.class, "id"); + + List models = listAll(IntegerFieldExtendedModel.class, "id"); assertEquals(100L, models.size()); + Long id = models.get(0).getId(); - for(int i = 1; i < 100; i++) { - assertTrue(id models = - SugarRecord.listAll(IntegerFieldExtendedModel.class, "raw_integer"); + + List models = listAll(IntegerFieldExtendedModel.class, "raw_integer"); + assertEquals(100L, models.size()); + int raw = models.get(0).getInt(); - for(int i = 1; i < 100; i++) { + + for (int i = 1; i < 100; i++) { assertTrue(raw < models.get(i).getInt()); } } diff --git a/library/src/test/java/com/orm/record/LongFieldTests.java b/library/src/test/java/com/orm/record/LongFieldTests.java index bcb95c24..d1516a42 100644 --- a/library/src/test/java/com/orm/record/LongFieldTests.java +++ b/library/src/test/java/com/orm/record/LongFieldTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.LongFieldAnnotatedModel; import com.orm.model.LongFieldExtendedModel; @@ -12,68 +11,70 @@ import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.findById; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +@SuppressWarnings("all") @RunWith(RobolectricGradleTestRunner.class) @Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) -public class LongFieldTests { +public final class LongFieldTests { + private Long aLong = Long.valueOf(25L); @Test public void nullLongExtendedTest() { save(new LongFieldExtendedModel()); - LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); + LongFieldExtendedModel model = findById(LongFieldExtendedModel.class, 1); assertNull(model.getLong()); } @Test public void nullRawLongExtendedTest() { save(new LongFieldExtendedModel()); - LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); + LongFieldExtendedModel model = findById(LongFieldExtendedModel.class, 1); assertEquals(0L, model.getRawLong()); } @Test public void nullLongAnnotatedTest() { save(new LongFieldAnnotatedModel()); - LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); + LongFieldAnnotatedModel model = findById(LongFieldAnnotatedModel.class, 1); assertNull(model.getLong()); } @Test public void nullRawLongAnnotatedTest() { save(new LongFieldAnnotatedModel()); - LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); + LongFieldAnnotatedModel model = findById(LongFieldAnnotatedModel.class, 1); assertEquals(0L, model.getRawLong()); } @Test public void objectLongExtendedTest() { - Long objectLong = 25L; - save(new LongFieldExtendedModel(objectLong)); - LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); - assertEquals(objectLong, model.getLong()); + save(new LongFieldExtendedModel(aLong)); + LongFieldExtendedModel model = findById(LongFieldExtendedModel.class, 1); + assertEquals(aLong, model.getLong()); } @Test public void rawLongExtendedTest() { - save(new LongFieldExtendedModel(25L)); - LongFieldExtendedModel model = SugarRecord.findById(LongFieldExtendedModel.class, 1); - assertEquals(25L, model.getRawLong()); + save(new LongFieldExtendedModel(aLong.longValue())); + LongFieldExtendedModel model = findById(LongFieldExtendedModel.class, 1); + assertEquals(aLong.longValue(), model.getRawLong()); } @Test public void objectLongAnnotatedTest() { - Long objectLong = 25L; - save(new LongFieldAnnotatedModel(objectLong)); - LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); - assertEquals(objectLong, model.getLong()); + save(new LongFieldAnnotatedModel(aLong)); + LongFieldAnnotatedModel model = findById(LongFieldAnnotatedModel.class, 1); + assertEquals(aLong, model.getLong()); } @Test public void rawLongAnnotatedTest() { - save(new LongFieldAnnotatedModel(25L)); - LongFieldAnnotatedModel model = SugarRecord.findById(LongFieldAnnotatedModel.class, 1); - assertEquals(25L, model.getRawLong()); + save(new LongFieldAnnotatedModel(aLong.longValue())); + LongFieldAnnotatedModel model = findById(LongFieldAnnotatedModel.class, 1); + assertEquals(aLong.longValue(), model.getRawLong()); } } diff --git a/library/src/test/java/com/orm/record/MultipleSaveTests.java b/library/src/test/java/com/orm/record/MultipleSaveTests.java index 78406380..73b353ad 100644 --- a/library/src/test/java/com/orm/record/MultipleSaveTests.java +++ b/library/src/test/java/com/orm/record/MultipleSaveTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.StringFieldAnnotatedModel; import com.orm.model.StringFieldAnnotatedNoIdModel; @@ -13,103 +12,100 @@ import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.findById; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @RunWith(RobolectricGradleTestRunner.class) @Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) -public class MultipleSaveTests { +public final class MultipleSaveTests { + private String testString = "Test String"; + private String anotherString = "Another test"; @Test public void stringMultipleSaveOriginalExtendedTest() { - String string = "Test String"; - StringFieldExtendedModel model = new StringFieldExtendedModel(string); + StringFieldExtendedModel model = new StringFieldExtendedModel(testString); long id = save(model); - StringFieldExtendedModel query = SugarRecord.findById(StringFieldExtendedModel.class, id); + StringFieldExtendedModel query = findById(StringFieldExtendedModel.class, id); if (null != query) { - assertEquals(string, query.getString()); + assertEquals(testString, query.getString()); } - model.setString("Another test"); + model.setString(anotherString); + assertEquals(id, save(model)); - assertNull(SugarRecord.findById(StringFieldExtendedModel.class, 2)); + assertNull(findById(StringFieldExtendedModel.class, 2)); } @Test public void stringMultipleSaveQueriedExtendedTest() { - String string = "Test String"; - StringFieldExtendedModel model = new StringFieldExtendedModel(string); + StringFieldExtendedModel model = new StringFieldExtendedModel(testString); long id = save(model); - StringFieldExtendedModel query = SugarRecord.findById(StringFieldExtendedModel.class, id); + StringFieldExtendedModel query = findById(StringFieldExtendedModel.class, id); if (null != query) { - assertEquals(string, query.getString()); - query.setString("Another test"); + assertEquals(testString, query.getString()); + query.setString(anotherString); assertEquals(id, save(query)); - assertNull(SugarRecord.findById(StringFieldExtendedModel.class, 2)); + assertNull(findById(StringFieldExtendedModel.class, 2)); } } @Test public void stringMultipleSaveOriginalAnnotatedTest() { - String string = "Test String"; - StringFieldAnnotatedModel model = new StringFieldAnnotatedModel(string); + StringFieldAnnotatedModel model = new StringFieldAnnotatedModel(testString); long id = save(model); - StringFieldAnnotatedModel query = SugarRecord.findById(StringFieldAnnotatedModel.class, id); + StringFieldAnnotatedModel query = findById(StringFieldAnnotatedModel.class, id); if (null != query) { - assertEquals(string, query.getString()); - model.setString("Another test"); + assertEquals(testString, query.getString()); + model.setString(anotherString); assertEquals(id, save(model)); - assertNull(SugarRecord.findById(StringFieldAnnotatedModel.class, 2)); + assertNull(findById(StringFieldAnnotatedModel.class, 2)); } } @Test public void stringMultipleSaveQueriedAnnotatedTest() { - String string = "Test String"; - StringFieldAnnotatedModel model = new StringFieldAnnotatedModel(string); + StringFieldAnnotatedModel model = new StringFieldAnnotatedModel(testString); long id = save(model); - StringFieldAnnotatedModel query = SugarRecord.findById(StringFieldAnnotatedModel.class, id); + StringFieldAnnotatedModel query = findById(StringFieldAnnotatedModel.class, id); if (null != query) { - assertEquals(string, query.getString()); - query.setString("Another test"); + assertEquals(testString, query.getString()); + query.setString(anotherString); assertEquals(id, save(query)); - assertNull(SugarRecord.findById(StringFieldAnnotatedModel.class, 2)); + assertNull(findById(StringFieldAnnotatedModel.class, 2)); } } @Test public void stringMultipleSaveOriginalAnnotatedNoIdTest() { - String string = "Test String"; - StringFieldAnnotatedNoIdModel model = new StringFieldAnnotatedNoIdModel(string); + StringFieldAnnotatedNoIdModel model = new StringFieldAnnotatedNoIdModel(testString); long id = save(model); - StringFieldAnnotatedNoIdModel query = - SugarRecord.findById(StringFieldAnnotatedNoIdModel.class, id); + StringFieldAnnotatedNoIdModel query = findById(StringFieldAnnotatedNoIdModel.class, id); if (null != query) { - assertEquals(string, query.getString()); - model.setString("Another test"); + assertEquals(testString, query.getString()); + model.setString(anotherString); assertEquals(id, save(model)); - assertNull(SugarRecord.findById(StringFieldAnnotatedNoIdModel.class, 2)); + assertNull(findById(StringFieldAnnotatedNoIdModel.class, 2)); } } @Test public void stringMultipleSaveQueriedAnnotatedNoIdTest() { - String string = "Test String"; - StringFieldAnnotatedNoIdModel model = new StringFieldAnnotatedNoIdModel(string); + StringFieldAnnotatedNoIdModel model = new StringFieldAnnotatedNoIdModel(testString); long id = save(model); - StringFieldAnnotatedNoIdModel query = - SugarRecord.findById(StringFieldAnnotatedNoIdModel.class, id); + StringFieldAnnotatedNoIdModel query = findById(StringFieldAnnotatedNoIdModel.class, id); if (null != query) { - assertEquals(string, query.getString()); - query.setString("Another test"); + assertEquals(testString, query.getString()); + query.setString(anotherString); assertEquals(id, save(query)); - assertNull(SugarRecord.findById(StringFieldAnnotatedNoIdModel.class, 2)); + assertNull(findById(StringFieldAnnotatedNoIdModel.class, 2)); } } } diff --git a/library/src/test/java/com/orm/record/NestedAnnotatedTests.java b/library/src/test/java/com/orm/record/NestedAnnotatedTests.java index 66fc2c79..d7c8ef43 100644 --- a/library/src/test/java/com/orm/record/NestedAnnotatedTests.java +++ b/library/src/test/java/com/orm/record/NestedAnnotatedTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.NestedAnnotatedModel; import com.orm.model.RelationshipAnnotatedModel; @@ -15,6 +14,9 @@ import java.util.List; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.count; +import static com.orm.SugarRecord.listAll; + import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) @@ -23,9 +25,9 @@ public final class NestedAnnotatedTests { @Test public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(NestedAnnotatedModel.class)); - assertEquals(0L, SugarRecord.count(RelationshipAnnotatedModel.class)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(0L, count(NestedAnnotatedModel.class)); + assertEquals(0L, count(RelationshipAnnotatedModel.class)); + assertEquals(0L, count(SimpleAnnotatedModel.class)); } @Test @@ -35,9 +37,9 @@ public void oneSaveTest() throws Exception { RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); save(nested); save(new NestedAnnotatedModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(NestedAnnotatedModel.class)); + assertEquals(1L, count(SimpleAnnotatedModel.class)); + assertEquals(1L, count(RelationshipAnnotatedModel.class)); + assertEquals(1L, count(NestedAnnotatedModel.class)); } @Test @@ -48,26 +50,27 @@ public void twoSameSaveTest() throws Exception { save(nested); save(new NestedAnnotatedModel(nested)); save(new NestedAnnotatedModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(NestedAnnotatedModel.class)); + + assertEquals(1L, count(SimpleAnnotatedModel.class)); + assertEquals(1L, count(RelationshipAnnotatedModel.class)); + assertEquals(2L, count(NestedAnnotatedModel.class)); } @Test public void twoDifferentSaveTest() throws Exception { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); - SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); - save(another_simple); + SimpleAnnotatedModel anotherSimple = new SimpleAnnotatedModel(); + save(anotherSimple); RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); save(nested); - RelationshipAnnotatedModel another_nested = new RelationshipAnnotatedModel(another_simple); - save(another_nested); + RelationshipAnnotatedModel anotherNested = new RelationshipAnnotatedModel(anotherSimple); + save(anotherNested); save(new NestedAnnotatedModel(nested)); - save(new NestedAnnotatedModel(another_nested)); - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(NestedAnnotatedModel.class)); + save(new NestedAnnotatedModel(anotherNested)); + assertEquals(2L, count(SimpleAnnotatedModel.class)); + assertEquals(2L, count(RelationshipAnnotatedModel.class)); + assertEquals(2L, count(NestedAnnotatedModel.class)); } @Test @@ -79,9 +82,9 @@ public void manySameSaveTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new NestedAnnotatedModel(nested)); } - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(NestedAnnotatedModel.class)); + assertEquals(1L, count(SimpleAnnotatedModel.class)); + assertEquals(1L, count(RelationshipAnnotatedModel.class)); + assertEquals(100L, count(NestedAnnotatedModel.class)); } @Test @@ -93,9 +96,9 @@ public void manyDifferentSaveTest() throws Exception { save(nested); save(new NestedAnnotatedModel(nested)); } - assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(NestedAnnotatedModel.class)); + assertEquals(100L, count(SimpleAnnotatedModel.class)); + assertEquals(100L, count(RelationshipAnnotatedModel.class)); + assertEquals(100L, count(NestedAnnotatedModel.class)); } @Test @@ -104,11 +107,14 @@ public void listAllSameTest() throws Exception { save(simple); RelationshipAnnotatedModel nested = new RelationshipAnnotatedModel(simple); save(nested); + for (int i = 1; i <= 100; i++) { save(new NestedAnnotatedModel(nested)); } - List models = SugarRecord.listAll(NestedAnnotatedModel.class); + + List models = listAll(NestedAnnotatedModel.class); assertEquals(100, models.size()); + for (NestedAnnotatedModel model : models) { assertEquals(nested.getId(), model.getNested().getId()); assertEquals(simple.getId(), model.getNested().getSimple().getId()); @@ -124,8 +130,10 @@ public void listAllDifferentTest() throws Exception { save(nested); save(new NestedAnnotatedModel(nested)); } - List models = SugarRecord.listAll(NestedAnnotatedModel.class); + + List models = listAll(NestedAnnotatedModel.class); assertEquals(100, models.size()); + for (NestedAnnotatedModel model : models) { assertEquals(model.getId(), model.getNested().getId()); assertEquals(model.getId(), model.getNested().getSimple().getId()); diff --git a/library/src/test/java/com/orm/record/NestedExtendedTests.java b/library/src/test/java/com/orm/record/NestedExtendedTests.java index 33823754..eaf8c120 100644 --- a/library/src/test/java/com/orm/record/NestedExtendedTests.java +++ b/library/src/test/java/com/orm/record/NestedExtendedTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.NestedExtendedModel; import com.orm.model.RelationshipExtendedModel; @@ -15,6 +14,9 @@ import java.util.List; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.count; +import static com.orm.SugarRecord.listAll; + import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) @@ -23,9 +25,9 @@ public final class NestedExtendedTests { @Test public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(NestedExtendedModel.class)); - assertEquals(0L, SugarRecord.count(RelationshipExtendedModel.class)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(0L, count(NestedExtendedModel.class)); + assertEquals(0L, count(RelationshipExtendedModel.class)); + assertEquals(0L, count(SimpleExtendedModel.class)); } @Test @@ -35,9 +37,9 @@ public void oneSaveTest() throws Exception { RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); save(nested); save(new NestedExtendedModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipExtendedModel.class)); - assertEquals(1L, SugarRecord.count(NestedExtendedModel.class)); + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(1L, count(RelationshipExtendedModel.class)); + assertEquals(1L, count(NestedExtendedModel.class)); } @Test @@ -48,26 +50,26 @@ public void twoSameSaveTest() throws Exception { save(nested); save(new NestedExtendedModel(nested)); save(new NestedExtendedModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipExtendedModel.class)); - assertEquals(2L, SugarRecord.count(NestedExtendedModel.class)); + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(1L, count(RelationshipExtendedModel.class)); + assertEquals(2L, count(NestedExtendedModel.class)); } @Test public void twoDifferentSaveTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); - SimpleExtendedModel another_simple = new SimpleExtendedModel(); - save(another_simple); + SimpleExtendedModel anotherSimple = new SimpleExtendedModel(); + save(anotherSimple); RelationshipExtendedModel nested = new RelationshipExtendedModel(simple); save(nested); - RelationshipExtendedModel another_nested = new RelationshipExtendedModel(another_simple); - save(another_nested); + RelationshipExtendedModel anotherNested = new RelationshipExtendedModel(anotherSimple); + save(anotherNested); save(new NestedExtendedModel(nested)); - save(new NestedExtendedModel(another_nested)); - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipExtendedModel.class)); - assertEquals(2L, SugarRecord.count(NestedExtendedModel.class)); + save(new NestedExtendedModel(anotherNested)); + assertEquals(2L, count(SimpleExtendedModel.class)); + assertEquals(2L, count(RelationshipExtendedModel.class)); + assertEquals(2L, count(NestedExtendedModel.class)); } @Test @@ -79,9 +81,9 @@ public void manySameSaveTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new NestedExtendedModel(nested)); } - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipExtendedModel.class)); - assertEquals(100L, SugarRecord.count(NestedExtendedModel.class)); + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(1L, count(RelationshipExtendedModel.class)); + assertEquals(100L, count(NestedExtendedModel.class)); } @Test @@ -93,9 +95,9 @@ public void manyDifferentSaveTest() throws Exception { save(nested); save(new NestedExtendedModel(nested)); } - assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipExtendedModel.class)); - assertEquals(100L, SugarRecord.count(NestedExtendedModel.class)); + assertEquals(100L, count(SimpleExtendedModel.class)); + assertEquals(100L, count(RelationshipExtendedModel.class)); + assertEquals(100L, count(NestedExtendedModel.class)); } @Test @@ -107,8 +109,10 @@ public void listAllSameTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new NestedExtendedModel(nested)); } - List models = SugarRecord.listAll(NestedExtendedModel.class); + + List models = listAll(NestedExtendedModel.class); assertEquals(100, models.size()); + for (NestedExtendedModel model : models) { assertEquals(nested.getId(), model.getNested().getId()); assertEquals(simple.getId(), model.getNested().getSimple().getId()); @@ -124,8 +128,10 @@ public void listAllDifferentTest() throws Exception { save(nested); save(new NestedExtendedModel(nested)); } - List models = SugarRecord.listAll(NestedExtendedModel.class); + + List models = listAll(NestedExtendedModel.class); assertEquals(100, models.size()); + for (NestedExtendedModel model : models) { assertEquals(model.getId(), model.getNested().getId()); assertEquals(model.getId(), model.getNested().getSimple().getId()); diff --git a/library/src/test/java/com/orm/record/NestedMixedAATests.java b/library/src/test/java/com/orm/record/NestedMixedAATests.java index 1a8f1b43..9daf3fa9 100644 --- a/library/src/test/java/com/orm/record/NestedMixedAATests.java +++ b/library/src/test/java/com/orm/record/NestedMixedAATests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.NestedMixedAAModel; import com.orm.model.RelationshipMixedAModel; @@ -15,6 +14,9 @@ import java.util.List; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.count; +import static com.orm.SugarRecord.listAll; + import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) @@ -23,9 +25,9 @@ public final class NestedMixedAATests { @Test public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(NestedMixedAAModel.class)); - assertEquals(0L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(0L, count(NestedMixedAAModel.class)); + assertEquals(0L, count(RelationshipMixedAModel.class)); + assertEquals(0L, count(SimpleAnnotatedModel.class)); } @Test @@ -35,9 +37,9 @@ public void oneSaveTest() throws Exception { RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); save(nested); save(new NestedMixedAAModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(1L, SugarRecord.count(NestedMixedAAModel.class)); + assertEquals(1L, count(SimpleAnnotatedModel.class)); + assertEquals(1L, count(RelationshipMixedAModel.class)); + assertEquals(1L, count(NestedMixedAAModel.class)); } @Test @@ -48,26 +50,26 @@ public void twoSameSaveTest() throws Exception { save(nested); save(new NestedMixedAAModel(nested)); save(new NestedMixedAAModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedAAModel.class)); + assertEquals(1L, count(SimpleAnnotatedModel.class)); + assertEquals(1L, count(RelationshipMixedAModel.class)); + assertEquals(2L, count(NestedMixedAAModel.class)); } @Test public void twoDifferentSaveTest() throws Exception { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); - SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); - save(another_simple); + SimpleAnnotatedModel anotherSimple = new SimpleAnnotatedModel(); + save(anotherSimple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); save(nested); - RelationshipMixedAModel another_nested = new RelationshipMixedAModel(another_simple); - save(another_nested); + RelationshipMixedAModel anotherNested = new RelationshipMixedAModel(anotherSimple); + save(anotherNested); save(new NestedMixedAAModel(nested)); - save(new NestedMixedAAModel(another_nested)); - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedAAModel.class)); + save(new NestedMixedAAModel(anotherNested)); + assertEquals(2L, count(SimpleAnnotatedModel.class)); + assertEquals(2L, count(RelationshipMixedAModel.class)); + assertEquals(2L, count(NestedMixedAAModel.class)); } @Test @@ -79,9 +81,9 @@ public void manySameSaveTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new NestedMixedAAModel(nested)); } - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedAAModel.class)); + assertEquals(1L, count(SimpleAnnotatedModel.class)); + assertEquals(1L, count(RelationshipMixedAModel.class)); + assertEquals(100L, count(NestedMixedAAModel.class)); } @Test @@ -93,9 +95,9 @@ public void manyDifferentSaveTest() throws Exception { save(nested); save(new NestedMixedAAModel(nested)); } - assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedAAModel.class)); + assertEquals(100L, count(SimpleAnnotatedModel.class)); + assertEquals(100L, count(RelationshipMixedAModel.class)); + assertEquals(100L, count(NestedMixedAAModel.class)); } @Test @@ -104,11 +106,14 @@ public void listAllSameTest() throws Exception { save(simple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); save(nested); + for (int i = 1; i <= 100; i++) { save(new NestedMixedAAModel(nested)); } - List models = SugarRecord.listAll(NestedMixedAAModel.class); + + List models = listAll(NestedMixedAAModel.class); assertEquals(100, models.size()); + for (NestedMixedAAModel model : models) { assertEquals(nested.getId(), model.getNested().getId()); assertEquals(simple.getId(), model.getNested().getSimple().getId()); @@ -124,8 +129,10 @@ public void listAllDifferentTest() throws Exception { save(nested); save(new NestedMixedAAModel(nested)); } - List models = SugarRecord.listAll(NestedMixedAAModel.class); + + List models = listAll(NestedMixedAAModel.class); assertEquals(100, models.size()); + for (NestedMixedAAModel model : models) { assertEquals(model.getId(), model.getNested().getId()); assertEquals(model.getId(), model.getNested().getSimple().getId()); diff --git a/library/src/test/java/com/orm/record/NestedMixedABTests.java b/library/src/test/java/com/orm/record/NestedMixedABTests.java index 110d82c0..fa16b506 100644 --- a/library/src/test/java/com/orm/record/NestedMixedABTests.java +++ b/library/src/test/java/com/orm/record/NestedMixedABTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.NestedMixedABModel; import com.orm.model.RelationshipMixedBModel; @@ -15,6 +14,9 @@ import java.util.List; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.count; +import static com.orm.SugarRecord.listAll; + import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) @@ -23,9 +25,9 @@ public class NestedMixedABTests { @Test public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(NestedMixedABModel.class)); - assertEquals(0L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(0L, count(NestedMixedABModel.class)); + assertEquals(0L, count(RelationshipMixedBModel.class)); + assertEquals(0L, count(SimpleExtendedModel.class)); } @Test @@ -33,11 +35,13 @@ public void oneSaveTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); save(new NestedMixedABModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(1L, SugarRecord.count(NestedMixedABModel.class)); + + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(1L, count(RelationshipMixedBModel.class)); + assertEquals(1L, count(NestedMixedABModel.class)); } @Test @@ -45,29 +49,33 @@ public void twoSameSaveTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); save(new NestedMixedABModel(nested)); save(new NestedMixedABModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedABModel.class)); + + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(1L, count(RelationshipMixedBModel.class)); + assertEquals(2L, count(NestedMixedABModel.class)); } @Test public void twoDifferentSaveTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); - SimpleExtendedModel another_simple = new SimpleExtendedModel(); - save(another_simple); + SimpleExtendedModel anotherSimple = new SimpleExtendedModel(); + save(anotherSimple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); save(nested); - RelationshipMixedBModel another_nested = new RelationshipMixedBModel(another_simple); - save(another_nested); + RelationshipMixedBModel anotherNested = new RelationshipMixedBModel(anotherSimple); + + save(anotherNested); save(new NestedMixedABModel(nested)); - save(new NestedMixedABModel(another_nested)); - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedABModel.class)); + save(new NestedMixedABModel(anotherNested)); + + assertEquals(2L, count(SimpleExtendedModel.class)); + assertEquals(2L, count(RelationshipMixedBModel.class)); + assertEquals(2L, count(NestedMixedABModel.class)); } @Test @@ -76,12 +84,14 @@ public void manySameSaveTest() throws Exception { save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); save(nested); + for (int i = 1; i <= 100; i++) { save(new NestedMixedABModel(nested)); } - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedABModel.class)); + + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(1L, count(RelationshipMixedBModel.class)); + assertEquals(100L, count(NestedMixedABModel.class)); } @Test @@ -93,9 +103,10 @@ public void manyDifferentSaveTest() throws Exception { save(nested); save(new NestedMixedABModel(nested)); } - assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedABModel.class)); + + assertEquals(100L, count(SimpleExtendedModel.class)); + assertEquals(100L, count(RelationshipMixedBModel.class)); + assertEquals(100L, count(NestedMixedABModel.class)); } @Test @@ -104,11 +115,14 @@ public void listAllSameTest() throws Exception { save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); save(nested); + for (int i = 1; i <= 100; i++) { save(new NestedMixedABModel(nested)); } - List models = SugarRecord.listAll(NestedMixedABModel.class); + + List models = listAll(NestedMixedABModel.class); assertEquals(100, models.size()); + for (NestedMixedABModel model : models) { assertEquals(nested.getId(), model.getNested().getId()); assertEquals(simple.getId(), model.getNested().getSimple().getId()); @@ -124,8 +138,10 @@ public void listAllDifferentTest() throws Exception { save(nested); save(new NestedMixedABModel(nested)); } - List models = SugarRecord.listAll(NestedMixedABModel.class); + + List models = listAll(NestedMixedABModel.class); assertEquals(100, models.size()); + for (NestedMixedABModel model : models) { assertEquals(model.getId(), model.getNested().getId()); assertEquals(model.getId(), model.getNested().getSimple().getId()); diff --git a/library/src/test/java/com/orm/record/NestedMixedBATests.java b/library/src/test/java/com/orm/record/NestedMixedBATests.java index edc1c575..c6f07da5 100644 --- a/library/src/test/java/com/orm/record/NestedMixedBATests.java +++ b/library/src/test/java/com/orm/record/NestedMixedBATests.java @@ -15,6 +15,9 @@ import java.util.List; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.count; +import static com.orm.SugarRecord.listAll; + import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) @@ -23,9 +26,9 @@ public final class NestedMixedBATests { @Test public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(NestedMixedBAModel.class)); - assertEquals(0L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(0L, count(NestedMixedBAModel.class)); + assertEquals(0L, count(RelationshipMixedAModel.class)); + assertEquals(0L, count(SimpleAnnotatedModel.class)); } @Test @@ -33,11 +36,13 @@ public void oneSaveTest() throws Exception { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); save(new NestedMixedBAModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(1L, SugarRecord.count(NestedMixedBAModel.class)); + + assertEquals(1L, count(SimpleAnnotatedModel.class)); + assertEquals(1L, count(RelationshipMixedAModel.class)); + assertEquals(1L, count(NestedMixedBAModel.class)); } @Test @@ -45,29 +50,33 @@ public void twoSameSaveTest() throws Exception { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); + save(nested); save(new NestedMixedBAModel(nested)); save(new NestedMixedBAModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedBAModel.class)); + + assertEquals(1L, count(SimpleAnnotatedModel.class)); + assertEquals(1L, count(RelationshipMixedAModel.class)); + assertEquals(2L, count(NestedMixedBAModel.class)); } @Test public void twoDifferentSaveTest() throws Exception { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); - SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); - save(another_simple); + SimpleAnnotatedModel anotherSimple = new SimpleAnnotatedModel(); + save(anotherSimple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); save(nested); - RelationshipMixedAModel another_nested = new RelationshipMixedAModel(another_simple); - save(another_nested); + RelationshipMixedAModel anotherNested = new RelationshipMixedAModel(anotherSimple); + + save(anotherNested); save(new NestedMixedBAModel(nested)); - save(new NestedMixedBAModel(another_nested)); - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedBAModel.class)); + save(new NestedMixedBAModel(anotherNested)); + + assertEquals(2L, count(SimpleAnnotatedModel.class)); + assertEquals(2L, count(RelationshipMixedAModel.class)); + assertEquals(2L, count(NestedMixedBAModel.class)); } @Test @@ -76,12 +85,14 @@ public void manySameSaveTest() throws Exception { save(simple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); save(nested); + for (int i = 1; i <= 100; i++) { save(new NestedMixedBAModel(nested)); } - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedBAModel.class)); + + assertEquals(1L, count(SimpleAnnotatedModel.class)); + assertEquals(1L, count(RelationshipMixedAModel.class)); + assertEquals(100L, count(NestedMixedBAModel.class)); } @Test @@ -93,9 +104,10 @@ public void manyDifferentSaveTest() throws Exception { save(nested); save(new NestedMixedBAModel(nested)); } - assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipMixedAModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedBAModel.class)); + + assertEquals(100L, count(SimpleAnnotatedModel.class)); + assertEquals(100L, count(RelationshipMixedAModel.class)); + assertEquals(100L, count(NestedMixedBAModel.class)); } @Test @@ -104,11 +116,14 @@ public void listAllSameTest() throws Exception { save(simple); RelationshipMixedAModel nested = new RelationshipMixedAModel(simple); save(nested); + for (int i = 1; i <= 100; i++) { save(new NestedMixedBAModel(nested)); } - List models = SugarRecord.listAll(NestedMixedBAModel.class); + + List models = listAll(NestedMixedBAModel.class); assertEquals(100, models.size()); + for (NestedMixedBAModel model : models) { assertEquals(nested.getId(), model.getNested().getId()); assertEquals(simple.getId(), model.getNested().getSimple().getId()); @@ -124,8 +139,10 @@ public void listAllDifferentTest() throws Exception { save(nested); save(new NestedMixedBAModel(nested)); } - List models = SugarRecord.listAll(NestedMixedBAModel.class); + + List models = listAll(NestedMixedBAModel.class); assertEquals(100, models.size()); + for (NestedMixedBAModel model : models) { assertEquals(model.getId(), model.getNested().getId()); assertEquals(model.getId(), model.getNested().getSimple().getId()); diff --git a/library/src/test/java/com/orm/record/NestedMixedBBTests.java b/library/src/test/java/com/orm/record/NestedMixedBBTests.java index c5ebd4ca..1eeb2a7d 100644 --- a/library/src/test/java/com/orm/record/NestedMixedBBTests.java +++ b/library/src/test/java/com/orm/record/NestedMixedBBTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.NestedMixedBBModel; import com.orm.model.RelationshipMixedBModel; @@ -15,6 +14,9 @@ import java.util.List; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.count; +import static com.orm.SugarRecord.listAll; + import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) @@ -23,21 +25,23 @@ public class NestedMixedBBTests { @Test public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(NestedMixedBBModel.class)); - assertEquals(0L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(0L, count(NestedMixedBBModel.class)); + assertEquals(0L, count(RelationshipMixedBModel.class)); + assertEquals(0L, count(SimpleExtendedModel.class)); } @Test public void oneSaveTest() throws Exception { - SimpleExtendedModel simple = new SimpleExtendedModel(); + SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); save(new NestedMixedBBModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(1L, SugarRecord.count(NestedMixedBBModel.class)); + + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(1L, count(RelationshipMixedBModel.class)); + assertEquals(1L, count(NestedMixedBBModel.class)); } @Test @@ -45,29 +49,33 @@ public void twoSameSaveTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); + save(nested); save(new NestedMixedBBModel(nested)); save(new NestedMixedBBModel(nested)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedBBModel.class)); + + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(1L, count(RelationshipMixedBModel.class)); + assertEquals(2L, count(NestedMixedBBModel.class)); } @Test public void twoDifferentSaveTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); - SimpleExtendedModel another_simple = new SimpleExtendedModel(); - save(another_simple); + SimpleExtendedModel anotherSimple = new SimpleExtendedModel(); + save(anotherSimple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); save(nested); - RelationshipMixedBModel another_nested = new RelationshipMixedBModel(another_simple); - save(another_nested); + RelationshipMixedBModel anotherNested = new RelationshipMixedBModel(anotherSimple); + + save(anotherNested); save(new NestedMixedBBModel(nested)); - save(new NestedMixedBBModel(another_nested)); - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(2L, SugarRecord.count(NestedMixedBBModel.class)); + save(new NestedMixedBBModel(anotherNested)); + + assertEquals(2L, count(SimpleExtendedModel.class)); + assertEquals(2L, count(RelationshipMixedBModel.class)); + assertEquals(2L, count(NestedMixedBBModel.class)); } @Test @@ -76,12 +84,14 @@ public void manySameSaveTest() throws Exception { save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); save(nested); + for (int i = 1; i <= 100; i++) { save(new NestedMixedBBModel(nested)); } - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedBBModel.class)); + + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(1L, count(RelationshipMixedBModel.class)); + assertEquals(100L, count(NestedMixedBBModel.class)); } @Test @@ -93,9 +103,10 @@ public void manyDifferentSaveTest() throws Exception { save(nested); save(new NestedMixedBBModel(nested)); } - assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(100L, SugarRecord.count(NestedMixedBBModel.class)); + + assertEquals(100L, count(SimpleExtendedModel.class)); + assertEquals(100L, count(RelationshipMixedBModel.class)); + assertEquals(100L, count(NestedMixedBBModel.class)); } @Test @@ -104,11 +115,14 @@ public void listAllSameTest() throws Exception { save(simple); RelationshipMixedBModel nested = new RelationshipMixedBModel(simple); save(nested); + for (int i = 1; i <= 100; i++) { save(new NestedMixedBBModel(nested)); } - List models = SugarRecord.listAll(NestedMixedBBModel.class); + + List models = listAll(NestedMixedBBModel.class); assertEquals(100, models.size()); + for (NestedMixedBBModel model : models) { assertEquals(nested.getId(), model.getNested().getId()); assertEquals(simple.getId(), model.getNested().getSimple().getId()); @@ -124,8 +138,10 @@ public void listAllDifferentTest() throws Exception { save(nested); save(new NestedMixedBBModel(nested)); } - List models = SugarRecord.listAll(NestedMixedBBModel.class); + + List models = listAll(NestedMixedBBModel.class); assertEquals(100, models.size()); + for (NestedMixedBBModel model : models) { assertEquals(model.getId(), model.getNested().getId()); assertEquals(model.getId(), model.getNested().getSimple().getId()); diff --git a/library/src/test/java/com/orm/record/NoSugarModelTests.java b/library/src/test/java/com/orm/record/NoSugarModelTests.java index 14b16493..08f43154 100644 --- a/library/src/test/java/com/orm/record/NoSugarModelTests.java +++ b/library/src/test/java/com/orm/record/NoSugarModelTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.NoSugarModel; @@ -10,6 +9,10 @@ import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; +import static com.orm.SugarRecord.delete; +import static com.orm.SugarRecord.count; +import static com.orm.SugarRecord.saveInTx; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -20,12 +23,12 @@ public final class NoSugarModelTests { @Test public void deleteTest() throws Exception { NoSugarModel model = new NoSugarModel(); - assertFalse(SugarRecord.delete(model)); + assertFalse(delete(model)); } @Test public void saveInTransactionTest() throws Exception { - SugarRecord.saveInTx(new NoSugarModel(), new NoSugarModel()); - assertEquals(-1L, SugarRecord.count(NoSugarModel.class)); + saveInTx(new NoSugarModel(), new NoSugarModel()); + assertEquals(-1L, count(NoSugarModel.class)); } } diff --git a/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java b/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java index fec134db..506e0264 100644 --- a/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java +++ b/library/src/test/java/com/orm/record/RelationshipAnnotatedTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.RelationshipAnnotatedModel; import com.orm.model.SimpleAnnotatedModel; @@ -14,6 +13,9 @@ import java.util.List; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.count; +import static com.orm.SugarRecord.listAll; + import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) @@ -22,50 +24,58 @@ public final class RelationshipAnnotatedTests { @Test public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(RelationshipAnnotatedModel.class)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(0L, count(RelationshipAnnotatedModel.class)); + assertEquals(0L, count(SimpleAnnotatedModel.class)); } @Test public void oneSaveTest() throws Exception { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); save(new RelationshipAnnotatedModel(simple)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipAnnotatedModel.class)); + + assertEquals(1L, count(SimpleAnnotatedModel.class)); + assertEquals(1L, count(RelationshipAnnotatedModel.class)); } @Test public void twoSameSaveTest() throws Exception { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); save(new RelationshipAnnotatedModel(simple)); save(new RelationshipAnnotatedModel(simple)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipAnnotatedModel.class)); + + assertEquals(1L, count(SimpleAnnotatedModel.class)); + assertEquals(2L, count(RelationshipAnnotatedModel.class)); } @Test public void twoDifferentSaveTest() throws Exception { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); - SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); - save(another_simple); + SimpleAnnotatedModel anotherSimple = new SimpleAnnotatedModel(); + + save(anotherSimple); save(new RelationshipAnnotatedModel(simple)); - save(new RelationshipAnnotatedModel(another_simple)); - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipAnnotatedModel.class)); + save(new RelationshipAnnotatedModel(anotherSimple)); + + assertEquals(2L, count(SimpleAnnotatedModel.class)); + assertEquals(2L, count(RelationshipAnnotatedModel.class)); } @Test public void manySameSaveTest() throws Exception { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); + for (int i = 1; i <= 100; i++) { save(new RelationshipAnnotatedModel(simple)); } - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipAnnotatedModel.class)); + + assertEquals(1L, count(SimpleAnnotatedModel.class)); + assertEquals(100L, count(RelationshipAnnotatedModel.class)); } @Test @@ -75,20 +85,23 @@ public void manyDifferentSaveTest() throws Exception { save(simple); save(new RelationshipAnnotatedModel(simple)); } - assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipAnnotatedModel.class)); + + assertEquals(100L, count(SimpleAnnotatedModel.class)); + assertEquals(100L, count(RelationshipAnnotatedModel.class)); } @Test public void listAllSameTest() throws Exception { SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); save(simple); + for (int i = 1; i <= 100; i++) { save(new RelationshipAnnotatedModel(simple)); } - List models = - SugarRecord.listAll(RelationshipAnnotatedModel.class); + + List models = listAll(RelationshipAnnotatedModel.class); assertEquals(100, models.size()); + for (RelationshipAnnotatedModel model : models) { assertEquals(simple.getId(), model.getSimple().getId()); } @@ -101,9 +114,10 @@ public void listAllDifferentTest() throws Exception { save(simple); save(new RelationshipAnnotatedModel(simple)); } - List models = - SugarRecord.listAll(RelationshipAnnotatedModel.class); + + List models = listAll(RelationshipAnnotatedModel.class); assertEquals(100, models.size()); + for (RelationshipAnnotatedModel model : models) { assertEquals(model.getId(), model.getSimple().getId()); } diff --git a/library/src/test/java/com/orm/record/RelationshipExtendedTests.java b/library/src/test/java/com/orm/record/RelationshipExtendedTests.java index 1ff689ee..f9faeedd 100644 --- a/library/src/test/java/com/orm/record/RelationshipExtendedTests.java +++ b/library/src/test/java/com/orm/record/RelationshipExtendedTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.RelationshipExtendedModel; import com.orm.model.SimpleExtendedModel; @@ -14,6 +13,9 @@ import java.util.List; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.count; +import static com.orm.SugarRecord.listAll; + import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) @@ -22,50 +24,58 @@ public class RelationshipExtendedTests { @Test public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(RelationshipExtendedModel.class)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(0L, count(RelationshipExtendedModel.class)); + assertEquals(0L, count(SimpleExtendedModel.class)); } @Test public void oneSaveTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); save(new RelationshipExtendedModel(simple)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipExtendedModel.class)); + + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(1L, count(RelationshipExtendedModel.class)); } @Test public void twoSameSaveTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); save(new RelationshipExtendedModel(simple)); save(new RelationshipExtendedModel(simple)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipExtendedModel.class)); + + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(2L, count(RelationshipExtendedModel.class)); } @Test public void twoDifferentSaveTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); - SimpleExtendedModel another_simple = new SimpleExtendedModel(); - save(another_simple); + SimpleExtendedModel anotherSimple = new SimpleExtendedModel(); + + save(anotherSimple); save(new RelationshipExtendedModel(simple)); - save(new RelationshipExtendedModel(another_simple)); - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipExtendedModel.class)); + save(new RelationshipExtendedModel(anotherSimple)); + + assertEquals(2L, count(SimpleExtendedModel.class)); + assertEquals(2L, count(RelationshipExtendedModel.class)); } @Test public void manySameSaveTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); + for (int i = 1; i <= 100; i++) { save(new RelationshipExtendedModel(simple)); } - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipExtendedModel.class)); + + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(100L, count(RelationshipExtendedModel.class)); } @Test @@ -75,20 +85,23 @@ public void manyDifferentSaveTest() throws Exception { save(simple); save(new RelationshipExtendedModel(simple)); } - assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipExtendedModel.class)); + + assertEquals(100L, count(SimpleExtendedModel.class)); + assertEquals(100L, count(RelationshipExtendedModel.class)); } @Test public void listAllSameTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); + for (int i = 1; i <= 100; i++) { save(new RelationshipExtendedModel(simple)); } - List models = - SugarRecord.listAll(RelationshipExtendedModel.class); + + List models = listAll(RelationshipExtendedModel.class); assertEquals(100, models.size()); + for (RelationshipExtendedModel model : models) { assertEquals(simple.getId(), model.getSimple().getId()); } @@ -101,9 +114,10 @@ public void listAllDifferentTest() throws Exception { save(simple); save(new RelationshipExtendedModel(simple)); } - List models = - SugarRecord.listAll(RelationshipExtendedModel.class); + + List models = listAll(RelationshipExtendedModel.class); assertEquals(100, models.size()); + for (RelationshipExtendedModel model : models) { assertEquals(model.getId(), model.getSimple().getId()); } diff --git a/library/src/test/java/com/orm/record/RelationshipMixedBTests.java b/library/src/test/java/com/orm/record/RelationshipMixedBTests.java index 112cdd50..7792fce8 100644 --- a/library/src/test/java/com/orm/record/RelationshipMixedBTests.java +++ b/library/src/test/java/com/orm/record/RelationshipMixedBTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.RelationshipMixedBModel; import com.orm.model.SimpleExtendedModel; @@ -14,6 +13,9 @@ import java.util.List; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.count; +import static com.orm.SugarRecord.listAll; + import static org.junit.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) @@ -22,50 +24,58 @@ public class RelationshipMixedBTests { @Test public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(RelationshipMixedBModel.class)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(0L, count(RelationshipMixedBModel.class)); + assertEquals(0L, count(SimpleExtendedModel.class)); } @Test public void oneSaveTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); save(new RelationshipMixedBModel(simple)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(1L, SugarRecord.count(RelationshipMixedBModel.class)); + + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(1L, count(RelationshipMixedBModel.class)); } @Test public void twoSameSaveTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); save(new RelationshipMixedBModel(simple)); save(new RelationshipMixedBModel(simple)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipMixedBModel.class)); + + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(2L, count(RelationshipMixedBModel.class)); } @Test public void twoDifferentSaveTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); - SimpleExtendedModel another_simple = new SimpleExtendedModel(); - save(another_simple); + SimpleExtendedModel anotherSimple = new SimpleExtendedModel(); + + save(anotherSimple); save(new RelationshipMixedBModel(simple)); - save(new RelationshipMixedBModel(another_simple)); - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2L, SugarRecord.count(RelationshipMixedBModel.class)); + save(new RelationshipMixedBModel(anotherSimple)); + + assertEquals(2L, count(SimpleExtendedModel.class)); + assertEquals(2L, count(RelationshipMixedBModel.class)); } @Test public void manySameSaveTest() throws Exception { SimpleExtendedModel simple = new SimpleExtendedModel(); save(simple); + for (int i = 1; i <= 100; i++) { save(new RelationshipMixedBModel(simple)); } - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipMixedBModel.class)); + + assertEquals(1L, count(SimpleExtendedModel.class)); + assertEquals(100L, count(RelationshipMixedBModel.class)); } @Test @@ -75,8 +85,9 @@ public void manyDifferentSaveTest() throws Exception { save(simple); save(new RelationshipMixedBModel(simple)); } - assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(100L, SugarRecord.count(RelationshipMixedBModel.class)); + + assertEquals(100L, count(SimpleExtendedModel.class)); + assertEquals(100L, count(RelationshipMixedBModel.class)); } @Test @@ -86,8 +97,7 @@ public void listAllSameTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new RelationshipMixedBModel(simple)); } - List models = - SugarRecord.listAll(RelationshipMixedBModel.class); + List models = listAll(RelationshipMixedBModel.class); assertEquals(100, models.size()); for (RelationshipMixedBModel model : models) { assertEquals(simple.getId(), model.getSimple().getId()); @@ -101,8 +111,7 @@ public void listAllDifferentTest() throws Exception { save(simple); save(new RelationshipMixedBModel(simple)); } - List models = - SugarRecord.listAll(RelationshipMixedBModel.class); + List models = listAll(RelationshipMixedBModel.class); assertEquals(100, models.size()); for (RelationshipMixedBModel model : models) { assertEquals(model.getId(), model.getSimple().getId()); diff --git a/library/src/test/java/com/orm/record/ShortFieldTests.java b/library/src/test/java/com/orm/record/ShortFieldTests.java index 489325fc..1a65f676 100644 --- a/library/src/test/java/com/orm/record/ShortFieldTests.java +++ b/library/src/test/java/com/orm/record/ShortFieldTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.ShortFieldAnnotatedModel; import com.orm.model.ShortFieldExtendedModel; @@ -12,68 +11,70 @@ import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.findById; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +@SuppressWarnings("all") @RunWith(RobolectricGradleTestRunner.class) @Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public final class ShortFieldTests { + private Short aShort = Short.valueOf((short) 25); @Test public void nullShortExtendedTest() { save(new ShortFieldExtendedModel()); - ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); + ShortFieldExtendedModel model = findById(ShortFieldExtendedModel.class, 1); assertNull(model.getShort()); } @Test public void nullRawShortExtendedTest() { save(new ShortFieldExtendedModel()); - ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); + ShortFieldExtendedModel model = findById(ShortFieldExtendedModel.class, 1); assertEquals((short) 0, model.getRawShort()); } @Test public void nullShortAnnotatedTest() { save(new ShortFieldAnnotatedModel()); - ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); + ShortFieldAnnotatedModel model = findById(ShortFieldAnnotatedModel.class, 1); assertNull(model.getShort()); } @Test public void nullRawShortAnnotatedTest() { save(new ShortFieldAnnotatedModel()); - ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); + ShortFieldAnnotatedModel model = findById(ShortFieldAnnotatedModel.class, 1); assertEquals((short) 0, model.getRawShort()); } @Test public void objectShortExtendedTest() { - Short objectShort = 25; - save(new ShortFieldExtendedModel(objectShort)); - ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); - assertEquals(objectShort, model.getShort()); + save(new ShortFieldExtendedModel(aShort)); + ShortFieldExtendedModel model = findById(ShortFieldExtendedModel.class, 1); + assertEquals(aShort, model.getShort()); } @Test public void rawShortExtendedTest() { - save(new ShortFieldExtendedModel((short) 25)); - ShortFieldExtendedModel model = SugarRecord.findById(ShortFieldExtendedModel.class, 1); - assertEquals((short) 25, model.getRawShort()); + save(new ShortFieldExtendedModel(aShort.shortValue())); + ShortFieldExtendedModel model = findById(ShortFieldExtendedModel.class, 1); + assertEquals(aShort.shortValue(), model.getRawShort()); } @Test public void objectShortAnnotatedTest() { - Short objectShort = 25; - save(new ShortFieldAnnotatedModel(objectShort)); - ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); - assertEquals(objectShort, model.getShort()); + save(new ShortFieldAnnotatedModel(aShort)); + ShortFieldAnnotatedModel model = findById(ShortFieldAnnotatedModel.class, 1); + assertEquals(aShort, model.getShort()); } @Test public void rawShortAnnotatedTest() { - save(new ShortFieldAnnotatedModel((short) 25)); - ShortFieldAnnotatedModel model = SugarRecord.findById(ShortFieldAnnotatedModel.class, 1); - assertEquals((short) 25, model.getRawShort()); + save(new ShortFieldAnnotatedModel(aShort.shortValue())); + ShortFieldAnnotatedModel model = findById(ShortFieldAnnotatedModel.class, 1); + assertEquals(aShort.shortValue(), model.getRawShort()); } } diff --git a/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java b/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java index 6e4b9a7a..a201a9eb 100644 --- a/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java +++ b/library/src/test/java/com/orm/record/SimpleAnnotatedModelTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.helper.NamingHelper; import com.orm.model.SimpleAnnotatedModel; @@ -18,6 +17,20 @@ import java.util.NoSuchElementException; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.count; +import static com.orm.SugarRecord.deleteAll; +import static com.orm.SugarRecord.delete; +import static com.orm.SugarRecord.deleteInTx; +import static com.orm.SugarRecord.listAll; +import static com.orm.SugarRecord.findById; +import static com.orm.SugarRecord.saveInTx; +import static com.orm.SugarRecord.find; +import static com.orm.SugarRecord.findAsIterator; +import static com.orm.SugarRecord.findWithQuery; +import static com.orm.SugarRecord.findAll; +import static com.orm.SugarRecord.findWithQueryAsIterator; +import static com.orm.SugarRecord.executeQuery; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -30,20 +43,20 @@ public final class SimpleAnnotatedModelTests { @Test public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(0L, count(SimpleAnnotatedModel.class)); } @Test public void oneSaveTest() throws Exception { save(new SimpleAnnotatedModel()); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, count(SimpleAnnotatedModel.class)); } @Test public void twoSaveTest() throws Exception { save(new SimpleAnnotatedModel()); save(new SimpleAnnotatedModel()); - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(2L, count(SimpleAnnotatedModel.class)); } @Test @@ -51,7 +64,7 @@ public void manySaveTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new SimpleAnnotatedModel()); } - assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(100L, count(SimpleAnnotatedModel.class)); } @Test @@ -63,73 +76,75 @@ public void defaultIdTest() throws Exception { public void whereCountTest() throws Exception { save(new SimpleAnnotatedModel()); save(new SimpleAnnotatedModel()); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class, "id = ?", new String[]{"1"})); + assertEquals(1L, count(SimpleAnnotatedModel.class, "id = ?", new String[]{"1"})); } @Test public void whereNoCountTest() throws Exception { - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class, "id = ?", new String[]{"1"})); + assertEquals(0L, count(SimpleAnnotatedModel.class, "id = ?", new String[]{"1"})); save(new SimpleAnnotatedModel()); save(new SimpleAnnotatedModel()); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class, "id = ?", new String[]{"3"})); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class, "id = ?", new String[]{"a"})); + assertEquals(0L, count(SimpleAnnotatedModel.class, "id = ?", new String[]{"3"})); + assertEquals(0L, count(SimpleAnnotatedModel.class, "id = ?", new String[]{"a"})); } @Test public void whereBrokenCountTest() throws Exception { save(new SimpleAnnotatedModel()); save(new SimpleAnnotatedModel()); - assertEquals(-1L, SugarRecord.count(SimpleAnnotatedModel.class, "di = ?", new String[]{"1"})); + assertEquals(-1L, count(SimpleAnnotatedModel.class, "di = ?", new String[]{"1"})); } @Test public void deleteTest() throws Exception { SimpleAnnotatedModel model = new SimpleAnnotatedModel(); save(model); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertTrue(SugarRecord.delete(model)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, count(SimpleAnnotatedModel.class)); + assertTrue(delete(model)); + assertEquals(0L, count(SimpleAnnotatedModel.class)); } @Test public void deleteUnsavedTest() throws Exception { SimpleAnnotatedModel model = new SimpleAnnotatedModel(); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertFalse(SugarRecord.delete(model)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(0L, count(SimpleAnnotatedModel.class)); + assertFalse(delete(model)); + assertEquals(0L, count(SimpleAnnotatedModel.class)); } @Test public void deleteWrongTest() throws Exception { SimpleAnnotatedModel model = new SimpleAnnotatedModel(); save(model); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, count(SimpleAnnotatedModel.class)); + Field idField = model.getClass().getDeclaredField("id"); idField.setAccessible(true); idField.set(model, Long.MAX_VALUE); - assertFalse(SugarRecord.delete(model)); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + + assertFalse(delete(model)); + assertEquals(1L, count(SimpleAnnotatedModel.class)); } @Test public void deleteAllTest() throws Exception { - int elementNumber = 100; - for (int i = 1; i <= elementNumber; i++) { + for (int i = 1; i <= 100; i++) { save(new SimpleAnnotatedModel()); } - assertEquals(elementNumber, SugarRecord.deleteAll(SimpleAnnotatedModel.class)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + + assertEquals(100, deleteAll(SimpleAnnotatedModel.class)); + assertEquals(0L, count(SimpleAnnotatedModel.class)); } @Test @SuppressWarnings("all") public void deleteAllWhereTest() throws Exception { - int elementNumber = 100; - for (int i = 1; i <= elementNumber; i++) { + for (int i = 1; i <= 100; i++) { save(new SimpleAnnotatedModel()); } - assertEquals(elementNumber - 1, SugarRecord.deleteAll(SimpleAnnotatedModel.class, "id > ?", new String[]{"1"})); - assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + + assertEquals(99, deleteAll(SimpleAnnotatedModel.class, "id > ?", new String[]{"1"})); + assertEquals(1L, count(SimpleAnnotatedModel.class)); } @Test @@ -140,32 +155,33 @@ public void deleteInTransactionFewTest() throws Exception { save(first); save(second); // Not saving last model - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(2, SugarRecord.deleteInTx(first, second, third)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(2L, count(SimpleAnnotatedModel.class)); + assertEquals(2, deleteInTx(first, second, third)); + assertEquals(0L, count(SimpleAnnotatedModel.class)); } @Test public void deleteInTransactionManyTest() throws Exception { - long elementNumber = 100; List models = new ArrayList<>(); - for (int i = 1; i <= elementNumber; i++) { + + for (int i = 1; i <= 100; i++) { SimpleAnnotatedModel model = new SimpleAnnotatedModel(); models.add(model); // Not saving last model - if (i < elementNumber) { + if (i < 100) { save(model); } } - assertEquals(elementNumber - 1, SugarRecord.count(SimpleAnnotatedModel.class)); - assertEquals(elementNumber - 1, SugarRecord.deleteInTx(models)); - assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + + assertEquals(99, count(SimpleAnnotatedModel.class)); + assertEquals(99, deleteInTx(models)); + assertEquals(0L, count(SimpleAnnotatedModel.class)); } @Test public void saveInTransactionTest() throws Exception { - SugarRecord.saveInTx(new SimpleAnnotatedModel(), new SimpleAnnotatedModel()); - assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); + saveInTx(new SimpleAnnotatedModel(), new SimpleAnnotatedModel()); + assertEquals(2L, count(SimpleAnnotatedModel.class)); } @Test @@ -173,8 +189,10 @@ public void listAllTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new SimpleAnnotatedModel()); } - List models = SugarRecord.listAll(SimpleAnnotatedModel.class); + + List models = listAll(SimpleAnnotatedModel.class); assertEquals(100, models.size()); + for (long i = 1; i <= 100; i++) { assertEquals(Long.valueOf(i), models.get((int) i - 1).getId()); } @@ -184,8 +202,9 @@ public void listAllTest() throws Exception { public void findTest() throws Exception { save(new SimpleAnnotatedModel()); save(new SimpleAnnotatedModel()); - List models = - SugarRecord.find(SimpleAnnotatedModel.class, "id = ?", "2"); + + List models = find(SimpleAnnotatedModel.class, "id = ?", "2"); + assertEquals(1, models.size()); assertEquals(2L, models.get(0).getId().longValue()); } @@ -195,10 +214,11 @@ public void findWithQueryTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new SimpleAnnotatedModel()); } - List models = - SugarRecord.findWithQuery(SimpleAnnotatedModel.class, "Select * from " + + + List models = findWithQuery(SimpleAnnotatedModel.class, "Select * from " + NamingHelper.toTableName(SimpleAnnotatedModel.class) + " where id >= ? ", "50"); + for (SimpleAnnotatedModel model : models) { assertEquals(75L, model.getId(), 25L); } @@ -208,26 +228,25 @@ public void findWithQueryTest() throws Exception { @SuppressWarnings("all") public void findByIdTest() throws Exception { save(new SimpleAnnotatedModel()); - assertEquals(1L, SugarRecord.findById(SimpleAnnotatedModel.class, 1L).getId().longValue()); + assertEquals(1L, findById(SimpleAnnotatedModel.class, 1L).getId().longValue()); } @Test public void findByIdIntegerTest() throws Exception { save(new SimpleAnnotatedModel()); - assertEquals(1L, SugarRecord.findById(SimpleAnnotatedModel.class, 1).getId().longValue()); + assertEquals(1L, findById(SimpleAnnotatedModel.class, 1).getId().longValue()); } @Test public void findByIdStringsNullTest() throws Exception { save(new SimpleAnnotatedModel()); - assertEquals(0, SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{""}).size()); + assertEquals(0, findById(SimpleAnnotatedModel.class, new String[]{""}).size()); } @Test public void findByIdStringsOneTest() throws Exception { save(new SimpleAnnotatedModel()); - List models = - SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{"1"}); + List models = findById(SimpleAnnotatedModel.class, new String[]{"1"}); assertEquals(1, models.size()); assertEquals(1L, models.get(0).getId().longValue()); } @@ -237,8 +256,7 @@ public void findByIdStringsTwoTest() throws Exception { save(new SimpleAnnotatedModel()); save(new SimpleAnnotatedModel()); save(new SimpleAnnotatedModel()); - List models = - SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{"1", "3"}); + List models = findById(SimpleAnnotatedModel.class, new String[]{"1", "3"}); assertEquals(2, models.size()); assertEquals(Long.valueOf(1L), models.get(0).getId()); assertEquals(Long.valueOf(3L), models.get(1).getId()); @@ -249,8 +267,7 @@ public void findByIdStringsManyTest() throws Exception { for (int i = 1; i <= 10; i++) { save(new SimpleAnnotatedModel()); } - List models = - SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{"1", "3", "6", "10"}); + List models = findById(SimpleAnnotatedModel.class, new String[]{"1", "3", "6", "10"}); assertEquals(4, models.size()); assertEquals(Long.valueOf(1L), models.get(0).getId()); assertEquals(Long.valueOf(3L), models.get(1).getId()); @@ -263,8 +280,7 @@ public void findByIdStringsOrderTest() throws Exception { for (int i = 1; i <= 10; i++) { save(new SimpleAnnotatedModel()); } - List models = - SugarRecord.findById(SimpleAnnotatedModel.class, new String[]{"10", "6", "3", "1"}); + List models = findById(SimpleAnnotatedModel.class, new String[]{"10", "6", "3", "1"}); assertEquals(4, models.size()); // The order of the query doesn't matter assertEquals(Long.valueOf(1L), models.get(0).getId()); @@ -276,7 +292,7 @@ public void findByIdStringsOrderTest() throws Exception { @Test public void findByIdNullTest() throws Exception { save(new SimpleAnnotatedModel()); - assertNull(SugarRecord.findById(SimpleAnnotatedModel.class, 2L)); + assertNull(findById(SimpleAnnotatedModel.class, 2L)); } @Test @@ -284,7 +300,7 @@ public void findAllTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new SimpleAnnotatedModel()); } - Iterator cursor = SugarRecord.findAll(SimpleAnnotatedModel.class); + Iterator cursor = findAll(SimpleAnnotatedModel.class); for (int i = 1; i <= 100; i++) { assertTrue(cursor.hasNext()); SimpleAnnotatedModel model = cursor.next(); @@ -298,7 +314,7 @@ public void findAsIteratorTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new SimpleAnnotatedModel()); } - Iterator cursor = SugarRecord.findAsIterator(SimpleAnnotatedModel.class, + Iterator cursor = findAsIterator(SimpleAnnotatedModel.class, "id >= ?", "50"); for (int i = 50; i <= 100; i++) { assertTrue(cursor.hasNext()); @@ -313,8 +329,7 @@ public void findWithQueryAsIteratorTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new SimpleAnnotatedModel()); } - Iterator cursor = - SugarRecord.findWithQueryAsIterator(SimpleAnnotatedModel.class, + Iterator cursor = findWithQueryAsIterator(SimpleAnnotatedModel.class, "Select * from " + NamingHelper.toTableName(SimpleAnnotatedModel.class) + " where id >= ? ", "50"); @@ -329,7 +344,7 @@ public void findWithQueryAsIteratorTest() throws Exception { @Test(expected=NoSuchElementException.class) public void findAsIteratorOutOfBoundsTest() throws Exception { save(new SimpleAnnotatedModel()); - Iterator cursor = SugarRecord.findAsIterator(SimpleAnnotatedModel.class, + Iterator cursor = findAsIterator(SimpleAnnotatedModel.class, "id = ?", "1"); assertTrue(cursor.hasNext()); SimpleAnnotatedModel model = cursor.next(); @@ -342,8 +357,7 @@ public void findAsIteratorOutOfBoundsTest() throws Exception { @Test(expected=UnsupportedOperationException.class) public void disallowRemoveCursorTest() throws Exception { save(new SimpleAnnotatedModel()); - Iterator cursor = SugarRecord.findAsIterator(SimpleAnnotatedModel.class, - "id = ?", "1"); + Iterator cursor = findAsIterator(SimpleAnnotatedModel.class, "id = ?", "1"); assertTrue(cursor.hasNext()); SimpleAnnotatedModel model = cursor.next(); assertNotNull(model); @@ -354,6 +368,6 @@ public void disallowRemoveCursorTest() throws Exception { @Test public void vacuumTest() throws Exception { - SugarRecord.executeQuery("Vacuum"); + executeQuery("Vacuum"); } } \ No newline at end of file diff --git a/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java b/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java index 4cd7bdbe..e809405d 100644 --- a/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java +++ b/library/src/test/java/com/orm/record/SimpleExtendedModelTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.helper.NamingHelper; import com.orm.model.SimpleExtendedModel; @@ -18,6 +17,20 @@ import java.util.NoSuchElementException; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.count; +import static com.orm.SugarRecord.delete; +import static com.orm.SugarRecord.deleteAll; +import static com.orm.SugarRecord.executeQuery; +import static com.orm.SugarRecord.find; +import static com.orm.SugarRecord.findAll; +import static com.orm.SugarRecord.findById; +import static com.orm.SugarRecord.findWithQuery; +import static com.orm.SugarRecord.findAsIterator; +import static com.orm.SugarRecord.findWithQueryAsIterator; +import static com.orm.SugarRecord.deleteInTx; +import static com.orm.SugarRecord.saveInTx; +import static com.orm.SugarRecord.listAll; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -27,23 +40,24 @@ @RunWith(RobolectricGradleTestRunner.class) @Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public final class SimpleExtendedModelTests { + private String id = "id = ?"; @Test public void emptyDatabaseTest() throws Exception { - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(0L, count(SimpleExtendedModel.class)); } @Test public void oneSaveTest() throws Exception { save(new SimpleExtendedModel()); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, count(SimpleExtendedModel.class)); } @Test public void twoSaveTest() throws Exception { save(new SimpleExtendedModel()); save(new SimpleExtendedModel()); - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(2L, count(SimpleExtendedModel.class)); } @Test @@ -51,7 +65,8 @@ public void manySaveTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new SimpleExtendedModel()); } - assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); + + assertEquals(100L, count(SimpleExtendedModel.class)); } @Test @@ -63,59 +78,59 @@ public void defaultIdTest() throws Exception { public void whereCountTest() throws Exception { save(new SimpleExtendedModel()); save(new SimpleExtendedModel()); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class, "id = ?", new String[]{"1"})); + assertEquals(1L, count(SimpleExtendedModel.class, id, new String[]{"1"})); } @Test public void whereNoCountTest() throws Exception { - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class, "id = ?", new String[]{"1"})); + assertEquals(0L, count(SimpleExtendedModel.class, id, new String[]{"1"})); save(new SimpleExtendedModel()); save(new SimpleExtendedModel()); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class, "id = ?", new String[]{"3"})); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class, "id = ?", new String[]{"a"})); + assertEquals(0L, count(SimpleExtendedModel.class, id, new String[]{"3"})); + assertEquals(0L, count(SimpleExtendedModel.class, id, new String[]{"a"})); } @Test public void whereBrokenCountTest() throws Exception { save(new SimpleExtendedModel()); save(new SimpleExtendedModel()); - assertEquals(-1L, SugarRecord.count(SimpleExtendedModel.class, "di = ?", new String[]{"1"})); + assertEquals(-1L, count(SimpleExtendedModel.class, "di = ?", new String[]{"1"})); } @Test public void saveMethodTest() throws Exception { SimpleExtendedModel model = new SimpleExtendedModel(); model.save(); - assertEquals(-1L, SugarRecord.count(SimpleExtendedModel.class, "di = ?", new String[]{"1"})); + assertEquals(-1L, count(SimpleExtendedModel.class, "di = ?", new String[]{"1"})); } @Test public void deleteTest() throws Exception { SimpleExtendedModel model = new SimpleExtendedModel(); save(model); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); - assertTrue(SugarRecord.delete(model)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, count(SimpleExtendedModel.class)); + assertTrue(delete(model)); + assertEquals(0L, count(SimpleExtendedModel.class)); } @Test public void deleteUnsavedTest() throws Exception { SimpleExtendedModel model = new SimpleExtendedModel(); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); - assertFalse(SugarRecord.delete(model)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(0L, count(SimpleExtendedModel.class)); + assertFalse(delete(model)); + assertEquals(0L, count(SimpleExtendedModel.class)); } @Test public void deleteWrongTest() throws Exception { SimpleExtendedModel model = new SimpleExtendedModel(); save(model); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, count(SimpleExtendedModel.class)); Field idField = model.getClass().getSuperclass().getDeclaredField("id"); idField.setAccessible(true); idField.set(model, Long.MAX_VALUE); - assertFalse(SugarRecord.delete(model)); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertFalse(delete(model)); + assertEquals(1L, count(SimpleExtendedModel.class)); } @Test @@ -124,8 +139,8 @@ public void deleteAllTest() throws Exception { for (int i = 1; i <= elementNumber; i++) { save(new SimpleExtendedModel()); } - assertEquals(elementNumber, SugarRecord.deleteAll(SimpleExtendedModel.class)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(elementNumber, deleteAll(SimpleExtendedModel.class)); + assertEquals(0L, count(SimpleExtendedModel.class)); } @Test @@ -135,10 +150,8 @@ public void deleteAllWhereTest() throws Exception { for (int i = 1; i <= elementNumber; i++) { save(new SimpleExtendedModel()); } - assertEquals(elementNumber - 1, SugarRecord.deleteAll(SimpleExtendedModel.class, - "id > ?", - new String[]{"1"})); - assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(elementNumber - 1, deleteAll(SimpleExtendedModel.class, "id > ?", new String[]{"1"})); + assertEquals(1L, count(SimpleExtendedModel.class)); } @Test @@ -149,9 +162,9 @@ public void deleteInTransactionFewTest() throws Exception { save(first); save(second); // Not saving last model - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(2, SugarRecord.deleteInTx(first, second, third)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(2L, count(SimpleExtendedModel.class)); + assertEquals(2, deleteInTx(first, second, third)); + assertEquals(0L, count(SimpleExtendedModel.class)); } @Test @@ -166,15 +179,15 @@ public void deleteInTransactionManyTest() throws Exception { save(model); } } - assertEquals(elementNumber - 1, SugarRecord.count(SimpleExtendedModel.class)); - assertEquals(elementNumber - 1, SugarRecord.deleteInTx(models)); - assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(elementNumber - 1, count(SimpleExtendedModel.class)); + assertEquals(elementNumber - 1, deleteInTx(models)); + assertEquals(0L, count(SimpleExtendedModel.class)); } @Test public void saveInTransactionTest() throws Exception { - SugarRecord.saveInTx(new SimpleExtendedModel(), new SimpleExtendedModel()); - assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); + saveInTx(new SimpleExtendedModel(), new SimpleExtendedModel()); + assertEquals(2L, count(SimpleExtendedModel.class)); } @Test @@ -182,7 +195,7 @@ public void listAllTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new SimpleExtendedModel()); } - List models = SugarRecord.listAll(SimpleExtendedModel.class); + List models = listAll(SimpleExtendedModel.class); assertEquals(100, models.size()); for (long i = 1; i <= 100; i++) { assertEquals(Long.valueOf(i), models.get((int) i - 1).getId()); @@ -193,8 +206,7 @@ public void listAllTest() throws Exception { public void findTest() throws Exception { save(new SimpleExtendedModel()); save(new SimpleExtendedModel()); - List models = - SugarRecord.find(SimpleExtendedModel.class, "id = ?", "2"); + List models = find(SimpleExtendedModel.class, "id = ?", "2"); assertEquals(1, models.size()); assertEquals(Long.valueOf(2L), models.get(0).getId()); } @@ -204,8 +216,7 @@ public void findWithQueryTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new SimpleExtendedModel()); } - List models = - SugarRecord.findWithQuery(SimpleExtendedModel.class, "Select * from " + + List models = findWithQuery(SimpleExtendedModel.class, "Select * from " + NamingHelper.toTableName(SimpleExtendedModel.class) + " where id >= ? ", "50"); for (SimpleExtendedModel model : models) { @@ -217,26 +228,25 @@ public void findWithQueryTest() throws Exception { @SuppressWarnings("all") public void findByIdTest() throws Exception { save(new SimpleExtendedModel()); - assertEquals(Long.valueOf(1L), SugarRecord.findById(SimpleExtendedModel.class, 1L).getId()); + assertEquals(Long.valueOf(1L), findById(SimpleExtendedModel.class, 1L).getId()); } @Test public void findByIdIntegerTest() throws Exception { save(new SimpleExtendedModel()); - assertEquals(Long.valueOf(1L), SugarRecord.findById(SimpleExtendedModel.class, 1).getId()); + assertEquals(Long.valueOf(1L), findById(SimpleExtendedModel.class, 1).getId()); } @Test public void findByIdStringsNullTest() throws Exception { save(new SimpleExtendedModel()); - assertEquals(0, SugarRecord.findById(SimpleExtendedModel.class, new String[]{""}).size()); + assertEquals(0, findById(SimpleExtendedModel.class, new String[]{""}).size()); } @Test public void findByIdStringsOneTest() throws Exception { save(new SimpleExtendedModel()); - List models = - SugarRecord.findById(SimpleExtendedModel.class, new String[]{"1"}); + List models = findById(SimpleExtendedModel.class, new String[]{"1"}); assertEquals(1, models.size()); assertEquals(Long.valueOf(1L), models.get(0).getId()); } @@ -246,8 +256,9 @@ public void findByIdStringsTwoTest() throws Exception { save(new SimpleExtendedModel()); save(new SimpleExtendedModel()); save(new SimpleExtendedModel()); - List models = - SugarRecord.findById(SimpleExtendedModel.class, new String[]{"1", "3"}); + + List models = findById(SimpleExtendedModel.class, new String[]{"1", "3"}); + assertEquals(2, models.size()); assertEquals(Long.valueOf(1L), models.get(0).getId()); assertEquals(Long.valueOf(3L), models.get(1).getId()); @@ -258,8 +269,9 @@ public void findByIdStringsManyTest() throws Exception { for (int i = 1; i <= 10; i++) { save(new SimpleExtendedModel()); } - List models = - SugarRecord.findById(SimpleExtendedModel.class, new String[]{"1", "3", "6", "10"}); + + List models = findById(SimpleExtendedModel.class, new String[]{"1", "3", "6", "10"}); + assertEquals(4, models.size()); assertEquals(Long.valueOf(1L), models.get(0).getId()); assertEquals(Long.valueOf(3L), models.get(1).getId()); @@ -272,8 +284,9 @@ public void findByIdStringsOrderTest() throws Exception { for (int i = 1; i <= 10; i++) { save(new SimpleExtendedModel()); } - List models = - SugarRecord.findById(SimpleExtendedModel.class, new String[]{"10", "6", "3", "1"}); + + List models = findById(SimpleExtendedModel.class, new String[]{"10", "6", "3", "1"}); + assertEquals(4, models.size()); // The order of the query doesn't matter assertEquals(Long.valueOf(1L), models.get(0).getId()); @@ -285,7 +298,7 @@ public void findByIdStringsOrderTest() throws Exception { @Test public void findByIdNullTest() throws Exception { save(new SimpleExtendedModel()); - assertNull(SugarRecord.findById(SimpleExtendedModel.class, 2L)); + assertNull(findById(SimpleExtendedModel.class, 2L)); } @Test @@ -293,7 +306,9 @@ public void findAllTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new SimpleExtendedModel()); } - Iterator cursor = SugarRecord.findAll(SimpleExtendedModel.class); + + Iterator cursor = findAll(SimpleExtendedModel.class); + for (int i = 1; i <= 100; i++) { assertTrue(cursor.hasNext()); SimpleExtendedModel model = cursor.next(); @@ -307,8 +322,9 @@ public void findAsIteratorTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new SimpleExtendedModel()); } - Iterator cursor = SugarRecord.findAsIterator(SimpleExtendedModel.class, - "id >= ?", "50"); + + Iterator cursor = findAsIterator(SimpleExtendedModel.class, "id >= ?", "50"); + for (int i = 50; i <= 100; i++) { assertTrue(cursor.hasNext()); SimpleExtendedModel model = cursor.next(); @@ -322,8 +338,8 @@ public void findWithQueryAsIteratorTest() throws Exception { for (int i = 1; i <= 100; i++) { save(new SimpleExtendedModel()); } - Iterator cursor = - SugarRecord.findWithQueryAsIterator(SimpleExtendedModel.class, + + Iterator cursor = findWithQueryAsIterator(SimpleExtendedModel.class, "Select * from " + NamingHelper.toTableName(SimpleExtendedModel.class) + " where id >= ? ", "50"); @@ -338,8 +354,7 @@ public void findWithQueryAsIteratorTest() throws Exception { @Test(expected=NoSuchElementException.class) public void findAsIteratorOutOfBoundsTest() throws Exception { save(new SimpleExtendedModel()); - Iterator cursor = SugarRecord.findAsIterator(SimpleExtendedModel.class, - "id = ?", "1"); + Iterator cursor = findAsIterator(SimpleExtendedModel.class, id, "1"); assertTrue(cursor.hasNext()); SimpleExtendedModel model = cursor.next(); assertNotNull(model); @@ -351,8 +366,7 @@ public void findAsIteratorOutOfBoundsTest() throws Exception { @Test(expected=UnsupportedOperationException.class) public void disallowRemoveCursorTest() throws Exception { save(new SimpleExtendedModel()); - Iterator cursor = SugarRecord.findAsIterator(SimpleExtendedModel.class, - "id = ?", "1"); + Iterator cursor = findAsIterator(SimpleExtendedModel.class, id, "1"); assertTrue(cursor.hasNext()); SimpleExtendedModel model = cursor.next(); assertNotNull(model); @@ -363,6 +377,6 @@ public void disallowRemoveCursorTest() throws Exception { @Test public void vacuumTest() throws Exception { - SugarRecord.executeQuery("Vacuum"); + executeQuery("Vacuum"); } } \ No newline at end of file diff --git a/library/src/test/java/com/orm/record/StringFieldTests.java b/library/src/test/java/com/orm/record/StringFieldTests.java index 8b6af73a..d9641ad7 100644 --- a/library/src/test/java/com/orm/record/StringFieldTests.java +++ b/library/src/test/java/com/orm/record/StringFieldTests.java @@ -1,7 +1,6 @@ package com.orm.record; import com.orm.app.ClientApp; -import com.orm.SugarRecord; import com.orm.dsl.BuildConfig; import com.orm.model.StringFieldAnnotatedModel; import com.orm.model.StringFieldExtendedModel; @@ -12,40 +11,41 @@ import org.robolectric.annotation.Config; import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.findById; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @RunWith(RobolectricGradleTestRunner.class) @Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public final class StringFieldTests { + private String string = "Test String"; @Test public void nullStringExtendedTest() { save(new StringFieldExtendedModel()); - StringFieldExtendedModel model = SugarRecord.findById(StringFieldExtendedModel.class, 1); + StringFieldExtendedModel model = findById(StringFieldExtendedModel.class, 1); assertNull(model.getString()); } @Test public void nullStringAnnotatedTest() { save(new StringFieldAnnotatedModel()); - StringFieldAnnotatedModel model = SugarRecord.findById(StringFieldAnnotatedModel.class, 1); + StringFieldAnnotatedModel model = findById(StringFieldAnnotatedModel.class, 1); assertNull(model.getString()); } @Test public void stringExtendedTest() { - String string = "Test String"; save(new StringFieldExtendedModel(string)); - StringFieldExtendedModel model = SugarRecord.findById(StringFieldExtendedModel.class, 1); + StringFieldExtendedModel model = findById(StringFieldExtendedModel.class, 1); assertEquals(string, model.getString()); } @Test public void stringAnnotatedTest() { - String string = "Test String"; save(new StringFieldAnnotatedModel(string)); - StringFieldAnnotatedModel model = SugarRecord.findById(StringFieldAnnotatedModel.class, 1); + StringFieldAnnotatedModel model = findById(StringFieldAnnotatedModel.class, 1); assertEquals(string, model.getString()); } } From 2786aaa337c4a11aa24281553a0dec226ced093f Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 9 Apr 2016 17:22:43 -0300 Subject: [PATCH 086/139] try to fix error with Coverrals --- build.gradle | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/build.gradle b/build.gradle index 952f49e1..b4e791fa 100644 --- a/build.gradle +++ b/build.gradle @@ -80,6 +80,10 @@ task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') { xml.enabled = true } + onlyIf = { + true + } + doFirst { executionData = files(executionData.findAll { it.exists() }) } From e083fd45d0f735d86e4e433e298352c8170f5631 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 9 Apr 2016 17:49:57 -0300 Subject: [PATCH 087/139] only checks in library, not in example --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index b4e791fa..394430fe 100644 --- a/build.gradle +++ b/build.gradle @@ -90,7 +90,7 @@ task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') { } coveralls { - sourceDirs = files('example/src/main/java').flatten() + files('library/src/main/java').flatten() + sourceDirs = files('library/src/main/java').flatten() jacocoReportPath = "${buildDir}/reports/jacoco/jacocoRootReport/jacocoRootReport.xml" } From 584e9de8fe513ea2fa4f71f138a5ba554850173e Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 9 Apr 2016 17:54:00 -0300 Subject: [PATCH 088/139] Updated gradle plugin to version 2.0.0 Updated gradle-wrapper to version 2.10 Extracted example project from Jacoco Reports --- build.gradle | 8 ++++---- gradle/wrapper/gradle-wrapper.properties | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index 394430fe..030e1cb3 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:1.5.0' + classpath 'com.android.tools.build:gradle:2.0.0' classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.3.1' } } @@ -70,9 +70,9 @@ task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') { description = 'Generates an aggregate report from all subprojects' dependsOn(subprojects.jacocoTestReport) - additionalSourceDirs = files('example/src/main/java') + files('library/src/main/java') - sourceDirectories = files('example/src/main/java') + files('library/src/main/java') - classDirectories = files('example/build/intermediates/classes/debug') + files('library/build/intermediates/classes/debug') + additionalSourceDirs = files('library/src/main/java') + sourceDirectories = files('library/src/main/java') + classDirectories = files('library/build/intermediates/classes/debug') executionData = files(subprojects.jacocoTestReport.executionData) reports { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0c71e760..b0bb3ffb 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Apr 10 15:27:10 PDT 2013 +#Sat Apr 09 17:51:14 ART 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip From 9828d5184eeb261a10cba77a266b57b11261a48d Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 9 Apr 2016 18:05:26 -0300 Subject: [PATCH 089/139] call to gradle wrapper in travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index db407323..1c0ca090 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ before_script: - sudo service rsync stop || true - sudo service x11-common stop || true script: - - gradle clean build connectedCheck coveralls + - gradlew clean build connectedCheck coveralls cache: directories: From 914ca1925228f08b7c99bf7a0f5c484d64b79985 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 9 Apr 2016 18:06:06 -0300 Subject: [PATCH 090/139] call to gradle wrapper in travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1c0ca090..6e0beddd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ before_script: - sudo service rsync stop || true - sudo service x11-common stop || true script: - - gradlew clean build connectedCheck coveralls + - ./gradlew clean build connectedCheck coveralls cache: directories: From 7743c6c19cc849a7c7789bb9f8525e6d9cc91181 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 9 Apr 2016 19:02:36 -0300 Subject: [PATCH 091/139] Updated build.gradle files Added @SuppressWarnings annotation to avoid deprecated api errors --- example/build.gradle | 32 ++----------------- library/build.gradle | 3 -- .../java/com/orm/helper/MultiDexHelper.java | 1 + 3 files changed, 4 insertions(+), 32 deletions(-) diff --git a/example/build.gradle b/example/build.gradle index b9fe1be7..a8b90f05 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.application' + android { compileSdkVersion 23 buildToolsVersion "23.0.3" @@ -8,44 +9,17 @@ android { minSdkVersion 9 targetSdkVersion 23 } - buildTypes { release { minifyEnabled false } } - - testOptions{ - unitTests.returnDefaultValues = true - } - lintOptions{ + lintOptions { abortOnError false } } dependencies { compile project (':library') - compile 'com.android.support:appcompat-v7:23.2.1' - testCompile 'org.robolectric:robolectric:3.0' - testCompile 'junit:junit:4.12' + compile 'com.android.support:appcompat-v7:23.3.0' } - -//robolectric { -// // Configure includes / excludes -// include '**/*Tests.class' -// exclude '**/espresso/**/*.class' -// -// // Configure max heap size of the test JVM -// maxHeapSize = '2048m' -// -// // Configure the test JVM arguments - Does not apply to Java 8 -// jvmArgs '-XX:MaxPermSize=512m', '-XX:-UseSplitVerifier' -// -// // configure whether failing tests should fail the build -// ignoreFailures true -// -// // use afterTest to listen to the test execution results -// afterTest { descriptor, result -> -// println "Executing test for ${descriptor.name} with result: ${result.resultType}" -// } -//} diff --git a/library/build.gradle b/library/build.gradle index f04be5fc..bc32d26a 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -9,17 +9,14 @@ android { minSdkVersion 9 targetSdkVersion 23 } - buildTypes { release { minifyEnabled false } } - testOptions{ unitTests.returnDefaultValues = true } - lintOptions{ abortOnError false } diff --git a/library/src/main/java/com/orm/helper/MultiDexHelper.java b/library/src/main/java/com/orm/helper/MultiDexHelper.java index d17105f4..588908cd 100644 --- a/library/src/main/java/com/orm/helper/MultiDexHelper.java +++ b/library/src/main/java/com/orm/helper/MultiDexHelper.java @@ -33,6 +33,7 @@ public final class MultiDexHelper { //Prevent instantiation.. private MultiDexHelper() { } + @SuppressWarnings("all") private static SharedPreferences getMultiDexPreferences() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { return getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE); From 6b3182c859040a4034a138a7c9686f657a26e6cc Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 10 Apr 2016 18:31:19 -0300 Subject: [PATCH 092/139] Added Test for SugarConfig --- .../java/com/orm/util/SugarConfigTest.java | 21 ++++++++++++++++++- 1 file changed, 20 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 d27eb85d..5f73aaef 100644 --- a/library/src/test/java/com/orm/util/SugarConfigTest.java +++ b/library/src/test/java/com/orm/util/SugarConfigTest.java @@ -1,8 +1,27 @@ package com.orm.util; +import com.orm.model.TestRecord; + +import junit.framework.Assert; + +import org.junit.Test; + +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.List; + /** * @author jonatan.salas */ -public class SugarConfigTest { +public final class SugarConfigTest { + + @Test + public void testSetGetFields() { + Field[] fields = TestRecord.class.getFields(); + + List fieldList = Arrays.asList(fields); + SugarConfig.setFields(TestRecord.class, fieldList); + Assert.assertEquals(fieldList, SugarConfig.getFields(TestRecord.class)); + } } From c9f21a54b55124c6db7a42cf26f7c05eb5d4e534 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 10 Apr 2016 18:43:00 -0300 Subject: [PATCH 093/139] Added test for private constructor in KeyWordUtil --- library/src/test/java/com/orm/util/KeyWordUtilTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/library/src/test/java/com/orm/util/KeyWordUtilTest.java b/library/src/test/java/com/orm/util/KeyWordUtilTest.java index d2d44b09..8c1e70fc 100644 --- a/library/src/test/java/com/orm/util/KeyWordUtilTest.java +++ b/library/src/test/java/com/orm/util/KeyWordUtilTest.java @@ -3,12 +3,19 @@ import org.junit.Test; import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; /** * @author jonatan.salas */ public final class KeyWordUtilTest { + @Test(expected = IllegalAccessException.class) + public void testPrivateConstructor() throws Exception { + KeyWordUtil keyWordUtil = (KeyWordUtil) Class.forName(KeyWordUtil.class.getName()).getDeclaredConstructor().newInstance(); + assertNull(keyWordUtil); + } + @Test public void testKeyWord() { assertEquals(true, KeyWordUtil.isKeyword("SELECT")); From a8e28b2a16ef7a1d06cf0e972ec9db92f7f719b0 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 10 Apr 2016 18:51:20 -0300 Subject: [PATCH 094/139] Commented test in ReflectionUtil --- .../java/com/orm/util/ReflectionUtilTest.java | 26 +++++++++---------- .../java/com/orm/util/SugarConfigTest.java | 6 +++++ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/library/src/test/java/com/orm/util/ReflectionUtilTest.java b/library/src/test/java/com/orm/util/ReflectionUtilTest.java index eac94abc..099dcc01 100644 --- a/library/src/test/java/com/orm/util/ReflectionUtilTest.java +++ b/library/src/test/java/com/orm/util/ReflectionUtilTest.java @@ -1,7 +1,5 @@ package com.orm.util; -import com.orm.app.ClientApp; -import com.orm.dsl.BuildConfig; import com.orm.model.TestRecord; import org.junit.Test; @@ -17,16 +15,16 @@ */ public final class ReflectionUtilTest { - @Test - public void testGetTableFields() { - List fieldList = ReflectionUtil.getTableFields(TestRecord.class); - List strings = new ArrayList<>(); - - for (Field field: fieldList) { - strings.add(field.getName()); - } - - assertEquals(true, strings.contains("id")); - assertEquals(true, strings.contains("name")); - } +// @Test +// public void testGetTableFields() { +// List fieldList = ReflectionUtil.getTableFields(TestRecord.class); +// List strings = new ArrayList<>(); +// +// for (Field field: fieldList) { +// strings.add(field.getName()); +// } +// +// assertEquals(true, strings.contains("id")); +// assertEquals(true, strings.contains("name")); +// } } diff --git a/library/src/test/java/com/orm/util/SugarConfigTest.java b/library/src/test/java/com/orm/util/SugarConfigTest.java index 5f73aaef..82943e9e 100644 --- a/library/src/test/java/com/orm/util/SugarConfigTest.java +++ b/library/src/test/java/com/orm/util/SugarConfigTest.java @@ -24,4 +24,10 @@ public void testSetGetFields() { Assert.assertEquals(fieldList, SugarConfig.getFields(TestRecord.class)); } + + @Test + public void testClearCache() { + SugarConfig.clearCache(); + Assert.assertEquals(true, SugarConfig.fields.isEmpty()); + } } From cb51e01302be7390a9768dee05202612c42cf277 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 10 Apr 2016 19:22:59 -0300 Subject: [PATCH 095/139] Added test in ReflectionUtil --- .../java/com/orm/util/ReflectionUtilTest.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/library/src/test/java/com/orm/util/ReflectionUtilTest.java b/library/src/test/java/com/orm/util/ReflectionUtilTest.java index 099dcc01..ae9e07e9 100644 --- a/library/src/test/java/com/orm/util/ReflectionUtilTest.java +++ b/library/src/test/java/com/orm/util/ReflectionUtilTest.java @@ -2,13 +2,9 @@ import com.orm.model.TestRecord; -import org.junit.Test; - -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.List; +import junit.framework.Assert; -import static org.junit.Assert.assertEquals; +import org.junit.Test; /** * @author jonatan.salas @@ -27,4 +23,14 @@ public final class ReflectionUtilTest { // assertEquals(true, strings.contains("id")); // assertEquals(true, strings.contains("name")); // } + + @Test + public void testSetFieldValueForId() { + TestRecord record = new TestRecord(); + record.setName("Bla bla"); + + ReflectionUtil.setFieldValueForId(record, 1L); + Assert.assertEquals(1L, record.getId().longValue()); + } + } From 6557f58a6a46c8ac8da412d55b235401ca083c26 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 10 Apr 2016 19:43:27 -0300 Subject: [PATCH 096/139] Added more test in ReflectionUtil --- .../java/com/orm/util/ReflectionUtilTest.java | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/library/src/test/java/com/orm/util/ReflectionUtilTest.java b/library/src/test/java/com/orm/util/ReflectionUtilTest.java index ae9e07e9..61aae9ae 100644 --- a/library/src/test/java/com/orm/util/ReflectionUtilTest.java +++ b/library/src/test/java/com/orm/util/ReflectionUtilTest.java @@ -1,28 +1,39 @@ package com.orm.util; +import com.orm.app.ClientApp; +import com.orm.dsl.BuildConfig; import com.orm.model.TestRecord; import junit.framework.Assert; import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.annotation.Config; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; /** * @author jonatan.salas */ +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public final class ReflectionUtilTest { -// @Test -// public void testGetTableFields() { -// List fieldList = ReflectionUtil.getTableFields(TestRecord.class); -// List strings = new ArrayList<>(); -// -// for (Field field: fieldList) { -// strings.add(field.getName()); -// } -// -// assertEquals(true, strings.contains("id")); -// assertEquals(true, strings.contains("name")); -// } + @Test + public void testGetTableFields() { + List fieldList = ReflectionUtil.getTableFields(TestRecord.class); + List strings = new ArrayList<>(); + + for (Field field: fieldList) { + strings.add(field.getName()); + } + + Assert.assertEquals(true, strings.contains("id")); + Assert.assertEquals(true, strings.contains("name")); + } @Test public void testSetFieldValueForId() { @@ -33,4 +44,9 @@ public void testSetFieldValueForId() { Assert.assertEquals(1L, record.getId().longValue()); } + @Test + public void testGetAllClasses() { + List classes = ReflectionUtil.getDomainClasses(); + Assert.assertEquals(40, classes.size()); + } } From 29fe48c9a8b989629316065d07d2b5eb2070b5cc Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 10 Apr 2016 20:12:26 -0300 Subject: [PATCH 097/139] Added more test to ReflectionUtil --- library/src/main/java/com/orm/SugarContext.java | 2 +- .../java/com/orm/util/ReflectionUtilTest.java | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/SugarContext.java b/library/src/main/java/com/orm/SugarContext.java index 7b0f8f8b..61609986 100644 --- a/library/src/main/java/com/orm/SugarContext.java +++ b/library/src/main/java/com/orm/SugarContext.java @@ -67,7 +67,7 @@ public SugarDb getSugarDb() { return sugarDb; } - Map getEntitiesMap() { + public Map getEntitiesMap() { return entitiesMap; } } diff --git a/library/src/test/java/com/orm/util/ReflectionUtilTest.java b/library/src/test/java/com/orm/util/ReflectionUtilTest.java index 61aae9ae..f76c8797 100644 --- a/library/src/test/java/com/orm/util/ReflectionUtilTest.java +++ b/library/src/test/java/com/orm/util/ReflectionUtilTest.java @@ -1,5 +1,8 @@ package com.orm.util; +import android.content.ContentValues; + +import com.orm.SugarContext; import com.orm.app.ClientApp; import com.orm.dsl.BuildConfig; import com.orm.model.TestRecord; @@ -35,6 +38,20 @@ public void testGetTableFields() { Assert.assertEquals(true, strings.contains("name")); } + @Test(expected = NoSuchFieldException.class) + public void testAddFieldValueToColumn() throws NoSuchFieldException { + SugarContext context = SugarContext.getSugarContext(); + TestRecord record = new TestRecord(); + record.setName("lala"); + + Field column = TestRecord.class.getField("name"); + ContentValues values = new ContentValues(); + + ReflectionUtil.addFieldValueToColumn(values, column, record, context.getEntitiesMap()); + + Assert.assertEquals(record.getName(), values.getAsString("NAME")); + } + @Test public void testSetFieldValueForId() { TestRecord record = new TestRecord(); From de7c38b62358a0873877bd241bb555d1817f5c78 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 10 Apr 2016 20:59:12 -0300 Subject: [PATCH 098/139] Added more tests to com.orm test package --- .../main/java/com/orm/SchemaGenerator.java | 2 +- .../java/com/orm/SchemaGeneratorTest.java | 66 +++++++++++++++++++ .../src/test/java/com/orm/SugarAppTest.java | 31 +++++++++ .../com/orm/helper/ManifestHelperTest.java | 9 +++ .../java/com/orm/helper/NamingHelperTest.java | 7 ++ .../helper/SugarTransactionHelperTest.java | 7 ++ .../java/com/orm/util/ContextUtilTest.java | 7 ++ .../java/com/orm/util/KeyWordUtilTest.java | 2 +- .../java/com/orm/util/ReflectionUtilTest.java | 22 +++++++ 9 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 library/src/test/java/com/orm/SugarAppTest.java diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index 2ad6529f..c2b1a4b9 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -255,7 +255,7 @@ protected String createTableSQL(Class table) { return sb.toString(); } - private void createTable(Class table, SQLiteDatabase sqLiteDatabase) { + protected void createTable(Class table, SQLiteDatabase sqLiteDatabase) { String createSQL = createTableSQL(table); if (!createSQL.isEmpty()) { diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index 9ef72cbc..c93a3634 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -1,5 +1,8 @@ package com.orm; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; + import com.orm.app.ClientApp; import com.orm.dsl.BuildConfig; import com.orm.model.EmptyModel; @@ -9,6 +12,9 @@ import com.orm.model.StringFieldExtendedModel; import com.orm.model.StringFieldExtendedModelAnnotatedColumn; import com.orm.helper.NamingHelper; +import com.orm.model.TestRecord; + +import junit.framework.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -78,4 +84,64 @@ public void testMultiColumnUniqueTableCreation() { "UNIQUE(A, B) ON CONFLICT REPLACE ) ", createSQL); } + + @Test + public void testTableCreation() { + SQLiteDatabase sqLiteDatabase = SugarContext.getSugarContext().getSugarDb().getDB(); + SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); + schemaGenerator.createTable(TestRecord.class, sqLiteDatabase); + String sql = "select count(*) from sqlite_master where type='table' and name='%s';"; + + String tableName = NamingHelper.toTableName(TestRecord.class); + Cursor c = sqLiteDatabase.rawQuery(String.format(sql, tableName), null); + + if (c.moveToFirst()) { + Assert.assertEquals(1, c.getInt(0)); + } + + if (!c.isClosed()) { + c.close(); + } + } + + @Test + public void testAllTableCreation() { + SQLiteDatabase sqLiteDatabase = SugarContext.getSugarContext().getSugarDb().getDB(); + SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); + + schemaGenerator.createDatabase(sqLiteDatabase); + String sql = "select count(*) from sqlite_master where type='table';"; + + Cursor c = sqLiteDatabase.rawQuery(sql, null); + + if (c.moveToFirst()) { + Assert.assertEquals(42, c.getInt(0)); + } + + if (!c.isClosed()) { + c.close(); + } + } + + @Test + public void testDeleteAllTables() { + SQLiteDatabase sqLiteDatabase = SugarContext.getSugarContext().getSugarDb().getDB(); + SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); + + schemaGenerator.createDatabase(sqLiteDatabase); + schemaGenerator.deleteTables(sqLiteDatabase); + + String sql = "select count(*) from sqlite_master where type='table';"; + + Cursor c = sqLiteDatabase.rawQuery(sql, null); + + if (c.moveToFirst()) { + //Two tables are by default created by SQLite + Assert.assertEquals(2, c.getInt(0)); + } + + if (!c.isClosed()) { + c.close(); + } + } } diff --git a/library/src/test/java/com/orm/SugarAppTest.java b/library/src/test/java/com/orm/SugarAppTest.java new file mode 100644 index 00000000..0f247ae8 --- /dev/null +++ b/library/src/test/java/com/orm/SugarAppTest.java @@ -0,0 +1,31 @@ +package com.orm; + +import junit.framework.Assert; + +import org.junit.Test; + +/** + * @author jonatan.salas + */ +public final class SugarAppTest { + + @Test + public void testOnCreate() { + SugarApp app = new SugarApp(); + app.onCreate(); + + SugarContext context = SugarContext.getSugarContext(); + Assert.assertNotNull(context); + } + + + @Test(expected = NullPointerException.class) + public void testOnTerminate() { + SugarApp app = new SugarApp(); + app.onCreate(); + app.onTerminate(); + + SugarContext context = SugarContext.getSugarContext(); + Assert.assertNull(context); + } +} diff --git a/library/src/test/java/com/orm/helper/ManifestHelperTest.java b/library/src/test/java/com/orm/helper/ManifestHelperTest.java index b8bfbbf9..2fad344c 100644 --- a/library/src/test/java/com/orm/helper/ManifestHelperTest.java +++ b/library/src/test/java/com/orm/helper/ManifestHelperTest.java @@ -2,12 +2,14 @@ import com.orm.app.ClientApp; import com.orm.dsl.BuildConfig; +import com.orm.util.KeyWordUtil; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; +import static junit.framework.Assert.assertNull; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -24,6 +26,13 @@ @Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public final class ManifestHelperTest { + @Test(expected = IllegalAccessException.class) + public void testPrivateConstructor() throws Exception { + ManifestHelper helper = ManifestHelper.class.getDeclaredConstructor().newInstance(); + assertNull(helper); + } + + @Test public void testGetDbName() { assertEquals(DATABASE_DEFAULT_NAME, getDatabaseName()); diff --git a/library/src/test/java/com/orm/helper/NamingHelperTest.java b/library/src/test/java/com/orm/helper/NamingHelperTest.java index 54c3684c..0cc8a9d6 100644 --- a/library/src/test/java/com/orm/helper/NamingHelperTest.java +++ b/library/src/test/java/com/orm/helper/NamingHelperTest.java @@ -10,6 +10,7 @@ import java.util.List; import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; import static com.orm.helper.NamingHelper.toColumnName; @@ -18,6 +19,12 @@ public final class NamingHelperTest { + @Test(expected = IllegalAccessException.class) + public void testPrivateConstructor() throws Exception { + NamingHelper helper = NamingHelper.class.getDeclaredConstructor().newInstance(); + assertNull(helper); + } + @Test public void testToSQLNameFromField() { List fieldList = ReflectionUtil.getTableFields(TestRecord.class); diff --git a/library/src/test/java/com/orm/helper/SugarTransactionHelperTest.java b/library/src/test/java/com/orm/helper/SugarTransactionHelperTest.java index 396b3887..1fe73708 100644 --- a/library/src/test/java/com/orm/helper/SugarTransactionHelperTest.java +++ b/library/src/test/java/com/orm/helper/SugarTransactionHelperTest.java @@ -17,6 +17,7 @@ import java.util.List; import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; /** * @author jonatan.salas @@ -47,6 +48,12 @@ public void setUp() { recordList.add(record3); } + @Test(expected = IllegalAccessException.class) + public void testPrivateConstructor() throws Exception { + SugarTransactionHelper helper = SugarTransactionHelper.class.getDeclaredConstructor().newInstance(); + assertNull(helper); + } + @Test public void testDoInTransaction() { SugarTransactionHelper.doInTransaction(new SugarTransactionHelper.Callback() { diff --git a/library/src/test/java/com/orm/util/ContextUtilTest.java b/library/src/test/java/com/orm/util/ContextUtilTest.java index 46372db9..9ce16c7d 100644 --- a/library/src/test/java/com/orm/util/ContextUtilTest.java +++ b/library/src/test/java/com/orm/util/ContextUtilTest.java @@ -22,6 +22,13 @@ @Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public final class ContextUtilTest { + @Test(expected = IllegalAccessException.class) + public void testPrivateConstructor() throws Exception { + ContextUtil contextUtil = ContextUtil.class.getDeclaredConstructor().newInstance(); + assertNull(contextUtil); + } + + @Test public void testInitContext() { assertNotNull(getContext()); diff --git a/library/src/test/java/com/orm/util/KeyWordUtilTest.java b/library/src/test/java/com/orm/util/KeyWordUtilTest.java index 8c1e70fc..417f1b21 100644 --- a/library/src/test/java/com/orm/util/KeyWordUtilTest.java +++ b/library/src/test/java/com/orm/util/KeyWordUtilTest.java @@ -12,7 +12,7 @@ public final class KeyWordUtilTest { @Test(expected = IllegalAccessException.class) public void testPrivateConstructor() throws Exception { - KeyWordUtil keyWordUtil = (KeyWordUtil) Class.forName(KeyWordUtil.class.getName()).getDeclaredConstructor().newInstance(); + KeyWordUtil keyWordUtil = KeyWordUtil.class.getDeclaredConstructor().newInstance(); assertNull(keyWordUtil); } diff --git a/library/src/test/java/com/orm/util/ReflectionUtilTest.java b/library/src/test/java/com/orm/util/ReflectionUtilTest.java index f76c8797..18123b8c 100644 --- a/library/src/test/java/com/orm/util/ReflectionUtilTest.java +++ b/library/src/test/java/com/orm/util/ReflectionUtilTest.java @@ -1,11 +1,13 @@ package com.orm.util; import android.content.ContentValues; +import android.database.Cursor; import com.orm.SugarContext; import com.orm.app.ClientApp; import com.orm.dsl.BuildConfig; import com.orm.model.TestRecord; +import com.orm.query.Select; import junit.framework.Assert; @@ -25,6 +27,12 @@ @Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public final class ReflectionUtilTest { + @Test(expected = IllegalAccessException.class) + public void testPrivateConstructor() throws Exception { + ReflectionUtil reflectionUtil = ReflectionUtil.class.getDeclaredConstructor().newInstance(); + Assert.assertNull(reflectionUtil); + } + @Test public void testGetTableFields() { List fieldList = ReflectionUtil.getTableFields(TestRecord.class); @@ -66,4 +74,18 @@ public void testGetAllClasses() { List classes = ReflectionUtil.getDomainClasses(); Assert.assertEquals(40, classes.size()); } + + @Test(expected = NoSuchFieldException.class) + public void testSetFieldValueFromCursor() throws NoSuchFieldException { + final TestRecord record = new TestRecord().setName("bla bla"); + Long id = record.save(); + record.setId(id); + + Cursor cursor = Select.from(TestRecord.class).getCursor(); + + TestRecord testRecord = new TestRecord(); + Field field = TestRecord.class.getField("name"); + + ReflectionUtil.setFieldValueFromCursor(cursor, field, testRecord); + } } From 3831f3f8a12725efc9437e4a4e6bfdc6c3f5c8b4 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 10 Apr 2016 21:23:34 -0300 Subject: [PATCH 099/139] Added more test to SchemaGeneratorTest Added new model class to use in SchemaGeneratorTest --- .../main/java/com/orm/SchemaGenerator.java | 2 +- .../java/com/orm/SchemaGeneratorTest.java | 34 ++++++++++++++++++- .../java/com/orm/model/AllAnotatedModel.java | 25 ++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 library/src/test/java/com/orm/model/AllAnotatedModel.java diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index c2b1a4b9..579e22b8 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -65,7 +65,7 @@ public void doUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVers executeSugarUpgrade(sqLiteDatabase, oldVersion, newVersion); } - private ArrayList getColumnNames(SQLiteDatabase sqLiteDatabase, String tableName) { + protected ArrayList getColumnNames(SQLiteDatabase sqLiteDatabase, String tableName) { Cursor resultsQuery = sqLiteDatabase.query(tableName, null, null, null, null, null, null); //Check if columns match vs the one on the domain class ArrayList columnNames = new ArrayList<>(); diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index c93a3634..fa75cdea 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -5,6 +5,7 @@ import com.orm.app.ClientApp; import com.orm.dsl.BuildConfig; +import com.orm.model.AllAnotatedModel; import com.orm.model.EmptyModel; import com.orm.model.IntUniqueModel; import com.orm.model.MultiColumnUniqueModel; @@ -21,6 +22,8 @@ import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; +import java.util.List; + import static junit.framework.Assert.assertEquals; @RunWith(RobolectricGradleTestRunner.class) @@ -104,6 +107,25 @@ public void testTableCreation() { } } + @Test + public void testAnnotatedModelTableCreation() { + SQLiteDatabase sqLiteDatabase = SugarContext.getSugarContext().getSugarDb().getDB(); + SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); + schemaGenerator.createTable(AllAnotatedModel.class, sqLiteDatabase); + String sql = "select count(*) from sqlite_master where type='table' and name='%s';"; + + String tableName = NamingHelper.toTableName(AllAnotatedModel.class); + Cursor c = sqLiteDatabase.rawQuery(String.format(sql, tableName), null); + + if (c.moveToFirst()) { + Assert.assertEquals(1, c.getInt(0)); + } + + if (!c.isClosed()) { + c.close(); + } + } + @Test public void testAllTableCreation() { SQLiteDatabase sqLiteDatabase = SugarContext.getSugarContext().getSugarDb().getDB(); @@ -115,7 +137,7 @@ public void testAllTableCreation() { Cursor c = sqLiteDatabase.rawQuery(sql, null); if (c.moveToFirst()) { - Assert.assertEquals(42, c.getInt(0)); + Assert.assertEquals(43, c.getInt(0)); } if (!c.isClosed()) { @@ -144,4 +166,14 @@ public void testDeleteAllTables() { c.close(); } } + + @Test + public void testGetColumnNames() { + SQLiteDatabase sqLiteDatabase = SugarContext.getSugarContext().getSugarDb().getDB(); + SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); + schemaGenerator.createTable(TestRecord.class, sqLiteDatabase); + + List columnNames = schemaGenerator.getColumnNames(sqLiteDatabase, NamingHelper.toTableName(TestRecord.class)); + Assert.assertEquals(2, columnNames.size()); + } } diff --git a/library/src/test/java/com/orm/model/AllAnotatedModel.java b/library/src/test/java/com/orm/model/AllAnotatedModel.java new file mode 100644 index 00000000..c2fb888d --- /dev/null +++ b/library/src/test/java/com/orm/model/AllAnotatedModel.java @@ -0,0 +1,25 @@ +package com.orm.model; + +import com.orm.annotation.Column; +import com.orm.annotation.Ignore; +import com.orm.annotation.NotNull; +import com.orm.annotation.Table; +import com.orm.annotation.Unique; + +/** + * @author jonatan.salas + */ +@Table +public class AllAnotatedModel { + + @NotNull @Unique + private Long id; + + @Column(notNull = true, name = "name", unique = true) + private String name; + + @Ignore + private String surname; + + public AllAnotatedModel() { } +} From eede8a52bb2a02f273ba229365a9b4955ed80a46 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 10 Apr 2016 21:44:46 -0300 Subject: [PATCH 100/139] Solved error in ReflectionUtilTest --- library/src/test/java/com/orm/util/ReflectionUtilTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/test/java/com/orm/util/ReflectionUtilTest.java b/library/src/test/java/com/orm/util/ReflectionUtilTest.java index 18123b8c..5d0dd12b 100644 --- a/library/src/test/java/com/orm/util/ReflectionUtilTest.java +++ b/library/src/test/java/com/orm/util/ReflectionUtilTest.java @@ -72,7 +72,7 @@ public void testSetFieldValueForId() { @Test public void testGetAllClasses() { List classes = ReflectionUtil.getDomainClasses(); - Assert.assertEquals(40, classes.size()); + Assert.assertEquals(41, classes.size()); } @Test(expected = NoSuchFieldException.class) From e2396b0ae12e7dcb4b30983f265637c1a86b0b66 Mon Sep 17 00:00:00 2001 From: Marcus Balbi Date: Wed, 13 Apr 2016 16:16:15 -0300 Subject: [PATCH 101/139] add method afterTableCreated that executes a sql file with the name of the domain class. --- library/src/main/java/com/orm/SchemaGenerator.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index 579e22b8..e3a1c6b1 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -46,7 +46,13 @@ public void createDatabase(SQLiteDatabase sqLiteDatabase) { List domainClasses = getDomainClasses(); for (Class domain : domainClasses) { createTable(domain, sqLiteDatabase); + afterTableCreated(domain,sqLiteDatabase); } + + } + + public void afterTableCreated(Class table, SQLiteDatabase sqLiteDatabase) { + executeScript(sqLiteDatabase, table.getSimpleName() + ".sql"); } public void doUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { From f2ff6274f3acc240f4daf2bb75f3d0b9060d4a7f Mon Sep 17 00:00:00 2001 From: Marcus Balbi Date: Wed, 13 Apr 2016 17:36:28 -0300 Subject: [PATCH 102/139] refactoting executeScriptFile to receive a path that could be sugar_upgrades or sugar_afeter_create --- library/src/main/java/com/orm/SchemaGenerator.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index e3a1c6b1..5aa090cd 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -52,7 +52,9 @@ public void createDatabase(SQLiteDatabase sqLiteDatabase) { } public void afterTableCreated(Class table, SQLiteDatabase sqLiteDatabase) { - executeScript(sqLiteDatabase, table.getSimpleName() + ".sql"); + String fileName = table.getSimpleName() + ".sql"; + executeScript(sqLiteDatabase,"sugar_after_create/" ,fileName); + } public void doUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { @@ -104,7 +106,7 @@ private boolean executeSugarUpgrade(SQLiteDatabase db, int oldVersion, int newVe int version = Integer.valueOf(file.replace(".sql", "")); if ((version > oldVersion) && (version <= newVersion)) { - executeScript(db, file); + executeScript(db,"sugar_upgrades/" ,file); isSuccess = true; } } catch (NumberFormatException e) { @@ -119,9 +121,9 @@ private boolean executeSugarUpgrade(SQLiteDatabase db, int oldVersion, int newVe return isSuccess; } - private void executeScript(SQLiteDatabase db, String file) { + private void executeScript(SQLiteDatabase db,String path ,String file) { try { - InputStream is = getAssets().open("sugar_upgrades/" + file); + InputStream is = getAssets().open(path + file); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line; From 4eb17aa1a6a807117b3a4ad592e1f04130b4e8f1 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 16 Apr 2016 19:30:39 -0300 Subject: [PATCH 103/139] Updated build.gradle files to add support for RetroLambda Added ThreadUtil class which provides a methoc to perform a generic async execution of a callable Added SugarDataSource class which provides async execution of SugarRecord and result via callbacks. --- build.gradle | 1 + library/build.gradle | 9 +- .../main/java/com/orm/SugarDataSource.java | 243 ++++++++++++++++++ .../main/java/com/orm/util/ThreadUtil.java | 26 ++ 4 files changed, 277 insertions(+), 2 deletions(-) create mode 100644 library/src/main/java/com/orm/SugarDataSource.java create mode 100644 library/src/main/java/com/orm/util/ThreadUtil.java diff --git a/build.gradle b/build.gradle index 030e1cb3..ba428694 100644 --- a/build.gradle +++ b/build.gradle @@ -8,6 +8,7 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:2.0.0' + classpath 'me.tatarka:gradle-retrolambda:3.2.3' classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.3.1' } } diff --git a/library/build.gradle b/library/build.gradle index bc32d26a..a9b12441 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.library' +apply plugin: 'me.tatarka.retrolambda' apply from: '../maven_push.gradle' android { @@ -14,10 +15,14 @@ android { minifyEnabled false } } - testOptions{ + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + testOptions { unitTests.returnDefaultValues = true } - lintOptions{ + lintOptions { abortOnError false } } diff --git a/library/src/main/java/com/orm/SugarDataSource.java b/library/src/main/java/com/orm/SugarDataSource.java new file mode 100644 index 00000000..678866c5 --- /dev/null +++ b/library/src/main/java/com/orm/SugarDataSource.java @@ -0,0 +1,243 @@ +package com.orm; + +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; + +import static com.orm.util.ThreadUtil.*; + +/** + * SugarDataSource provides basic crud operations and simplifies SugarRecord by using callbacks and + * performing Asynchronous execution to run queries. + * + * @author jonatan.salas + */ +public final class SugarDataSource { + private final Class sClass; + + /** + * + * @param tClass + */ + private SugarDataSource(Class tClass) { + if (null == tClass) { + throw new IllegalArgumentException("sClass shouldn't be null!"); + } + + this.sClass = tClass; + } + + /** + * + * @param sClass + * @param + * @return + */ + public static SugarDataSource getInstance(Class sClass) { + return new SugarDataSource<>(sClass); + } + + /** + * + * @param object + * @param success + * @param error + */ + @SuppressWarnings("all") + public void insert(final T object, final SuccessCallback success, final ErrorCallback error) { + checkNotNull(success); + checkNotNull(object); + checkNotNull(error); + + final Callable call = () -> { return SugarRecord.save(object); }; + final Future future = doInBackground(call); + + Long id = null; + + try { + if (future.isDone()) { + id = future.get(); + } + + if (null == object) { + error.onError(new Exception("Error when performing insert of " + object.toString())); + } else { + success.onSuccess(id); + } + + } catch (Exception e) { + error.onError(e); + } + } + + /** + * + * @param id + * @param success + * @param error + */ + @SuppressWarnings("all") + public void findById(final Long id, final SuccessCallback success, final ErrorCallback error) { + checkNotNull(success); + checkNotNull(error); + checkNotNull(id); + + final Callable call = () -> { return SugarRecord.findById(getSugarClass(), id); }; + final Future future = doInBackground(call); + + T object = null; + + try { + if (future.isDone()) { + object = future.get(); + } + + if (null == object) { + error.onError(new Exception("The object with " + id.toString() + "doesn't exist in database")); + } else { + success.onSuccess(object); + } + + } catch (Exception e) { + error.onError(e); + } + } + + /** + * + * @param orderBy + * @param success + * @param error + */ + @SuppressWarnings("all") + public void listAll(final String orderBy, final SuccessCallback> success, final ErrorCallback error) { + checkNotNull(success); + checkNotNull(error); + + final Callable> call = () -> { return SugarRecord.listAll(getSugarClass(), orderBy); }; + final Future> future = doInBackground(call); + + List objects = null; + + try { + if (future.isDone()) { + objects = future.get(); + } + + if (null == objects || objects.isEmpty()) { + error.onError(new Exception("There are no objects in the database")); + } else { + success.onSuccess(objects); + } + + } catch (Exception e) { + error.onError(e); + } + } + + + /** + * + * @param object + * @param success + * @param error + */ + @SuppressWarnings("all") + public void update(final T object, final SuccessCallback success, final ErrorCallback error) { + checkNotNull(success); + checkNotNull(object); + checkNotNull(error); + + final Callable call = () -> { return SugarRecord.update(object); }; + final Future future = doInBackground(call); + + Long id = null; + + try { + if (future.isDone()) { + id = future.get(); + } + + if (null == object) { + error.onError(new Exception("Error when performing update of " + object.toString())); + } else { + success.onSuccess(id); + } + + } catch (Exception e) { + error.onError(e); + } + } + + /** + * + * @param object + * @param success + * @param error + */ + @SuppressWarnings("all") + public void delete(final T object, final SuccessCallback success, final ErrorCallback error) { + checkNotNull(success); + checkNotNull(object); + checkNotNull(error); + + final Callable call = () -> { return SugarRecord.delete(object); }; + final Future future = doInBackground(call); + + Boolean deleted = null; + + try { + if (future.isDone()) { + deleted = future.get(); + } + + if (null == object) { + error.onError(new Exception("Error when performing delete of " + object.toString())); + } else { + success.onSuccess(deleted); + } + + } catch (Exception e) { + error.onError(e); + } + } + + /** + * + * @param object + */ + private void checkNotNull(Object object) { + if (null == object) { + throw new IllegalArgumentException("object shouldn't be null"); + } + } + + public Class getSugarClass() { + return sClass; + } + + /** + * @author jonatan.salas + * @param + */ + public interface SuccessCallback { + + /** + * + * @param object + */ + void onSuccess(final S object); + } + + /** + * @author jonatan.salas + */ + public interface ErrorCallback { + + /** + * + * @param e + */ + void onError(final Exception e); + } +} diff --git a/library/src/main/java/com/orm/util/ThreadUtil.java b/library/src/main/java/com/orm/util/ThreadUtil.java new file mode 100644 index 00000000..bcd9e03d --- /dev/null +++ b/library/src/main/java/com/orm/util/ThreadUtil.java @@ -0,0 +1,26 @@ +package com.orm.util; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +/** + * @author jonatan.salas + */ +public final class ThreadUtil { + + //Prevent instantiation.. + private ThreadUtil() { } + + public static Future doInBackground(Callable callable) { + final ExecutorService executor = Executors.newSingleThreadExecutor(); + Future future = executor.submit(callable); + + if(executor.isTerminated()) { + executor.shutdown(); + } + + return future; + } +} From 23587eec97f02853090eb1f0dce92dec835142b4 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 16 Apr 2016 20:45:29 -0300 Subject: [PATCH 104/139] Added delete, delete with params and deleteAll methods --- .../main/java/com/orm/SugarDataSource.java | 167 ++++++++++++------ .../src/main/java/com/orm/SugarRecord.java | 2 - 2 files changed, 117 insertions(+), 52 deletions(-) diff --git a/library/src/main/java/com/orm/SugarDataSource.java b/library/src/main/java/com/orm/SugarDataSource.java index 678866c5..cb636505 100644 --- a/library/src/main/java/com/orm/SugarDataSource.java +++ b/library/src/main/java/com/orm/SugarDataSource.java @@ -12,6 +12,7 @@ * * @author jonatan.salas */ +@SuppressWarnings("all") public final class SugarDataSource { private final Class sClass; @@ -40,14 +41,13 @@ public static SugarDataSource getInstance(Class sClass) { /** * * @param object - * @param success - * @param error + * @param successCallback + * @param errorCallback */ - @SuppressWarnings("all") - public void insert(final T object, final SuccessCallback success, final ErrorCallback error) { - checkNotNull(success); + public void insert(final T object, final SuccessCallback successCallback, final ErrorCallback errorCallback) { + checkNotNull(successCallback); + checkNotNull(errorCallback); checkNotNull(object); - checkNotNull(error); final Callable call = () -> { return SugarRecord.save(object); }; final Future future = doInBackground(call); @@ -59,27 +59,26 @@ public void insert(final T object, final SuccessCallback success, final Er id = future.get(); } - if (null == object) { - error.onError(new Exception("Error when performing insert of " + object.toString())); + if (null == id) { + errorCallback.onError(new Exception("Error when performing insert of " + object.toString())); } else { - success.onSuccess(id); + successCallback.onSuccess(id); } } catch (Exception e) { - error.onError(e); + errorCallback.onError(e); } } /** * * @param id - * @param success - * @param error + * @param successCallback + * @param errorCallback */ - @SuppressWarnings("all") - public void findById(final Long id, final SuccessCallback success, final ErrorCallback error) { - checkNotNull(success); - checkNotNull(error); + public void findById(final Long id, final SuccessCallback successCallback, final ErrorCallback errorCallback) { + checkNotNull(successCallback); + checkNotNull(errorCallback); checkNotNull(id); final Callable call = () -> { return SugarRecord.findById(getSugarClass(), id); }; @@ -93,26 +92,25 @@ public void findById(final Long id, final SuccessCallback success, final Erro } if (null == object) { - error.onError(new Exception("The object with " + id.toString() + "doesn't exist in database")); + errorCallback.onError(new Exception("The object with " + id.toString() + "doesn't exist in database")); } else { - success.onSuccess(object); + successCallback.onSuccess(object); } } catch (Exception e) { - error.onError(e); + errorCallback.onError(e); } } /** * * @param orderBy - * @param success - * @param error + * @param successCallback + * @param errorCallback */ - @SuppressWarnings("all") - public void listAll(final String orderBy, final SuccessCallback> success, final ErrorCallback error) { - checkNotNull(success); - checkNotNull(error); + public void listAll(final String orderBy, final SuccessCallback> successCallback, final ErrorCallback errorCallback) { + checkNotNull(successCallback); + checkNotNull(errorCallback); final Callable> call = () -> { return SugarRecord.listAll(getSugarClass(), orderBy); }; final Future> future = doInBackground(call); @@ -125,13 +123,13 @@ public void listAll(final String orderBy, final SuccessCallback> success } if (null == objects || objects.isEmpty()) { - error.onError(new Exception("There are no objects in the database")); + errorCallback.onError(new Exception("There are no objects in the database")); } else { - success.onSuccess(objects); + successCallback.onSuccess(objects); } } catch (Exception e) { - error.onError(e); + errorCallback.onError(e); } } @@ -139,14 +137,13 @@ public void listAll(final String orderBy, final SuccessCallback> success /** * * @param object - * @param success - * @param error + * @param successCallback + * @param errorCallback */ - @SuppressWarnings("all") - public void update(final T object, final SuccessCallback success, final ErrorCallback error) { - checkNotNull(success); + public void update(final T object, final SuccessCallback successCallback, final ErrorCallback errorCallback) { + checkNotNull(successCallback); + checkNotNull(errorCallback); checkNotNull(object); - checkNotNull(error); final Callable call = () -> { return SugarRecord.update(object); }; final Future future = doInBackground(call); @@ -158,47 +155,117 @@ public void update(final T object, final SuccessCallback success, final Er id = future.get(); } - if (null == object) { - error.onError(new Exception("Error when performing update of " + object.toString())); + if (null == id) { + errorCallback.onError(new Exception("Error when performing update of " + object.toString())); } else { - success.onSuccess(id); + successCallback.onSuccess(id); } } catch (Exception e) { - error.onError(e); + errorCallback.onError(e); } } /** * * @param object - * @param success - * @param error + * @param successCallback + * @param errorCallback */ - @SuppressWarnings("all") - public void delete(final T object, final SuccessCallback success, final ErrorCallback error) { - checkNotNull(success); + public void delete(final T object, final SuccessCallback successCallback, final ErrorCallback errorCallback) { + checkNotNull(successCallback); + checkNotNull(errorCallback); checkNotNull(object); - checkNotNull(error); final Callable call = () -> { return SugarRecord.delete(object); }; final Future future = doInBackground(call); - Boolean deleted = null; + Boolean isDeleted = null; try { if (future.isDone()) { - deleted = future.get(); + isDeleted = future.get(); } - if (null == object) { - error.onError(new Exception("Error when performing delete of " + object.toString())); + if (null == isDeleted || !isDeleted) { + errorCallback.onError(new Exception("Error when performing delete of " + object.toString())); + } else { + successCallback.onSuccess(isDeleted); + } + + } catch (Exception e) { + errorCallback.onError(e); + } + } + + /** + * + * @param whereClause + * @param whereArgs + * @param successCallback + * @param errorCallback + */ + public void delete(String whereClause, String[] whereArgs, final SuccessCallback successCallback, final ErrorCallback errorCallback) { + checkNotNull(successCallback); + checkNotNull(errorCallback); + + final Callable call = () -> { return SugarRecord.deleteAll(getSugarClass(), whereClause, whereArgs); }; + final Future future = doInBackground(call); + + Integer count = null; + + try { + if (future.isDone()) { + count = future.get(); + } + + if (null == count || count == 0) { + errorCallback.onError(new Exception("Error when performing deleete of all elements")); + } else { + successCallback.onSuccess(count); + } + + } catch (Exception e) { + errorCallback.onError(e); + } + } + + /** + * + * @param successCallback + * @param errorCallback + */ + public void deleteAll(final SuccessCallback successCallback, final ErrorCallback errorCallback) { + delete(null, null, successCallback, errorCallback); + } + + /** + * + * @param successCallback + * @param errorCallback + */ + public void count(final SuccessCallback successCallback, final ErrorCallback errorCallback) { + checkNotNull(successCallback); + checkNotNull(errorCallback); + + final Callable call = () -> { return SugarRecord.count(getSugarClass()); }; + final Future future = doInBackground(call); + + Long count = null; + + try { + if (future.isDone()) { + count = future.get(); + } + + if (null == count) { + errorCallback.onError(new Exception("Error when trying to get count")); } else { - success.onSuccess(deleted); + successCallback.onSuccess(count); } } catch (Exception e) { - error.onError(e); + errorCallback.onError(e); } } diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index a6d22f04..040ab353 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -349,8 +349,6 @@ static long update(SQLiteDatabase db, Object object) { } } - - public static boolean isSugarEntity(Class objectClass) { return objectClass.isAnnotationPresent(Table.class) || SugarRecord.class.isAssignableFrom(objectClass); } From f2bca5b708c8379efe8ee166eb0876b39f39b93f Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 16 Apr 2016 22:17:43 -0300 Subject: [PATCH 105/139] Deleted all future.isDone calls Start adding SugarDataSourceTest to test all DataSource functionallity --- .../main/java/com/orm/SugarDataSource.java | 63 ++++++--- .../java/com/orm/SugarDataSourceTest.java | 124 ++++++++++++++++++ 2 files changed, 166 insertions(+), 21 deletions(-) create mode 100644 library/src/test/java/com/orm/SugarDataSourceTest.java diff --git a/library/src/main/java/com/orm/SugarDataSource.java b/library/src/main/java/com/orm/SugarDataSource.java index cb636505..db3edf03 100644 --- a/library/src/main/java/com/orm/SugarDataSource.java +++ b/library/src/main/java/com/orm/SugarDataSource.java @@ -1,5 +1,7 @@ package com.orm; +import android.database.Cursor; + import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.Future; @@ -55,9 +57,7 @@ public void insert(final T object, final SuccessCallback successCallback, Long id = null; try { - if (future.isDone()) { - id = future.get(); - } + id = future.get(); if (null == id) { errorCallback.onError(new Exception("Error when performing insert of " + object.toString())); @@ -87,9 +87,7 @@ public void findById(final Long id, final SuccessCallback successCallback, fi T object = null; try { - if (future.isDone()) { - object = future.get(); - } + object = future.get(); if (null == object) { errorCallback.onError(new Exception("The object with " + id.toString() + "doesn't exist in database")); @@ -102,6 +100,39 @@ public void findById(final Long id, final SuccessCallback successCallback, fi } } + /** + * + * @param whereClause + * @param whereArgs + * @param groupBy + * @param orderBy + * @param limit + * @param successCallback + * @param errorCallback + */ + public void query(String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit, final SuccessCallback successCallback, final ErrorCallback errorCallback) { + checkNotNull(successCallback); + checkNotNull(errorCallback); + + final Callable call = () -> { return SugarRecord.getCursor(getSugarClass(), whereClause, whereArgs, groupBy, orderBy, limit); }; + final Future future = doInBackground(call); + + Cursor cursor = null; + + try { + cursor = future.get(); + + if (null == cursor) { + errorCallback.onError(new Exception("Problem when trying to get the cursor")); + } else { + successCallback.onSuccess(cursor); + } + + } catch (Exception e) { + errorCallback.onError(e); + } + } + /** * * @param orderBy @@ -118,9 +149,7 @@ public void listAll(final String orderBy, final SuccessCallback> success List objects = null; try { - if (future.isDone()) { - objects = future.get(); - } + objects = future.get(); if (null == objects || objects.isEmpty()) { errorCallback.onError(new Exception("There are no objects in the database")); @@ -151,9 +180,7 @@ public void update(final T object, final SuccessCallback successCallback, Long id = null; try { - if (future.isDone()) { - id = future.get(); - } + id = future.get(); if (null == id) { errorCallback.onError(new Exception("Error when performing update of " + object.toString())); @@ -183,9 +210,7 @@ public void delete(final T object, final SuccessCallback successCallbac Boolean isDeleted = null; try { - if (future.isDone()) { - isDeleted = future.get(); - } + isDeleted = future.get(); if (null == isDeleted || !isDeleted) { errorCallback.onError(new Exception("Error when performing delete of " + object.toString())); @@ -215,9 +240,7 @@ public void delete(String whereClause, String[] whereArgs, final SuccessCallback Integer count = null; try { - if (future.isDone()) { - count = future.get(); - } + count = future.get(); if (null == count || count == 0) { errorCallback.onError(new Exception("Error when performing deleete of all elements")); @@ -254,9 +277,7 @@ public void count(final SuccessCallback successCallback, final ErrorCallba Long count = null; try { - if (future.isDone()) { - count = future.get(); - } + count = future.get(); if (null == count) { errorCallback.onError(new Exception("Error when trying to get count")); diff --git a/library/src/test/java/com/orm/SugarDataSourceTest.java b/library/src/test/java/com/orm/SugarDataSourceTest.java new file mode 100644 index 00000000..645be074 --- /dev/null +++ b/library/src/test/java/com/orm/SugarDataSourceTest.java @@ -0,0 +1,124 @@ +package com.orm; + +import com.orm.app.ClientApp; +import com.orm.dsl.BuildConfig; +import com.orm.model.TestRecord; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.annotation.Config; + +/** + * @author jonatan.salas + */ +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class SugarDataSourceTest { + private SugarDataSource recordSugarDataSource; + + @Before + public void setUp() { + recordSugarDataSource = SugarDataSource.getInstance(TestRecord.class); + } + + @Test + @SuppressWarnings("all") + public void testInsertAndDelete() { + TestRecord record = new TestRecord(); + record.setName("lalala"); + + recordSugarDataSource.insert( + record, + id -> { + record.setId(id); + }, + e -> { + e.printStackTrace(); + } + ); + + Assert.assertNotNull(record.getId()); + + recordSugarDataSource.delete( + record, + result -> { + Assert.assertNotNull(result); + Assert.assertEquals(true, result.booleanValue()); + }, + e -> { + e.printStackTrace(); + } + ); + } + + @Test + @SuppressWarnings("all") + public void testInsertAndFindById() { + TestRecord record = new TestRecord(); + record.setName("lalala"); + + recordSugarDataSource.insert( + record, + id -> { + record.setId(id); + }, + e -> { + e.printStackTrace(); + } + ); + + recordSugarDataSource.findById( + record.getId(), + result -> { + Assert.assertEquals(record.getId(), result.getId()); + Assert.assertEquals(record.getName(), result.getName()); + }, + e -> { + e.printStackTrace(); + } + ); + } + + @Test + @SuppressWarnings("all") + public void testInsertUpdateAndFindById() { + TestRecord record = new TestRecord(); + record.setName("lalala"); + + recordSugarDataSource.insert( + record, + id -> { + record.setId(id); + }, + e -> { + e.printStackTrace(); + } + ); + + record.setName("fulano"); + recordSugarDataSource.insert( + record, + id -> { + Assert.assertEquals(record.getId(), id); + }, + e -> { + e.printStackTrace(); + } + ); + + recordSugarDataSource.findById( + record.getId(), + result -> { + Assert.assertEquals(record.getId(), result.getId()); + Assert.assertEquals("fulano", result.getName()); + }, + e -> { + e.printStackTrace(); + } + ); + } +} From 776a33c763a54de87a10f13a0101c1a0a6ae1ec2 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 16 Apr 2016 22:56:57 -0300 Subject: [PATCH 106/139] Added more tests to SugarDataSourceTest --- .../java/com/orm/SugarDataSourceTest.java | 186 +++++++++++++++++- 1 file changed, 185 insertions(+), 1 deletion(-) diff --git a/library/src/test/java/com/orm/SugarDataSourceTest.java b/library/src/test/java/com/orm/SugarDataSourceTest.java index 645be074..8141f467 100644 --- a/library/src/test/java/com/orm/SugarDataSourceTest.java +++ b/library/src/test/java/com/orm/SugarDataSourceTest.java @@ -100,7 +100,7 @@ public void testInsertUpdateAndFindById() { ); record.setName("fulano"); - recordSugarDataSource.insert( + recordSugarDataSource.update( record, id -> { Assert.assertEquals(record.getId(), id); @@ -121,4 +121,188 @@ public void testInsertUpdateAndFindById() { } ); } + + @Test + @SuppressWarnings("all") + public void testInsertAndListAll() { + TestRecord record = new TestRecord(); + record.setName("lalala"); + + recordSugarDataSource.insert( + record, + id -> { + record.setId(id); + }, + e -> { + e.printStackTrace(); + } + ); + + TestRecord record1 = new TestRecord(); + record1.setName("fulano"); + + recordSugarDataSource.insert( + record1, + id -> { + record1.setId(id); + }, + e -> { + e.printStackTrace(); + } + ); + + + TestRecord record2 = new TestRecord(); + record2.setName("mengano"); + + recordSugarDataSource.insert( + record2, + id -> { + record2.setId(id); + }, + e -> { + e.printStackTrace(); + } + ); + + recordSugarDataSource.listAll( + null, + list -> { + Assert.assertEquals(3, list.size()); + }, + e -> { + e.printStackTrace(); + } + ); + + recordSugarDataSource.deleteAll( + count -> { + Assert.assertEquals(3, count.longValue()); + }, + e -> { + e.printStackTrace(); + } + ); + } + + @Test + @SuppressWarnings("all") + public void testInsertAndCount() { + TestRecord record = new TestRecord(); + record.setName("lalala"); + + recordSugarDataSource.insert( + record, + id -> { + record.setId(id); + }, + e -> { + e.printStackTrace(); + } + ); + + TestRecord record1 = new TestRecord(); + record1.setName("fulano"); + + recordSugarDataSource.insert( + record1, + id -> { + record1.setId(id); + }, + e -> { + e.printStackTrace(); + } + ); + + + TestRecord record2 = new TestRecord(); + record2.setName("mengano"); + + recordSugarDataSource.insert( + record2, + id -> { + record2.setId(id); + }, + e -> { + e.printStackTrace(); + } + ); + + recordSugarDataSource.count( + count -> { + Assert.assertEquals(3, count.longValue()); + }, + e -> { + e.printStackTrace(); + } + ); + } + + @Test + @SuppressWarnings("all") + public void testInsertAndGetCursor() { + TestRecord record = new TestRecord(); + record.setName("lalala"); + + recordSugarDataSource.insert( + record, + id -> { + record.setId(id); + }, + e -> { + e.printStackTrace(); + } + ); + + TestRecord record1 = new TestRecord(); + record1.setName("fulano"); + + recordSugarDataSource.insert( + record1, + id -> { + record1.setId(id); + }, + e -> { + e.printStackTrace(); + } + ); + + + TestRecord record2 = new TestRecord(); + record2.setName("mengano"); + + recordSugarDataSource.insert( + record2, + id -> { + record2.setId(id); + }, + e -> { + e.printStackTrace(); + } + ); + + recordSugarDataSource.listAll( + null, + list -> { + Assert.assertEquals(3, list.size()); + }, + e -> { + e.printStackTrace(); + } + ); + + recordSugarDataSource.query( + null, + null, + null, + null, + null, + cursor -> { + Assert.assertNotNull(cursor); + }, + e -> { + e.printStackTrace(); + } + ); + } } From cd1949b1b46136b3594956e3b74b5a6f3f543902 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 16 Apr 2016 23:31:37 -0300 Subject: [PATCH 107/139] Extracted retrolambda because of travis CI troubleshooting around JAVA_HOME var. --- build.gradle | 1 - library/build.gradle | 5 - .../main/java/com/orm/SugarDataSource.java | 68 +++- .../java/com/orm/SugarDataSourceTest.java | 329 ++++++++++++------ 4 files changed, 287 insertions(+), 116 deletions(-) diff --git a/build.gradle b/build.gradle index ba428694..030e1cb3 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,6 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:2.0.0' - classpath 'me.tatarka:gradle-retrolambda:3.2.3' classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.3.1' } } diff --git a/library/build.gradle b/library/build.gradle index a9b12441..907162ee 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -1,5 +1,4 @@ apply plugin: 'com.android.library' -apply plugin: 'me.tatarka.retrolambda' apply from: '../maven_push.gradle' android { @@ -15,10 +14,6 @@ android { minifyEnabled false } } - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 - } testOptions { unitTests.returnDefaultValues = true } diff --git a/library/src/main/java/com/orm/SugarDataSource.java b/library/src/main/java/com/orm/SugarDataSource.java index db3edf03..35f84c66 100644 --- a/library/src/main/java/com/orm/SugarDataSource.java +++ b/library/src/main/java/com/orm/SugarDataSource.java @@ -51,7 +51,13 @@ public void insert(final T object, final SuccessCallback successCallback, checkNotNull(errorCallback); checkNotNull(object); - final Callable call = () -> { return SugarRecord.save(object); }; + final Callable call = new Callable() { + @Override + public Long call() throws Exception { + return SugarRecord.save(object); + } + }; + final Future future = doInBackground(call); Long id = null; @@ -81,7 +87,13 @@ public void findById(final Long id, final SuccessCallback successCallback, fi checkNotNull(errorCallback); checkNotNull(id); - final Callable call = () -> { return SugarRecord.findById(getSugarClass(), id); }; + final Callable call = new Callable() { + @Override + public T call() throws Exception { + return SugarRecord.findById(getSugarClass(), id); + } + }; + final Future future = doInBackground(call); T object = null; @@ -110,11 +122,17 @@ public void findById(final Long id, final SuccessCallback successCallback, fi * @param successCallback * @param errorCallback */ - public void query(String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit, final SuccessCallback successCallback, final ErrorCallback errorCallback) { + public void query(final String whereClause, final String[] whereArgs, final String groupBy, final String orderBy, final String limit, final SuccessCallback successCallback, final ErrorCallback errorCallback) { checkNotNull(successCallback); checkNotNull(errorCallback); - final Callable call = () -> { return SugarRecord.getCursor(getSugarClass(), whereClause, whereArgs, groupBy, orderBy, limit); }; + final Callable call = new Callable() { + @Override + public Cursor call() throws Exception { + return SugarRecord.getCursor(getSugarClass(), whereClause, whereArgs, groupBy, orderBy, limit); + } + }; + final Future future = doInBackground(call); Cursor cursor = null; @@ -143,7 +161,13 @@ public void listAll(final String orderBy, final SuccessCallback> success checkNotNull(successCallback); checkNotNull(errorCallback); - final Callable> call = () -> { return SugarRecord.listAll(getSugarClass(), orderBy); }; + final Callable> call = new Callable>() { + @Override + public List call() throws Exception { + return SugarRecord.listAll(getSugarClass(), orderBy); + } + }; + final Future> future = doInBackground(call); List objects = null; @@ -174,7 +198,13 @@ public void update(final T object, final SuccessCallback successCallback, checkNotNull(errorCallback); checkNotNull(object); - final Callable call = () -> { return SugarRecord.update(object); }; + final Callable call = new Callable() { + @Override + public Long call() throws Exception { + return SugarRecord.update(object); + } + }; + final Future future = doInBackground(call); Long id = null; @@ -204,7 +234,13 @@ public void delete(final T object, final SuccessCallback successCallbac checkNotNull(errorCallback); checkNotNull(object); - final Callable call = () -> { return SugarRecord.delete(object); }; + final Callable call = new Callable() { + @Override + public Boolean call() throws Exception { + return SugarRecord.delete(object); + } + }; + final Future future = doInBackground(call); Boolean isDeleted = null; @@ -230,11 +266,17 @@ public void delete(final T object, final SuccessCallback successCallbac * @param successCallback * @param errorCallback */ - public void delete(String whereClause, String[] whereArgs, final SuccessCallback successCallback, final ErrorCallback errorCallback) { + public void delete(final String whereClause, final String[] whereArgs, final SuccessCallback successCallback, final ErrorCallback errorCallback) { checkNotNull(successCallback); checkNotNull(errorCallback); - final Callable call = () -> { return SugarRecord.deleteAll(getSugarClass(), whereClause, whereArgs); }; + final Callable call = new Callable() { + @Override + public Integer call() throws Exception { + return SugarRecord.deleteAll(getSugarClass(), whereClause, whereArgs); + } + }; + final Future future = doInBackground(call); Integer count = null; @@ -271,7 +313,13 @@ public void count(final SuccessCallback successCallback, final ErrorCallba checkNotNull(successCallback); checkNotNull(errorCallback); - final Callable call = () -> { return SugarRecord.count(getSugarClass()); }; + final Callable call = new Callable() { + @Override + public Long call() throws Exception { + return SugarRecord.count(getSugarClass()); + } + }; + final Future future = doInBackground(call); Long count = null; diff --git a/library/src/test/java/com/orm/SugarDataSourceTest.java b/library/src/test/java/com/orm/SugarDataSourceTest.java index 8141f467..a922c0e9 100644 --- a/library/src/test/java/com/orm/SugarDataSourceTest.java +++ b/library/src/test/java/com/orm/SugarDataSourceTest.java @@ -1,5 +1,7 @@ package com.orm; +import android.database.Cursor; + import com.orm.app.ClientApp; import com.orm.dsl.BuildConfig; import com.orm.model.TestRecord; @@ -12,6 +14,8 @@ import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; +import java.util.List; + /** * @author jonatan.salas */ @@ -28,16 +32,22 @@ public void setUp() { @Test @SuppressWarnings("all") public void testInsertAndDelete() { - TestRecord record = new TestRecord(); + final TestRecord record = new TestRecord(); record.setName("lalala"); recordSugarDataSource.insert( record, - id -> { - record.setId(id); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Long id) { + record.setId(id); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); @@ -45,12 +55,18 @@ public void testInsertAndDelete() { recordSugarDataSource.delete( record, - result -> { - Assert.assertNotNull(result); - Assert.assertEquals(true, result.booleanValue()); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Boolean result) { + Assert.assertNotNull(result); + Assert.assertEquals(true, result.booleanValue()); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); } @@ -58,27 +74,39 @@ public void testInsertAndDelete() { @Test @SuppressWarnings("all") public void testInsertAndFindById() { - TestRecord record = new TestRecord(); + final TestRecord record = new TestRecord(); record.setName("lalala"); recordSugarDataSource.insert( record, - id -> { - record.setId(id); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Long id) { + record.setId(id); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); recordSugarDataSource.findById( record.getId(), - result -> { - Assert.assertEquals(record.getId(), result.getId()); - Assert.assertEquals(record.getName(), result.getName()); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(TestRecord result) { + Assert.assertEquals(record.getId(), result.getId()); + Assert.assertEquals(record.getName(), result.getName()); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); } @@ -86,38 +114,56 @@ public void testInsertAndFindById() { @Test @SuppressWarnings("all") public void testInsertUpdateAndFindById() { - TestRecord record = new TestRecord(); + final TestRecord record = new TestRecord(); record.setName("lalala"); recordSugarDataSource.insert( record, - id -> { - record.setId(id); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Long id) { + record.setId(id); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); record.setName("fulano"); recordSugarDataSource.update( record, - id -> { - Assert.assertEquals(record.getId(), id); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Long id) { + Assert.assertEquals(record.getId(), id); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); recordSugarDataSource.findById( record.getId(), - result -> { - Assert.assertEquals(record.getId(), result.getId()); - Assert.assertEquals("fulano", result.getName()); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(TestRecord result) { + Assert.assertEquals(record.getId(), result.getId()); + Assert.assertEquals("fulano", result.getName()); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); } @@ -125,62 +171,91 @@ public void testInsertUpdateAndFindById() { @Test @SuppressWarnings("all") public void testInsertAndListAll() { - TestRecord record = new TestRecord(); + final TestRecord record = new TestRecord(); record.setName("lalala"); recordSugarDataSource.insert( record, - id -> { - record.setId(id); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Long id) { + record.setId(id); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); - TestRecord record1 = new TestRecord(); + final TestRecord record1 = new TestRecord(); record1.setName("fulano"); recordSugarDataSource.insert( record1, - id -> { - record1.setId(id); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Long id) { + record1.setId(id); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); - - TestRecord record2 = new TestRecord(); + final TestRecord record2 = new TestRecord(); record2.setName("mengano"); recordSugarDataSource.insert( record2, - id -> { - record2.setId(id); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Long id) { + record2.setId(id); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); recordSugarDataSource.listAll( null, - list -> { - Assert.assertEquals(3, list.size()); + new SugarDataSource.SuccessCallback>() { + @Override + public void onSuccess(List list) { + Assert.assertEquals(3, list.size()); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); recordSugarDataSource.deleteAll( - count -> { - Assert.assertEquals(3, count.longValue()); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Integer count) { + Assert.assertEquals(3, count.intValue()); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); } @@ -188,52 +263,76 @@ public void testInsertAndListAll() { @Test @SuppressWarnings("all") public void testInsertAndCount() { - TestRecord record = new TestRecord(); + final TestRecord record = new TestRecord(); record.setName("lalala"); recordSugarDataSource.insert( record, - id -> { - record.setId(id); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Long id) { + record.setId(id); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); - TestRecord record1 = new TestRecord(); + final TestRecord record1 = new TestRecord(); record1.setName("fulano"); recordSugarDataSource.insert( record1, - id -> { - record1.setId(id); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Long id) { + record1.setId(id); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); - TestRecord record2 = new TestRecord(); + final TestRecord record2 = new TestRecord(); record2.setName("mengano"); recordSugarDataSource.insert( record2, - id -> { - record2.setId(id); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Long id) { + record2.setId(id); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); recordSugarDataSource.count( - count -> { - Assert.assertEquals(3, count.longValue()); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Long count) { + Assert.assertEquals(3, count.longValue()); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); } @@ -241,53 +340,77 @@ public void testInsertAndCount() { @Test @SuppressWarnings("all") public void testInsertAndGetCursor() { - TestRecord record = new TestRecord(); + final TestRecord record = new TestRecord(); record.setName("lalala"); recordSugarDataSource.insert( record, - id -> { - record.setId(id); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Long id) { + record.setId(id); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); - TestRecord record1 = new TestRecord(); + final TestRecord record1 = new TestRecord(); record1.setName("fulano"); recordSugarDataSource.insert( record1, - id -> { - record1.setId(id); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Long id) { + record1.setId(id); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); - TestRecord record2 = new TestRecord(); + final TestRecord record2 = new TestRecord(); record2.setName("mengano"); recordSugarDataSource.insert( record2, - id -> { - record2.setId(id); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Long id) { + record2.setId(id); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); recordSugarDataSource.listAll( null, - list -> { - Assert.assertEquals(3, list.size()); + new SugarDataSource.SuccessCallback>() { + @Override + public void onSuccess(List list) { + Assert.assertEquals(3, list.size()); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); @@ -297,11 +420,17 @@ public void testInsertAndGetCursor() { null, null, null, - cursor -> { - Assert.assertNotNull(cursor); + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(Cursor cursor) { + Assert.assertNotNull(cursor); + } }, - e -> { - e.printStackTrace(); + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } } ); } From c7d3961fe83ea369d101b3477bc01e4618af171a Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 17 Apr 2016 00:12:02 -0300 Subject: [PATCH 108/139] Added bulkInsert method Added bulkInsert test and datasource constructor test --- .../main/java/com/orm/SugarDataSource.java | 46 +++++++- .../java/com/orm/SugarDataSourceTest.java | 104 ++++++++++++++++++ 2 files changed, 149 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/SugarDataSource.java b/library/src/main/java/com/orm/SugarDataSource.java index 35f84c66..b88dac3c 100644 --- a/library/src/main/java/com/orm/SugarDataSource.java +++ b/library/src/main/java/com/orm/SugarDataSource.java @@ -2,6 +2,7 @@ import android.database.Cursor; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.Future; @@ -76,6 +77,49 @@ public Long call() throws Exception { } } + /** + * + * @param objects + * @param successCallback + * @param errorCallback + */ + public void bulkInsert(final List objects, final SuccessCallback> successCallback, final ErrorCallback errorCallback) { + checkNotNull(successCallback); + checkNotNull(errorCallback); + checkNotNull(objects); + + final Callable> call = new Callable>() { + @Override + public List call() throws Exception { + List ids = new ArrayList<>(objects.size()); + + for (int i = 0; i < objects.size(); i++) { + Long id = SugarRecord.save(objects.get(i)); + ids.add(i, id); + } + + return ids; + } + }; + + final Future> future = doInBackground(call); + + List ids = null; + + try { + ids = future.get(); + + if (null == ids || ids.isEmpty()) { + errorCallback.onError(new Exception("Error when performing bulk insert")); + } else { + successCallback.onSuccess(ids); + } + + } catch (Exception e) { + errorCallback.onError(e); + } + } + /** * * @param id @@ -285,7 +329,7 @@ public Integer call() throws Exception { count = future.get(); if (null == count || count == 0) { - errorCallback.onError(new Exception("Error when performing deleete of all elements")); + errorCallback.onError(new Exception("Error when performing delete of all elements")); } else { successCallback.onSuccess(count); } diff --git a/library/src/test/java/com/orm/SugarDataSourceTest.java b/library/src/test/java/com/orm/SugarDataSourceTest.java index a922c0e9..fafd2beb 100644 --- a/library/src/test/java/com/orm/SugarDataSourceTest.java +++ b/library/src/test/java/com/orm/SugarDataSourceTest.java @@ -14,6 +14,7 @@ import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; +import java.util.ArrayList; import java.util.List; /** @@ -434,4 +435,107 @@ public void onError(Exception e) { } ); } + + @Test + public void bulkInsertAndListAllTest() { + final TestRecord record = new TestRecord(); + record.setName("lalala"); + + final TestRecord record1 = new TestRecord(); + record1.setName("fulano"); + + final TestRecord record2 = new TestRecord(); + record2.setName("mengano"); + + final List list = new ArrayList<>(); + list.add(record); + list.add(record1); + list.add(record2); + + recordSugarDataSource.bulkInsert( + list, + new SugarDataSource.SuccessCallback>() { + @Override + public void onSuccess(List ids) { + for (int i = 0; i < list.size(); i++) { + list.get(i).setId(ids.get(i)); + } + } + }, + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } + } + ); + + recordSugarDataSource.listAll( + null, + new SugarDataSource.SuccessCallback>() { + @Override + public void onSuccess(List testRecords) { + for (int i = 0; i < list.size(); i++) { + TestRecord record1 = list.get(i); + TestRecord record2 = testRecords.get(i); + + Assert.assertEquals(record1.getId(), record2.getId()); + Assert.assertEquals(record1.getName(), record2.getName()); + } + } + }, + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + e.printStackTrace(); + } + } + ); + } + + @Test + public void nullFindById() { + TestRecord record = new TestRecord(); + record.setId(0L); + + recordSugarDataSource.findById( + record.getId(), + new SugarDataSource.SuccessCallback() { + @Override + public void onSuccess(TestRecord object) { + Assert.assertNull(object); + } + }, + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + Assert.assertNotNull(e.getMessage()); + } + } + ); + } + + @Test + public void nullListAll() { + recordSugarDataSource.listAll( + null, + new SugarDataSource.SuccessCallback>() { + @Override + public void onSuccess(List object) { + Assert.assertNull(object); + } + }, + new SugarDataSource.ErrorCallback() { + @Override + public void onError(Exception e) { + Assert.assertNotNull(e.getMessage()); + } + } + ); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullConstructor() { + SugarDataSource dataSource = SugarDataSource.getInstance(null); + } } From a716a34bc960e1df9de9ed2a5b7a22cdb55d30fa Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 17 Apr 2016 00:36:15 -0300 Subject: [PATCH 109/139] Added class documentation --- .../main/java/com/orm/SugarDataSource.java | 97 +++++++++++-------- 1 file changed, 59 insertions(+), 38 deletions(-) diff --git a/library/src/main/java/com/orm/SugarDataSource.java b/library/src/main/java/com/orm/SugarDataSource.java index b88dac3c..e7e76bd8 100644 --- a/library/src/main/java/com/orm/SugarDataSource.java +++ b/library/src/main/java/com/orm/SugarDataSource.java @@ -20,8 +20,9 @@ public final class SugarDataSource { private final Class sClass; /** + * SugarDataSource constructor with params * - * @param tClass + * @param tClass class argument used then to run SugarRecord class queries */ private SugarDataSource(Class tClass) { if (null == tClass) { @@ -32,20 +33,23 @@ private SugarDataSource(Class tClass) { } /** + * SugarDataSource static method to construct an Instance of this class. * - * @param sClass - * @param - * @return + * @param sClass class argument used then to run SugarRecord class queries + * @param generic argument that must be a SugarRecord extended class or @Table annotated class + * @return an instance of SugarDataSource */ public static SugarDataSource getInstance(Class sClass) { return new SugarDataSource<>(sClass); } /** + * Method used to perform an Asynchronous insert. It works on top of SugarRecord class, executes the + * insert query using Futures. * - * @param object - * @param successCallback - * @param errorCallback + * @param object the object you want to insert. It must be a SugarRecord extended class or @Table annotated class + * @param successCallback the callback for a successful insert operation + * @param errorCallback the callback for an error in insert operation */ public void insert(final T object, final SuccessCallback successCallback, final ErrorCallback errorCallback) { checkNotNull(successCallback); @@ -78,10 +82,12 @@ public Long call() throws Exception { } /** + * Method that performs a bulk insert. It works on top of SugarRecord class, and executes the query + * asynchronously using Futures. * - * @param objects - * @param successCallback - * @param errorCallback + * @param objects the list of objects that you want to insert. They must be SugarRecord extended objects or @Table annotatd objects. + * @param successCallback the callback for successful bulk insert operation + * @param errorCallback the callback for an error in bulk insert operation */ public void bulkInsert(final List objects, final SuccessCallback> successCallback, final ErrorCallback errorCallback) { checkNotNull(successCallback); @@ -121,10 +127,12 @@ public List call() throws Exception { } /** + * Method that performs a findById, It works on top of SugarRecord class providing asynchronous + * execution with the use of Futures. * - * @param id - * @param successCallback - * @param errorCallback + * @param id the id of the object you want to retrieve + * @param successCallback the callback to execute when the operation is successful + * @param errorCallback the callback to execute when the operation has a trouble */ public void findById(final Long id, final SuccessCallback successCallback, final ErrorCallback errorCallback) { checkNotNull(successCallback); @@ -157,14 +165,16 @@ public T call() throws Exception { } /** + * Method that provides you the ability of perform a custom query and retrieve a cursor. It works on top of SugarRecord class, + * All the code is executed asynchronously with the usage of Futures and callbacks. * - * @param whereClause - * @param whereArgs - * @param groupBy - * @param orderBy - * @param limit - * @param successCallback - * @param errorCallback + * @param whereClause the clause of the search + * @param whereArgs the arguments for the search + * @param groupBy the form that you want to group them + * @param orderBy the form that you want to order + * @param limit the limit of objects to want + * @param successCallback the callback to be executed if the operation is successful + * @param errorCallback the callback to be executed if the operation has an error */ public void query(final String whereClause, final String[] whereArgs, final String groupBy, final String orderBy, final String limit, final SuccessCallback successCallback, final ErrorCallback errorCallback) { checkNotNull(successCallback); @@ -196,10 +206,12 @@ public Cursor call() throws Exception { } /** + * Method that list all elements. It run a SugarRecord.listAll but it's code is performed asynchronously + * with the usage of Futures and callbacks. * - * @param orderBy - * @param successCallback - * @param errorCallback + * @param orderBy the way you want to order the objects you get + * @param successCallback the callback that is performed if the operation is successful + * @param errorCallback the callback that is performed if your code has an error */ public void listAll(final String orderBy, final SuccessCallback> successCallback, final ErrorCallback errorCallback) { checkNotNull(successCallback); @@ -232,10 +244,12 @@ public List call() throws Exception { /** + * Method that works on top of SugarRecord.update and runs the code asynchronously via Futures + * and callbacks. * - * @param object - * @param successCallback - * @param errorCallback + * @param object the object you want to update + * @param successCallback the callback that will be performed if the update is successful + * @param errorCallback the callback that will be performed if the update has an error */ public void update(final T object, final SuccessCallback successCallback, final ErrorCallback errorCallback) { checkNotNull(successCallback); @@ -268,10 +282,12 @@ public Long call() throws Exception { } /** + * This method works on top of SugarRecord and provides asynchronous code execution via the usage of + * Futures and callbacks to handle success result and error. * - * @param object - * @param successCallback - * @param errorCallback + * @param object the object you want to delete + * @param successCallback the callback to be performed when the operation is successful + * @param errorCallback the callback to be performed when the operation has an error */ public void delete(final T object, final SuccessCallback successCallback, final ErrorCallback errorCallback) { checkNotNull(successCallback); @@ -304,11 +320,13 @@ public Boolean call() throws Exception { } /** + * Method that performs a selective delete. The code is executed asynchronously via the usage of Futures + * and result callbacks * - * @param whereClause - * @param whereArgs - * @param successCallback - * @param errorCallback + * @param whereClause the clause for the search + * @param whereArgs the values + * @param successCallback the callback to be executed if there is no trouble + * @param errorCallback the callback to be executed if there is an error */ public void delete(final String whereClause, final String[] whereArgs, final SuccessCallback successCallback, final ErrorCallback errorCallback) { checkNotNull(successCallback); @@ -340,18 +358,20 @@ public Integer call() throws Exception { } /** + * Method that deletes all data in a SQLite table. * - * @param successCallback - * @param errorCallback + * @param successCallback the callback that is executed if the operation is succesful + * @param errorCallback the callback that is executed if there is an error */ public void deleteAll(final SuccessCallback successCallback, final ErrorCallback errorCallback) { delete(null, null, successCallback, errorCallback); } /** + * Method that performs a count * - * @param successCallback - * @param errorCallback + * @param successCallback the callback that is executed if this is successful + * @param errorCallback the callback that is executed if there is an error */ public void count(final SuccessCallback successCallback, final ErrorCallback errorCallback) { checkNotNull(successCallback); @@ -383,8 +403,9 @@ public Long call() throws Exception { } /** + * Method that checks an object to be not null * - * @param object + * @param object the object to be checked */ private void checkNotNull(Object object) { if (null == object) { From b85f2268e96d333d756256778f2aab176be92af3 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 17 Apr 2016 13:32:47 -0300 Subject: [PATCH 110/139] Added more test Modified from private to protected --- .../main/java/com/orm/SugarDataSource.java | 2 +- .../java/com/orm/SugarDataSourceTest.java | 58 ++++++++++--------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/library/src/main/java/com/orm/SugarDataSource.java b/library/src/main/java/com/orm/SugarDataSource.java index e7e76bd8..01e12d1c 100644 --- a/library/src/main/java/com/orm/SugarDataSource.java +++ b/library/src/main/java/com/orm/SugarDataSource.java @@ -407,7 +407,7 @@ public Long call() throws Exception { * * @param object the object to be checked */ - private void checkNotNull(Object object) { + protected void checkNotNull(Object object) { if (null == object) { throw new IllegalArgumentException("object shouldn't be null"); } diff --git a/library/src/test/java/com/orm/SugarDataSourceTest.java b/library/src/test/java/com/orm/SugarDataSourceTest.java index fafd2beb..9f3d25ba 100644 --- a/library/src/test/java/com/orm/SugarDataSourceTest.java +++ b/library/src/test/java/com/orm/SugarDataSourceTest.java @@ -6,8 +6,6 @@ import com.orm.dsl.BuildConfig; import com.orm.model.TestRecord; -import junit.framework.Assert; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -17,6 +15,8 @@ import java.util.ArrayList; import java.util.List; +import static junit.framework.Assert.*; + /** * @author jonatan.salas */ @@ -31,7 +31,6 @@ public void setUp() { } @Test - @SuppressWarnings("all") public void testInsertAndDelete() { final TestRecord record = new TestRecord(); record.setName("lalala"); @@ -52,15 +51,15 @@ public void onError(Exception e) { } ); - Assert.assertNotNull(record.getId()); + assertNotNull(record.getId()); recordSugarDataSource.delete( record, new SugarDataSource.SuccessCallback() { @Override public void onSuccess(Boolean result) { - Assert.assertNotNull(result); - Assert.assertEquals(true, result.booleanValue()); + assertNotNull(result); + assertEquals(true, result.booleanValue()); } }, new SugarDataSource.ErrorCallback() { @@ -73,7 +72,6 @@ public void onError(Exception e) { } @Test - @SuppressWarnings("all") public void testInsertAndFindById() { final TestRecord record = new TestRecord(); record.setName("lalala"); @@ -99,8 +97,8 @@ public void onError(Exception e) { new SugarDataSource.SuccessCallback() { @Override public void onSuccess(TestRecord result) { - Assert.assertEquals(record.getId(), result.getId()); - Assert.assertEquals(record.getName(), result.getName()); + assertEquals(record.getId(), result.getId()); + assertEquals(record.getName(), result.getName()); } }, new SugarDataSource.ErrorCallback() { @@ -113,7 +111,6 @@ public void onError(Exception e) { } @Test - @SuppressWarnings("all") public void testInsertUpdateAndFindById() { final TestRecord record = new TestRecord(); record.setName("lalala"); @@ -140,7 +137,7 @@ public void onError(Exception e) { new SugarDataSource.SuccessCallback() { @Override public void onSuccess(Long id) { - Assert.assertEquals(record.getId(), id); + assertEquals(record.getId(), id); } }, new SugarDataSource.ErrorCallback() { @@ -156,8 +153,8 @@ public void onError(Exception e) { new SugarDataSource.SuccessCallback() { @Override public void onSuccess(TestRecord result) { - Assert.assertEquals(record.getId(), result.getId()); - Assert.assertEquals("fulano", result.getName()); + assertEquals(record.getId(), result.getId()); + assertEquals("fulano", result.getName()); } }, new SugarDataSource.ErrorCallback() { @@ -170,7 +167,6 @@ public void onError(Exception e) { } @Test - @SuppressWarnings("all") public void testInsertAndListAll() { final TestRecord record = new TestRecord(); record.setName("lalala"); @@ -234,7 +230,7 @@ public void onError(Exception e) { new SugarDataSource.SuccessCallback>() { @Override public void onSuccess(List list) { - Assert.assertEquals(3, list.size()); + assertEquals(3, list.size()); } }, new SugarDataSource.ErrorCallback() { @@ -249,7 +245,7 @@ public void onError(Exception e) { new SugarDataSource.SuccessCallback() { @Override public void onSuccess(Integer count) { - Assert.assertEquals(3, count.intValue()); + assertEquals(3, count.intValue()); } }, new SugarDataSource.ErrorCallback() { @@ -262,7 +258,6 @@ public void onError(Exception e) { } @Test - @SuppressWarnings("all") public void testInsertAndCount() { final TestRecord record = new TestRecord(); record.setName("lalala"); @@ -326,7 +321,7 @@ public void onError(Exception e) { new SugarDataSource.SuccessCallback() { @Override public void onSuccess(Long count) { - Assert.assertEquals(3, count.longValue()); + assertEquals(3, count.longValue()); } }, new SugarDataSource.ErrorCallback() { @@ -339,7 +334,6 @@ public void onError(Exception e) { } @Test - @SuppressWarnings("all") public void testInsertAndGetCursor() { final TestRecord record = new TestRecord(); record.setName("lalala"); @@ -404,7 +398,7 @@ public void onError(Exception e) { new SugarDataSource.SuccessCallback>() { @Override public void onSuccess(List list) { - Assert.assertEquals(3, list.size()); + assertEquals(3, list.size()); } }, new SugarDataSource.ErrorCallback() { @@ -424,7 +418,7 @@ public void onError(Exception e) { new SugarDataSource.SuccessCallback() { @Override public void onSuccess(Cursor cursor) { - Assert.assertNotNull(cursor); + assertNotNull(cursor); } }, new SugarDataSource.ErrorCallback() { @@ -479,8 +473,8 @@ public void onSuccess(List testRecords) { TestRecord record1 = list.get(i); TestRecord record2 = testRecords.get(i); - Assert.assertEquals(record1.getId(), record2.getId()); - Assert.assertEquals(record1.getName(), record2.getName()); + assertEquals(record1.getId(), record2.getId()); + assertEquals(record1.getName(), record2.getName()); } } }, @@ -503,39 +497,47 @@ public void nullFindById() { new SugarDataSource.SuccessCallback() { @Override public void onSuccess(TestRecord object) { - Assert.assertNull(object); + assertNull(object); } }, new SugarDataSource.ErrorCallback() { @Override public void onError(Exception e) { - Assert.assertNotNull(e.getMessage()); + assertNotNull(e.getMessage()); } } ); } @Test - public void nullListAll() { + public void testNullListAll() { recordSugarDataSource.listAll( null, new SugarDataSource.SuccessCallback>() { @Override public void onSuccess(List object) { - Assert.assertNull(object); + assertNull(object); } }, new SugarDataSource.ErrorCallback() { @Override public void onError(Exception e) { - Assert.assertNotNull(e.getMessage()); + assertNotNull(e.getMessage()); } } ); } @Test(expected = IllegalArgumentException.class) + @SuppressWarnings("all") public void testNullConstructor() { SugarDataSource dataSource = SugarDataSource.getInstance(null); } + + @Test(expected = IllegalArgumentException.class) + @SuppressWarnings("all") + public void testCheckNotNull() { + TestRecord record = null; + recordSugarDataSource.checkNotNull(record); + } } From ae23fa60fea2def23533126e7569f18d43ddebe9 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 17 Apr 2016 13:33:36 -0300 Subject: [PATCH 111/139] modified count method --- library/src/main/java/com/orm/SugarDataSource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/SugarDataSource.java b/library/src/main/java/com/orm/SugarDataSource.java index 01e12d1c..2aa2f8d4 100644 --- a/library/src/main/java/com/orm/SugarDataSource.java +++ b/library/src/main/java/com/orm/SugarDataSource.java @@ -346,7 +346,7 @@ public Integer call() throws Exception { try { count = future.get(); - if (null == count || count == 0) { + if (null == count) { errorCallback.onError(new Exception("Error when performing delete of all elements")); } else { successCallback.onSuccess(count); From badf5463b4e4487e42d9016c7f90c12f1bbf2e5b Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 17 Apr 2016 14:24:49 -0300 Subject: [PATCH 112/139] Added test to UpdateInTx --- .../java/com/orm/record/UpdateInTxTest.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 library/src/test/java/com/orm/record/UpdateInTxTest.java diff --git a/library/src/test/java/com/orm/record/UpdateInTxTest.java b/library/src/test/java/com/orm/record/UpdateInTxTest.java new file mode 100644 index 00000000..2c5a032f --- /dev/null +++ b/library/src/test/java/com/orm/record/UpdateInTxTest.java @@ -0,0 +1,70 @@ +package com.orm.record; + +import com.orm.SugarRecord; +import com.orm.app.ClientApp; +import com.orm.dsl.BuildConfig; +import com.orm.model.TestRecord; + +import junit.framework.Assert; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.annotation.Config; + +import java.util.List; + +/** + * @author jonatan.salas + */ +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public final class UpdateInTxTest { + + @Test + public void testUpdateInTx() { + final TestRecord record = new TestRecord(); + record.setName("lalala"); + + Long id = SugarRecord.save(record); + record.setId(id); + + final TestRecord record1 = new TestRecord(); + record1.setName("lalala"); + + Long id1 = SugarRecord.save(record1); + record1.setId(id1); + + final TestRecord record2 = new TestRecord(); + record2.setName("lalala"); + + Long id2 = SugarRecord.save(record2); + record2.setId(id2); + + final TestRecord record3 = new TestRecord(); + record3.setName("lalala"); + + Long id3 = SugarRecord.save(record3); + record3.setId(id3); + + final TestRecord record4 = new TestRecord(); + record4.setName("lalala"); + + Long id4 = SugarRecord.save(record4); + record.setId(id4); + + record.setName("fulano"); + record1.setName("fulano"); + record2.setName("fulano"); + record3.setName("fulano"); + record4.setName("fulano"); + + SugarRecord.updateInTx(record, record1, record2, record3, record4); + + List list = SugarRecord.listAll(TestRecord.class); + + for (TestRecord r: list) { + Assert.assertEquals(record.getName(), r.getName()); + } + } +} From 4059753f44222f6f7f0dea447ccf59f366c6b406 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 17 Apr 2016 18:55:13 -0300 Subject: [PATCH 113/139] Added Interfaces documentation --- library/src/main/java/com/orm/SugarDataSource.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/library/src/main/java/com/orm/SugarDataSource.java b/library/src/main/java/com/orm/SugarDataSource.java index 2aa2f8d4..eff4b734 100644 --- a/library/src/main/java/com/orm/SugarDataSource.java +++ b/library/src/main/java/com/orm/SugarDataSource.java @@ -418,26 +418,32 @@ public Class getSugarClass() { } /** + * The callback to be executed when some SugarDataSource operation is successful. + * * @author jonatan.salas - * @param + * @param the parameter of the result that is passed to onSuccess method */ public interface SuccessCallback { /** + * This code is executed if there is no trouble on any SugarDataSource operation. * - * @param object + * @param result the result of some SugarDatasource operation */ - void onSuccess(final S object); + void onSuccess(final S result); } /** + * The callback to be executed when some SugarDataSource operation has an error. + * * @author jonatan.salas */ public interface ErrorCallback { /** + * This method is executed if some trouble is detected when using some SugarDataSource method. * - * @param e + * @param e the exception thrown by the method of SugarDataSource you have invoked */ void onError(final Exception e); } From d9bceb06ec4bbd556d4f3be7272b1f6bea74909b Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 17 Apr 2016 19:21:25 -0300 Subject: [PATCH 114/139] Added ThreadUtil documentation --- library/src/main/java/com/orm/util/ThreadUtil.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/library/src/main/java/com/orm/util/ThreadUtil.java b/library/src/main/java/com/orm/util/ThreadUtil.java index bcd9e03d..665e5c2d 100644 --- a/library/src/main/java/com/orm/util/ThreadUtil.java +++ b/library/src/main/java/com/orm/util/ThreadUtil.java @@ -6,6 +6,8 @@ import java.util.concurrent.Future; /** + * Util class to deal with threads. + * * @author jonatan.salas */ public final class ThreadUtil { @@ -13,6 +15,12 @@ public final class ThreadUtil { //Prevent instantiation.. private ThreadUtil() { } + /** + * Submits a Callable object and returns a Future ready to use. + * + * @param callable the callable you want to submit + * @return a Future object + */ public static Future doInBackground(Callable callable) { final ExecutorService executor = Executors.newSingleThreadExecutor(); Future future = executor.submit(callable); From 7f60eb98d28cae633570433edd218435f807a353 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sun, 17 Apr 2016 19:49:12 -0300 Subject: [PATCH 115/139] Deleted all null assigns --- .../main/java/com/orm/SugarDataSource.java | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/library/src/main/java/com/orm/SugarDataSource.java b/library/src/main/java/com/orm/SugarDataSource.java index eff4b734..d2d9aa2c 100644 --- a/library/src/main/java/com/orm/SugarDataSource.java +++ b/library/src/main/java/com/orm/SugarDataSource.java @@ -64,8 +64,7 @@ public Long call() throws Exception { }; final Future future = doInBackground(call); - - Long id = null; + Long id; try { id = future.get(); @@ -109,8 +108,7 @@ public List call() throws Exception { }; final Future> future = doInBackground(call); - - List ids = null; + List ids; try { ids = future.get(); @@ -147,8 +145,7 @@ public T call() throws Exception { }; final Future future = doInBackground(call); - - T object = null; + T object; try { object = future.get(); @@ -188,8 +185,7 @@ public Cursor call() throws Exception { }; final Future future = doInBackground(call); - - Cursor cursor = null; + Cursor cursor; try { cursor = future.get(); @@ -225,8 +221,7 @@ public List call() throws Exception { }; final Future> future = doInBackground(call); - - List objects = null; + List objects; try { objects = future.get(); @@ -264,8 +259,7 @@ public Long call() throws Exception { }; final Future future = doInBackground(call); - - Long id = null; + Long id; try { id = future.get(); @@ -302,8 +296,7 @@ public Boolean call() throws Exception { }; final Future future = doInBackground(call); - - Boolean isDeleted = null; + Boolean isDeleted; try { isDeleted = future.get(); @@ -340,8 +333,7 @@ public Integer call() throws Exception { }; final Future future = doInBackground(call); - - Integer count = null; + Integer count; try { count = future.get(); @@ -385,8 +377,7 @@ public Long call() throws Exception { }; final Future future = doInBackground(call); - - Long count = null; + Long count; try { count = future.get(); From 92b675e0d1ba4c9958116990d50e2403f5cc42d0 Mon Sep 17 00:00:00 2001 From: Tim Siebels Date: Sat, 14 May 2016 13:25:44 +0200 Subject: [PATCH 116/139] Implement SugarRecord.sum() --- .../src/main/java/com/orm/SugarRecord.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index a6d22f04..3cc5c2ab 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -261,6 +261,36 @@ public static long count(Class type, String whereClause, String[] whereAr return result; } + public static long sum(Class type, String field) { + return sum(type, field, null, null); + } + + public static long sum(Class type, String field, String whereClause, String[] whereArgs) { + long result = -1; + String filter = (!TextUtils.isEmpty(whereClause)) ? " where " + whereClause : ""; + SQLiteStatement sqLiteStatement; + try { + sqLiteStatement = getSugarDataBase().compileStatement("SELECT sum(" + field + ") FROM " + NamingHelper.toTableName(type) + filter); + } catch (SQLiteException e) { + e.printStackTrace(); + return result; + } + + if (whereArgs != null) { + for (int i = whereArgs.length; i != 0; i--) { + sqLiteStatement.bindString(i, whereArgs[i - 1]); + } + } + + try { + result = sqLiteStatement.simpleQueryForLong(); + } finally { + sqLiteStatement.close(); + } + + return result; + } + public static long save(Object object) { return save(getSugarDataBase(), object); } From f43255f24d7c39edc6ab796bfa6a00bc5e27ddf9 Mon Sep 17 00:00:00 2001 From: Tim Siebels Date: Mon, 16 May 2016 18:12:54 +0200 Subject: [PATCH 117/139] Add tests for sum() --- .../com/orm/record/IntegerFieldTests.java | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/library/src/test/java/com/orm/record/IntegerFieldTests.java b/library/src/test/java/com/orm/record/IntegerFieldTests.java index 5bb78a47..c30b13a5 100644 --- a/library/src/test/java/com/orm/record/IntegerFieldTests.java +++ b/library/src/test/java/com/orm/record/IntegerFieldTests.java @@ -10,9 +10,9 @@ import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; -import static com.orm.SugarRecord.save; import static com.orm.SugarRecord.findById; - +import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.sum; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -76,4 +76,32 @@ public void intAnnotatedTest() { IntegerFieldAnnotatedModel model = findById(IntegerFieldAnnotatedModel.class, 1); assertEquals(integer.intValue(), model.getInt()); } + + + @Test + public void sumTest() { + save(new IntegerFieldAnnotatedModel(integer.intValue())); + save(new IntegerFieldAnnotatedModel(integer.intValue())); + assertEquals(2 * integer, sum(IntegerFieldAnnotatedModel.class, "raw_integer")); + } + + @Test + public void whereSumTest() { + save(new IntegerFieldAnnotatedModel(integer.intValue())); + save(new IntegerFieldAnnotatedModel(integer.intValue())); + assertEquals((long) integer, sum(IntegerFieldAnnotatedModel.class, + "raw_integer", "id = ?", new String[]{"1"})); + } + + @Test + public void noSumTest() { + assertEquals(0, sum(IntegerFieldAnnotatedModel.class, "raw_integer")); + } + + @Test + public void brokenSumTest() { + save(new IntegerFieldAnnotatedModel(integer.intValue())); + save(new IntegerFieldAnnotatedModel(integer.intValue())); + assertEquals(-1, sum(IntegerFieldAnnotatedModel.class, "wrongfield")); + } } From dabff002218a0f255debf0d7dce246ff1ba0e7e0 Mon Sep 17 00:00:00 2001 From: Tim Siebels Date: Mon, 16 May 2016 18:26:25 +0200 Subject: [PATCH 118/139] Make SugarRecord API more versatile this should not change the API, but makes it easier to use. Especially for cases like wanting to count for one specific value, resulting in a where with only one or a few args. Currently we need to create a String array for that, resulting in a ugly new String[]{"bla"} block. Now it is possible to directly pass "bla" count was the motivation for this change, but I changed other signatures where this was possible as well. --- library/src/main/java/com/orm/SugarRecord.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index a6d22f04..44ce2e01 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -142,7 +142,7 @@ public static T findById(Class type, Integer id) { return findById(type, Long.valueOf(id)); } - public static List findById(Class type, String[] ids) { + public static List findById(Class type, String... ids) { String whereClause = "id IN (" + QueryBuilder.generatePlaceholders(ids.length) + ")"; return find(type, whereClause, ids); } @@ -231,7 +231,7 @@ public static long count(Class type) { return count(type, null, null, null, null, null); } - public static long count(Class type, String whereClause, String[] whereArgs) { + public static long count(Class type, String whereClause, String... whereArgs) { return count(type, whereClause, whereArgs, null, null, null); } From 59a2a918ce3328945c76586b8efc36d672f98857 Mon Sep 17 00:00:00 2001 From: Tim Siebels Date: Mon, 16 May 2016 18:33:00 +0200 Subject: [PATCH 119/139] Improve API to use ... operator --- library/src/main/java/com/orm/SugarRecord.java | 2 +- library/src/test/java/com/orm/record/IntegerFieldTests.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 3cc5c2ab..668b8b67 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -265,7 +265,7 @@ public static long sum(Class type, String field) { return sum(type, field, null, null); } - public static long sum(Class type, String field, String whereClause, String[] whereArgs) { + public static long sum(Class type, String field, String whereClause, String... whereArgs) { long result = -1; String filter = (!TextUtils.isEmpty(whereClause)) ? " where " + whereClause : ""; SQLiteStatement sqLiteStatement; diff --git a/library/src/test/java/com/orm/record/IntegerFieldTests.java b/library/src/test/java/com/orm/record/IntegerFieldTests.java index c30b13a5..faf5fa84 100644 --- a/library/src/test/java/com/orm/record/IntegerFieldTests.java +++ b/library/src/test/java/com/orm/record/IntegerFieldTests.java @@ -90,7 +90,7 @@ public void whereSumTest() { save(new IntegerFieldAnnotatedModel(integer.intValue())); save(new IntegerFieldAnnotatedModel(integer.intValue())); assertEquals((long) integer, sum(IntegerFieldAnnotatedModel.class, - "raw_integer", "id = ?", new String[]{"1"})); + "raw_integer", "id = ?", "1")); } @Test From 0ada490151dc7e3e343b48e563466252c8445688 Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Mon, 16 May 2016 15:45:41 -0300 Subject: [PATCH 120/139] adding missing info of how to use master version --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index fa8a2c14..594782a7 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,11 @@ dependencies { } ``` +You should also comment this line just comment this line (library/build.gradle): https://github.com/satyan/sugar/blob/master/library%2Fbuild.gradle#L2 + +```gradle +// apply from: '../maven_push.gradle' +``` =================== After installing, check out how to set up your first database and models [here](http://satyan.github.io/sugar/getting-started.html) **Outdated**. Check examples of 1.4 and master below: From af9abfaad12eb62f3e3f58d0e1270fd22040c366 Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Thu, 16 Jun 2016 17:30:40 -0300 Subject: [PATCH 121/139] using ManifestHelper.isDebugEnabled() to disable logs based on meta-data on manifest, closes satyan/sugar#375 --- .../main/java/com/orm/SchemaGenerator.java | 42 ++++++++++++----- library/src/main/java/com/orm/SugarDb.java | 13 ++++-- .../src/main/java/com/orm/SugarRecord.java | 45 ++++++++++++++----- .../java/com/orm/helper/ManifestHelper.java | 10 +++-- .../orm/helper/SugarTransactionHelper.java | 12 +++-- .../java/com/orm/util/ReflectionUtil.java | 39 +++++++++++----- 6 files changed, 120 insertions(+), 41 deletions(-) diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index 5aa090cd..c25407fc 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -9,6 +9,8 @@ import com.orm.annotation.MultiUnique; import com.orm.annotation.NotNull; import com.orm.annotation.Unique; +import com.orm.dsl.BuildConfig; +import com.orm.helper.ManifestHelper; import com.orm.util.KeyWordUtil; import com.orm.util.MigrationFileParser; import com.orm.helper.NamingHelper; @@ -100,7 +102,9 @@ private boolean executeSugarUpgrade(SQLiteDatabase db, int oldVersion, int newVe List files = Arrays.asList(getAssets().list("sugar_upgrades")); Collections.sort(files, new NumberComparator()); for (String file : files) { - Log.i(SUGAR, "filename : " + file); + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "filename : " + file); + } try { int version = Integer.valueOf(file.replace(".sql", "")); @@ -110,12 +114,16 @@ private boolean executeSugarUpgrade(SQLiteDatabase db, int oldVersion, int newVe isSuccess = true; } } catch (NumberFormatException e) { - Log.i(SUGAR, "not a sugar script. ignored." + file); + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "not a sugar script. ignored." + file); + } } } } catch (IOException e) { - Log.e(SUGAR, e.getMessage()); + if(ManifestHelper.isDebugEnabled()) { + Log.e(SUGAR, e.getMessage()); + } } return isSuccess; @@ -132,17 +140,23 @@ private void executeScript(SQLiteDatabase db,String path ,String file) { } MigrationFileParser migrationFileParser = new MigrationFileParser(sb.toString()); for(String statement: migrationFileParser.getStatements()){ - Log.i("Sugar script", statement); + if(ManifestHelper.isDebugEnabled()) { + Log.i("Sugar script", statement); + } if (!statement.isEmpty()) { db.execSQL(statement); } } } catch (IOException e) { - Log.e(SUGAR, e.getMessage()); + if(ManifestHelper.isDebugEnabled()) { + Log.e(SUGAR, e.getMessage()); + } } - Log.i(SUGAR, "Script executed"); + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "Script executed"); + } } private void addColumns(Class table, SQLiteDatabase sqLiteDatabase) { @@ -179,18 +193,24 @@ private void addColumns(Class table, SQLiteDatabase sqLiteDatabase) { } for (String command : alterCommands) { - Log.i("Sugar", command); + if(ManifestHelper.isDebugEnabled()) { + Log.i("Sugar", command); + } sqLiteDatabase.execSQL(command); } } protected String createTableSQL(Class table) { - Log.i(SUGAR, "Create table if not exists"); + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "Create table if not exists"); + } List fields = ReflectionUtil.getTableFields(table); String tableName = NamingHelper.toTableName(table); if(KeyWordUtil.isKeyword(tableName)) { - Log.i(SUGAR,"ERROR, SQLITE RESERVED WORD USED IN " + tableName); + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "ERROR, SQLITE RESERVED WORD USED IN " + tableName); + } } StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS "); @@ -258,7 +278,9 @@ protected String createTableSQL(Class table) { } sb.append(" ) "); - Log.i(SUGAR, "Creating table " + tableName); + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "Creating table " + tableName); + } return sb.toString(); } diff --git a/library/src/main/java/com/orm/SugarDb.java b/library/src/main/java/com/orm/SugarDb.java index ed05b80a..bc71ba23 100644 --- a/library/src/main/java/com/orm/SugarDb.java +++ b/library/src/main/java/com/orm/SugarDb.java @@ -4,6 +4,7 @@ import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; +import com.orm.dsl.BuildConfig; import com.orm.helper.ManifestHelper; import com.orm.util.SugarCursorFactory; @@ -62,17 +63,23 @@ public synchronized SQLiteDatabase getDB() { @Override public synchronized SQLiteDatabase getReadableDatabase() { - Log.d(LOG_TAG, "getReadableDatabase"); + if(ManifestHelper.isDebugEnabled()) { + Log.d(LOG_TAG, "getReadableDatabase"); + } openedConnections++; return super.getReadableDatabase(); } @Override public synchronized void close() { - Log.d(LOG_TAG, "getReadableDatabase"); + if(ManifestHelper.isDebugEnabled()) { + Log.d(LOG_TAG, "getReadableDatabase"); + } openedConnections--; if(openedConnections == 0) { - Log.d(LOG_TAG, "closing"); + if(ManifestHelper.isDebugEnabled()) { + Log.d(LOG_TAG, "closing"); + } super.close(); } } diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index a6d22f04..50a85d73 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -10,6 +10,7 @@ import com.orm.annotation.Table; import com.orm.annotation.Unique; +import com.orm.dsl.BuildConfig; import com.orm.helper.ManifestHelper; import com.orm.helper.NamingHelper; import com.orm.util.QueryBuilder; @@ -66,7 +67,9 @@ public static void saveInTx(Collection objects) { } sqLiteDatabase.setTransactionSuccessful(); } catch (Exception e) { - Log.i(SUGAR, "Error in saving in transaction " + e.getMessage()); + if (ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "Error in saving in transaction " + e.getMessage()); + } } finally { sqLiteDatabase.endTransaction(); sqLiteDatabase.setLockingEnabled(true); @@ -89,7 +92,9 @@ public static void updateInTx(Collection objects) { } sqLiteDatabase.setTransactionSuccessful(); } catch (Exception e) { - Log.i(SUGAR, "Error in saving in transaction " + e.getMessage()); + if (ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "Error in saving in transaction " + e.getMessage()); + } } finally { sqLiteDatabase.endTransaction(); sqLiteDatabase.setLockingEnabled(true); @@ -116,7 +121,9 @@ public static int deleteInTx(Collection objects) { sqLiteDatabase.setTransactionSuccessful(); } catch (Exception e) { deletedRows = 0; - Log.i(SUGAR, "Error in deleting in transaction " + e.getMessage()); + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "Error in deleting in transaction " + e.getMessage()); + } } finally { sqLiteDatabase.endTransaction(); sqLiteDatabase.setLockingEnabled(true); @@ -127,7 +134,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); } @@ -381,14 +388,18 @@ public boolean delete() { Long id = getId(); Class type = getClass(); if (id != null && id > 0L) { - Log.i(SUGAR, type.getSimpleName() + " deleted : " + id); + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, type.getSimpleName() + " deleted : " + id); + } return getSugarDataBase().delete(NamingHelper.toTableName(type), "Id=?", new String[]{id.toString()}) == 1; } else { - Log.i(SUGAR, "Cannot delete object: " + type.getSimpleName() + " - object has not been saved"); + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "Cannot delete object: " + type.getSimpleName() + " - object has not been saved"); + } return false; } } - + public static boolean delete(Object object) { Class type = object.getClass(); if (type.isAnnotationPresent(Table.class)) { @@ -398,23 +409,33 @@ public static boolean delete(Object object) { Long id = (Long) field.get(object); if (id != null && id > 0L) { boolean deleted = getSugarDataBase().delete(NamingHelper.toTableName(type), "Id=?", new String[]{id.toString()}) == 1; - Log.i(SUGAR, type.getSimpleName() + " deleted : " + id); + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, type.getSimpleName() + " deleted : " + id); + } return deleted; } else { - Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - object has not been saved"); + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - object has not been saved"); + } return false; } } catch (NoSuchFieldException e) { - Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - annotated object has no id"); + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - annotated object has no id"); + } return false; } catch (IllegalAccessException e) { - Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - can't access id"); + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - can't access id"); + } return false; } } else if (SugarRecord.class.isAssignableFrom(type)) { return ((SugarRecord) object).delete(); } else { - Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - not persisted"); + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - not persisted"); + } return false; } } diff --git a/library/src/main/java/com/orm/helper/ManifestHelper.java b/library/src/main/java/com/orm/helper/ManifestHelper.java index 1c20b4fe..cab85fd2 100644 --- a/library/src/main/java/com/orm/helper/ManifestHelper.java +++ b/library/src/main/java/com/orm/helper/ManifestHelper.java @@ -50,7 +50,7 @@ public static int getDatabaseVersion() { } /** - * Grabs the domain name of the model classes from the manifest. + * Grabs the domain name of the model classes from the manifest. * * @return the package String that Sugar uses to search for model classes */ @@ -101,7 +101,9 @@ private static String getMetaDataString(String name) { ApplicationInfo ai = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); value = ai.metaData.getString(name); } catch (Exception e) { - Log.d(LOG_TAG, "Couldn't find config value: " + name); + if (ManifestHelper.isDebugEnabled()) { + Log.d(LOG_TAG, "Couldn't find config value: " + name); + } } return value; @@ -115,7 +117,9 @@ private static Integer getMetaDataInteger(String name) { ApplicationInfo ai = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); value = ai.metaData.getInt(name); } catch (Exception e) { - Log.d(LOG_TAG, "Couldn't find config value: " + name); + if (ManifestHelper.isDebugEnabled()) { + Log.d(LOG_TAG, "Couldn't find config value: " + name); + } } return value; diff --git a/library/src/main/java/com/orm/helper/SugarTransactionHelper.java b/library/src/main/java/com/orm/helper/SugarTransactionHelper.java index 82077a4a..dbb9ca26 100644 --- a/library/src/main/java/com/orm/helper/SugarTransactionHelper.java +++ b/library/src/main/java/com/orm/helper/SugarTransactionHelper.java @@ -16,14 +16,20 @@ public static void doInTransaction(Callback callback) { database.beginTransaction(); try { - Log.d(LOG_TAG, "Callback executing within transaction"); + if (ManifestHelper.isDebugEnabled()) { + Log.d(LOG_TAG, "Callback executing within transaction"); + } callback.manipulateInTransaction(); database.setTransactionSuccessful(); - Log.d(LOG_TAG, "Callback successfully executed within transaction"); + if (ManifestHelper.isDebugEnabled()) { + Log.d(LOG_TAG, "Callback successfully executed within transaction"); + } } catch (Throwable e) { - Log.d(LOG_TAG, "Could execute callback within transaction", e); + if (ManifestHelper.isDebugEnabled()) { + Log.d(LOG_TAG, "Could execute callback within transaction", e); + } } finally { database.endTransaction(); } diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index 05502bf6..7c0f168a 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -37,7 +37,9 @@ public static List getTableFields(Class table) { List fieldList = SugarConfig.getFields(table); if (fieldList != null) return fieldList; - Log.d("Sugar", "Fetching properties"); + if (ManifestHelper.isDebugEnabled()) { + Log.d("Sugar", "Fetching properties"); + } List typeFields = new ArrayList<>(); getAllFields(typeFields, table); @@ -144,7 +146,9 @@ public static void addFieldValueToColumn(ContentValues values, Field column, Obj } } catch (IllegalAccessException e) { - Log.e("Sugar", e.getMessage()); + if (ManifestHelper.isDebugEnabled()) { + Log.e("Sugar", e.getMessage()); + } } } @@ -158,7 +162,9 @@ public static void setFieldValueFromCursor(Cursor cursor, Field field, Object ob //TODO auto upgrade to add new columns if (columnIndex < 0) { - Log.e("SUGAR", "Invalid colName, you should upgrade database"); + if (ManifestHelper.isDebugEnabled()) { + Log.e("SUGAR", "Invalid colName, you should upgrade database"); + } return; } @@ -218,12 +224,19 @@ public static void setFieldValueFromCursor(Cursor cursor, Field field, Object ob Object enumVal = valueOf.invoke(field.getType(), strVal); field.set(object, enumVal); } catch (Exception e) { - Log.e("Sugar", "Enum cannot be read from Sqlite3 database. Please check the type of field " + field.getName()); + if (ManifestHelper.isDebugEnabled()) { + Log.e("Sugar", "Enum cannot be read from Sqlite3 database. Please check the type of field " + field.getName()); + } + } + } else { + if (ManifestHelper.isDebugEnabled()) { + Log.e("Sugar", "Class cannot be read from Sqlite3 database. Please check the type of field " + field.getName() + "(" + field.getType().getName() + ")"); } - } else - Log.e("Sugar", "Class cannot be read from Sqlite3 database. Please check the type of field " + field.getName() + "(" + field.getType().getName() + ")"); + } } catch (IllegalArgumentException | IllegalAccessException e) { - Log.e("field set error", e.getMessage()); + if (ManifestHelper.isDebugEnabled()) { + Log.e("field set error", e.getMessage()); + } } } @@ -258,7 +271,9 @@ public static List getDomainClasses() { if (domainClass != null) domainClasses.add(domainClass); } } catch (IOException | PackageManager.NameNotFoundException e) { - Log.e("Sugar", e.getMessage()); + if (ManifestHelper.isDebugEnabled()) { + Log.e("Sugar", e.getMessage()); + } } return domainClasses; @@ -271,7 +286,9 @@ private static Class getDomainClass(String className) { discoveredClass = Class.forName(className, true, Thread.currentThread().getContextClassLoader()); } catch (Throwable e) { String error = (e.getMessage() == null) ? "getDomainClass " + className + " error" : e.getMessage(); - Log.e("Sugar", error); + if (ManifestHelper.isDebugEnabled()) { + Log.e("Sugar", error); + } } if ((discoveredClass != null) && @@ -280,7 +297,9 @@ private static Class getDomainClass(String className) { discoveredClass.isAnnotationPresent(Table.class)) && !Modifier.isAbstract(discoveredClass.getModifiers())) { - Log.i("Sugar", "domain class : " + discoveredClass.getSimpleName()); + if (ManifestHelper.isDebugEnabled()) { + Log.i("Sugar", "domain class : " + discoveredClass.getSimpleName()); + } return discoveredClass; } else { From 5f3c63d491c69888ee8284019dd674d25439742a Mon Sep 17 00:00:00 2001 From: Vookash Date: Wed, 3 Aug 2016 21:26:54 +0200 Subject: [PATCH 122/139] Added OneToMany relation support. --- .../src/main/java/com/orm/SugarRecord.java | 54 ++++++++--- .../java/com/orm/annotation/OneToMany.java | 16 ++++ .../java/com/orm/util/ReflectionUtil.java | 2 + .../java/com/orm/SchemaGeneratorTest.java | 2 +- .../java/com/orm/model/ManyToOneModel.java | 55 +++++++++++ .../java/com/orm/model/OneToManyModel.java | 59 ++++++++++++ .../java/com/orm/record/OneToManyTest.java | 96 +++++++++++++++++++ .../java/com/orm/util/ReflectionUtilTest.java | 2 +- 8 files changed, 271 insertions(+), 15 deletions(-) create mode 100644 library/src/main/java/com/orm/annotation/OneToMany.java create mode 100644 library/src/test/java/com/orm/model/ManyToOneModel.java create mode 100644 library/src/test/java/com/orm/model/OneToManyModel.java create mode 100644 library/src/test/java/com/orm/record/OneToManyTest.java diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 1135a56c..bf7c0985 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -7,10 +7,9 @@ import android.database.sqlite.SQLiteStatement; import android.text.TextUtils; import android.util.Log; - +import com.orm.annotation.OneToMany; import com.orm.annotation.Table; import com.orm.annotation.Unique; -import com.orm.dsl.BuildConfig; import com.orm.helper.ManifestHelper; import com.orm.helper.NamingHelper; import com.orm.util.QueryBuilder; @@ -18,13 +17,8 @@ import com.orm.util.SugarCursor; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; +import java.lang.reflect.ParameterizedType; +import java.util.*; import static com.orm.SugarContext.getSugarContext; @@ -216,13 +210,27 @@ public static List find(Class type, String whereClause, String[] where return getEntitiesFromCursor(cursor, type); } + private static List findOneToMany(Class type, String relationFieldName, Object relationObject, Long relationObjectId) { + String args[] = { String.valueOf(relationObjectId) }; + String whereClause = NamingHelper.toSQLNameDefault(relationFieldName) + " = ?"; + + Cursor cursor = getSugarDataBase().query(NamingHelper.toTableName(type), null, whereClause, args, + null, null, null, null); + + return getEntitiesFromCursor(cursor, type, relationFieldName, relationObject); + } + public static List getEntitiesFromCursor(Cursor cursor, Class type){ + return getEntitiesFromCursor(cursor, type, null, null); + } + + public static List getEntitiesFromCursor(Cursor cursor, Class type, String relationFieldName, Object relationObject){ T entity; List result = new ArrayList<>(); try { while (cursor.moveToNext()) { entity = type.getDeclaredConstructor().newInstance(); - inflate(cursor, entity, getSugarContext().getEntitiesMap()); + inflate(cursor, entity, getSugarContext().getEntitiesMap(), relationFieldName, relationObject); result.add(entity); } } catch (Exception e) { @@ -391,9 +399,14 @@ public static boolean isSugarEntity(Class objectClass) { } private static void inflate(Cursor cursor, Object object, Map entitiesMap) { + inflate(cursor, object, entitiesMap, null, null); + } + + private static void inflate(Cursor cursor, Object object, Map entitiesMap, String oneToManyFieldName, Object oneToManyObject) { List columns = ReflectionUtil.getTableFields(object.getClass()); + Long objectId = cursor.getLong(cursor.getColumnIndex(("ID"))); if (!entitiesMap.containsKey(object)) { - entitiesMap.put(object, cursor.getLong(cursor.getColumnIndex(("ID")))); + entitiesMap.put(object, objectId); } for (Field field : columns) { @@ -401,11 +414,26 @@ private static void inflate(Cursor cursor, Object object, Map enti Class fieldType = field.getType(); if (isSugarEntity(fieldType)) { try { - long id = cursor.getLong(cursor.getColumnIndex(NamingHelper.toColumnName(field))); - field.set(object, (id > 0) ? findById(fieldType, id) : null); + if (field.getName().equals(oneToManyFieldName)) { + field.set(object, oneToManyObject); + } else { + long id = cursor.getLong(cursor.getColumnIndex(NamingHelper.toColumnName(field))); + field.set(object, (id > 0) ? findById(fieldType, id) : null); + } } catch (IllegalAccessException e) { e.printStackTrace(); } + } else if (fieldType.equals(List.class)) { + if (field.isAnnotationPresent(OneToMany.class)) { + try { + ParameterizedType genericListType = (ParameterizedType) field.getGenericType(); + Class genericListClass = (Class) genericListType.getActualTypeArguments()[0]; + String targetName = field.getAnnotation(OneToMany.class).targetField(); + field.set(object, findOneToMany(genericListClass, targetName, object, objectId)); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } } else { ReflectionUtil.setFieldValueFromCursor(cursor, field, object); } diff --git a/library/src/main/java/com/orm/annotation/OneToMany.java b/library/src/main/java/com/orm/annotation/OneToMany.java new file mode 100644 index 00000000..5423256b --- /dev/null +++ b/library/src/main/java/com/orm/annotation/OneToMany.java @@ -0,0 +1,16 @@ +package com.orm.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Created by Łukasz Wesołowski on 28.07.2016. + */ + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface OneToMany { + String targetField(); +} diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index 7c0f168a..71a8b8c6 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -134,6 +134,8 @@ public static void addFieldValueToColumn(ContentValues values, Field column, Obj } else { values.put(columnName, (byte[]) columnValue); } + } else if (columnType.equals(List.class)) { + //ignore } else { if (columnValue == null) { values.putNull(columnName); diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index fa75cdea..3b39c38c 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -137,7 +137,7 @@ public void testAllTableCreation() { Cursor c = sqLiteDatabase.rawQuery(sql, null); if (c.moveToFirst()) { - Assert.assertEquals(43, c.getInt(0)); + Assert.assertEquals(45, c.getInt(0)); } if (!c.isClosed()) { diff --git a/library/src/test/java/com/orm/model/ManyToOneModel.java b/library/src/test/java/com/orm/model/ManyToOneModel.java new file mode 100644 index 00000000..4e704142 --- /dev/null +++ b/library/src/test/java/com/orm/model/ManyToOneModel.java @@ -0,0 +1,55 @@ +package com.orm.model; + +import com.orm.SugarRecord; + +/** + * Created by Łukasz Wesołowski on 28.07.2016. + */ +public class ManyToOneModel extends SugarRecord { + private String name; + private OneToManyModel model; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public OneToManyModel getModel() { + return model; + } + + public void setModel(OneToManyModel model) { + this.model = model; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ManyToOneModel that = (ManyToOneModel) o; + + if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null) return false; + return name != null ? name.equals(that.name) : that.name == null; + + } + + @Override + public int hashCode() { + int result = getId() != null ? getId().hashCode() : 0; + result = 31 * result + (name != null ? name.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "ManyToOneModel{" + + "id=" + getId() + + ", name='" + name + '\'' + + ", model=" + model + + '}'; + } +} diff --git a/library/src/test/java/com/orm/model/OneToManyModel.java b/library/src/test/java/com/orm/model/OneToManyModel.java new file mode 100644 index 00000000..e23b2a1c --- /dev/null +++ b/library/src/test/java/com/orm/model/OneToManyModel.java @@ -0,0 +1,59 @@ +package com.orm.model; + +import com.orm.SugarRecord; +import com.orm.annotation.OneToMany; + +import java.util.List; + +/** + * Created by Łukasz Wesołowski on 28.07.2016. + */ +public class OneToManyModel extends SugarRecord { + private String name; + @OneToMany(targetField = "model") + private List models; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getModels() { + return models; + } + + public void setModels(List models) { + this.models = models; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + OneToManyModel that = (OneToManyModel) o; + + if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null) return false; + return name != null ? name.equals(that.name) : that.name == null; + + } + + @Override + public int hashCode() { + int result = getId() != null ? getId().hashCode() : 0; + result = 31 * result + (name != null ? name.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "OneToManyModel{" + + "id=" + getId() + + ", name='" + name + '\'' + + ", models=" + models + + '}'; + } +} diff --git a/library/src/test/java/com/orm/record/OneToManyTest.java b/library/src/test/java/com/orm/record/OneToManyTest.java new file mode 100644 index 00000000..f9ddd9f4 --- /dev/null +++ b/library/src/test/java/com/orm/record/OneToManyTest.java @@ -0,0 +1,96 @@ +package com.orm.record; + +import com.orm.app.ClientApp; +import com.orm.dsl.BuildConfig; +import com.orm.model.ManyToOneModel; +import com.orm.model.OneToManyModel; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.annotation.Config; + +/** + * Created by Łukasz Wesołowski on 28.07.2016. + */ + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) +public class OneToManyTest { + @Test + public void saveOneToManyRelationTest() { + OneToManyModel oneToManyModel = new OneToManyModel(); + oneToManyModel.setId(1l); + oneToManyModel.setName("oneToMany"); + oneToManyModel.save(); + + ManyToOneModel manyToOneModel1 = new ManyToOneModel(); + manyToOneModel1.setId(1l); + manyToOneModel1.setName("manyToOne 1"); + manyToOneModel1.setModel(oneToManyModel); + manyToOneModel1.save(); + + ManyToOneModel manyToOneModel2 = new ManyToOneModel(); + manyToOneModel2.setId(2l); + manyToOneModel2.setName("manyToOne 2"); + manyToOneModel2.setModel(oneToManyModel); + manyToOneModel2.save(); + + ManyToOneModel manyToOneModel3 = new ManyToOneModel(); + manyToOneModel3.setId(3l); + manyToOneModel3.setName("manyToOne 3"); + manyToOneModel3.setModel(oneToManyModel); + manyToOneModel3.save(); + + OneToManyModel result = OneToManyModel.findById(OneToManyModel.class, 1l); + + Assert.assertEquals(3, result.getModels().size()); + + Assert.assertTrue(result.getModels().contains(manyToOneModel1)); + Assert.assertTrue(result.getModels().contains(manyToOneModel2)); + Assert.assertTrue(result.getModels().contains(manyToOneModel3)); + + Assert.assertEquals(result, result.getModels().get(0).getModel()); + Assert.assertEquals(result, result.getModels().get(1).getModel()); + Assert.assertEquals(result, result.getModels().get(2).getModel()); + } + + @Test + public void removeOneToManyRelationTest() { + OneToManyModel oneToManyModel = new OneToManyModel(); + oneToManyModel.setId(1l); + oneToManyModel.setName("oneToMany"); + oneToManyModel.save(); + + ManyToOneModel manyToOneModel1 = new ManyToOneModel(); + manyToOneModel1.setId(1l); + manyToOneModel1.setName("manyToOne 1"); + manyToOneModel1.setModel(oneToManyModel); + manyToOneModel1.save(); + + ManyToOneModel manyToOneModel2 = new ManyToOneModel(); + manyToOneModel2.setId(2l); + manyToOneModel2.setName("manyToOne 2"); + manyToOneModel2.setModel(oneToManyModel); + manyToOneModel2.save(); + + ManyToOneModel manyToOneModel3 = new ManyToOneModel(); + manyToOneModel3.setId(3l); + manyToOneModel3.setName("manyToOne 3"); + manyToOneModel3.setModel(oneToManyModel); + manyToOneModel3.save(); + + OneToManyModel result = OneToManyModel.findById(OneToManyModel.class, 1l); + + Assert.assertEquals(3, result.getModels().size()); + + ManyToOneModel.delete(manyToOneModel2); + + result = OneToManyModel.findById(OneToManyModel.class, 1l); + + Assert.assertEquals(2, result.getModels().size()); + + Assert.assertTrue(result.getModels().contains(manyToOneModel1)); + Assert.assertTrue(result.getModels().contains(manyToOneModel3)); + } +} diff --git a/library/src/test/java/com/orm/util/ReflectionUtilTest.java b/library/src/test/java/com/orm/util/ReflectionUtilTest.java index 5d0dd12b..09fd4755 100644 --- a/library/src/test/java/com/orm/util/ReflectionUtilTest.java +++ b/library/src/test/java/com/orm/util/ReflectionUtilTest.java @@ -72,7 +72,7 @@ public void testSetFieldValueForId() { @Test public void testGetAllClasses() { List classes = ReflectionUtil.getDomainClasses(); - Assert.assertEquals(41, classes.size()); + Assert.assertEquals(43, classes.size()); } @Test(expected = NoSuchFieldException.class) From 96c843aaaa5aae48e8712b8247ac463ccce966df Mon Sep 17 00:00:00 2001 From: Vookash Date: Wed, 3 Aug 2016 23:22:33 +0200 Subject: [PATCH 123/139] Refactored sugar record inflation. --- .../src/main/java/com/orm/SugarRecord.java | 69 +++++------------ .../java/com/orm/inflater/ObjectInflater.java | 75 +++++++++++++++++++ .../inflater/field/DefaultFieldInflater.java | 21 ++++++ .../inflater/field/EntityFieldInflater.java | 27 +++++++ .../com/orm/inflater/field/FieldInflater.java | 24 ++++++ .../orm/inflater/field/ListFieldInflater.java | 38 ++++++++++ .../field/RelationEntityFieldInflater.java | 29 +++++++ 7 files changed, 234 insertions(+), 49 deletions(-) create mode 100644 library/src/main/java/com/orm/inflater/ObjectInflater.java create mode 100644 library/src/main/java/com/orm/inflater/field/DefaultFieldInflater.java create mode 100644 library/src/main/java/com/orm/inflater/field/EntityFieldInflater.java create mode 100644 library/src/main/java/com/orm/inflater/field/FieldInflater.java create mode 100644 library/src/main/java/com/orm/inflater/field/ListFieldInflater.java create mode 100644 library/src/main/java/com/orm/inflater/field/RelationEntityFieldInflater.java diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index bf7c0985..d72c2b7e 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -7,17 +7,16 @@ import android.database.sqlite.SQLiteStatement; import android.text.TextUtils; import android.util.Log; -import com.orm.annotation.OneToMany; import com.orm.annotation.Table; import com.orm.annotation.Unique; import com.orm.helper.ManifestHelper; import com.orm.helper.NamingHelper; +import com.orm.inflater.ObjectInflater; import com.orm.util.QueryBuilder; import com.orm.util.ReflectionUtil; import com.orm.util.SugarCursor; import java.lang.reflect.Field; -import java.lang.reflect.ParameterizedType; import java.util.*; import static com.orm.SugarContext.getSugarContext; @@ -192,7 +191,7 @@ public static List find(Class type, String whereClause, String... wher public static List findWithQuery(Class type, String query, String... arguments) { Cursor cursor = getSugarDataBase().rawQuery(query, arguments); - return getEntitiesFromCursor(cursor, type); + return getEntitiesFromCursor(cursor, type); } public static void executeQuery(String query, String... arguments) { @@ -210,7 +209,7 @@ public static List find(Class type, String whereClause, String[] where return getEntitiesFromCursor(cursor, type); } - private static List findOneToMany(Class type, String relationFieldName, Object relationObject, Long relationObjectId) { + public static List findOneToMany(Class type, String relationFieldName, Object relationObject, Long relationObjectId) { String args[] = { String.valueOf(relationObjectId) }; String whereClause = NamingHelper.toSQLNameDefault(relationFieldName) + " = ?"; @@ -230,7 +229,13 @@ public static List getEntitiesFromCursor(Cursor cursor, Class type, St try { while (cursor.moveToNext()) { entity = type.getDeclaredConstructor().newInstance(); - inflate(cursor, entity, getSugarContext().getEntitiesMap(), relationFieldName, relationObject); + new ObjectInflater() + .withCursor(cursor) + .withObject(entity) + .withEntitiesMap(getSugarContext().getEntitiesMap()) + .withRelationFieldName(relationFieldName) + .withRelationObject(relationObject) + .inflate(); result.add(entity); } } catch (Exception e) { @@ -398,48 +403,6 @@ public static boolean isSugarEntity(Class objectClass) { return objectClass.isAnnotationPresent(Table.class) || SugarRecord.class.isAssignableFrom(objectClass); } - private static void inflate(Cursor cursor, Object object, Map entitiesMap) { - inflate(cursor, object, entitiesMap, null, null); - } - - private static void inflate(Cursor cursor, Object object, Map entitiesMap, String oneToManyFieldName, Object oneToManyObject) { - List columns = ReflectionUtil.getTableFields(object.getClass()); - Long objectId = cursor.getLong(cursor.getColumnIndex(("ID"))); - if (!entitiesMap.containsKey(object)) { - entitiesMap.put(object, objectId); - } - - for (Field field : columns) { - field.setAccessible(true); - Class fieldType = field.getType(); - if (isSugarEntity(fieldType)) { - try { - if (field.getName().equals(oneToManyFieldName)) { - field.set(object, oneToManyObject); - } else { - long id = cursor.getLong(cursor.getColumnIndex(NamingHelper.toColumnName(field))); - field.set(object, (id > 0) ? findById(fieldType, id) : null); - } - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } else if (fieldType.equals(List.class)) { - if (field.isAnnotationPresent(OneToMany.class)) { - try { - ParameterizedType genericListType = (ParameterizedType) field.getGenericType(); - Class genericListClass = (Class) genericListType.getActualTypeArguments()[0]; - String targetName = field.getAnnotation(OneToMany.class).targetField(); - field.set(object, findOneToMany(genericListClass, targetName, object, objectId)); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - } else { - ReflectionUtil.setFieldValueFromCursor(cursor, field, object); - } - } - } - public boolean delete() { Long id = getId(); Class type = getClass(); @@ -506,7 +469,11 @@ public long update() { @SuppressWarnings("unchecked") void inflate(Cursor cursor) { - inflate(cursor, this, getSugarContext().getEntitiesMap()); + new ObjectInflater() + .withCursor(cursor) + .withObject(this) + .withEntitiesMap(getSugarContext().getEntitiesMap()) + .inflate(); } public Long getId() { @@ -544,7 +511,11 @@ public E next() { try { entity = type.getDeclaredConstructor().newInstance(); - inflate(cursor, entity, getSugarContext().getEntitiesMap()); + new ObjectInflater() + .withCursor(cursor) + .withObject(entity) + .withEntitiesMap(getSugarContext().getEntitiesMap()) + .inflate(); } catch (Exception e) { e.printStackTrace(); } finally { diff --git a/library/src/main/java/com/orm/inflater/ObjectInflater.java b/library/src/main/java/com/orm/inflater/ObjectInflater.java new file mode 100644 index 00000000..10ed4213 --- /dev/null +++ b/library/src/main/java/com/orm/inflater/ObjectInflater.java @@ -0,0 +1,75 @@ +package com.orm.inflater; + +import android.database.Cursor; +import com.orm.SugarRecord; +import com.orm.inflater.field.*; +import com.orm.util.ReflectionUtil; + +import java.lang.reflect.Field; +import java.util.List; +import java.util.Map; + +/** + * Created by Łukasz Wesołowski on 03.08.2016. + */ +public class ObjectInflater { + private Cursor cursor; + private Object object; + private Object relationObject; + private String relationFieldName; + private Map entitiesMap; + + public ObjectInflater withCursor(Cursor cursor) { + this.cursor = cursor; + return this; + } + + public ObjectInflater withObject(Object object) { + this.object = object; + return this; + } + + public ObjectInflater withRelationObject(Object relationObject) { + this.relationObject = relationObject; + return this; + } + + public ObjectInflater withRelationFieldName(String relationFieldName) { + this.relationFieldName = relationFieldName; + return this; + } + + public ObjectInflater withEntitiesMap(Map entitiesMap) { + this.entitiesMap = entitiesMap; + return this; + } + + public void inflate() { + List columns = ReflectionUtil.getTableFields(object.getClass()); + Long objectId = cursor.getLong(cursor.getColumnIndex(("ID"))); + if (!entitiesMap.containsKey(object)) { + entitiesMap.put(object, objectId); + } + + FieldInflater fieldInflater; + + for (Field field : columns) { + field.setAccessible(true); + Class fieldType = field.getType(); + + if (SugarRecord.isSugarEntity(fieldType)) { + if (field.getName().equals(relationFieldName)) { + fieldInflater = new RelationEntityFieldInflater(field, cursor, object, fieldType, relationObject); + } else { + fieldInflater = new EntityFieldInflater(field, cursor, object, fieldType); + } + } else if (fieldType.equals(List.class)) { + fieldInflater = new ListFieldInflater(field, cursor, object, fieldType); + } else { + fieldInflater = new DefaultFieldInflater(field, cursor, object, fieldType); + } + + fieldInflater.inflate(); + } + } +} diff --git a/library/src/main/java/com/orm/inflater/field/DefaultFieldInflater.java b/library/src/main/java/com/orm/inflater/field/DefaultFieldInflater.java new file mode 100644 index 00000000..d1cd8542 --- /dev/null +++ b/library/src/main/java/com/orm/inflater/field/DefaultFieldInflater.java @@ -0,0 +1,21 @@ +package com.orm.inflater.field; + +import android.database.Cursor; +import com.orm.util.ReflectionUtil; + +import java.lang.reflect.Field; + +/** + * Created by Łukasz Wesołowski on 03.08.2016. + */ +public class DefaultFieldInflater extends FieldInflater { + + public DefaultFieldInflater(Field field, Cursor cursor, Object object, Class fieldType) { + super(field, cursor, object, fieldType); + } + + @Override + public void inflate() { + ReflectionUtil.setFieldValueFromCursor(cursor, field, object); + } +} diff --git a/library/src/main/java/com/orm/inflater/field/EntityFieldInflater.java b/library/src/main/java/com/orm/inflater/field/EntityFieldInflater.java new file mode 100644 index 00000000..e56fc742 --- /dev/null +++ b/library/src/main/java/com/orm/inflater/field/EntityFieldInflater.java @@ -0,0 +1,27 @@ +package com.orm.inflater.field; + +import android.database.Cursor; +import com.orm.SugarRecord; +import com.orm.helper.NamingHelper; + +import java.lang.reflect.Field; + +/** + * Created by Łukasz Wesołowski on 03.08.2016. + */ +public class EntityFieldInflater extends FieldInflater { + + public EntityFieldInflater(Field field, Cursor cursor, Object object, Class fieldType) { + super(field, cursor, object, fieldType); + } + + @Override + public void inflate() { + try { + long id = cursor.getLong(cursor.getColumnIndex(NamingHelper.toColumnName(field))); + field.set(object, (id > 0) ? SugarRecord.findById(fieldType, id) : null); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } +} diff --git a/library/src/main/java/com/orm/inflater/field/FieldInflater.java b/library/src/main/java/com/orm/inflater/field/FieldInflater.java new file mode 100644 index 00000000..87795340 --- /dev/null +++ b/library/src/main/java/com/orm/inflater/field/FieldInflater.java @@ -0,0 +1,24 @@ +package com.orm.inflater.field; + +import android.database.Cursor; + +import java.lang.reflect.Field; + +/** + * Created by Łukasz Wesołowski on 03.08.2016. + */ +public abstract class FieldInflater { + protected Field field; + protected Cursor cursor; + protected Object object; + protected Class fieldType; + + public FieldInflater(Field field, Cursor cursor, Object object, Class fieldType) { + this.field = field; + this.cursor = cursor; + this.object = object; + this.fieldType = fieldType; + } + + public abstract void inflate(); +} diff --git a/library/src/main/java/com/orm/inflater/field/ListFieldInflater.java b/library/src/main/java/com/orm/inflater/field/ListFieldInflater.java new file mode 100644 index 00000000..8de64722 --- /dev/null +++ b/library/src/main/java/com/orm/inflater/field/ListFieldInflater.java @@ -0,0 +1,38 @@ +package com.orm.inflater.field; + +import android.database.Cursor; +import android.util.Log; +import com.orm.SugarRecord; +import com.orm.annotation.OneToMany; + +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; + +/** + * Created by Łukasz Wesołowski on 03.08.2016. + */ +public class ListFieldInflater extends FieldInflater { + private static final String LOG_TAG = "ListFieldInflater"; + + public ListFieldInflater(Field field, Cursor cursor, Object object, Class fieldType) { + super(field, cursor, object, fieldType); + } + + @Override + public void inflate() { + if (field.isAnnotationPresent(OneToMany.class)) { + try { + Long objectId = cursor.getLong(cursor.getColumnIndex(("ID"))); + + ParameterizedType genericListType = (ParameterizedType) field.getGenericType(); + Class genericListClass = (Class) genericListType.getActualTypeArguments()[0]; + String targetName = field.getAnnotation(OneToMany.class).targetField(); + field.set(object, SugarRecord.findOneToMany(genericListClass, targetName, object, objectId)); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } else { + Log.w(LOG_TAG, String.format("List field %s has not OneToMany annotation", field)); + } + } +} diff --git a/library/src/main/java/com/orm/inflater/field/RelationEntityFieldInflater.java b/library/src/main/java/com/orm/inflater/field/RelationEntityFieldInflater.java new file mode 100644 index 00000000..72df20f7 --- /dev/null +++ b/library/src/main/java/com/orm/inflater/field/RelationEntityFieldInflater.java @@ -0,0 +1,29 @@ +package com.orm.inflater.field; + +import android.database.Cursor; +import android.util.Log; + +import java.lang.reflect.Field; + +/** + * Created by Łukasz Wesołowski on 03.08.2016. + */ +public class RelationEntityFieldInflater extends EntityFieldInflater { + private static final String LOG_TAG = "RelEntityFieldInflater"; + + protected Object relationObject; + + public RelationEntityFieldInflater(Field field, Cursor cursor, Object object, Class fieldType, Object relationObject) { + super(field, cursor, object, fieldType); + this.relationObject = relationObject; + } + + @Override + public void inflate() { + try { + field.set(object, relationObject); + } catch (IllegalAccessException e) { + Log.e(LOG_TAG, String.format("Error while inflating %s field", field), e); + } + } +} From 463bf3e9cb39b7e7b3c66d7683a03a14637a727f Mon Sep 17 00:00:00 2001 From: Vookash Date: Thu, 18 Aug 2016 21:12:52 +0200 Subject: [PATCH 124/139] Refactored ObjectInflater name. --- library/src/main/java/com/orm/SugarRecord.java | 8 ++++---- .../{ObjectInflater.java => EntityInflater.java} | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) rename library/src/main/java/com/orm/inflater/{ObjectInflater.java => EntityInflater.java} (85%) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index d72c2b7e..f4f4bd26 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -11,7 +11,7 @@ import com.orm.annotation.Unique; import com.orm.helper.ManifestHelper; import com.orm.helper.NamingHelper; -import com.orm.inflater.ObjectInflater; +import com.orm.inflater.EntityInflater; import com.orm.util.QueryBuilder; import com.orm.util.ReflectionUtil; import com.orm.util.SugarCursor; @@ -229,7 +229,7 @@ public static List getEntitiesFromCursor(Cursor cursor, Class type, St try { while (cursor.moveToNext()) { entity = type.getDeclaredConstructor().newInstance(); - new ObjectInflater() + new EntityInflater() .withCursor(cursor) .withObject(entity) .withEntitiesMap(getSugarContext().getEntitiesMap()) @@ -469,7 +469,7 @@ public long update() { @SuppressWarnings("unchecked") void inflate(Cursor cursor) { - new ObjectInflater() + new EntityInflater() .withCursor(cursor) .withObject(this) .withEntitiesMap(getSugarContext().getEntitiesMap()) @@ -511,7 +511,7 @@ public E next() { try { entity = type.getDeclaredConstructor().newInstance(); - new ObjectInflater() + new EntityInflater() .withCursor(cursor) .withObject(entity) .withEntitiesMap(getSugarContext().getEntitiesMap()) diff --git a/library/src/main/java/com/orm/inflater/ObjectInflater.java b/library/src/main/java/com/orm/inflater/EntityInflater.java similarity index 85% rename from library/src/main/java/com/orm/inflater/ObjectInflater.java rename to library/src/main/java/com/orm/inflater/EntityInflater.java index 10ed4213..5d71e5df 100644 --- a/library/src/main/java/com/orm/inflater/ObjectInflater.java +++ b/library/src/main/java/com/orm/inflater/EntityInflater.java @@ -12,34 +12,34 @@ /** * Created by Łukasz Wesołowski on 03.08.2016. */ -public class ObjectInflater { +public class EntityInflater { private Cursor cursor; private Object object; private Object relationObject; private String relationFieldName; private Map entitiesMap; - public ObjectInflater withCursor(Cursor cursor) { + public EntityInflater withCursor(Cursor cursor) { this.cursor = cursor; return this; } - public ObjectInflater withObject(Object object) { + public EntityInflater withObject(Object object) { this.object = object; return this; } - public ObjectInflater withRelationObject(Object relationObject) { + public EntityInflater withRelationObject(Object relationObject) { this.relationObject = relationObject; return this; } - public ObjectInflater withRelationFieldName(String relationFieldName) { + public EntityInflater withRelationFieldName(String relationFieldName) { this.relationFieldName = relationFieldName; return this; } - public ObjectInflater withEntitiesMap(Map entitiesMap) { + public EntityInflater withEntitiesMap(Map entitiesMap) { this.entitiesMap = entitiesMap; return this; } From 0f466d7785cb30029f40ddb91817b43626ce5c01 Mon Sep 17 00:00:00 2001 From: Vookash Date: Thu, 18 Aug 2016 21:49:45 +0200 Subject: [PATCH 125/139] Improved tests. --- .../java/com/orm/model/ManyToOneModel.java | 37 +---- .../java/com/orm/model/OneToManyModel.java | 37 +---- .../java/com/orm/record/OneToManyTest.java | 144 +++++++++++------- 3 files changed, 93 insertions(+), 125 deletions(-) diff --git a/library/src/test/java/com/orm/model/ManyToOneModel.java b/library/src/test/java/com/orm/model/ManyToOneModel.java index 4e704142..c1dbf434 100644 --- a/library/src/test/java/com/orm/model/ManyToOneModel.java +++ b/library/src/test/java/com/orm/model/ManyToOneModel.java @@ -6,15 +6,14 @@ * Created by Łukasz Wesołowski on 28.07.2016. */ public class ManyToOneModel extends SugarRecord { - private String name; private OneToManyModel model; - public String getName() { - return name; + public ManyToOneModel() { } - public void setName(String name) { - this.name = name; + public ManyToOneModel(Long id, OneToManyModel model) { + setId(id); + this.model = model; } public OneToManyModel getModel() { @@ -24,32 +23,4 @@ public OneToManyModel getModel() { public void setModel(OneToManyModel model) { this.model = model; } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - ManyToOneModel that = (ManyToOneModel) o; - - if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null) return false; - return name != null ? name.equals(that.name) : that.name == null; - - } - - @Override - public int hashCode() { - int result = getId() != null ? getId().hashCode() : 0; - result = 31 * result + (name != null ? name.hashCode() : 0); - return result; - } - - @Override - public String toString() { - return "ManyToOneModel{" + - "id=" + getId() + - ", name='" + name + '\'' + - ", model=" + model + - '}'; - } } diff --git a/library/src/test/java/com/orm/model/OneToManyModel.java b/library/src/test/java/com/orm/model/OneToManyModel.java index e23b2a1c..f4d24766 100644 --- a/library/src/test/java/com/orm/model/OneToManyModel.java +++ b/library/src/test/java/com/orm/model/OneToManyModel.java @@ -9,16 +9,15 @@ * Created by Łukasz Wesołowski on 28.07.2016. */ public class OneToManyModel extends SugarRecord { - private String name; + Long id; @OneToMany(targetField = "model") private List models; - public String getName() { - return name; + public OneToManyModel() { } - public void setName(String name) { - this.name = name; + public OneToManyModel(Long id) { + setId(id); } public List getModels() { @@ -28,32 +27,4 @@ public List getModels() { public void setModels(List models) { this.models = models; } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - OneToManyModel that = (OneToManyModel) o; - - if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null) return false; - return name != null ? name.equals(that.name) : that.name == null; - - } - - @Override - public int hashCode() { - int result = getId() != null ? getId().hashCode() : 0; - result = 31 * result + (name != null ? name.hashCode() : 0); - return result; - } - - @Override - public String toString() { - return "OneToManyModel{" + - "id=" + getId() + - ", name='" + name + '\'' + - ", models=" + models + - '}'; - } } diff --git a/library/src/test/java/com/orm/record/OneToManyTest.java b/library/src/test/java/com/orm/record/OneToManyTest.java index f9ddd9f4..f0ed48c4 100644 --- a/library/src/test/java/com/orm/record/OneToManyTest.java +++ b/library/src/test/java/com/orm/record/OneToManyTest.java @@ -10,6 +10,12 @@ import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; +import java.util.Arrays; +import java.util.List; + +import static com.orm.SugarRecord.save; +import static com.orm.SugarRecord.findById; + /** * Created by Łukasz Wesołowski on 28.07.2016. */ @@ -18,79 +24,99 @@ @Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public class OneToManyTest { @Test - public void saveOneToManyRelationTest() { - OneToManyModel oneToManyModel = new OneToManyModel(); - oneToManyModel.setId(1l); - oneToManyModel.setName("oneToMany"); - oneToManyModel.save(); - - ManyToOneModel manyToOneModel1 = new ManyToOneModel(); - manyToOneModel1.setId(1l); - manyToOneModel1.setName("manyToOne 1"); - manyToOneModel1.setModel(oneToManyModel); - manyToOneModel1.save(); - - ManyToOneModel manyToOneModel2 = new ManyToOneModel(); - manyToOneModel2.setId(2l); - manyToOneModel2.setName("manyToOne 2"); - manyToOneModel2.setModel(oneToManyModel); - manyToOneModel2.save(); - - ManyToOneModel manyToOneModel3 = new ManyToOneModel(); - manyToOneModel3.setId(3l); - manyToOneModel3.setName("manyToOne 3"); - manyToOneModel3.setModel(oneToManyModel); - manyToOneModel3.save(); - - OneToManyModel result = OneToManyModel.findById(OneToManyModel.class, 1l); + public void shouldSaveWithOneToManyRelation() { + List relationIds = Arrays.asList(1l, 2l, 3l, 4l); - Assert.assertEquals(3, result.getModels().size()); + OneToManyModel model = new OneToManyModel(1l); + save(model); + + for (long i : relationIds) { + save(new ManyToOneModel(i, model)); + } - Assert.assertTrue(result.getModels().contains(manyToOneModel1)); - Assert.assertTrue(result.getModels().contains(manyToOneModel2)); - Assert.assertTrue(result.getModels().contains(manyToOneModel3)); + OneToManyModel result = findById(OneToManyModel.class, 1l); + + Assert.assertEquals(4, result.getModels().size()); + + Assert.assertTrue(relationIds.contains(result.getModels().get(0).getId())); + Assert.assertTrue(relationIds.contains(result.getModels().get(1).getId())); + Assert.assertTrue(relationIds.contains(result.getModels().get(2).getId())); + Assert.assertTrue(relationIds.contains(result.getModels().get(3).getId())); Assert.assertEquals(result, result.getModels().get(0).getModel()); Assert.assertEquals(result, result.getModels().get(1).getModel()); Assert.assertEquals(result, result.getModels().get(2).getModel()); + Assert.assertEquals(result, result.getModels().get(3).getModel()); } @Test - public void removeOneToManyRelationTest() { - OneToManyModel oneToManyModel = new OneToManyModel(); - oneToManyModel.setId(1l); - oneToManyModel.setName("oneToMany"); - oneToManyModel.save(); - - ManyToOneModel manyToOneModel1 = new ManyToOneModel(); - manyToOneModel1.setId(1l); - manyToOneModel1.setName("manyToOne 1"); - manyToOneModel1.setModel(oneToManyModel); - manyToOneModel1.save(); - - ManyToOneModel manyToOneModel2 = new ManyToOneModel(); - manyToOneModel2.setId(2l); - manyToOneModel2.setName("manyToOne 2"); - manyToOneModel2.setModel(oneToManyModel); - manyToOneModel2.save(); - - ManyToOneModel manyToOneModel3 = new ManyToOneModel(); - manyToOneModel3.setId(3l); - manyToOneModel3.setName("manyToOne 3"); - manyToOneModel3.setModel(oneToManyModel); - manyToOneModel3.save(); - - OneToManyModel result = OneToManyModel.findById(OneToManyModel.class, 1l); + public void shouldRemoveOneOfManyToOneRelation() { + OneToManyModel model = new OneToManyModel(1l); + save(model); + + for (long i : Arrays.asList(1l, 2l, 3l, 4l)) { + save(new ManyToOneModel(i, model)); + } + + OneToManyModel result = findById(OneToManyModel.class, 1l); + + Assert.assertEquals(4, result.getModels().size()); + + ManyToOneModel.deleteAll(ManyToOneModel.class, "id = ?", String.valueOf(3l)); + + result = findById(OneToManyModel.class, 1l); Assert.assertEquals(3, result.getModels().size()); - ManyToOneModel.delete(manyToOneModel2); + Assert.assertTrue(result.getModels().get(0).getId() != 3l); + Assert.assertTrue(result.getModels().get(1).getId() != 3l); + Assert.assertTrue(result.getModels().get(2).getId() != 3l); + } + + @Test + public void shouldNotRemoveRelation() { + OneToManyModel model = new OneToManyModel(1l); + save(model); + + for (long i : Arrays.asList(1l, 2l, 3l, 4l)) { + save(new ManyToOneModel(i, model)); + } - result = OneToManyModel.findById(OneToManyModel.class, 1l); + OneToManyModel result = findById(OneToManyModel.class, 1l); - Assert.assertEquals(2, result.getModels().size()); + result.getModels().clear(); - Assert.assertTrue(result.getModels().contains(manyToOneModel1)); - Assert.assertTrue(result.getModels().contains(manyToOneModel3)); + save(model); + + result = findById(OneToManyModel.class, 1l); + + Assert.assertEquals(4, result.getModels().size()); + } + + @Test + public void shouldNotAddRelation() { + List relationIds = Arrays.asList(1l, 2l, 3l, 4l); + OneToManyModel model = new OneToManyModel(1l); + save(model); + + for (long i : relationIds) { + save(new ManyToOneModel(i, model)); + } + + save(new ManyToOneModel(5l, null)); + + OneToManyModel result = findById(OneToManyModel.class, 1l); + + Assert.assertEquals(4, result.getModels().size()); + + Assert.assertTrue(relationIds.contains(result.getModels().get(0).getId())); + Assert.assertTrue(relationIds.contains(result.getModels().get(1).getId())); + Assert.assertTrue(relationIds.contains(result.getModels().get(2).getId())); + Assert.assertTrue(relationIds.contains(result.getModels().get(3).getId())); + + Assert.assertEquals(result, result.getModels().get(0).getModel()); + Assert.assertEquals(result, result.getModels().get(1).getModel()); + Assert.assertEquals(result, result.getModels().get(2).getModel()); + Assert.assertEquals(result, result.getModels().get(3).getModel()); } } From 1ca3ecce3dfccf09406c786e7dfa780d516e247c Mon Sep 17 00:00:00 2001 From: Vookash Date: Thu, 18 Aug 2016 21:54:37 +0200 Subject: [PATCH 126/139] Improved tests. --- library/src/test/java/com/orm/record/OneToManyTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/test/java/com/orm/record/OneToManyTest.java b/library/src/test/java/com/orm/record/OneToManyTest.java index f0ed48c4..73de0775 100644 --- a/library/src/test/java/com/orm/record/OneToManyTest.java +++ b/library/src/test/java/com/orm/record/OneToManyTest.java @@ -13,8 +13,8 @@ import java.util.Arrays; import java.util.List; -import static com.orm.SugarRecord.save; import static com.orm.SugarRecord.findById; +import static com.orm.SugarRecord.save; /** * Created by Łukasz Wesołowski on 28.07.2016. From e93b2f1dfc90dcf44e624552270e8bfa78eae573 Mon Sep 17 00:00:00 2001 From: Vookash Date: Mon, 22 Aug 2016 23:33:01 +0200 Subject: [PATCH 127/139] Fixed NamingHelperTest. --- .../java/com/orm/helper/NamingHelperTest.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/library/src/test/java/com/orm/helper/NamingHelperTest.java b/library/src/test/java/com/orm/helper/NamingHelperTest.java index 0cc8a9d6..d53769ad 100644 --- a/library/src/test/java/com/orm/helper/NamingHelperTest.java +++ b/library/src/test/java/com/orm/helper/NamingHelperTest.java @@ -1,22 +1,23 @@ package com.orm.helper; +import com.orm.app.ClientApp; +import com.orm.dsl.BuildConfig; import com.orm.model.TestRecord; import com.orm.util.ReflectionUtil; - import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.annotation.Config; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNull; -import static junit.framework.Assert.assertTrue; - -import static com.orm.helper.NamingHelper.toColumnName; -import static com.orm.helper.NamingHelper.toTableName; -import static com.orm.helper.NamingHelper.toSQLNameDefault; +import static com.orm.helper.NamingHelper.*; +import static junit.framework.Assert.*; +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk = 18, constants = BuildConfig.class, application = ClientApp.class, packageName = "com.orm.model", manifest = Config.NONE) public final class NamingHelperTest { @Test(expected = IllegalAccessException.class) From cbd08b4fca320aea255b8a40ed379ed23ce1bd3b Mon Sep 17 00:00:00 2001 From: Vookash Date: Sat, 27 Aug 2016 22:17:01 +0200 Subject: [PATCH 128/139] Improved tests and logging. --- .../inflater/field/EntityFieldInflater.java | 4 ++- .../orm/inflater/field/ListFieldInflater.java | 2 +- .../java/com/orm/SchemaGeneratorTest.java | 2 +- .../model/{ => onetomany}/OneToManyModel.java | 9 +++-- .../OneToManyRelationModel.java} | 8 ++--- .../WithoutOneToManyAnnotationModel.java | 27 +++++++++++++++ ...thoutOneToManyAnnotationRelationModel.java | 26 +++++++++++++++ .../java/com/orm/record/OneToManyTest.java | 33 ++++++++++++++----- .../java/com/orm/util/ReflectionUtilTest.java | 2 +- 9 files changed, 92 insertions(+), 21 deletions(-) rename library/src/test/java/com/orm/model/{ => onetomany}/OneToManyModel.java (66%) rename library/src/test/java/com/orm/model/{ManyToOneModel.java => onetomany/OneToManyRelationModel.java} (63%) create mode 100644 library/src/test/java/com/orm/model/onetomany/WithoutOneToManyAnnotationModel.java create mode 100644 library/src/test/java/com/orm/model/onetomany/WithoutOneToManyAnnotationRelationModel.java diff --git a/library/src/main/java/com/orm/inflater/field/EntityFieldInflater.java b/library/src/main/java/com/orm/inflater/field/EntityFieldInflater.java index e56fc742..9eda8f8e 100644 --- a/library/src/main/java/com/orm/inflater/field/EntityFieldInflater.java +++ b/library/src/main/java/com/orm/inflater/field/EntityFieldInflater.java @@ -1,6 +1,7 @@ package com.orm.inflater.field; import android.database.Cursor; +import android.util.Log; import com.orm.SugarRecord; import com.orm.helper.NamingHelper; @@ -10,6 +11,7 @@ * Created by Łukasz Wesołowski on 03.08.2016. */ public class EntityFieldInflater extends FieldInflater { + private static final String LOG_TAG = "EntityFieldInflater"; public EntityFieldInflater(Field field, Cursor cursor, Object object, Class fieldType) { super(field, cursor, object, fieldType); @@ -21,7 +23,7 @@ public void inflate() { long id = cursor.getLong(cursor.getColumnIndex(NamingHelper.toColumnName(field))); field.set(object, (id > 0) ? SugarRecord.findById(fieldType, id) : null); } catch (IllegalAccessException e) { - e.printStackTrace(); + Log.e(LOG_TAG, String.format("Error while inflating entity field %s", field), e); } } } diff --git a/library/src/main/java/com/orm/inflater/field/ListFieldInflater.java b/library/src/main/java/com/orm/inflater/field/ListFieldInflater.java index 8de64722..5fa7496c 100644 --- a/library/src/main/java/com/orm/inflater/field/ListFieldInflater.java +++ b/library/src/main/java/com/orm/inflater/field/ListFieldInflater.java @@ -29,7 +29,7 @@ public void inflate() { String targetName = field.getAnnotation(OneToMany.class).targetField(); field.set(object, SugarRecord.findOneToMany(genericListClass, targetName, object, objectId)); } catch (IllegalAccessException e) { - e.printStackTrace(); + Log.e(LOG_TAG, String.format("Error while inflating list field %s", field), e); } } else { Log.w(LOG_TAG, String.format("List field %s has not OneToMany annotation", field)); diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index 3b39c38c..90f35c07 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -137,7 +137,7 @@ public void testAllTableCreation() { Cursor c = sqLiteDatabase.rawQuery(sql, null); if (c.moveToFirst()) { - Assert.assertEquals(45, c.getInt(0)); + Assert.assertEquals(47, c.getInt(0)); } if (!c.isClosed()) { diff --git a/library/src/test/java/com/orm/model/OneToManyModel.java b/library/src/test/java/com/orm/model/onetomany/OneToManyModel.java similarity index 66% rename from library/src/test/java/com/orm/model/OneToManyModel.java rename to library/src/test/java/com/orm/model/onetomany/OneToManyModel.java index f4d24766..bb7bd96a 100644 --- a/library/src/test/java/com/orm/model/OneToManyModel.java +++ b/library/src/test/java/com/orm/model/onetomany/OneToManyModel.java @@ -1,4 +1,4 @@ -package com.orm.model; +package com.orm.model.onetomany; import com.orm.SugarRecord; import com.orm.annotation.OneToMany; @@ -9,9 +9,8 @@ * Created by Łukasz Wesołowski on 28.07.2016. */ public class OneToManyModel extends SugarRecord { - Long id; @OneToMany(targetField = "model") - private List models; + private List models; public OneToManyModel() { } @@ -20,11 +19,11 @@ public OneToManyModel(Long id) { setId(id); } - public List getModels() { + public List getModels() { return models; } - public void setModels(List models) { + public void setModels(List models) { this.models = models; } } diff --git a/library/src/test/java/com/orm/model/ManyToOneModel.java b/library/src/test/java/com/orm/model/onetomany/OneToManyRelationModel.java similarity index 63% rename from library/src/test/java/com/orm/model/ManyToOneModel.java rename to library/src/test/java/com/orm/model/onetomany/OneToManyRelationModel.java index c1dbf434..4c17ec56 100644 --- a/library/src/test/java/com/orm/model/ManyToOneModel.java +++ b/library/src/test/java/com/orm/model/onetomany/OneToManyRelationModel.java @@ -1,17 +1,17 @@ -package com.orm.model; +package com.orm.model.onetomany; import com.orm.SugarRecord; /** * Created by Łukasz Wesołowski on 28.07.2016. */ -public class ManyToOneModel extends SugarRecord { +public class OneToManyRelationModel extends SugarRecord { private OneToManyModel model; - public ManyToOneModel() { + public OneToManyRelationModel() { } - public ManyToOneModel(Long id, OneToManyModel model) { + public OneToManyRelationModel(Long id, OneToManyModel model) { setId(id); this.model = model; } diff --git a/library/src/test/java/com/orm/model/onetomany/WithoutOneToManyAnnotationModel.java b/library/src/test/java/com/orm/model/onetomany/WithoutOneToManyAnnotationModel.java new file mode 100644 index 00000000..d48e38f0 --- /dev/null +++ b/library/src/test/java/com/orm/model/onetomany/WithoutOneToManyAnnotationModel.java @@ -0,0 +1,27 @@ +package com.orm.model.onetomany; + +import com.orm.SugarRecord; + +import java.util.List; + +/** + * Created by Łukasz Wesołowski on 27.08.2016. + */ +public class WithoutOneToManyAnnotationModel extends SugarRecord { + private List models; + + public WithoutOneToManyAnnotationModel() { + } + + public WithoutOneToManyAnnotationModel(Long id) { + setId(id); + } + + public List getModels() { + return models; + } + + public void setModels(List models) { + this.models = models; + } +} diff --git a/library/src/test/java/com/orm/model/onetomany/WithoutOneToManyAnnotationRelationModel.java b/library/src/test/java/com/orm/model/onetomany/WithoutOneToManyAnnotationRelationModel.java new file mode 100644 index 00000000..34e1eb2a --- /dev/null +++ b/library/src/test/java/com/orm/model/onetomany/WithoutOneToManyAnnotationRelationModel.java @@ -0,0 +1,26 @@ +package com.orm.model.onetomany; + +import com.orm.SugarRecord; + +/** + * Created by Łukasz Wesołowski on 27.08.2016. + */ +public class WithoutOneToManyAnnotationRelationModel extends SugarRecord { + private WithoutOneToManyAnnotationModel model; + + public WithoutOneToManyAnnotationRelationModel() { + } + + public WithoutOneToManyAnnotationRelationModel(Long id, WithoutOneToManyAnnotationModel model) { + setId(id); + this.model = model; + } + + public WithoutOneToManyAnnotationModel getModel() { + return model; + } + + public void setModel(WithoutOneToManyAnnotationModel model) { + this.model = model; + } +} diff --git a/library/src/test/java/com/orm/record/OneToManyTest.java b/library/src/test/java/com/orm/record/OneToManyTest.java index 73de0775..73a60b57 100644 --- a/library/src/test/java/com/orm/record/OneToManyTest.java +++ b/library/src/test/java/com/orm/record/OneToManyTest.java @@ -2,8 +2,10 @@ import com.orm.app.ClientApp; import com.orm.dsl.BuildConfig; -import com.orm.model.ManyToOneModel; -import com.orm.model.OneToManyModel; +import com.orm.model.onetomany.OneToManyRelationModel; +import com.orm.model.onetomany.OneToManyModel; +import com.orm.model.onetomany.WithoutOneToManyAnnotationModel; +import com.orm.model.onetomany.WithoutOneToManyAnnotationRelationModel; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -31,7 +33,7 @@ public void shouldSaveWithOneToManyRelation() { save(model); for (long i : relationIds) { - save(new ManyToOneModel(i, model)); + save(new OneToManyRelationModel(i, model)); } OneToManyModel result = findById(OneToManyModel.class, 1l); @@ -55,14 +57,14 @@ public void shouldRemoveOneOfManyToOneRelation() { save(model); for (long i : Arrays.asList(1l, 2l, 3l, 4l)) { - save(new ManyToOneModel(i, model)); + save(new OneToManyRelationModel(i, model)); } OneToManyModel result = findById(OneToManyModel.class, 1l); Assert.assertEquals(4, result.getModels().size()); - ManyToOneModel.deleteAll(ManyToOneModel.class, "id = ?", String.valueOf(3l)); + OneToManyRelationModel.deleteAll(OneToManyRelationModel.class, "id = ?", String.valueOf(3l)); result = findById(OneToManyModel.class, 1l); @@ -79,7 +81,7 @@ public void shouldNotRemoveRelation() { save(model); for (long i : Arrays.asList(1l, 2l, 3l, 4l)) { - save(new ManyToOneModel(i, model)); + save(new OneToManyRelationModel(i, model)); } OneToManyModel result = findById(OneToManyModel.class, 1l); @@ -100,10 +102,10 @@ public void shouldNotAddRelation() { save(model); for (long i : relationIds) { - save(new ManyToOneModel(i, model)); + save(new OneToManyRelationModel(i, model)); } - save(new ManyToOneModel(5l, null)); + save(new OneToManyRelationModel(5l, null)); OneToManyModel result = findById(OneToManyModel.class, 1l); @@ -119,4 +121,19 @@ public void shouldNotAddRelation() { Assert.assertEquals(result, result.getModels().get(2).getModel()); Assert.assertEquals(result, result.getModels().get(3).getModel()); } + + @Test + public void shouldNotInflateList() { + List relationIds = Arrays.asList(1l, 2l, 3l, 4l); + WithoutOneToManyAnnotationModel model = new WithoutOneToManyAnnotationModel(1l); + save(model); + + for (long i : relationIds) { + save(new WithoutOneToManyAnnotationRelationModel(i, model)); + } + + WithoutOneToManyAnnotationModel result = findById(WithoutOneToManyAnnotationModel.class, 1l); + + Assert.assertEquals(null, result.getModels()); + } } diff --git a/library/src/test/java/com/orm/util/ReflectionUtilTest.java b/library/src/test/java/com/orm/util/ReflectionUtilTest.java index 09fd4755..318aa40d 100644 --- a/library/src/test/java/com/orm/util/ReflectionUtilTest.java +++ b/library/src/test/java/com/orm/util/ReflectionUtilTest.java @@ -72,7 +72,7 @@ public void testSetFieldValueForId() { @Test public void testGetAllClasses() { List classes = ReflectionUtil.getDomainClasses(); - Assert.assertEquals(43, classes.size()); + Assert.assertEquals(45, classes.size()); } @Test(expected = NoSuchFieldException.class) From df12176aec8eb9d04e66d96b3951dac61f75f618 Mon Sep 17 00:00:00 2001 From: bioker Date: Thu, 29 Sep 2016 00:44:35 +0300 Subject: [PATCH 129/139] NullPointerException when try to add null foreign record fix --- .../java/com/orm/util/ReflectionUtil.java | 8 +++++--- .../orm/model/foreignnull/OriginRecord.java | 20 +++++++++++++++++++ .../java/com/orm/util/ReflectionUtilTest.java | 8 ++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 library/src/test/java/com/orm/model/foreignnull/OriginRecord.java diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index 71a8b8c6..18943d09 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -78,9 +78,11 @@ public static void addFieldValueToColumn(ContentValues values, Field column, Obj try { field = columnType.getDeclaredField("id"); field.setAccessible(true); - values.put(columnName, - (field != null) - ? String.valueOf(field.get(columnValue)) : "0"); + if(columnValue != null) { + values.put(columnName,String.valueOf(field.get(columnValue))); + } else { + values.putNull(columnName); + } } catch (NoSuchFieldException e) { if (entitiesMap.containsKey(columnValue)) { values.put(columnName, entitiesMap.get(columnValue)); diff --git a/library/src/test/java/com/orm/model/foreignnull/OriginRecord.java b/library/src/test/java/com/orm/model/foreignnull/OriginRecord.java new file mode 100644 index 00000000..dbcd84bc --- /dev/null +++ b/library/src/test/java/com/orm/model/foreignnull/OriginRecord.java @@ -0,0 +1,20 @@ +package com.orm.model.foreignnull; + +import com.orm.annotation.Table; + +@Table +public class OriginRecord { + + private Long id; + private OriginRecord origin; + + public OriginRecord() { + } + + public OriginRecord(Long id, OriginRecord origin) { + this.id = id; + this.origin = origin; + } + + +} diff --git a/library/src/test/java/com/orm/util/ReflectionUtilTest.java b/library/src/test/java/com/orm/util/ReflectionUtilTest.java index 318aa40d..12738be3 100644 --- a/library/src/test/java/com/orm/util/ReflectionUtilTest.java +++ b/library/src/test/java/com/orm/util/ReflectionUtilTest.java @@ -4,9 +4,11 @@ import android.database.Cursor; import com.orm.SugarContext; +import com.orm.SugarRecord; import com.orm.app.ClientApp; import com.orm.dsl.BuildConfig; import com.orm.model.TestRecord; +import com.orm.model.foreignnull.OriginRecord; import com.orm.query.Select; import junit.framework.Assert; @@ -88,4 +90,10 @@ public void testSetFieldValueFromCursor() throws NoSuchFieldException { ReflectionUtil.setFieldValueFromCursor(cursor, field, testRecord); } + + @Test + public void testForeignNull() throws NoSuchFieldException { + final OriginRecord record = new OriginRecord(null,null); + SugarRecord.save(record); + } } From d057a0ca08e4b634da01f2b911bf339b2723bc0a Mon Sep 17 00:00:00 2001 From: bioker Date: Thu, 29 Sep 2016 08:13:55 +0300 Subject: [PATCH 130/139] fixes of tests for foreign record allow null integration --- library/src/test/java/com/orm/SchemaGeneratorTest.java | 2 +- library/src/test/java/com/orm/util/ReflectionUtilTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index 90f35c07..811d1aa1 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -137,7 +137,7 @@ public void testAllTableCreation() { Cursor c = sqLiteDatabase.rawQuery(sql, null); if (c.moveToFirst()) { - Assert.assertEquals(47, c.getInt(0)); + Assert.assertEquals(48, c.getInt(0)); } if (!c.isClosed()) { diff --git a/library/src/test/java/com/orm/util/ReflectionUtilTest.java b/library/src/test/java/com/orm/util/ReflectionUtilTest.java index 12738be3..a93e059a 100644 --- a/library/src/test/java/com/orm/util/ReflectionUtilTest.java +++ b/library/src/test/java/com/orm/util/ReflectionUtilTest.java @@ -74,7 +74,7 @@ public void testSetFieldValueForId() { @Test public void testGetAllClasses() { List classes = ReflectionUtil.getDomainClasses(); - Assert.assertEquals(45, classes.size()); + Assert.assertEquals(46, classes.size()); } @Test(expected = NoSuchFieldException.class) From b800c4a23f8fa84a90487987eb42be0c6d4665f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20Rossin=C3=A8s?= Date: Thu, 6 Oct 2016 15:11:14 +0200 Subject: [PATCH 131/139] * The @MultiUnique annotation is now inherited, so that we can use it on parent SugarRecord classes that are subclassed --- library/src/main/java/com/orm/annotation/MultiUnique.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/src/main/java/com/orm/annotation/MultiUnique.java b/library/src/main/java/com/orm/annotation/MultiUnique.java index 4c312f15..fbdaa0df 100644 --- a/library/src/main/java/com/orm/annotation/MultiUnique.java +++ b/library/src/main/java/com/orm/annotation/MultiUnique.java @@ -1,12 +1,15 @@ package com.orm.annotation; import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) +@Inherited public @interface MultiUnique { String value(); } + From e261342b9d5f0868d3132239dd95c848165b8538 Mon Sep 17 00:00:00 2001 From: Sibelius Seraphini Date: Fri, 23 Dec 2016 09:14:35 -0200 Subject: [PATCH 132/139] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 594782a7..d4d7c79d 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,11 @@ Insanely easy way to work with Android databases. Official documentation can be found [here](http://satyan.github.io/sugar) - Check some examples below. The example application is provided in the **example** folder in the source. +## Looking for contributors +We need contributors to help maintain this project, ask @satyan for repo permission + +Otherwise you can use another ORM, like https://github.com/requery/requery or https://realm.io/ + ## Features Sugar ORM was built in contrast to other ORM's to have: From 5dac15166154a4e9b8dc7f002960a60332fe1269 Mon Sep 17 00:00:00 2001 From: Rahul Ravindran Date: Sun, 25 Dec 2016 23:26:02 +0530 Subject: [PATCH 133/139] initial commit for max & average feature --- .../src/main/java/com/orm/SugarRecord.java | 101 ++++++++++++------ 1 file changed, 68 insertions(+), 33 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 2ade2ba3..fe260292 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -2,6 +2,7 @@ import android.content.ContentValues; import android.database.Cursor; +import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteStatement; @@ -311,6 +312,40 @@ public static long sum(Class type, String field, String whereClause, Stri return result; } + public static float average(Class type, String[] fields) { + return average(type, fields, null, null); + } + + public static float average(Class type, String[] fields, String whereClause, String... whereArgs) { + long result = -1; + String filter = (!TextUtils.isEmpty(whereClause)) ? "where" + whereClause : ""; + SQLiteStatement statement; + + String average = "( " + TextUtils.join(",", fields) + " )"; + try { + statement = getSugarDataBase().compileStatement("SELECT average" + average + " FROM " + + NamingHelper.toTableName(type) + filter); + } catch (SQLException e) { + e.printStackTrace(); + return result; + } + if (whereArgs != null) { + + } + + return result; + } + + public static long max(Class type, String[] fields) { + return max(type, fields, null, null); + } + + public static long max(Class type, String[] fields, String whereClause, String... whereArgs) { + long result = -1; + + return result; + } + public static long save(Object object) { return save(getSugarDataBase(), object); } @@ -403,22 +438,6 @@ public static boolean isSugarEntity(Class objectClass) { return objectClass.isAnnotationPresent(Table.class) || SugarRecord.class.isAssignableFrom(objectClass); } - public boolean delete() { - Long id = getId(); - Class type = getClass(); - if (id != null && id > 0L) { - if(ManifestHelper.isDebugEnabled()) { - Log.i(SUGAR, type.getSimpleName() + " deleted : " + id); - } - return getSugarDataBase().delete(NamingHelper.toTableName(type), "Id=?", new String[]{id.toString()}) == 1; - } else { - if(ManifestHelper.isDebugEnabled()) { - Log.i(SUGAR, "Cannot delete object: " + type.getSimpleName() + " - object has not been saved"); - } - return false; - } - } - public static boolean delete(Object object) { Class type = object.getClass(); if (type.isAnnotationPresent(Table.class)) { @@ -428,23 +447,23 @@ public static boolean delete(Object object) { Long id = (Long) field.get(object); if (id != null && id > 0L) { boolean deleted = getSugarDataBase().delete(NamingHelper.toTableName(type), "Id=?", new String[]{id.toString()}) == 1; - if(ManifestHelper.isDebugEnabled()) { + if (ManifestHelper.isDebugEnabled()) { Log.i(SUGAR, type.getSimpleName() + " deleted : " + id); } return deleted; } else { - if(ManifestHelper.isDebugEnabled()) { + if (ManifestHelper.isDebugEnabled()) { Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - object has not been saved"); } return false; } } catch (NoSuchFieldException e) { - if(ManifestHelper.isDebugEnabled()) { + if (ManifestHelper.isDebugEnabled()) { Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - annotated object has no id"); } return false; } catch (IllegalAccessException e) { - if(ManifestHelper.isDebugEnabled()) { + if (ManifestHelper.isDebugEnabled()) { Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - can't access id"); } return false; @@ -459,6 +478,35 @@ public static boolean delete(Object object) { } } + public static String[] replaceArgs(String[] args) { + + String[] replace = new String[args.length]; + for (int i = 0; i < args.length; i++) { + + replace[i] = (args[i].equals("true")) ? replace[i] = "1" : (args[i].equals("false")) ? replace[i] = "0" : args[i]; + + } + + return replace; + + } + + public boolean delete() { + Long id = getId(); + Class type = getClass(); + if (id != null && id > 0L) { + if (ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, type.getSimpleName() + " deleted : " + id); + } + return getSugarDataBase().delete(NamingHelper.toTableName(type), "Id=?", new String[]{id.toString()}) == 1; + } else { + if(ManifestHelper.isDebugEnabled()) { + Log.i(SUGAR, "Cannot delete object: " + type.getSimpleName() + " - object has not been saved"); + } + return false; + } + } + public long save() { return save(getSugarDataBase(), this); } @@ -534,17 +582,4 @@ public void remove() { } } - public static String[] replaceArgs(String[] args){ - - String [] replace = new String[args.length]; - for (int i=0; i Date: Mon, 26 Dec 2016 16:05:44 +0530 Subject: [PATCH 134/139] min & max methods added --- .../src/main/java/com/orm/SugarRecord.java | 75 +++++++++++++++++-- 1 file changed, 69 insertions(+), 6 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index fe260292..d8867d5d 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -18,6 +18,7 @@ import com.orm.util.SugarCursor; import java.lang.reflect.Field; +import java.sql.Statement; import java.util.*; import static com.orm.SugarContext.getSugarContext; @@ -312,27 +313,33 @@ public static long sum(Class type, String field, String whereClause, Stri return result; } - public static float average(Class type, String[] fields) { - return average(type, fields, null, null); + public static float average(Class type, String field) { + return average(type, field, null, null); } - public static float average(Class type, String[] fields, String whereClause, String... whereArgs) { + public static float average(Class type, String field, String whereClause, String... whereArgs) { long result = -1; String filter = (!TextUtils.isEmpty(whereClause)) ? "where" + whereClause : ""; SQLiteStatement statement; - String average = "( " + TextUtils.join(",", fields) + " )"; try { - statement = getSugarDataBase().compileStatement("SELECT average" + average + " FROM " + statement = getSugarDataBase().compileStatement("SELECT average(" + field + ") FROM " + NamingHelper.toTableName(type) + filter); } catch (SQLException e) { e.printStackTrace(); return result; } if (whereArgs != null) { - + for (int i = whereArgs.length ; i !=0 ; i--) { + statement.bindString(i,whereArgs[i-1]); + } } + try { + result = statement.simpleQueryForLong(); + } finally { + statement.close(); + } return result; } @@ -342,10 +349,66 @@ public static long max(Class type, String[] fields) { public static long max(Class type, String[] fields, String whereClause, String... whereArgs) { long result = -1; + String filter = (!TextUtils.isEmpty(whereClause))? "where" + whereClause : ""; + SQLiteStatement statement; + + String maxString = "max( " + TextUtils.join("," ,fields) + " )"; + + try { + statement = getSugarDataBase().compileStatement("SELECT " + maxString + " FROM " + + NamingHelper.toTableName(type) + filter); + } catch (SQLiteException e) { + e.printStackTrace(); + return result; + } + + if (whereArgs != null) { + for (int i= whereArgs.length - 1; i !=0 ; i--){ + statement.bindString(i,whereArgs[i-1]); + } + } + + try { + result = statement.simpleQueryForLong(); + } finally { + statement.close(); + } + + return result; + } + + public static long min(Class type, String[] fields){return min(type,fields,null,null);} + + public static long min(Class type , String[] fields , String whereClause, String... whereArgs){ + long result = -1; + String filter = (!TextUtils.isEmpty(whereClause)) ? "where" + whereClause : ""; + SQLiteStatement statement; + String minString = "min( " + TextUtils.join("," , fields); + + try { + statement = getSugarDataBase().compileStatement("SELECT " + minString + " FROM " + + NamingHelper.toTableName(type) + filter); + } catch (SQLiteException e) { + e.printStackTrace(); + return result; + } + + if (whereArgs != null){ + for (int i= whereArgs.length - 1; i !=0 ; i--){ + statement.bindString(i,whereArgs[i-1]); + } + } + try { + result = statement.simpleQueryForLong(); + } finally { + statement.close(); + } return result; } + + public static long save(Object object) { return save(getSugarDataBase(), object); } From fecf7365af3f0dbc45d86d101a475f0f978c004a Mon Sep 17 00:00:00 2001 From: Rahul Ravindran Date: Mon, 26 Dec 2016 18:04:34 +0530 Subject: [PATCH 135/139] sanity check for fields being null. --- library/src/main/java/com/orm/SugarRecord.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index d8867d5d..02e1593f 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -352,6 +352,14 @@ public static long max(Class type, String[] fields, String whereClause, S String filter = (!TextUtils.isEmpty(whereClause))? "where" + whereClause : ""; SQLiteStatement statement; + + //sanity check + for (String field: fields){ + if (field.isEmpty()) { + throw new NullPointerException("Field " + field + " is null."); + } + } + String maxString = "max( " + TextUtils.join("," ,fields) + " )"; try { @@ -393,6 +401,12 @@ public static long min(Class type , String[] fields , String whereClause, return result; } + //sanity check + for (String field : fields){ + if (field.isEmpty()) throw new NullPointerException("Field " + field + " is null."); + } + + if (whereArgs != null){ for (int i= whereArgs.length - 1; i !=0 ; i--){ statement.bindString(i,whereArgs[i-1]); From 11cc4972eb32222e72592477c2856e0ffd349555 Mon Sep 17 00:00:00 2001 From: Rahul Ravindran Date: Tue, 27 Dec 2016 20:02:26 +0530 Subject: [PATCH 136/139] raw query revert --- .../src/main/java/com/orm/SugarRecord.java | 110 ++++++++---------- 1 file changed, 46 insertions(+), 64 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 02e1593f..4b9e48d0 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -22,6 +22,7 @@ import java.util.*; import static com.orm.SugarContext.getSugarContext; +import static com.orm.SugarContext.init; public class SugarRecord { public static final String SUGAR = "Sugar"; @@ -318,105 +319,86 @@ public static float average(Class type, String field) { } public static float average(Class type, String field, String whereClause, String... whereArgs) { - long result = -1; - String filter = (!TextUtils.isEmpty(whereClause)) ? "where" + whereClause : ""; - SQLiteStatement statement; + float result = -1; + Cursor cursor; + String filter = (!TextUtils.isEmpty(whereClause))? "where" + whereClause : ""; + if (field.isEmpty()) throw new NullPointerException("field is empty"); try { - statement = getSugarDataBase().compileStatement("SELECT average(" + field + ") FROM " - + NamingHelper.toTableName(type) + filter); - } catch (SQLException e) { + cursor = (whereArgs != null) ? getSugarDataBase().query(NamingHelper.toTableName(type), + new String[]{field}, whereClause, whereArgs, null, null, null) // default helper method + : getSugarDataBase().rawQuery("SELECT AVG( " + field + ") FROM " // raw query + + NamingHelper.toTableName(type) + filter, null); + } catch (SQLiteException e){ e.printStackTrace(); - return result; - } - if (whereArgs != null) { - for (int i = whereArgs.length ; i !=0 ; i--) { - statement.bindString(i,whereArgs[i-1]); - } + return result; } try { - result = statement.simpleQueryForLong(); + cursor.moveToFirst(); + result = cursor.getFloat(0); } finally { - statement.close(); + cursor.close(); } + return result; } - public static long max(Class type, String[] fields) { - return max(type, fields, null, null); + public static long max(Class type, String field) { + return max(type, field, null, null); } - public static long max(Class type, String[] fields, String whereClause, String... whereArgs) { + public static long max(Class type, String field, String whereClause, String... whereArgs) { + // handles only single column max long result = -1; + Cursor cursor; String filter = (!TextUtils.isEmpty(whereClause))? "where" + whereClause : ""; - SQLiteStatement statement; - - - //sanity check - for (String field: fields){ - if (field.isEmpty()) { - throw new NullPointerException("Field " + field + " is null."); - } - } - String maxString = "max( " + TextUtils.join("," ,fields) + " )"; + if (field.isEmpty()) throw new NullPointerException("field "+ field + " is empty."); try { - statement = getSugarDataBase().compileStatement("SELECT " + maxString + " FROM " - + NamingHelper.toTableName(type) + filter); - } catch (SQLiteException e) { + cursor = (whereArgs != null) ? getSugarDataBase().query(NamingHelper.toTableName(type), + new String[]{field}, whereClause, whereArgs, null, null, null) // default helper method + : getSugarDataBase().rawQuery("SELECT MAX( " + field + ") FROM " // raw query + + NamingHelper.toTableName(type) + filter, null); + } catch (SQLiteException e){ e.printStackTrace(); - return result; - } - - if (whereArgs != null) { - for (int i= whereArgs.length - 1; i !=0 ; i--){ - statement.bindString(i,whereArgs[i-1]); - } + return result; } try { - result = statement.simpleQueryForLong(); + cursor.moveToFirst(); + result = cursor.getLong(0); } finally { - statement.close(); + cursor.close(); } - return result; } - public static long min(Class type, String[] fields){return min(type,fields,null,null);} + public static long min(Class type, String field){return min(type,field,null,null);} - public static long min(Class type , String[] fields , String whereClause, String... whereArgs){ + public static long min(Class type , String field , String whereClause, String... whereArgs){ long result = -1; - String filter = (!TextUtils.isEmpty(whereClause)) ? "where" + whereClause : ""; - SQLiteStatement statement; - String minString = "min( " + TextUtils.join("," , fields); + Cursor cursor; + String filter = (!TextUtils.isEmpty(whereClause))? "where" + whereClause : ""; + + if (field.isEmpty()) throw new NullPointerException("field "+ field + " is empty."); try { - statement = getSugarDataBase().compileStatement("SELECT " + minString + " FROM " - + NamingHelper.toTableName(type) + filter); - } catch (SQLiteException e) { + cursor = (whereArgs != null) ? getSugarDataBase().query(NamingHelper.toTableName(type), + new String[]{field}, whereClause, whereArgs, null, null, null) // default helper method + : getSugarDataBase().rawQuery("SELECT MAX( " + field + ") FROM " // raw query + + NamingHelper.toTableName(type) + filter, null); + } catch (SQLiteException e){ e.printStackTrace(); - return result; - } - - //sanity check - for (String field : fields){ - if (field.isEmpty()) throw new NullPointerException("Field " + field + " is null."); - } - - - if (whereArgs != null){ - for (int i= whereArgs.length - 1; i !=0 ; i--){ - statement.bindString(i,whereArgs[i-1]); - } + return result; } try { - result = statement.simpleQueryForLong(); + cursor.moveToFirst(); + result = cursor.getLong(0); } finally { - statement.close(); + cursor.close(); } return result; } @@ -511,7 +493,7 @@ static long update(SQLiteDatabase db, Object object) { } } - public static boolean isSugarEntity(Class objectClass) { + public static boolean isSugarEntity(Class objectClass) { return objectClass.isAnnotationPresent(Table.class) || SugarRecord.class.isAssignableFrom(objectClass); } From da838b37984ee5232eec11ed98b399e087b2e780 Mon Sep 17 00:00:00 2001 From: Rahul Ravindran Date: Tue, 27 Dec 2016 20:09:08 +0530 Subject: [PATCH 137/139] statement correction --- library/src/main/java/com/orm/SugarRecord.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 4b9e48d0..ef399664 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -387,7 +387,7 @@ public static long min(Class type , String field , String whereClause, St try { cursor = (whereArgs != null) ? getSugarDataBase().query(NamingHelper.toTableName(type), new String[]{field}, whereClause, whereArgs, null, null, null) // default helper method - : getSugarDataBase().rawQuery("SELECT MAX( " + field + ") FROM " // raw query + : getSugarDataBase().rawQuery("SELECT MIN( " + field + ") FROM " // raw query + NamingHelper.toTableName(type) + filter, null); } catch (SQLiteException e){ e.printStackTrace(); From 72b4a6f6436dc332bfbb9ac74e6446615c62100d Mon Sep 17 00:00:00 2001 From: Rahul Ravindran Date: Tue, 27 Dec 2016 20:51:01 +0530 Subject: [PATCH 138/139] code changes --- library/src/main/java/com/orm/SugarRecord.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index ef399664..ae6e3774 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -322,11 +322,12 @@ public static float average(Class type, String field, String whereClause, float result = -1; Cursor cursor; String filter = (!TextUtils.isEmpty(whereClause))? "where" + whereClause : ""; + String whereField = (!TextUtils.isEmpty(field))? "AVG( " + field + " ) " : ""; - if (field.isEmpty()) throw new NullPointerException("field is empty"); + if (whereField.isEmpty()) throw new NullPointerException("field " + field + "is empty"); try { cursor = (whereArgs != null) ? getSugarDataBase().query(NamingHelper.toTableName(type), - new String[]{field}, whereClause, whereArgs, null, null, null) // default helper method + new String[]{whereField}, whereClause, whereArgs, null, null, null) // default helper method : getSugarDataBase().rawQuery("SELECT AVG( " + field + ") FROM " // raw query + NamingHelper.toTableName(type) + filter, null); } catch (SQLiteException e){ @@ -353,12 +354,13 @@ public static long max(Class type, String field, String whereClause, Stri long result = -1; Cursor cursor; String filter = (!TextUtils.isEmpty(whereClause))? "where" + whereClause : ""; + String whereField = (!TextUtils.isEmpty(field))? "AVG( " + field + " ) " : ""; - if (field.isEmpty()) throw new NullPointerException("field "+ field + " is empty."); + if (whereField.isEmpty()) throw new NullPointerException("field" + field + "is empty"); try { cursor = (whereArgs != null) ? getSugarDataBase().query(NamingHelper.toTableName(type), - new String[]{field}, whereClause, whereArgs, null, null, null) // default helper method + new String[]{whereField}, whereClause, whereArgs, null, null, null) // default helper method : getSugarDataBase().rawQuery("SELECT MAX( " + field + ") FROM " // raw query + NamingHelper.toTableName(type) + filter, null); } catch (SQLiteException e){ @@ -381,12 +383,13 @@ public static long min(Class type , String field , String whereClause, St long result = -1; Cursor cursor; String filter = (!TextUtils.isEmpty(whereClause))? "where" + whereClause : ""; + String whereField = (!TextUtils.isEmpty(field))? "AVG( " + field + " ) " : ""; - if (field.isEmpty()) throw new NullPointerException("field "+ field + " is empty."); + if (whereField.isEmpty()) throw new NullPointerException("field" + field + "is empty"); try { cursor = (whereArgs != null) ? getSugarDataBase().query(NamingHelper.toTableName(type), - new String[]{field}, whereClause, whereArgs, null, null, null) // default helper method + new String[]{whereField}, whereClause, whereArgs, null, null, null) // default helper method : getSugarDataBase().rawQuery("SELECT MIN( " + field + ") FROM " // raw query + NamingHelper.toTableName(type) + filter, null); } catch (SQLiteException e){ From 85de7711b679647493da631a17160285e67dca58 Mon Sep 17 00:00:00 2001 From: Rahul Ravindran Date: Tue, 27 Dec 2016 20:54:08 +0530 Subject: [PATCH 139/139] nomenclature change --- library/src/main/java/com/orm/SugarRecord.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index ae6e3774..9c1c6ec2 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -322,12 +322,12 @@ public static float average(Class type, String field, String whereClause, float result = -1; Cursor cursor; String filter = (!TextUtils.isEmpty(whereClause))? "where" + whereClause : ""; - String whereField = (!TextUtils.isEmpty(field))? "AVG( " + field + " ) " : ""; + String avgField = (!TextUtils.isEmpty(field))? "AVG( " + field + " ) " : ""; - if (whereField.isEmpty()) throw new NullPointerException("field " + field + "is empty"); + if (avgField.isEmpty()) throw new NullPointerException("field " + field + "is empty"); try { cursor = (whereArgs != null) ? getSugarDataBase().query(NamingHelper.toTableName(type), - new String[]{whereField}, whereClause, whereArgs, null, null, null) // default helper method + new String[]{avgField}, whereClause, whereArgs, null, null, null) // default helper method : getSugarDataBase().rawQuery("SELECT AVG( " + field + ") FROM " // raw query + NamingHelper.toTableName(type) + filter, null); } catch (SQLiteException e){ @@ -354,13 +354,13 @@ public static long max(Class type, String field, String whereClause, Stri long result = -1; Cursor cursor; String filter = (!TextUtils.isEmpty(whereClause))? "where" + whereClause : ""; - String whereField = (!TextUtils.isEmpty(field))? "AVG( " + field + " ) " : ""; + String maxField = (!TextUtils.isEmpty(field))? "AVG( " + field + " ) " : ""; - if (whereField.isEmpty()) throw new NullPointerException("field" + field + "is empty"); + if (maxField.isEmpty()) throw new NullPointerException("field" + field + "is empty"); try { cursor = (whereArgs != null) ? getSugarDataBase().query(NamingHelper.toTableName(type), - new String[]{whereField}, whereClause, whereArgs, null, null, null) // default helper method + new String[]{maxField}, whereClause, whereArgs, null, null, null) // default helper method : getSugarDataBase().rawQuery("SELECT MAX( " + field + ") FROM " // raw query + NamingHelper.toTableName(type) + filter, null); } catch (SQLiteException e){ @@ -383,13 +383,13 @@ public static long min(Class type , String field , String whereClause, St long result = -1; Cursor cursor; String filter = (!TextUtils.isEmpty(whereClause))? "where" + whereClause : ""; - String whereField = (!TextUtils.isEmpty(field))? "AVG( " + field + " ) " : ""; + String minField = (!TextUtils.isEmpty(field))? "AVG( " + field + " ) " : ""; - if (whereField.isEmpty()) throw new NullPointerException("field" + field + "is empty"); + if (minField.isEmpty()) throw new NullPointerException("field" + field + "is empty"); try { cursor = (whereArgs != null) ? getSugarDataBase().query(NamingHelper.toTableName(type), - new String[]{whereField}, whereClause, whereArgs, null, null, null) // default helper method + new String[]{minField}, whereClause, whereArgs, null, null, null) // default helper method : getSugarDataBase().rawQuery("SELECT MIN( " + field + ") FROM " // raw query + NamingHelper.toTableName(type) + filter, null); } catch (SQLiteException e){