1 package net.pterodactylus.sone.core;
3 import static java.util.concurrent.TimeUnit.SECONDS;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.hamcrest.Matchers.is;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
9 import java.util.concurrent.atomic.AtomicInteger;
11 import net.pterodactylus.sone.core.SoneModificationDetector.LockableFingerprintProvider;
13 import com.google.common.base.Ticker;
14 import org.junit.Before;
15 import org.junit.Test;
18 * Unit test for {@link SoneModificationDetector}.
20 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
22 public class SoneModificationDetectorTest {
24 private final Ticker ticker = mock(Ticker.class);
25 private final AtomicInteger insertionDelay = new AtomicInteger(60);
26 private final SoneModificationDetector soneModificationDetector;
27 private final LockableFingerprintProvider lockableFingerprintProvider = mock(LockableFingerprintProvider.class);
29 public SoneModificationDetectorTest() {
30 when(lockableFingerprintProvider.getFingerprint()).thenReturn("original");
31 when(lockableFingerprintProvider.isLocked()).thenReturn(false);
32 soneModificationDetector = new SoneModificationDetector(ticker, lockableFingerprintProvider, insertionDelay);
35 private void modifySone() {
39 private void modifySone(String uniqueValue) {
40 when(lockableFingerprintProvider.getFingerprint()).thenReturn("modified" + uniqueValue);
43 private void passTime(int seconds) {
44 when(ticker.read()).thenReturn(SECONDS.toNanos(seconds));
47 private void lockSone() {
48 when(lockableFingerprintProvider.isLocked()).thenReturn(true);
51 private void unlockSone() {
52 when(lockableFingerprintProvider.isLocked()).thenReturn(false);
56 public void setupOriginalFingerprint() {
57 soneModificationDetector.setFingerprint("original");
61 public void normalConstructorCanBeCalled() {
62 new SoneModificationDetector(lockableFingerprintProvider, insertionDelay);
66 public void sonesStartOutAsNotEligible() {
67 assertThat(soneModificationDetector.isModified(), is(false));
68 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
72 public void originalFingerprintIsRetained() {
73 assertThat(soneModificationDetector.getLastInsertFingerprint(), is("original"));
77 public void modifiedSoneIsEligibleAfter60Seconds() {
79 assertThat(soneModificationDetector.isModified(), is(true));
80 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
82 assertThat(soneModificationDetector.isModified(), is(true));
83 assertThat(soneModificationDetector.isEligibleForInsert(), is(true));
87 public void modifiedAndRemodifiedSoneIsEligibleAfter90Seconds() {
89 assertThat(soneModificationDetector.isModified(), is(true));
90 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
93 assertThat(soneModificationDetector.isModified(), is(true));
94 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
96 assertThat(soneModificationDetector.isModified(), is(true));
97 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
99 assertThat(soneModificationDetector.isModified(), is(true));
100 assertThat(soneModificationDetector.isEligibleForInsert(), is(true));
104 public void modifiedSoneIsNotEligibleAfter30Seconds() {
107 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
111 public void lockedAndModifiedSoneIsNotEligibleAfter60Seconds() {
113 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
115 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
117 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
121 public void lockingAndUnlockingASoneRestartsTheWaitPeriod() {
125 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
127 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
129 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
131 assertThat(soneModificationDetector.isEligibleForInsert(), is(true));
135 public void settingFingerprintWillResetTheEligibility() {
137 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
139 assertThat(soneModificationDetector.isEligibleForInsert(), is(true));
140 soneModificationDetector.setFingerprint("modified");
141 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
145 public void changingInsertionDelayWillInfluenceEligibility() {
147 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
149 assertThat(soneModificationDetector.isEligibleForInsert(), is(true));
150 insertionDelay.set(120);
151 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
155 public void soneWithoutOriginalFingerprintIsNotEligibleAfter59Seconds() {
156 SoneModificationDetector soneModificationDetector = createDetectorWithoutOriginalFingerprint();
157 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
159 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
162 private SoneModificationDetector createDetectorWithoutOriginalFingerprint() {
163 return new SoneModificationDetector(ticker, new LockableFingerprintProvider() {
165 public boolean isLocked() {
170 public String getFingerprint() {
177 public void soneWithoutOriginalFingerprintIsEligibleAfter60Seconds() {
178 SoneModificationDetector soneModificationDetector = createDetectorWithoutOriginalFingerprint();
179 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
181 assertThat(soneModificationDetector.isEligibleForInsert(), is(true));