mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Implement PropertiesStorage
This commit is contained in:
@@ -79,8 +79,13 @@ public class Preferences
|
||||
{
|
||||
int defaultScoreInterval =
|
||||
storage.getInt("pref_score_view_interval", 1);
|
||||
|
||||
if (defaultScoreInterval > 5 || defaultScoreInterval < 0)
|
||||
{
|
||||
defaultScoreInterval = 1;
|
||||
storage.putInt("pref_score_view_interval", 1);
|
||||
}
|
||||
|
||||
return defaultScoreInterval;
|
||||
}
|
||||
|
||||
@@ -139,7 +144,7 @@ public class Preferences
|
||||
public String getSyncAddress()
|
||||
{
|
||||
return storage.getString("pref_sync_address",
|
||||
"https://sync.loophabits.org:4000");
|
||||
"https://sync.loophabits.org");
|
||||
}
|
||||
|
||||
public void setSyncAddress(String address)
|
||||
@@ -299,7 +304,7 @@ public class Preferences
|
||||
|
||||
int getInt(String key, int defValue);
|
||||
|
||||
long getLong(String key, int defValue);
|
||||
long getLong(String key, long defValue);
|
||||
|
||||
String getString(String key, String defValue);
|
||||
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker 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 General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.core.preferences;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class PropertiesStorage implements Preferences.Storage
|
||||
{
|
||||
@NonNull
|
||||
private final Properties props;
|
||||
|
||||
@NonNull
|
||||
private File file;
|
||||
|
||||
public PropertiesStorage(@NonNull File file)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.file = file;
|
||||
props = new Properties();
|
||||
props.load(new FileInputStream(file));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear()
|
||||
{
|
||||
for(String key : props.stringPropertyNames()) props.remove(key);
|
||||
flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String key, boolean defValue)
|
||||
{
|
||||
String value = props.getProperty(key, Boolean.toString(defValue));
|
||||
return Boolean.parseBoolean(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String key, int defValue)
|
||||
{
|
||||
String value = props.getProperty(key, Integer.toString(defValue));
|
||||
return Integer.parseInt(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLong(String key, long defValue)
|
||||
{
|
||||
String value = props.getProperty(key, Long.toString(defValue));
|
||||
return Long.parseLong(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String key, String defValue)
|
||||
{
|
||||
return props.getProperty(key, defValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttached(Preferences preferences)
|
||||
{
|
||||
// nop
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putBoolean(String key, boolean value)
|
||||
{
|
||||
props.setProperty(key, Boolean.toString(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putInt(String key, int value)
|
||||
{
|
||||
props.setProperty(key, Integer.toString(value));
|
||||
flush();
|
||||
}
|
||||
|
||||
private void flush()
|
||||
{
|
||||
try
|
||||
{
|
||||
props.store(new FileOutputStream(file), "");
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putLong(String key, long value)
|
||||
{
|
||||
props.setProperty(key, Long.toString(value));
|
||||
flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putString(String key, String value)
|
||||
{
|
||||
props.setProperty(key, value);
|
||||
flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(String key)
|
||||
{
|
||||
props.remove(key);
|
||||
flush();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker 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 General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.core.preferences;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.core.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.junit.*;
|
||||
import org.mockito.*;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
|
||||
public class PreferencesTest extends BaseUnitTest
|
||||
{
|
||||
@NonNull
|
||||
private Preferences prefs;
|
||||
|
||||
@Mock
|
||||
private Preferences.Listener listener;
|
||||
|
||||
private PropertiesStorage storage;
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
File file = File.createTempFile("prefs", ".properties");
|
||||
file.deleteOnExit();
|
||||
storage = new PropertiesStorage(file);
|
||||
prefs = new Preferences(storage);
|
||||
prefs.addListener(listener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHabitColor() throws Exception
|
||||
{
|
||||
assertThat(prefs.getDefaultHabitColor(999), equalTo(999));
|
||||
prefs.setDefaultHabitColor(10);
|
||||
assertThat(prefs.getDefaultHabitColor(999), equalTo(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultOrder() throws Exception
|
||||
{
|
||||
assertThat(prefs.getDefaultOrder(), equalTo(HabitList.Order.BY_POSITION));
|
||||
|
||||
prefs.setDefaultOrder(HabitList.Order.BY_SCORE);
|
||||
assertThat(prefs.getDefaultOrder(), equalTo(HabitList.Order.BY_SCORE));
|
||||
|
||||
storage.putString("pref_default_order", "BOGUS");
|
||||
assertThat(prefs.getDefaultOrder(), equalTo(HabitList.Order.BY_POSITION));
|
||||
assertThat(storage.getString("pref_default_order", ""), equalTo("BY_POSITION"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultSpinnerPosition() throws Exception
|
||||
{
|
||||
assertThat(prefs.getDefaultScoreSpinnerPosition(), equalTo(1));
|
||||
|
||||
prefs.setDefaultScoreSpinnerPosition(4);
|
||||
assertThat(prefs.getDefaultScoreSpinnerPosition(), equalTo(4));
|
||||
|
||||
storage.putInt("pref_score_view_interval", 9000);
|
||||
assertThat(prefs.getDefaultScoreSpinnerPosition(), equalTo(1));
|
||||
assertThat(storage.getInt("pref_score_view_interval", 0), equalTo(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker 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 General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.core.preferences;
|
||||
|
||||
import org.isoron.uhabits.core.*;
|
||||
import org.junit.*;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PropertiesStorageTest extends BaseUnitTest
|
||||
{
|
||||
private PropertiesStorage storage;
|
||||
|
||||
private File file;
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
file = File.createTempFile("test", ".properties");
|
||||
file.deleteOnExit();
|
||||
|
||||
storage = new PropertiesStorage(file);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPutGetRemove() throws Exception
|
||||
{
|
||||
storage.putBoolean("booleanKey", true);
|
||||
assertTrue(storage.getBoolean("booleanKey", false));
|
||||
assertFalse(storage.getBoolean("random", false));
|
||||
|
||||
storage.putInt("intKey", 64);
|
||||
assertThat(storage.getInt("intKey", 200), equalTo(64));
|
||||
assertThat(storage.getInt("random", 200), equalTo(200));
|
||||
|
||||
storage.putLong("longKey", 64L);
|
||||
assertThat(storage.getLong("intKey", 200L), equalTo(64L));
|
||||
assertThat(storage.getLong("random", 200L), equalTo(200L));
|
||||
|
||||
storage.putString("stringKey", "Hello");
|
||||
assertThat(storage.getString("stringKey", ""), equalTo("Hello"));
|
||||
assertThat(storage.getString("random", ""), equalTo(""));
|
||||
|
||||
storage.remove("stringKey");
|
||||
assertThat(storage.getString("stringKey", ""), equalTo(""));
|
||||
|
||||
storage.clear();
|
||||
assertThat(storage.getLong("intKey", 200L), equalTo(200L));
|
||||
assertFalse(storage.getBoolean("booleanKey", false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPersistence() throws Exception
|
||||
{
|
||||
storage.putBoolean("booleanKey", true);
|
||||
storage.putInt("intKey", 64);
|
||||
storage.putLong("longKey", 64L);
|
||||
storage.putString("stringKey", "Hello");
|
||||
|
||||
PropertiesStorage storage2 = new PropertiesStorage(file);
|
||||
assertTrue(storage2.getBoolean("booleanKey", false));
|
||||
assertThat(storage2.getInt("intKey", 200), equalTo(64));
|
||||
assertThat(storage2.getLong("intKey", 200L), equalTo(64L));
|
||||
assertThat(storage2.getString("stringKey", ""), equalTo("Hello"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user