commit fff9443dfa7f55caff67f6a327e723b04ca0eaf1 Author: Karsten Loesing karsten.loesing@gmx.net Date: Fri Sep 30 15:07:53 2016 +0200
Extend checkstyle to test sources.
Also fix MainTest.waitSec() by not oversleeping in case of an interruption. --- build.xml | 2 ++ .../java/org/torproject/collector/MainTest.java | 26 +++++++++++++--------- .../collector/conf/ConfigurationTest.java | 20 +++++++++-------- .../java/org/torproject/collector/cron/Broken.java | 4 ++-- .../collector/cron/CollecTorMainTest.java | 1 + .../org/torproject/collector/cron/Counter.java | 4 ++-- .../java/org/torproject/collector/cron/Dummy.java | 4 ++-- .../torproject/collector/cron/SchedulerTest.java | 19 ++++++++-------- 8 files changed, 46 insertions(+), 34 deletions(-)
diff --git a/build.xml b/build.xml index ffbeb31..83f2907 100644 --- a/build.xml +++ b/build.xml @@ -229,6 +229,8 @@ </taskdef> <target name="checks" depends="compile"> <checkstyle config="${testresources}/metrics_checks.xml"> + <fileset dir="${testsources}" + includes="**/*.java" /> <fileset dir="${sources}" includes="**/*.java" /> <classpath> diff --git a/src/test/java/org/torproject/collector/MainTest.java b/src/test/java/org/torproject/collector/MainTest.java index f65d2fa..ebc2021 100644 --- a/src/test/java/org/torproject/collector/MainTest.java +++ b/src/test/java/org/torproject/collector/MainTest.java @@ -1,5 +1,6 @@ /* Copyright 2016 The Tor Project * See LICENSE for licensing information */ + package org.torproject.collector;
import static org.junit.Assert.assertEquals; @@ -103,29 +104,34 @@ public class MainTest { waitSec(2); }
+ /** Wait for the given number of seconds. */ public static void waitSec(int sec) { - long now = System.currentTimeMillis(); - while (System.currentTimeMillis() - now < 1_000L * sec) { + long start = System.currentTimeMillis(); + long toWait = 1_000L * sec; + do { try { - Thread.sleep(sec * 1_000L); - } catch (Exception e) {/* ignored */} - } + Thread.sleep(toWait); + } catch (InterruptedException e) { + /* Ignore the interruption, but possibly resume sleeping if we didn't + * sleep long enough. */ + } + } while ((toWait = start + 1_000L * sec - System.currentTimeMillis()) > 0); }
- private void changeFilePathsAndSetActivation(File f, String a) + private void changeFilePathsAndSetActivation(File file, String activation) throws Exception { - List<String> lines = Files.readAllLines(f.toPath()); - BufferedWriter bw = Files.newBufferedWriter(f.toPath()); + List<String> lines = Files.readAllLines(file.toPath()); + BufferedWriter bw = Files.newBufferedWriter(file.toPath()); File in = tmpf.newFolder(); File out = tmpf.newFolder(); String inStr = "in/"; String outStr = "out/"; - for(String line : lines) { + for (String line : lines) { if (line.contains(inStr)) { line = line.replace(inStr, in.toString() + inStr); } else if (line.contains(outStr)) { line = line.replace(outStr, out.toString() + outStr); - } else if (line.contains(a)) { + } else if (line.contains(activation)) { line = line.replace("false", "true"); } bw.write(line); diff --git a/src/test/java/org/torproject/collector/conf/ConfigurationTest.java b/src/test/java/org/torproject/collector/conf/ConfigurationTest.java index fda9601..fdba6b5 100644 --- a/src/test/java/org/torproject/collector/conf/ConfigurationTest.java +++ b/src/test/java/org/torproject/collector/conf/ConfigurationTest.java @@ -1,5 +1,6 @@ /* Copyright 2016 The Tor Project * See LICENSE for licensing information */ + package org.torproject.collector.conf;
import static org.junit.Assert.assertArrayEquals; @@ -56,7 +57,7 @@ public class ConfigurationTest { @Test() public void testArrayValues() throws Exception { String[] array = new String[randomSource.nextInt(30) + 1]; - for (int i = 0; i < array.length; i++){ + for (int i = 0; i < array.length; i++) { array[i] = Integer.toBinaryString(randomSource.nextInt(100)); } String[] arrays = new String[] { @@ -65,7 +66,7 @@ public class ConfigurationTest { .replaceAll(" ", "") }; Configuration conf = new Configuration(); - for(String input : arrays) { + for (String input : arrays) { conf.clear(); conf.setProperty(Key.CachedRelayDescriptorsDirectories.name(), input); assertArrayEquals("expected " + Arrays.toString(array) + "\nreceived: " @@ -92,19 +93,20 @@ public class ConfigurationTest { conf.setProperty(Key.BridgeDescriptorMappingsLimit.name(), "inf"); assertEquals(Integer.MAX_VALUE, conf.getInt(Key.BridgeDescriptorMappingsLimit)); - int r = randomSource.nextInt(Integer.MAX_VALUE); + int randomInt = randomSource.nextInt(Integer.MAX_VALUE); conf.clear(); conf.load(new ByteArrayInputStream( - propLine(Key.BridgeDescriptorMappingsLimit, "" + r).getBytes())); - assertEquals(r, + propLine(Key.BridgeDescriptorMappingsLimit, + "" + randomInt).getBytes())); + assertEquals(randomInt, conf.getInt(Key.BridgeDescriptorMappingsLimit)); - } + }
@Test() public void testFileValues() throws Exception { String[] files = new String[] { "/the/path/file.txt", "another/path"}; Configuration conf = new Configuration(); - for(String file : files) { + for (String file : files) { conf.clear(); conf.setProperty(Key.DirectoryArchivesOutputDirectory.name(), file); assertEquals(new File(file), @@ -122,7 +124,7 @@ public class ConfigurationTest { Arrays.deepToString(sourceStrings).replace("[[", "").replace("]]", "") .replace("], [", Configuration.ARRAYSEP)); assertArrayEquals(sourceStrings, - conf.getStringArrayArray(Key.TorperfSources)); + conf.getStringArrayArray(Key.TorperfSources)); }
@Test(expected = ConfigurationException.class) @@ -211,7 +213,7 @@ public class ConfigurationTest { MainTest.waitSec(1); confFile.delete(); conf.setProperty(Key.ImportDirectoryArchives.name(), "false"); - Dummy dummy = new Dummy(conf); + final Dummy dummy = new Dummy(conf); tmpf.newFolder("empty"); MainTest.waitSec(6); assertFalse("Update was called.", called.get()); diff --git a/src/test/java/org/torproject/collector/cron/Broken.java b/src/test/java/org/torproject/collector/cron/Broken.java index 0a3bc93..f625acb 100644 --- a/src/test/java/org/torproject/collector/cron/Broken.java +++ b/src/test/java/org/torproject/collector/cron/Broken.java @@ -12,8 +12,8 @@ public class Broken extends CollecTorMain {
static AtomicInteger count = new AtomicInteger(0);
- public Broken(Configuration c) { - super(c); + public Broken(Configuration conf) { + super(conf); }
@Override diff --git a/src/test/java/org/torproject/collector/cron/CollecTorMainTest.java b/src/test/java/org/torproject/collector/cron/CollecTorMainTest.java index fecd497..befaad3 100644 --- a/src/test/java/org/torproject/collector/cron/CollecTorMainTest.java +++ b/src/test/java/org/torproject/collector/cron/CollecTorMainTest.java @@ -1,5 +1,6 @@ /* Copyright 2016 The Tor Project * See LICENSE for licensing information */ + package org.torproject.collector.cron;
import static org.junit.Assert.assertTrue; diff --git a/src/test/java/org/torproject/collector/cron/Counter.java b/src/test/java/org/torproject/collector/cron/Counter.java index 3b404b2..81aa34d 100644 --- a/src/test/java/org/torproject/collector/cron/Counter.java +++ b/src/test/java/org/torproject/collector/cron/Counter.java @@ -9,8 +9,8 @@ public class Counter extends CollecTorMain {
static AtomicInteger count = new AtomicInteger(0);
- public Counter(Configuration c) { - super(c); + public Counter(Configuration conf) { + super(conf); }
@Override diff --git a/src/test/java/org/torproject/collector/cron/Dummy.java b/src/test/java/org/torproject/collector/cron/Dummy.java index 449cc55..a44caae 100644 --- a/src/test/java/org/torproject/collector/cron/Dummy.java +++ b/src/test/java/org/torproject/collector/cron/Dummy.java @@ -5,8 +5,8 @@ import org.torproject.collector.conf.ConfigurationException;
public class Dummy extends CollecTorMain {
- public Dummy(Configuration c) { - super(c); + public Dummy(Configuration conf) { + super(conf); }
@Override diff --git a/src/test/java/org/torproject/collector/cron/SchedulerTest.java b/src/test/java/org/torproject/collector/cron/SchedulerTest.java index eba05ce..f152ccf 100644 --- a/src/test/java/org/torproject/collector/cron/SchedulerTest.java +++ b/src/test/java/org/torproject/collector/cron/SchedulerTest.java @@ -1,5 +1,6 @@ /* Copyright 2016 The Tor Project * See LICENSE for licensing information */ + package org.torproject.collector.cron;
import static org.junit.Assert.assertEquals; @@ -21,15 +22,15 @@ import java.util.concurrent.ScheduledThreadPoolExecutor; public class SchedulerTest {
private static final String runConfigProperties = - "TorperfActivated=true\nTorperfPeriodMinutes=1\nTorperfOffsetMinutes=0\n" - + "RelaydescsActivated=true\nRelaydescsPeriodMinutes=1" - + "\nRelaydescsOffsetMinutes=0\n" - + "ExitlistsActivated=true\nExitlistsPeriodMinutes=1\n" - + "ExitlistsOffsetMinutes=0\n" - + "UpdateindexActivated=true\nUpdateindexPeriodMinutes=1\n" - + "UpdateindexOffsetMinutes=0\n" - + "BridgedescsActivated=true\nBridgedescsPeriodMinutes=1\n" - + "BridgedescsOffsetMinutes=0\n"; + "TorperfActivated=true\nTorperfPeriodMinutes=1\nTorperfOffsetMinutes=0\n" + + "RelaydescsActivated=true\nRelaydescsPeriodMinutes=1" + + "\nRelaydescsOffsetMinutes=0\n" + + "ExitlistsActivated=true\nExitlistsPeriodMinutes=1\n" + + "ExitlistsOffsetMinutes=0\n" + + "UpdateindexActivated=true\nUpdateindexPeriodMinutes=1\n" + + "UpdateindexOffsetMinutes=0\n" + + "BridgedescsActivated=true\nBridgedescsPeriodMinutes=1\n" + + "BridgedescsOffsetMinutes=0\n";
@Test() public void testSimpleSchedule() throws Exception {
tor-commits@lists.torproject.org