mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-02-26 04:33:36 +00:00
Migrate some cursor utils to core-util.
This commit is contained in:
@@ -2,8 +2,8 @@ package org.thoughtcrime.securesms.database
|
||||
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import org.signal.core.util.SqlUtil
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId
|
||||
import org.thoughtcrime.securesms.util.SqlUtil
|
||||
|
||||
class ContactSearchSelectionBuilderTest {
|
||||
@Test(expected = IllegalStateException::class)
|
||||
|
||||
@@ -12,8 +12,8 @@ import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.signal.core.util.CursorUtil
|
||||
import org.thoughtcrime.securesms.testing.TestDatabaseUtil
|
||||
import org.thoughtcrime.securesms.util.CursorUtil
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(manifest = Config.NONE, application = Application::class)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.thoughtcrime.securesms.database
|
||||
|
||||
import android.net.Uri
|
||||
import org.signal.core.util.Bitmask
|
||||
import org.signal.zkgroup.profiles.ProfileKeyCredential
|
||||
import org.thoughtcrime.securesms.badges.models.Badge
|
||||
import org.thoughtcrime.securesms.conversation.colors.AvatarColor
|
||||
@@ -11,7 +12,6 @@ import org.thoughtcrime.securesms.profiles.ProfileName
|
||||
import org.thoughtcrime.securesms.recipients.Recipient
|
||||
import org.thoughtcrime.securesms.recipients.RecipientDetails
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId
|
||||
import org.thoughtcrime.securesms.util.Bitmask
|
||||
import org.thoughtcrime.securesms.wallpaper.ChatWallpaper
|
||||
import org.whispersystems.signalservice.api.push.ServiceId
|
||||
import java.util.Optional
|
||||
|
||||
@@ -4,7 +4,7 @@ import org.junit.Test;
|
||||
import org.signal.zkgroup.InvalidInputException;
|
||||
import org.signal.zkgroup.groups.GroupIdentifier;
|
||||
import org.signal.zkgroup.groups.GroupMasterKey;
|
||||
import org.thoughtcrime.securesms.util.Hex;
|
||||
import org.signal.core.util.Hex;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.signal.zkgroup.groups.GroupMasterKey;
|
||||
import org.thoughtcrime.securesms.util.Hex;
|
||||
import org.signal.core.util.Hex;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.mockito.Mockito
|
||||
import org.mockito.Mockito.mock
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.signal.core.util.Hex
|
||||
import org.signal.core.util.ThreadUtil
|
||||
import org.signal.core.util.logging.Log
|
||||
import org.signal.storageservice.protos.groups.Member
|
||||
@@ -33,7 +34,6 @@ import org.thoughtcrime.securesms.groups.v2.processing.GroupsV2StateProcessor
|
||||
import org.thoughtcrime.securesms.logging.CustomSignalProtocolLogger
|
||||
import org.thoughtcrime.securesms.recipients.Recipient
|
||||
import org.thoughtcrime.securesms.testutil.SystemOutLogger
|
||||
import org.thoughtcrime.securesms.util.Hex
|
||||
import org.whispersystems.libsignal.logging.SignalProtocolLoggerProvider
|
||||
import org.whispersystems.signalservice.api.groupsv2.ClientZkOperations
|
||||
import org.whispersystems.signalservice.api.groupsv2.GroupsV2Api
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.signal.zkgroup.InvalidInputException;
|
||||
import org.signal.zkgroup.groups.GroupMasterKey;
|
||||
import org.thoughtcrime.securesms.util.Hex;
|
||||
import org.signal.core.util.Hex;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -18,6 +18,7 @@ import org.mockito.Mockito.reset
|
||||
import org.mockito.Mockito.verify
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.signal.core.util.Hex.fromStringCondensed
|
||||
import org.signal.core.util.logging.Log
|
||||
import org.signal.storageservice.protos.groups.local.DecryptedGroup
|
||||
import org.signal.storageservice.protos.groups.local.DecryptedGroupChange
|
||||
@@ -38,7 +39,6 @@ import org.thoughtcrime.securesms.groups.GroupsV2Authorization
|
||||
import org.thoughtcrime.securesms.jobs.RequestGroupV2InfoJob
|
||||
import org.thoughtcrime.securesms.logging.CustomSignalProtocolLogger
|
||||
import org.thoughtcrime.securesms.testutil.SystemOutLogger
|
||||
import org.thoughtcrime.securesms.util.Hex.fromStringCondensed
|
||||
import org.whispersystems.libsignal.logging.SignalProtocolLoggerProvider
|
||||
import org.whispersystems.signalservice.api.groupsv2.GroupsV2Api
|
||||
import org.whispersystems.signalservice.api.groupsv2.PartialDecryptedGroup
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
|
||||
import org.thoughtcrime.securesms.util.Hex;
|
||||
import org.signal.core.util.Hex;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.signal.core.util.Bitmask;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
@@ -1,283 +0,0 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.ContentValues;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(manifest = Config.NONE, application = Application.class)
|
||||
public final class SqlUtilTest {
|
||||
|
||||
@Test
|
||||
public void buildTrueUpdateQuery_simple() {
|
||||
String selection = "_id = ?";
|
||||
String[] args = new String[]{"1"};
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("a", 2);
|
||||
|
||||
SqlUtil.Query updateQuery = SqlUtil.buildTrueUpdateQuery(selection, args, values);
|
||||
|
||||
assertEquals("(_id = ?) AND (a != ? OR a IS NULL)", updateQuery.getWhere());
|
||||
assertArrayEquals(new String[] { "1", "2" }, updateQuery.getWhereArgs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildTrueUpdateQuery_complexSelection() {
|
||||
String selection = "_id = ? AND (foo = ? OR bar != ?)";
|
||||
String[] args = new String[]{"1", "2", "3"};
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("a", 4);
|
||||
|
||||
SqlUtil.Query updateQuery = SqlUtil.buildTrueUpdateQuery(selection, args, values);
|
||||
|
||||
assertEquals("(_id = ? AND (foo = ? OR bar != ?)) AND (a != ? OR a IS NULL)", updateQuery.getWhere());
|
||||
assertArrayEquals(new String[] { "1", "2", "3", "4" }, updateQuery.getWhereArgs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildTrueUpdateQuery_multipleContentValues() {
|
||||
String selection = "_id = ?";
|
||||
String[] args = new String[]{"1"};
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("a", 2);
|
||||
values.put("b", 3);
|
||||
values.put("c", 4);
|
||||
|
||||
SqlUtil.Query updateQuery = SqlUtil.buildTrueUpdateQuery(selection, args, values);
|
||||
|
||||
assertEquals("(_id = ?) AND (a != ? OR a IS NULL OR b != ? OR b IS NULL OR c != ? OR c IS NULL)", updateQuery.getWhere());
|
||||
assertArrayEquals(new String[] { "1", "2", "3", "4"}, updateQuery.getWhereArgs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildTrueUpdateQuery_nullContentValue() {
|
||||
String selection = "_id = ?";
|
||||
String[] args = new String[]{"1"};
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("a", (String) null);
|
||||
|
||||
SqlUtil.Query updateQuery = SqlUtil.buildTrueUpdateQuery(selection, args, values);
|
||||
|
||||
assertEquals("(_id = ?) AND (a NOT NULL)", updateQuery.getWhere());
|
||||
assertArrayEquals(new String[] { "1" }, updateQuery.getWhereArgs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildTrueUpdateQuery_complexContentValue() {
|
||||
String selection = "_id = ?";
|
||||
String[] args = new String[]{"1"};
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("a", (String) null);
|
||||
values.put("b", 2);
|
||||
values.put("c", 3);
|
||||
values.put("d", (String) null);
|
||||
values.put("e", (String) null);
|
||||
|
||||
SqlUtil.Query updateQuery = SqlUtil.buildTrueUpdateQuery(selection, args, values);
|
||||
|
||||
assertEquals("(_id = ?) AND (a NOT NULL OR b != ? OR b IS NULL OR c != ? OR c IS NULL OR d NOT NULL OR e NOT NULL)", updateQuery.getWhere());
|
||||
assertArrayEquals(new String[] { "1", "2", "3" }, updateQuery.getWhereArgs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildTrueUpdateQuery_blobComplex() {
|
||||
String selection = "_id = ?";
|
||||
String[] args = new String[]{"1"};
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("a", hexToBytes("FF"));
|
||||
values.put("b", 2);
|
||||
values.putNull("c");
|
||||
|
||||
SqlUtil.Query updateQuery = SqlUtil.buildTrueUpdateQuery(selection, args, values);
|
||||
|
||||
assertEquals("(_id = ?) AND (hex(a) != ? OR a IS NULL OR b != ? OR b IS NULL OR c NOT NULL)", updateQuery.getWhere());
|
||||
assertArrayEquals(new String[] { "1", "FF", "2" }, updateQuery.getWhereArgs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildCollectionQuery_single() {
|
||||
SqlUtil.Query updateQuery = SqlUtil.buildCollectionQuery("a", Arrays.asList(1));
|
||||
|
||||
assertEquals("a IN (?)", updateQuery.getWhere());
|
||||
assertArrayEquals(new String[] { "1" }, updateQuery.getWhereArgs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildCollectionQuery_multiple() {
|
||||
SqlUtil.Query updateQuery = SqlUtil.buildCollectionQuery("a", Arrays.asList(1, 2, 3));
|
||||
|
||||
assertEquals("a IN (?, ?, ?)", updateQuery.getWhere());
|
||||
assertArrayEquals(new String[] { "1", "2", "3" }, updateQuery.getWhereArgs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildCollectionQuery_multipleRecipientIds() {
|
||||
SqlUtil.Query updateQuery = SqlUtil.buildCollectionQuery("a", Arrays.asList(RecipientId.from(1), RecipientId.from(2), RecipientId.from(3)));
|
||||
|
||||
assertEquals("a IN (?, ?, ?)", updateQuery.getWhere());
|
||||
assertArrayEquals(new String[] { "1", "2", "3" }, updateQuery.getWhereArgs());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void buildCollectionQuery_none() {
|
||||
SqlUtil.buildCollectionQuery("a", Collections.emptyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildCustomCollectionQuery_single_singleBatch() {
|
||||
List<String[]> args = new ArrayList<>();
|
||||
args.add(SqlUtil.buildArgs(1, 2));
|
||||
|
||||
List<SqlUtil.Query> queries = SqlUtil.buildCustomCollectionQuery("a = ? AND b = ?", args);
|
||||
|
||||
assertEquals(1, queries.size());
|
||||
assertEquals("(a = ? AND b = ?)", queries.get(0).getWhere());
|
||||
assertArrayEquals(new String[] { "1", "2" }, queries.get(0).getWhereArgs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildCustomCollectionQuery_multiple_singleBatch() {
|
||||
List<String[]> args = new ArrayList<>();
|
||||
args.add(SqlUtil.buildArgs(1, 2));
|
||||
args.add(SqlUtil.buildArgs(3, 4));
|
||||
args.add(SqlUtil.buildArgs(5, 6));
|
||||
|
||||
List<SqlUtil.Query> queries = SqlUtil.buildCustomCollectionQuery("a = ? AND b = ?", args);
|
||||
|
||||
assertEquals(1, queries.size());
|
||||
assertEquals("(a = ? AND b = ?) OR (a = ? AND b = ?) OR (a = ? AND b = ?)", queries.get(0).getWhere());
|
||||
assertArrayEquals(new String[] { "1", "2", "3", "4", "5", "6" }, queries.get(0).getWhereArgs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildCustomCollectionQuery_twoBatches() {
|
||||
List<String[]> args = new ArrayList<>();
|
||||
args.add(SqlUtil.buildArgs(1, 2));
|
||||
args.add(SqlUtil.buildArgs(3, 4));
|
||||
args.add(SqlUtil.buildArgs(5, 6));
|
||||
|
||||
List<SqlUtil.Query> queries = SqlUtil.buildCustomCollectionQuery("a = ? AND b = ?", args, 4);
|
||||
|
||||
assertEquals(2, queries.size());
|
||||
assertEquals("(a = ? AND b = ?) OR (a = ? AND b = ?)", queries.get(0).getWhere());
|
||||
assertArrayEquals(new String[] { "1", "2", "3", "4" }, queries.get(0).getWhereArgs());
|
||||
assertEquals("(a = ? AND b = ?)", queries.get(1).getWhere());
|
||||
assertArrayEquals(new String[] { "5", "6" }, queries.get(1).getWhereArgs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void splitStatements_singleStatement() {
|
||||
List<String> result = SqlUtil.splitStatements("SELECT * FROM foo;\n");
|
||||
assertEquals(Arrays.asList("SELECT * FROM foo"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void splitStatements_twoStatements() {
|
||||
List<String> result = SqlUtil.splitStatements("SELECT * FROM foo;\nSELECT * FROM bar;\n");
|
||||
assertEquals(Arrays.asList("SELECT * FROM foo", "SELECT * FROM bar"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void splitStatements_twoStatementsSeparatedByNewLines() {
|
||||
List<String> result = SqlUtil.splitStatements("SELECT * FROM foo;\n\nSELECT * FROM bar;\n");
|
||||
assertEquals(Arrays.asList("SELECT * FROM foo", "SELECT * FROM bar"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildBulkInsert_single_singleBatch() {
|
||||
List<ContentValues> contentValues = new ArrayList<>();
|
||||
|
||||
ContentValues cv1 = new ContentValues();
|
||||
cv1.put("a", 1);
|
||||
cv1.put("b", 2);
|
||||
|
||||
contentValues.add(cv1);
|
||||
|
||||
List<SqlUtil.Query> output = SqlUtil.buildBulkInsert("mytable", new String[] { "a", "b"}, contentValues);
|
||||
|
||||
assertEquals(1, output.size());
|
||||
assertEquals("INSERT INTO mytable (a, b) VALUES (?, ?)", output.get(0).getWhere());
|
||||
assertArrayEquals(new String[] { "1", "2" }, output.get(0).getWhereArgs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildBulkInsert_multiple_singleBatch() {
|
||||
List<ContentValues> contentValues = new ArrayList<>();
|
||||
|
||||
ContentValues cv1 = new ContentValues();
|
||||
cv1.put("a", 1);
|
||||
cv1.put("b", 2);
|
||||
|
||||
ContentValues cv2 = new ContentValues();
|
||||
cv2.put("a", 3);
|
||||
cv2.put("b", 4);
|
||||
|
||||
contentValues.add(cv1);
|
||||
contentValues.add(cv2);
|
||||
|
||||
List<SqlUtil.Query> output = SqlUtil.buildBulkInsert("mytable", new String[] { "a", "b"}, contentValues);
|
||||
|
||||
assertEquals(1, output.size());
|
||||
assertEquals("INSERT INTO mytable (a, b) VALUES (?, ?), (?, ?)", output.get(0).getWhere());
|
||||
assertArrayEquals(new String[] { "1", "2", "3", "4" }, output.get(0).getWhereArgs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildBulkInsert_twoBatches() {
|
||||
List<ContentValues> contentValues = new ArrayList<>();
|
||||
|
||||
ContentValues cv1 = new ContentValues();
|
||||
cv1.put("a", 1);
|
||||
cv1.put("b", 2);
|
||||
|
||||
ContentValues cv2 = new ContentValues();
|
||||
cv2.put("a", 3);
|
||||
cv2.put("b", 4);
|
||||
|
||||
ContentValues cv3 = new ContentValues();
|
||||
cv3.put("a", 5);
|
||||
cv3.put("b", 6);
|
||||
|
||||
contentValues.add(cv1);
|
||||
contentValues.add(cv2);
|
||||
contentValues.add(cv3);
|
||||
|
||||
List<SqlUtil.Query> output = SqlUtil.buildBulkInsert("mytable", new String[] { "a", "b"}, contentValues, 4);
|
||||
|
||||
assertEquals(2, output.size());
|
||||
|
||||
assertEquals("INSERT INTO mytable (a, b) VALUES (?, ?), (?, ?)", output.get(0).getWhere());
|
||||
assertArrayEquals(new String[] { "1", "2", "3", "4" }, output.get(0).getWhereArgs());
|
||||
|
||||
assertEquals("INSERT INTO mytable (a, b) VALUES (?, ?)", output.get(1).getWhere());
|
||||
assertArrayEquals(new String[] { "5", "6" }, output.get(1).getWhereArgs());
|
||||
}
|
||||
|
||||
private static byte[] hexToBytes(String hex) {
|
||||
try {
|
||||
return Hex.fromStringCondensed(hex);
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class UtilTest {
|
||||
|
||||
@Test
|
||||
public void chunk_oneChunk() {
|
||||
List<String> input = Arrays.asList("A", "B", "C");
|
||||
|
||||
List<List<String>> output = Util.chunk(input, 3);
|
||||
assertEquals(1, output.size());
|
||||
assertEquals(input, output.get(0));
|
||||
|
||||
output = Util.chunk(input, 4);
|
||||
assertEquals(1, output.size());
|
||||
assertEquals(input, output.get(0));
|
||||
|
||||
output = Util.chunk(input, 100);
|
||||
assertEquals(1, output.size());
|
||||
assertEquals(input, output.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void chunk_multipleChunks() {
|
||||
List<String> input = Arrays.asList("A", "B", "C", "D", "E");
|
||||
|
||||
List<List<String>> output = Util.chunk(input, 4);
|
||||
assertEquals(2, output.size());
|
||||
assertEquals(Arrays.asList("A", "B", "C", "D"), output.get(0));
|
||||
assertEquals(Arrays.asList("E"), output.get(1));
|
||||
|
||||
output = Util.chunk(input, 2);
|
||||
assertEquals(3, output.size());
|
||||
assertEquals(Arrays.asList("A", "B"), output.get(0));
|
||||
assertEquals(Arrays.asList("C", "D"), output.get(1));
|
||||
assertEquals(Arrays.asList("E"), output.get(2));
|
||||
|
||||
output = Util.chunk(input, 1);
|
||||
assertEquals(5, output.size());
|
||||
assertEquals(Arrays.asList("A"), output.get(0));
|
||||
assertEquals(Arrays.asList("B"), output.get(1));
|
||||
assertEquals(Arrays.asList("C"), output.get(2));
|
||||
assertEquals(Arrays.asList("D"), output.get(3));
|
||||
assertEquals(Arrays.asList("E"), output.get(4));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user