From: David ‘Bombe’ Roden Date: Thu, 12 Jun 2014 05:29:43 +0000 (+0200) Subject: Refactor test. X-Git-Tag: 0.9-rc1^2~3^2~239 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=0ddcaceecf1d4de91b332f640fe65a1d26d746b2;p=Sone.git Refactor test. --- diff --git a/src/test/java/net/pterodactylus/sone/core/SoneModificationDetectorTest.java b/src/test/java/net/pterodactylus/sone/core/SoneModificationDetectorTest.java index e85beb0..90f2935 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneModificationDetectorTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneModificationDetectorTest.java @@ -20,59 +20,62 @@ import org.junit.Test; */ public class SoneModificationDetectorTest { + private final Ticker ticker = mock(Ticker.class); + private final Sone sone = mock(Sone.class); + private final Core core = mock(Core.class); + private final AtomicInteger insertionDelay = new AtomicInteger(60); + private final SoneModificationDetector soneModificationDetector; + + public SoneModificationDetectorTest() { + when(sone.getFingerprint()).thenReturn("original"); + soneModificationDetector = new SoneModificationDetector(ticker, core, sone, insertionDelay); + } + + private void modifySone() { + when(sone.getFingerprint()).thenReturn("modified"); + } + + private void passTime(int seconds) { + when(ticker.read()).thenReturn(SECONDS.toNanos(seconds)); + } + + private void lockSone() { + when(core.isLocked(sone)).thenReturn(true); + } + @Test public void modifiedSoneIsEligibleAfter60Seconds() { - Ticker ticker = mock(Ticker.class); - Sone sone = mock(Sone.class); - when(sone.getFingerprint()).thenReturn("original"); - Core core = mock(Core.class); - SoneModificationDetector soneModificationDetector = new SoneModificationDetector(ticker, core, sone, new AtomicInteger(60)); assertThat(soneModificationDetector.isEligibleForInsert(), is(false)); - when(sone.getFingerprint()).thenReturn("modified"); + modifySone(); assertThat(soneModificationDetector.isEligibleForInsert(), is(false)); - when(ticker.read()).thenReturn(SECONDS.toNanos(100)); + passTime(100); assertThat(soneModificationDetector.isEligibleForInsert(), is(true)); } @Test public void modifiedSoneIsNotEligibleAfter30Seconds() { - Ticker ticker = mock(Ticker.class); - Sone sone = mock(Sone.class); - when(sone.getFingerprint()).thenReturn("original"); - Core core = mock(Core.class); - SoneModificationDetector soneModificationDetector = new SoneModificationDetector(ticker, core, sone, new AtomicInteger(60)); assertThat(soneModificationDetector.isEligibleForInsert(), is(false)); - when(sone.getFingerprint()).thenReturn("modified"); - when(ticker.read()).thenReturn(SECONDS.toNanos(30)); + modifySone(); + passTime(30); assertThat(soneModificationDetector.isEligibleForInsert(), is(false)); } @Test public void lockedAndModifiedSoneIsNotEligibleAfter60Seconds() { - Ticker ticker = mock(Ticker.class); - Sone sone = mock(Sone.class); - when(sone.getFingerprint()).thenReturn("original"); - Core core = mock(Core.class); - when(core.isLocked(sone)).thenReturn(true); - SoneModificationDetector soneModificationDetector = new SoneModificationDetector(ticker, core, sone, new AtomicInteger(60)); + lockSone(); assertThat(soneModificationDetector.isEligibleForInsert(), is(false)); - when(sone.getFingerprint()).thenReturn("modified"); + modifySone(); assertThat(soneModificationDetector.isEligibleForInsert(), is(false)); - when(ticker.read()).thenReturn(SECONDS.toNanos(100)); + passTime(100); assertThat(soneModificationDetector.isEligibleForInsert(), is(false)); } @Test public void settingFingerprintWillResetTheEligibility() { - Ticker ticker = mock(Ticker.class); - Sone sone = mock(Sone.class); - when(sone.getFingerprint()).thenReturn("original"); - Core core = mock(Core.class); - SoneModificationDetector soneModificationDetector = new SoneModificationDetector(ticker, core, sone, new AtomicInteger(60)); assertThat(soneModificationDetector.isEligibleForInsert(), is(false)); - when(sone.getFingerprint()).thenReturn("modified"); + modifySone(); assertThat(soneModificationDetector.isEligibleForInsert(), is(false)); - when(ticker.read()).thenReturn(SECONDS.toNanos(100)); + passTime(100); assertThat(soneModificationDetector.isEligibleForInsert(), is(true)); soneModificationDetector.setFingerprint("modified"); assertThat(soneModificationDetector.isEligibleForInsert(), is(false)); @@ -80,16 +83,10 @@ public class SoneModificationDetectorTest { @Test public void changingInsertionDelayWillInfluenceEligibility() { - Ticker ticker = mock(Ticker.class); - Sone sone = mock(Sone.class); - when(sone.getFingerprint()).thenReturn("original"); - Core core = mock(Core.class); - AtomicInteger insertionDelay = new AtomicInteger(60); - SoneModificationDetector soneModificationDetector = new SoneModificationDetector(ticker, core, sone, insertionDelay); assertThat(soneModificationDetector.isEligibleForInsert(), is(false)); - when(sone.getFingerprint()).thenReturn("modified"); + modifySone(); assertThat(soneModificationDetector.isEligibleForInsert(), is(false)); - when(ticker.read()).thenReturn(SECONDS.toNanos(100)); + passTime(100); assertThat(soneModificationDetector.isEligibleForInsert(), is(true)); insertionDelay.set(120); assertThat(soneModificationDetector.isEligibleForInsert(), is(false));