mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Add tests for equals, hashCode and toString
This commit is contained in:
@@ -25,6 +25,7 @@ dependencies {
|
||||
testImplementation 'org.mockito:mockito-core:2.8.9'
|
||||
testImplementation 'org.json:json:20160810'
|
||||
testImplementation 'org.xerial:sqlite-jdbc:3.18.0'
|
||||
testImplementation 'nl.jqno.equalsverifier:equalsverifier:2.3.1'
|
||||
|
||||
implementation('com.opencsv:opencsv:3.9') {
|
||||
exclude group: 'commons-logging', module: 'commons-logging'
|
||||
|
||||
@@ -348,7 +348,7 @@ public abstract class CheckmarkList
|
||||
add(buildCheckmarksFromIntervals(reps, intervals));
|
||||
}
|
||||
|
||||
static class Interval
|
||||
static final class Interval
|
||||
{
|
||||
final Timestamp begin;
|
||||
|
||||
|
||||
@@ -353,7 +353,7 @@ public class Habit
|
||||
data.position = newPosition;
|
||||
}
|
||||
|
||||
public static class HabitData
|
||||
public static final class HabitData
|
||||
{
|
||||
@NonNull
|
||||
public String name;
|
||||
@@ -471,4 +471,13 @@ public class Habit
|
||||
.toHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this)
|
||||
.append("id", id)
|
||||
.append("data", data)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,10 +19,6 @@
|
||||
|
||||
package org.isoron.uhabits.core.models;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class HabitMatcher
|
||||
{
|
||||
public static final HabitMatcher WITH_ALARM = new HabitMatcherBuilder()
|
||||
@@ -36,22 +32,13 @@ public class HabitMatcher
|
||||
|
||||
private final boolean completedAllowed;
|
||||
|
||||
private final List<Integer> allowedColors;
|
||||
|
||||
public HabitMatcher(boolean allowArchived,
|
||||
boolean reminderRequired,
|
||||
boolean completedAllowed,
|
||||
@NonNull List<Integer> allowedColors)
|
||||
boolean completedAllowed)
|
||||
{
|
||||
this.archivedAllowed = allowArchived;
|
||||
this.reminderRequired = reminderRequired;
|
||||
this.completedAllowed = completedAllowed;
|
||||
this.allowedColors = allowedColors;
|
||||
}
|
||||
|
||||
public List<Integer> getAllowedColors()
|
||||
{
|
||||
return allowedColors;
|
||||
}
|
||||
|
||||
public boolean isArchivedAllowed()
|
||||
@@ -74,7 +61,6 @@ public class HabitMatcher
|
||||
if (!isArchivedAllowed() && habit.isArchived()) return false;
|
||||
if (isReminderRequired() && !habit.hasReminder()) return false;
|
||||
if (!isCompletedAllowed() && habit.isCompletedToday()) return false;
|
||||
if (!allowedColors.contains(habit.getColor())) return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,10 +19,6 @@
|
||||
|
||||
package org.isoron.uhabits.core.models;
|
||||
|
||||
import org.isoron.uhabits.core.utils.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class HabitMatcherBuilder
|
||||
{
|
||||
private boolean archivedAllowed = false;
|
||||
@@ -31,20 +27,10 @@ public class HabitMatcherBuilder
|
||||
|
||||
private boolean completedAllowed = true;
|
||||
|
||||
private List<Integer> allowedColors = allColors();
|
||||
|
||||
private static List<Integer> allColors()
|
||||
{
|
||||
List<Integer> colors = new ArrayList<>();
|
||||
for(int i = 0; i < ColorConstants.CSV_PALETTE.length; i++)
|
||||
colors.add(i);
|
||||
return colors;
|
||||
}
|
||||
|
||||
public HabitMatcher build()
|
||||
{
|
||||
return new HabitMatcher(archivedAllowed, reminderRequired,
|
||||
completedAllowed, allowedColors);
|
||||
completedAllowed);
|
||||
}
|
||||
|
||||
public HabitMatcherBuilder setArchivedAllowed(boolean archivedAllowed)
|
||||
@@ -53,12 +39,6 @@ public class HabitMatcherBuilder
|
||||
return this;
|
||||
}
|
||||
|
||||
public HabitMatcherBuilder setAllowedColors(List<Integer> allowedColors)
|
||||
{
|
||||
this.allowedColors = allowedColors;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HabitMatcherBuilder setCompletedAllowed(boolean completedAllowed)
|
||||
{
|
||||
this.completedAllowed = completedAllowed;
|
||||
|
||||
@@ -20,19 +20,4 @@
|
||||
package org.isoron.uhabits.core.models;
|
||||
|
||||
public class HabitNotFoundException extends RuntimeException {
|
||||
public HabitNotFoundException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public HabitNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public HabitNotFoundException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public HabitNotFoundException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,4 +93,28 @@ public final class Score
|
||||
.append("value", value)
|
||||
.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
if (this == o) return true;
|
||||
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Score score = (Score) o;
|
||||
|
||||
return new EqualsBuilder()
|
||||
.append(value, score.value)
|
||||
.append(timestamp, score.timestamp)
|
||||
.isEquals();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return new HashCodeBuilder(17, 37)
|
||||
.append(timestamp)
|
||||
.append(value)
|
||||
.toHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,4 +69,28 @@ public final class Streak
|
||||
.append("end", end)
|
||||
.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
if (this == o) return true;
|
||||
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Streak streak = (Streak) o;
|
||||
|
||||
return new EqualsBuilder()
|
||||
.append(start, streak.start)
|
||||
.append(end, streak.end)
|
||||
.isEquals();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return new HashCodeBuilder(17, 37)
|
||||
.append(start)
|
||||
.append(end)
|
||||
.toHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ import java.util.*;
|
||||
|
||||
import static java.util.Calendar.DAY_OF_WEEK;
|
||||
|
||||
public class Timestamp
|
||||
public final class Timestamp
|
||||
{
|
||||
|
||||
public static final long DAY_LENGTH = 86400000;
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.apache.commons.lang3.builder.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class WeekdayList
|
||||
public final class WeekdayList
|
||||
{
|
||||
public static final WeekdayList EVERY_DAY = new WeekdayList(127);
|
||||
|
||||
|
||||
@@ -74,13 +74,6 @@ public class SQLiteHabitList extends HabitList
|
||||
}
|
||||
}
|
||||
|
||||
public static SQLiteHabitList getInstance(
|
||||
@NonNull ModelFactory modelFactory)
|
||||
{
|
||||
if (instance == null) instance = new SQLiteHabitList(modelFactory);
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void add(@NonNull Habit habit)
|
||||
{
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
|
||||
package org.isoron.uhabits.core.models.sqlite.records;
|
||||
|
||||
import org.apache.commons.lang3.builder.*;
|
||||
import org.isoron.uhabits.core.database.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
|
||||
@@ -129,78 +128,4 @@ public class HabitRecord
|
||||
new WeekdayList(reminderDays)));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
if (this == o) return true;
|
||||
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
HabitRecord that = (HabitRecord) o;
|
||||
|
||||
return new EqualsBuilder()
|
||||
.appendSuper(super.equals(o))
|
||||
.append(freqNum, that.freqNum)
|
||||
.append(freqDen, that.freqDen)
|
||||
.append(color, that.color)
|
||||
.append(position, that.position)
|
||||
.append(reminderDays, that.reminderDays)
|
||||
.append(highlight, that.highlight)
|
||||
.append(archived, that.archived)
|
||||
.append(type, that.type)
|
||||
.append(targetValue, that.targetValue)
|
||||
.append(targetType, that.targetType)
|
||||
.append(name, that.name)
|
||||
.append(description, that.description)
|
||||
.append(reminderHour, that.reminderHour)
|
||||
.append(reminderMin, that.reminderMin)
|
||||
.append(unit, that.unit)
|
||||
.isEquals();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return new HashCodeBuilder(17, 37)
|
||||
.appendSuper(super.hashCode())
|
||||
.append(name)
|
||||
.append(description)
|
||||
.append(freqNum)
|
||||
.append(freqDen)
|
||||
.append(color)
|
||||
.append(position)
|
||||
.append(reminderHour)
|
||||
.append(reminderMin)
|
||||
.append(reminderDays)
|
||||
.append(highlight)
|
||||
.append(archived)
|
||||
.append(type)
|
||||
.append(targetValue)
|
||||
.append(targetType)
|
||||
.append(unit)
|
||||
.toHashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this)
|
||||
.append("name", name)
|
||||
.append("description", description)
|
||||
.append("freqNum", freqNum)
|
||||
.append("freqDen", freqDen)
|
||||
.append("color", color)
|
||||
.append("position", position)
|
||||
.append("reminderHour", reminderHour)
|
||||
.append("reminderMin", reminderMin)
|
||||
.append("reminderDays", reminderDays)
|
||||
.append("highlight", highlight)
|
||||
.append("archived", archived)
|
||||
.append("type", type)
|
||||
.append("targetValue", targetValue)
|
||||
.append("targetType", targetType)
|
||||
.append("unit", unit)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,4 +28,9 @@ public class StringUtils
|
||||
{
|
||||
return new BigInteger(260, new Random()).toString(32).substring(0, 32);
|
||||
}
|
||||
|
||||
public static String removePointers(String original)
|
||||
{
|
||||
return original.replaceAll("@[0-9a-f]*", "@00000000");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,9 +26,12 @@ import org.junit.*;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import nl.jqno.equalsverifier.*;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.isoron.uhabits.core.models.Checkmark.*;
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.removePointers;
|
||||
|
||||
public class CheckmarkListTest extends BaseUnitTest
|
||||
{
|
||||
@@ -351,4 +354,34 @@ public class CheckmarkListTest extends BaseUnitTest
|
||||
DateUtils.setFixedLocalTime(
|
||||
FIXED_LOCAL_TIME + days * Timestamp.DAY_LENGTH);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception
|
||||
{
|
||||
Timestamp t = Timestamp.ZERO.plus(100);
|
||||
Checkmark checkmark = new Checkmark(t, 2);
|
||||
String s = removePointers(checkmark.toString());
|
||||
assertThat(s, equalTo(
|
||||
"org.isoron.uhabits.core.models.Checkmark@00000000[" +
|
||||
"timestamp=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=8640000000]," +
|
||||
"value=2]"));
|
||||
|
||||
|
||||
CheckmarkList.Interval interval =
|
||||
new CheckmarkList.Interval(t, t.plus(1), t.plus(2));
|
||||
s = removePointers(interval.toString());
|
||||
assertThat(s, equalTo(
|
||||
"org.isoron.uhabits.core.models.CheckmarkList$Interval@00000000[" +
|
||||
"begin=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=8640000000]," +
|
||||
"center=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=8726400000]," +
|
||||
"end=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=8812800000]]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() throws Exception
|
||||
{
|
||||
EqualsVerifier.forClass(Checkmark.class).verify();
|
||||
EqualsVerifier.forClass(Timestamp.class).verify();
|
||||
EqualsVerifier.forClass(CheckmarkList.Interval.class).verify();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,8 +23,11 @@ import org.isoron.uhabits.core.*;
|
||||
import org.junit.*;
|
||||
import org.junit.rules.*;
|
||||
|
||||
import nl.jqno.equalsverifier.*;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.isoron.uhabits.core.utils.DateUtils.*;
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.removePointers;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class HabitTest extends BaseUnitTest
|
||||
@@ -126,4 +129,43 @@ public class HabitTest extends BaseUnitTest
|
||||
assertThat(h.getUriString(),
|
||||
equalTo("content://org.isoron.uhabits/habit/0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() throws Exception
|
||||
{
|
||||
EqualsVerifier
|
||||
.forClass(Habit.HabitData.class)
|
||||
.suppress(Warning.NONFINAL_FIELDS)
|
||||
.verify();
|
||||
|
||||
EqualsVerifier.forClass(Repetition.class).verify();
|
||||
EqualsVerifier.forClass(Score.class).verify();
|
||||
EqualsVerifier.forClass(Streak.class).verify();
|
||||
EqualsVerifier.forClass(Reminder.class).verify();
|
||||
EqualsVerifier.forClass(WeekdayList.class).verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception
|
||||
{
|
||||
Habit h = modelFactory.buildHabit();
|
||||
h.setReminder(new Reminder(22, 30, WeekdayList.EVERY_DAY));
|
||||
|
||||
String s = removePointers(h.toString());
|
||||
|
||||
String expected =
|
||||
"org.isoron.uhabits.core.models.Habit@00000000[" +
|
||||
"id=<null>," +
|
||||
"data=org.isoron.uhabits.core.models.Habit$HabitData@00000000[" +
|
||||
"name=,description=," +
|
||||
"frequency=org.isoron.uhabits.core.models.Frequency@00000000[numerator=3,denominator=7]," +
|
||||
"color=8,archived=false,targetType=0,targetValue=100.0,type=0,unit=," +
|
||||
"reminder=org.isoron.uhabits.core.models.Reminder@00000000[" +
|
||||
"hour=22,minute=30," +
|
||||
"days=org.isoron.uhabits.core.models.WeekdayList@00000000[" +
|
||||
"weekdays={true,true,true,true,true,true,true}]]," +
|
||||
"position=0]]";
|
||||
|
||||
assertThat(s, equalTo(expected));
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ import java.util.*;
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.removePointers;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.*;
|
||||
@@ -186,4 +187,15 @@ public class RepetitionListTest extends BaseUnitTest
|
||||
verify(listener, times(2)).onModelChange();
|
||||
reset(listener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception
|
||||
{
|
||||
Repetition rep = new Repetition(Timestamp.ZERO.plus(100), 20);
|
||||
String s = removePointers(rep.toString());
|
||||
assertThat(s, equalTo(
|
||||
"org.isoron.uhabits.core.models.Repetition@00000000[" +
|
||||
"timestamp=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=8640000000]," +
|
||||
"value=20]"));
|
||||
}
|
||||
}
|
||||
@@ -26,8 +26,8 @@ import org.junit.*;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.hamcrest.number.IsCloseTo.*;
|
||||
|
||||
public class ScoreListTest extends BaseUnitTest
|
||||
|
||||
@@ -22,9 +22,12 @@ package org.isoron.uhabits.core.models;
|
||||
import org.isoron.uhabits.core.*;
|
||||
import org.junit.*;
|
||||
|
||||
import static org.hamcrest.number.IsCloseTo.*;
|
||||
import static org.isoron.uhabits.core.models.Score.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.hamcrest.number.IsCloseTo.closeTo;
|
||||
import static org.isoron.uhabits.core.models.Score.compute;
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.removePointers;
|
||||
|
||||
|
||||
public class ScoreTest extends BaseUnitTest
|
||||
{
|
||||
@@ -66,4 +69,16 @@ public class ScoreTest extends BaseUnitTest
|
||||
assertThat(compute(freq, 0.5, check), closeTo(0.491192, E));
|
||||
assertThat(compute(freq, 0.75, check), closeTo(0.736788, E));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception
|
||||
{
|
||||
Score score = new Score(Timestamp.ZERO.plus(100), 150.0);
|
||||
String string = removePointers(score.toString());
|
||||
assertThat(string, equalTo(
|
||||
"org.isoron.uhabits.core.models.Score@00000000[" +
|
||||
"timestamp=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=8640000000]," +
|
||||
"value=150.0]"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,9 @@ import org.junit.*;
|
||||
import java.util.*;
|
||||
|
||||
import static junit.framework.TestCase.*;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class StreakListTest extends BaseUnitTest
|
||||
@@ -117,4 +118,16 @@ public class StreakListTest extends BaseUnitTest
|
||||
s = streaks.getNewestComputed();
|
||||
assertNull(s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception
|
||||
{
|
||||
Timestamp time = Timestamp.ZERO.plus(100);
|
||||
Streak streak = new Streak(time, time.plus(10));
|
||||
String string = removePointers(streak.toString());
|
||||
assertThat(string, equalTo(
|
||||
"org.isoron.uhabits.core.models.Streak@00000000[" +
|
||||
"start=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=8640000000]," +
|
||||
"end=org.isoron.uhabits.core.models.Timestamp@00000000[unixTime=9504000000]]"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user