From 44fcb9ec7bf45a72b495885dbe695bef4fecc917 Mon Sep 17 00:00:00 2001 From: Fuzzlemann Date: Wed, 16 Aug 2017 17:14:11 +0200 Subject: [PATCH] Fixes failing of test when Hastebin is not available --- .../plan/utilities/file/dump/Hastebin.java | 2 +- .../plan/utilities/dump/HastebinTest.java | 49 +++++++++++++++++-- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/Hastebin.java b/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/Hastebin.java index f8361bbd2..ed8266088 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/Hastebin.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/file/dump/Hastebin.java @@ -66,7 +66,7 @@ public class Hastebin { * @param content The content * @return The link to the content */ - private static String upload(String content) throws IOException, ParseException { + public static String upload(String content) throws IOException, ParseException { HttpsURLConnection connection = null; try { URL url = new URL("https://hastebin.com/documents"); diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java b/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java index a4adcfc78..6889e0704 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java +++ b/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/dump/HastebinTest.java @@ -4,6 +4,8 @@ import com.google.common.collect.Iterables; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.utilities.file.dump.Hastebin; import org.bukkit.plugin.java.JavaPlugin; +import org.json.simple.parser.ParseException; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PowerMockIgnore; @@ -12,6 +14,9 @@ import org.powermock.modules.junit4.PowerMockRunner; import test.java.utils.RandomData; import test.java.utils.TestInit; +import java.io.IOException; +import java.util.concurrent.atomic.AtomicBoolean; + import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertNotNull; @@ -23,11 +28,44 @@ import static junit.framework.TestCase.assertNotNull; @PrepareForTest(JavaPlugin.class) public class HastebinTest { - private String content = RandomData.randomString(400000); + private AtomicBoolean hastebinAvailable = new AtomicBoolean(); + + @Before + public void checkAvailability() throws Exception { + TestInit.init(); + + Thread thread = new Thread(() -> { + String link = null; + try { + link = Hastebin.upload(RandomData.randomString(10)); + } catch (IOException e) { + if (e.getMessage().contains("503")) { + hastebinAvailable.set(false); + return; + } + } catch (ParseException e) { + /* Ignored */ + } + + if (link == null) { + hastebinAvailable.set(false); + } + + Log.info("Availability Test Link: " + link); + }); + + try { + thread.join(5000); + } catch (InterruptedException e) { + hastebinAvailable.set(false); + } + + Log.info("Hastebin Available: " + hastebinAvailable.get()); + } @Test public void testSplitting() { - Iterable parts = Hastebin.split(content); + Iterable parts = Hastebin.split(RandomData.randomString(500000)); int expPartCount = 2; int partCount = Iterables.size(parts); @@ -37,10 +75,13 @@ public class HastebinTest { @Test public void testUpload() throws Exception { + if (!hastebinAvailable.get()) { + return; + } + TestInit.init(); - String link = Hastebin.safeUpload(content); - + String link = Hastebin.safeUpload(RandomData.randomString(10)); assertNotNull(link); Log.info("Hastebin Link: " + link);