Implement tests for Archive and ChangeColor commands

This commit is contained in:
2016-03-31 07:11:24 -04:00
parent 21157eaa19
commit f24e300b40
15 changed files with 182 additions and 65 deletions

View File

@@ -22,7 +22,6 @@ package org.isoron.uhabits.commands;
import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit;
import java.util.LinkedList;
import java.util.List;
public class ArchiveHabitsCommand extends Command
@@ -30,12 +29,6 @@ public class ArchiveHabitsCommand extends Command
private List<Habit> habits;
public ArchiveHabitsCommand(Habit habit)
{
habits = new LinkedList<>();
habits.add(habit);
}
public ArchiveHabitsCommand(List<Habit> habits)
{
this.habits = habits;

View File

@@ -22,6 +22,7 @@ package org.isoron.uhabits.commands;
import com.activeandroid.ActiveAndroid;
import org.isoron.uhabits.R;
import org.isoron.uhabits.helpers.DatabaseHelper;
import org.isoron.uhabits.models.Habit;
import java.util.ArrayList;
@@ -52,23 +53,19 @@ public class ChangeHabitColorCommand extends Command
@Override
public void undo()
{
ActiveAndroid.beginTransaction();
try
DatabaseHelper.executeAsTransaction(new DatabaseHelper.Command()
{
int k = 0;
for(Habit h : habits)
@Override
public void execute()
{
h.color = originalColors.get(k++);
h.save();
int k = 0;
for(Habit h : habits)
{
h.color = originalColors.get(k++);
h.save();
}
}
ActiveAndroid.setTransactionSuccessful();
}
finally
{
ActiveAndroid.endTransaction();
}
});
}
public Integer getExecuteStringId()

View File

@@ -1,33 +0,0 @@
/*
* Copyright (C) 2016 Á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.commands;
public class CommandFailedException extends RuntimeException
{
public CommandFailedException()
{
super();
}
public CommandFailedException(String message)
{
super(message);
}
}

View File

@@ -51,7 +51,7 @@ public class CreateHabitCommand extends Command
public void undo()
{
Habit habit = Habit.get(savedId);
if(habit == null) throw new CommandFailedException("Habit not found");
if(habit == null) throw new RuntimeException("Habit not found");
habit.delete();
}

View File

@@ -42,7 +42,7 @@ public class EditHabitCommand extends Command
public void execute()
{
Habit habit = Habit.get(savedId);
if(habit == null) throw new CommandFailedException("Habit not found");
if(habit == null) throw new RuntimeException("Habit not found");
habit.copyAttributes(modified);
habit.save();
@@ -57,7 +57,7 @@ public class EditHabitCommand extends Command
public void undo()
{
Habit habit = Habit.get(savedId);
if(habit == null) throw new CommandFailedException("Habit not found");
if(habit == null) throw new RuntimeException("Habit not found");
habit.copyAttributes(original);
habit.save();