mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-09 18:48:51 -06:00
Implement JDBC database
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
package org.isoron.uhabits;
|
||||
|
||||
import org.isoron.uhabits.core.commands.*;
|
||||
import org.isoron.uhabits.core.db.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.models.memory.*;
|
||||
import org.isoron.uhabits.core.tasks.*;
|
||||
@@ -29,9 +30,11 @@ import org.junit.*;
|
||||
import org.junit.runner.*;
|
||||
import org.mockito.junit.*;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.validateMockitoUsage;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class BaseUnitTest
|
||||
@@ -51,7 +54,7 @@ public class BaseUnitTest
|
||||
protected static final long FIXED_LOCAL_TIME = 1422172800000L;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
DateUtils.setFixedLocalTime(FIXED_LOCAL_TIME);
|
||||
|
||||
@@ -81,4 +84,16 @@ public class BaseUnitTest
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected Database buildMemoryDatabase()
|
||||
{
|
||||
try
|
||||
{
|
||||
return new JdbcDatabase(DriverManager.getConnection("jdbc:sqlite::memory:"));
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class ArchiveHabitsCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ public class ChangeHabitColorCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ public class CommandParserTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
parser = new CommandParser(habitList, modelFactory);
|
||||
|
||||
@@ -35,7 +35,7 @@ public class CreateHabitCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ public class CreateRepetitionCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ public class DeleteHabitsCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
selected = new LinkedList<>();
|
||||
|
||||
@@ -36,7 +36,7 @@ public class EditHabitCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ public class ToggleRepetitionCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ public class UnarchiveHabitsCommandTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
private Habit numericalHabit;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ public class HabitListTest extends BaseUnitTest
|
||||
private HabitList reminderHabits;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
habitsArray = new ArrayList<>();
|
||||
|
||||
@@ -33,7 +33,7 @@ public class HabitTest extends BaseUnitTest
|
||||
public final ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ public class RepetitionListTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
habit = fixtures.createEmptyHabit();
|
||||
|
||||
@@ -38,7 +38,7 @@ public class ScoreListTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
habit = fixtures.createEmptyHabit();
|
||||
|
||||
@@ -32,7 +32,7 @@ public class ScoreTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ public class StreakListTest extends BaseUnitTest
|
||||
private ModelObservable.Listener listener;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
habit = fixtures.createLongHabit();
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Copyright (C) 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.models.sqlite;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.core.db.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.models.sqlite.records.*;
|
||||
import org.junit.*;
|
||||
import org.junit.rules.*;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
public class SQLiteHabitListTest extends BaseUnitTest
|
||||
{
|
||||
@Rule
|
||||
public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
private SQLiteHabitList habitList;
|
||||
|
||||
private Repository<HabitRecord> repository;
|
||||
|
||||
private ModelObservable.Listener listener;
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
Database db = buildMemoryDatabase();
|
||||
repository = new Repository<>(HabitRecord.class, db);
|
||||
habitList = new SQLiteHabitList(new SQLModelFactory(db));
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
Habit h = modelFactory.buildHabit();
|
||||
h.setName("habit " + i);
|
||||
h.setId((long) i);
|
||||
if (i % 2 == 0) h.setArchived(true);
|
||||
|
||||
HabitRecord record = new HabitRecord();
|
||||
record.copyFrom(h);
|
||||
record.position = i;
|
||||
repository.save(record);
|
||||
}
|
||||
|
||||
habitList.reload();
|
||||
|
||||
listener = mock(ModelObservable.Listener.class);
|
||||
habitList.getObservable().addListener(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tearDown()
|
||||
{
|
||||
habitList.getObservable().removeListener(listener);
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd_withDuplicate()
|
||||
{
|
||||
Habit habit = modelFactory.buildHabit();
|
||||
habitList.add(habit);
|
||||
verify(listener).onModelChange();
|
||||
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
habitList.add(habit);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd_withId()
|
||||
{
|
||||
Habit habit = modelFactory.buildHabit();
|
||||
habit.setName("Hello world with id");
|
||||
habit.setId(12300L);
|
||||
|
||||
habitList.add(habit);
|
||||
assertThat(habit.getId(), equalTo(12300L));
|
||||
|
||||
HabitRecord record = repository.find(12300L);
|
||||
assertNotNull(record);
|
||||
assertThat(record.name, equalTo(habit.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd_withoutId()
|
||||
{
|
||||
Habit habit = modelFactory.buildHabit();
|
||||
habit.setName("Hello world");
|
||||
assertNull(habit.getId());
|
||||
|
||||
habitList.add(habit);
|
||||
assertNotNull(habit.getId());
|
||||
|
||||
HabitRecord record = repository.find(habit.getId());
|
||||
assertNotNull(record);
|
||||
assertThat(record.name, equalTo(habit.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSize()
|
||||
{
|
||||
assertThat(habitList.size(), equalTo(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetById()
|
||||
{
|
||||
Habit h1 = habitList.getById(0);
|
||||
assertNotNull(h1);
|
||||
assertThat(h1.getName(), equalTo("habit 0"));
|
||||
|
||||
Habit h2 = habitList.getById(0);
|
||||
assertNotNull(h2);
|
||||
assertThat(h1, equalTo(h2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetById_withInvalid()
|
||||
{
|
||||
long invalidId = 9183792001L;
|
||||
Habit h1 = habitList.getById(invalidId);
|
||||
assertNull(h1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetByPosition()
|
||||
{
|
||||
Habit h = habitList.getByPosition(5);
|
||||
assertNotNull(h);
|
||||
assertThat(h.getName(), equalTo("habit 5"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIndexOf()
|
||||
{
|
||||
Habit h1 = habitList.getByPosition(5);
|
||||
assertNotNull(h1);
|
||||
assertThat(habitList.indexOf(h1), equalTo(5));
|
||||
|
||||
Habit h2 = modelFactory.buildHabit();
|
||||
assertThat(habitList.indexOf(h2), equalTo(-1));
|
||||
|
||||
h2.setId(1000L);
|
||||
assertThat(habitList.indexOf(h2), equalTo(-1));
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,6 @@ package org.isoron.uhabits.core.models.sqlite.records;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.models.sqlite.records.*;
|
||||
import org.junit.*;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
@@ -35,7 +34,7 @@ public class HabitRecordTest extends BaseUnitTest
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ public class ReminderSchedulerTest extends BaseUnitTest
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
habit = fixtures.createEmptyHabit();
|
||||
|
||||
@@ -35,7 +35,7 @@ public class SingleThreadTaskRunnerTest extends BaseUnitTest
|
||||
private Task task;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
runner = new SingleThreadTaskRunner();
|
||||
|
||||
@@ -39,8 +39,9 @@ public class AboutBehaviorTest extends BaseUnitTest
|
||||
private AboutBehavior.Screen screen;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
behavior = new AboutBehavior(prefs, screen);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ public class HabitCardListCacheTest extends BaseUnitTest
|
||||
private HabitCardListCache.Listener listener;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
habitList.removeAll();
|
||||
|
||||
@@ -46,7 +46,7 @@ public class HintListTest extends BaseUnitTest
|
||||
private long yesterday;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
today = DateUtils.getStartOfToday();
|
||||
|
||||
@@ -67,7 +67,7 @@ public class ListHabitsBehaviorTest extends BaseUnitTest
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
habit1 = fixtures.createShortHabit();
|
||||
|
||||
@@ -55,7 +55,7 @@ public class ListHabitsMenuBehaviorTest extends BaseUnitTest
|
||||
private ArgumentCaptor<HabitList.Order> orderCaptor;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
behavior =
|
||||
|
||||
@@ -139,7 +139,7 @@ public class ListHabitsSelectionMenuBehaviorTest extends BaseUnitTest
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ public class ShowHabitMenuBehaviorTest extends BaseUnitTest
|
||||
private ShowHabitMenuBehavior menu;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
system = mock(ShowHabitMenuBehavior.System.class);
|
||||
|
||||
Reference in New Issue
Block a user