Skip to content
Snippets Groups Projects
Commit 38ed9d69 authored by akwizgran's avatar akwizgran
Browse files

Wrap java.util.Timer in an interface so it can be mocked.

parent 8ed68f36
No related branches found
No related tags found
No related merge requests found
package net.sf.briar.api.clock;
import java.util.TimerTask;
/** Default timer implementation. */
public class SystemTimer implements Timer {
private final java.util.Timer timer = new java.util.Timer();
public void cancel() {
timer.cancel();
}
public int purge() {
return timer.purge();
}
public void schedule(TimerTask task, long delay) {
timer.schedule(task, delay);
}
public void schedule(TimerTask task, long delay, long period) {
timer.schedule(task, delay, period);
}
public void scheduleAtFixedRate(TimerTask task, long delay, long period) {
timer.scheduleAtFixedRate(task, delay, period);
}
}
package net.sf.briar.api.clock;
import java.util.TimerTask;
/**
* A wrapper around a {@link java.util.Timer} that allows it to be replaced for
* testing.
*/
public interface Timer {
/** @see {@link java.util.Timer#cancel()} */
void cancel();
/** @see {@link java.util.Timer#purge()} */
int purge();
/** @see {@link java.util.Timer#schedule(TimerTask, long)} */
void schedule(TimerTask task, long delay);
/** @see {@link java.util.Timer#schedule(TimerTask, long, long)} */
void schedule(TimerTask task, long delay, long period);
/**
* @see {@link java.util.Timer#scheduleAtFixedRate(TimerTask, long, long)}
*/
void scheduleAtFixedRate(TimerTask task, long delay, long period);
}
package net.sf.briar.api.crypto;
/** An interface that allows a java.security.MessageDigest to be wrapped. */
/**
* A wrapper around a {@link java.security.MessageDigest} that allows it to be
* replaced for testing.
*/
public interface MessageDigest {
/** @see {@link java.security.MessageDigest#digest()} */
......
......@@ -2,6 +2,8 @@ package net.sf.briar.clock;
import net.sf.briar.api.clock.Clock;
import net.sf.briar.api.clock.SystemClock;
import net.sf.briar.api.clock.SystemTimer;
import net.sf.briar.api.clock.Timer;
import com.google.inject.AbstractModule;
......@@ -10,5 +12,6 @@ public class ClockModule extends AbstractModule {
@Override
protected void configure() {
bind(Clock.class).to(SystemClock.class);
bind(Timer.class).to(SystemTimer.class);
}
}
......@@ -3,29 +3,35 @@ package net.sf.briar.db;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;
import net.sf.briar.api.clock.Timer;
import net.sf.briar.api.db.DbClosedException;
import net.sf.briar.api.db.DbException;
import com.google.inject.Inject;
class DatabaseCleanerImpl extends TimerTask implements DatabaseCleaner {
private static final Logger LOG =
Logger.getLogger(DatabaseCleanerImpl.class.getName());
private final Timer timer;
private volatile Callback callback = null;
private volatile Timer timer = null;
@Inject
DatabaseCleanerImpl(Timer timer) {
this.timer = timer;
}
public void startCleaning(Callback callback, long msBetweenSweeps) {
this.callback = callback;
timer = new Timer();
timer.scheduleAtFixedRate(this, 0L, msBetweenSweeps);
}
public void stopCleaning() {
if(timer == null) throw new IllegalStateException();
timer.cancel();
}
......
......@@ -8,11 +8,11 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.clock.Timer;
import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.KeyManager;
import net.sf.briar.api.db.DatabaseComponent;
......@@ -49,11 +49,11 @@ class KeyManagerImpl extends TimerTask implements KeyManager, DatabaseListener {
@Inject
public KeyManagerImpl(CryptoComponent crypto, DatabaseComponent db,
ConnectionRecogniser recogniser) {
ConnectionRecogniser recogniser, Timer timer) {
this.crypto = crypto;
this.db = db;
this.recogniser = recogniser;
timer = new Timer();
this.timer = timer;
outgoing = new HashMap<ContactTransportKey, TemporarySecret>();
incomingOld = new HashMap<ContactTransportKey, TemporarySecret>();
incomingNew = new HashMap<ContactTransportKey, TemporarySecret>();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment