Implement JDBC database

This commit is contained in:
2017-06-20 14:13:37 -04:00
parent b96385c4a7
commit ecb5352134
53 changed files with 566 additions and 124 deletions

View File

@@ -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);
}
}
}

View File

@@ -37,7 +37,7 @@ public class ArchiveHabitsCommandTest extends BaseUnitTest
@Override
@Before
public void setUp()
public void setUp() throws Exception
{
super.setUp();

View File

@@ -36,7 +36,7 @@ public class ChangeHabitColorCommandTest extends BaseUnitTest
@Override
@Before
public void setUp()
public void setUp() throws Exception
{
super.setUp();

View File

@@ -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);

View File

@@ -35,7 +35,7 @@ public class CreateHabitCommandTest extends BaseUnitTest
@Override
@Before
public void setUp()
public void setUp() throws Exception
{
super.setUp();

View File

@@ -39,7 +39,7 @@ public class CreateRepetitionCommandTest extends BaseUnitTest
@Override
@Before
public void setUp()
public void setUp() throws Exception
{
super.setUp();

View File

@@ -40,7 +40,7 @@ public class DeleteHabitsCommandTest extends BaseUnitTest
@Override
@Before
public void setUp()
public void setUp() throws Exception
{
super.setUp();
selected = new LinkedList<>();

View File

@@ -36,7 +36,7 @@ public class EditHabitCommandTest extends BaseUnitTest
@Override
@Before
public void setUp()
public void setUp() throws Exception
{
super.setUp();

View File

@@ -37,7 +37,7 @@ public class ToggleRepetitionCommandTest extends BaseUnitTest
@Override
@Before
public void setUp()
public void setUp() throws Exception
{
super.setUp();

View File

@@ -36,7 +36,7 @@ public class UnarchiveHabitsCommandTest extends BaseUnitTest
@Override
@Before
public void setUp()
public void setUp() throws Exception
{
super.setUp();

View File

@@ -45,7 +45,7 @@ public class CheckmarkListTest extends BaseUnitTest
private Habit numericalHabit;
@Override
public void setUp()
public void setUp() throws Exception
{
super.setUp();

View File

@@ -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<>();

View File

@@ -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();
}

View File

@@ -52,7 +52,7 @@ public class RepetitionListTest extends BaseUnitTest
@Override
@Before
public void setUp()
public void setUp() throws Exception
{
super.setUp();
habit = fixtures.createEmptyHabit();

View File

@@ -38,7 +38,7 @@ public class ScoreListTest extends BaseUnitTest
@Override
@Before
public void setUp()
public void setUp() throws Exception
{
super.setUp();
habit = fixtures.createEmptyHabit();

View File

@@ -32,7 +32,7 @@ public class ScoreTest extends BaseUnitTest
@Override
@Before
public void setUp()
public void setUp() throws Exception
{
super.setUp();
}

View File

@@ -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();

View File

@@ -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));
}
}

View File

@@ -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();

View File

@@ -43,7 +43,7 @@ public class ReminderSchedulerTest extends BaseUnitTest
@Before
@Override
public void setUp()
public void setUp() throws Exception
{
super.setUp();
habit = fixtures.createEmptyHabit();

View File

@@ -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();

View File

@@ -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);
}

View File

@@ -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();

View File

@@ -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();

View File

@@ -67,7 +67,7 @@ public class ListHabitsBehaviorTest extends BaseUnitTest
@Override
@Before
public void setUp()
public void setUp() throws Exception
{
super.setUp();
habit1 = fixtures.createShortHabit();

View File

@@ -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 =

View File

@@ -139,7 +139,7 @@ public class ListHabitsSelectionMenuBehaviorTest extends BaseUnitTest
}
@Override
public void setUp()
public void setUp() throws Exception
{
super.setUp();

View File

@@ -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);