commit aa87253196c7134762a2b2f4d047ad6d2b58326d Author: iwakeh iwakeh@torproject.org Date: Thu Nov 17 18:22:07 2016 +0100
Added tests for ReferenceChecker; prepares task-20695. --- .../collector/relaydescs/ReferenceCheckerTest.java | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+)
diff --git a/src/test/java/org/torproject/collector/relaydescs/ReferenceCheckerTest.java b/src/test/java/org/torproject/collector/relaydescs/ReferenceCheckerTest.java new file mode 100644 index 0000000..6ed3501 --- /dev/null +++ b/src/test/java/org/torproject/collector/relaydescs/ReferenceCheckerTest.java @@ -0,0 +1,78 @@ +/* Copyright 2016 The Tor Project + * See LICENSE for licensing information */ + +package org.torproject.collector.relaydescs; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.nio.charset.Charset; +import java.nio.file.Files; + +public class ReferenceCheckerTest { + + @Rule + public TemporaryFolder tmpf = new TemporaryFolder(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private static final String validReferenceJson + = "[{"referencing":"C-2016-09-17T03:00:00Z"," + + ""referenced":"S-D8736FB5916322CB4B0FC34FA9DA3D8ACBBCE352"," + + ""weight":0.028224668360146768," + + ""expiresAfterMillis":33455631600000}]"; + + @Test() + public void testValidJson() throws Exception { + File descDir = tmpf.newFolder(); + File refsFile = tmpf.newFile(); + File histFile = tmpf.newFile(); + Files.write(refsFile.toPath(), validReferenceJson.getBytes()); + assertEquals(validReferenceJson, Files.readAllLines(refsFile.toPath(), + Charset.forName("US-ASCII")).get(0)); + ReferenceChecker rc = new ReferenceChecker(descDir, refsFile, histFile); + rc.check(); + assertTrue(refsFile.exists()); + assertEquals(validReferenceJson, Files.readAllLines(refsFile.toPath(), + Charset.forName("US-ASCII")).get(0)); + } + + @Test() + public void testInvalidJson() throws Exception { + File descDir = tmpf.newFolder(); + File refsFile = tmpf.newFile(); + File histFile = tmpf.newFile(); + String badJson = "[{"xx":7]}"; + Files.write(refsFile.toPath(), badJson.getBytes()); + assertEquals(badJson, Files.readAllLines(refsFile.toPath(), + Charset.forName("US-ASCII")).get(0)); + ReferenceChecker rc = new ReferenceChecker(descDir, refsFile, histFile); + rc.check(); + assertTrue(refsFile.exists()); + assertEquals("[]", Files.readAllLines(refsFile.toPath(), + Charset.forName("US-ASCII")).get(0)); + } + + @Test() + public void testMinimalValidJson() throws Exception { + File descDir = tmpf.newFolder(); + File refsFile = tmpf.newFile(); + File histFile = tmpf.newFile(); + Files.write(refsFile.toPath(), "[]".getBytes()); + ReferenceChecker rc = new ReferenceChecker(descDir, refsFile, histFile); + rc.check(); + assertTrue(refsFile.exists()); + assertEquals("The file's contents shouldn't have changed, but did.", "[]", + Files.readAllLines(refsFile.toPath(), Charset.forName("US-ASCII")) + .get(0)); + } + +} +
tor-commits@lists.torproject.org