arbitrary data set: store "" instead of null

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
This commit is contained in:
tobiasKaminsky 2020-08-21 11:06:05 +02:00
parent de38e90acb
commit a79a780129
No known key found for this signature in database
GPG key ID: 0E00D4D47D0C5AF7
2 changed files with 105 additions and 12 deletions

View file

@ -0,0 +1,78 @@
/*
*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2020 Tobias Kaminsky
* Copyright (C) 2020 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.owncloud.android.datamodel
import com.owncloud.android.AbstractIT
import org.junit.Assert.assertEquals
import org.junit.Test
class ArbitraryDataProviderIT : AbstractIT() {
private val arbitraryDataProvider = ArbitraryDataProvider(targetContext.contentResolver)
@Test
fun testNull() {
val key = "DUMMY_KEY"
arbitraryDataProvider.storeOrUpdateKeyValue(user.accountName, key, null)
assertEquals("", arbitraryDataProvider.getValue(user.accountName, key))
}
@Test
fun testString() {
val key = "DUMMY_KEY"
var value = "123"
arbitraryDataProvider.storeOrUpdateKeyValue(user.accountName, key, value)
assertEquals(value, arbitraryDataProvider.getValue(user.accountName, key))
value = ""
arbitraryDataProvider.storeOrUpdateKeyValue(user.accountName, key, value)
assertEquals(value, arbitraryDataProvider.getValue(user.accountName, key))
value = "-1"
arbitraryDataProvider.storeOrUpdateKeyValue(user.accountName, key, value)
assertEquals(value, arbitraryDataProvider.getValue(user.accountName, key))
}
@Test
fun testBoolean() {
val key = "DUMMY_KEY"
var value = true
arbitraryDataProvider.storeOrUpdateKeyValue(user.accountName, key, value.toString())
assertEquals(value, arbitraryDataProvider.getBooleanValue(user.accountName, key))
value = false
arbitraryDataProvider.storeOrUpdateKeyValue(user.accountName, key, value.toString())
assertEquals(value, arbitraryDataProvider.getBooleanValue(user.accountName, key))
}
@Test
fun testInteger() {
val key = "DUMMY_KEY"
var value = 1
arbitraryDataProvider.storeOrUpdateKeyValue(user.accountName, key, value.toString())
assertEquals(value, arbitraryDataProvider.getIntegerValue(user.accountName, key))
value = -1
arbitraryDataProvider.storeOrUpdateKeyValue(user.accountName, key, value.toString())
assertEquals(value, arbitraryDataProvider.getIntegerValue(user.accountName, key))
}
}

View file

@ -30,6 +30,7 @@ import com.owncloud.android.db.ProviderMeta;
import com.owncloud.android.lib.common.utils.Log_OC;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* Database provider for handling the persistence aspects of arbitrary data table.
@ -52,40 +53,51 @@ public class ArbitraryDataProvider {
public int deleteKeyForAccount(String account, String key) {
return contentResolver.delete(
ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA,
ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID + " = ? AND " +
ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY + "= ?",
new String[]{account, key}
);
ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA,
ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID + " = ? AND " +
ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY + "= ?",
new String[]{account, key}
);
}
public void storeOrUpdateKeyValue(String accountName, String key, long newValue) {
storeOrUpdateKeyValue(accountName, key, String.valueOf(newValue));
}
public void storeOrUpdateKeyValue(String accountName, String key, String newValue) {
public void storeOrUpdateKeyValue(@NonNull String accountName,
@NonNull String key,
@Nullable String newValue) {
ArbitraryDataSet data = getArbitraryDataSet(accountName, key);
String value;
if (newValue == null) {
value = "";
} else {
value = newValue;
}
if (data == null) {
Log_OC.v(TAG, "Adding arbitrary data with cloud id: " + accountName + " key: " + key
+ " value: " + newValue);
+ " value: " + value);
ContentValues cv = new ContentValues();
cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID, accountName);
cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY, key);
cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_VALUE, newValue);
cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_VALUE, value);
Uri result = contentResolver.insert(ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA, cv);
if (result == null) {
Log_OC.v(TAG, "Failed to store arbitrary data with cloud id: " + accountName + " key: " + key
+ " value: " + newValue);
+ " value: " + value);
}
} else {
Log_OC.v(TAG, "Updating arbitrary data with cloud id: " + accountName + " key: " + key
+ " value: " + newValue);
+ " value: " + value);
ContentValues cv = new ContentValues();
cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID, data.getCloudId());
cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY, data.getKey());
cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_VALUE, newValue);
cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_VALUE, value);
int result = contentResolver.update(
ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA,
@ -96,7 +108,7 @@ public class ArbitraryDataProvider {
if (result == 0) {
Log_OC.v(TAG, "Failed to update arbitrary data with cloud id: " + accountName + " key: " + key
+ " value: " + newValue);
+ " value: " + value);
}
}
}
@ -204,6 +216,9 @@ public class ArbitraryDataProvider {
if (id == -1) {
Log_OC.e(TAG, "Arbitrary value could not be created from cursor");
} else {
if (dbValue == null) {
dbValue = "";
}
dataSet = new ArbitraryDataSet(id, dbAccount, dbKey, dbValue);
}
}