1
0
mirror of https://github.com/square/okhttp.git synced 2025-08-08 23:42:08 +03:00

Update remaining junit4 tests (#6421)

This commit is contained in:
Yuri Schimke
2020-11-14 17:21:04 +00:00
committed by GitHub
parent 5bd0a88ff3
commit 4036fa55b7
8 changed files with 82 additions and 104 deletions

View File

@@ -19,12 +19,12 @@ import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.List; import java.util.List;
import okio.ByteString; import okio.ByteString;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static okhttp3.dnsoverhttps.DnsRecordCodec.TYPE_A; import static okhttp3.dnsoverhttps.DnsRecordCodec.TYPE_A;
import static okhttp3.dnsoverhttps.DnsRecordCodec.TYPE_AAAA; import static okhttp3.dnsoverhttps.DnsRecordCodec.TYPE_AAAA;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail; import static org.assertj.core.api.Assertions.fail;
public class DnsRecordCodecTest { public class DnsRecordCodecTest {
@Test public void testGoogleDotComEncoding() { @Test public void testGoogleDotComEncoding() {
@@ -75,7 +75,7 @@ public class DnsRecordCodecTest {
+ "0000000100000e7364666c6b686673646c6b6a64660265650000010001c01b00060001000007070038026e" + "0000000100000e7364666c6b686673646c6b6a64660265650000010001c01b00060001000007070038026e"
+ "7303746c64c01b0a686f73746d61737465720d6565737469696e7465726e6574c01b5adb12c100000e1000" + "7303746c64c01b0a686f73746d61737465720d6565737469696e7465726e6574c01b5adb12c100000e1000"
+ "0003840012750000000e10")); + "0003840012750000000e10"));
fail(); fail("");
} catch (UnknownHostException uhe) { } catch (UnknownHostException uhe) {
assertThat(uhe.getMessage()).isEqualTo("sdflkhfsdlkjdf.ee: NXDOMAIN"); assertThat(uhe.getMessage()).isEqualTo("sdflkhfsdlkjdf.ee: NXDOMAIN");
} }

View File

@@ -15,31 +15,29 @@
*/ */
package okhttp3.internal.http2; package okhttp3.internal.http2;
import java.util.Collection; import java.util.List;
import okhttp3.SimpleProvider;
import okhttp3.internal.http2.hpackjson.Story; import okhttp3.internal.http2.hpackjson.Story;
import org.junit.Assume; import org.jetbrains.annotations.NotNull;
import org.junit.Test; import org.junit.jupiter.api.Assumptions;
import org.junit.runner.RunWith; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.runners.Parameterized; import org.junit.jupiter.params.provider.ArgumentsSource;
import static okhttp3.internal.http2.hpackjson.HpackJsonUtil.storiesForCurrentDraft; import static okhttp3.internal.http2.hpackjson.HpackJsonUtil.storiesForCurrentDraft;
@RunWith(Parameterized.class)
public class HpackDecodeInteropTest extends HpackDecodeTestBase { public class HpackDecodeInteropTest extends HpackDecodeTestBase {
public HpackDecodeInteropTest(Story story) { @ParameterizedTest
super(story); @ArgumentsSource(StoriesTestProvider.class)
public void testGoodDecoderInterop(Story story) throws Exception {
Assumptions.assumeFalse(story == Story.MISSING, "Test stories missing, checkout git submodule");
testDecoder(story);
} }
@Parameterized.Parameters(name = "{0}") static class StoriesTestProvider extends SimpleProvider {
public static Collection<Story[]> createStories() throws Exception { @NotNull @Override public List<Object> arguments() throws Exception {
return createStories(storiesForCurrentDraft()); return createStories(storiesForCurrentDraft());
} }
@Test
public void testGoodDecoderInterop() throws Exception {
Assume.assumeFalse("Test stories missing, checkout git submodule", getStory() == Story.MISSING);
testDecoder();
} }
} }

View File

@@ -36,17 +36,17 @@ public class HpackDecodeTestBase {
/** /**
* Reads all stories in the folders provided, asserts if no story found. * Reads all stories in the folders provided, asserts if no story found.
*/ */
protected static Collection<Story[]> createStories(String[] interopTests) protected static List<Object> createStories(String[] interopTests)
throws Exception { throws Exception {
if (interopTests.length == 0) { if (interopTests.length == 0) {
return Collections.singletonList(new Story[] {MISSING}); return Collections.singletonList(MISSING);
} }
List<Story[]> result = new ArrayList<>(); List<Object> result = new ArrayList<>();
for (String interopTestName : interopTests) { for (String interopTestName : interopTests) {
List<Story> stories = HpackJsonUtil.readStories(interopTestName); List<Story> stories = HpackJsonUtil.readStories(interopTestName);
for (Story story : stories) { for (Story story : stories) {
result.add(new Story[] {story}); result.add(story);
} }
} }
return result; return result;
@@ -55,19 +55,6 @@ public class HpackDecodeTestBase {
private final Buffer bytesIn = new Buffer(); private final Buffer bytesIn = new Buffer();
private final Hpack.Reader hpackReader = new Hpack.Reader(bytesIn, 4096); private final Hpack.Reader hpackReader = new Hpack.Reader(bytesIn, 4096);
private final Story story;
public HpackDecodeTestBase(Story story) {
this.story = story;
}
/**
* Expects wire to be set for all cases, and compares the decoder's output to expected headers.
*/
protected void testDecoder() throws Exception {
testDecoder(story);
}
protected void testDecoder(Story story) throws Exception { protected void testDecoder(Story story) throws Exception {
for (Case testCase : story.getCases()) { for (Case testCase : story.getCases()) {
bytesIn.write(testCase.getWire()); bytesIn.write(testCase.getWire());
@@ -88,8 +75,4 @@ public class HpackDecodeTestBase {
assertThat(new LinkedHashSet<>(observed)).overridingErrorMessage(message).isEqualTo( assertThat(new LinkedHashSet<>(observed)).overridingErrorMessage(message).isEqualTo(
new LinkedHashSet<>(expected)); new LinkedHashSet<>(expected));
} }
protected Story getStory() {
return story;
}
} }

View File

@@ -15,14 +15,15 @@
*/ */
package okhttp3.internal.http2; package okhttp3.internal.http2;
import java.util.Collection; import java.util.List;
import okhttp3.SimpleProvider;
import okhttp3.internal.http2.hpackjson.Case; import okhttp3.internal.http2.hpackjson.Case;
import okhttp3.internal.http2.hpackjson.Story; import okhttp3.internal.http2.hpackjson.Story;
import okio.Buffer; import okio.Buffer;
import org.junit.Assume; import org.jetbrains.annotations.NotNull;
import org.junit.Test; import org.junit.jupiter.api.Assumptions;
import org.junit.runner.RunWith; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.runners.Parameterized; import org.junit.jupiter.params.provider.ArgumentsSource;
/** /**
* Tests for round-tripping headers through hpack.. * Tests for round-tripping headers through hpack..
@@ -30,35 +31,31 @@ import org.junit.runners.Parameterized;
// TODO: update hpack-test-case with the output of our encoder. // TODO: update hpack-test-case with the output of our encoder.
// This test will hide complementary bugs in the encoder and decoder, // This test will hide complementary bugs in the encoder and decoder,
// We should test that the encoder is producing responses that are // We should test that the encoder is producing responses that are
// d]
@RunWith(Parameterized.class)
public class HpackRoundTripTest extends HpackDecodeTestBase { public class HpackRoundTripTest extends HpackDecodeTestBase {
private static final String[] RAW_DATA = {"raw-data"}; private static final String[] RAW_DATA = {"raw-data"};
@Parameterized.Parameters(name = "{0}") static class StoriesTestProvider extends SimpleProvider {
public static Collection<Story[]> getStories() throws Exception { @NotNull @Override public List<Object> arguments() throws Exception {
return createStories(RAW_DATA); return createStories(RAW_DATA);
}
} }
private Buffer bytesOut = new Buffer(); private Buffer bytesOut = new Buffer();
private Hpack.Writer hpackWriter = new Hpack.Writer(bytesOut); private Hpack.Writer hpackWriter = new Hpack.Writer(bytesOut);
public HpackRoundTripTest(Story story) { @ParameterizedTest
super(story); @ArgumentsSource(StoriesTestProvider.class)
} public void testRoundTrip(Story story) throws Exception {
Assumptions.assumeFalse(story == Story.MISSING, "Test stories missing, checkout git submodule");
@Test Story story2 = story.clone();
public void testRoundTrip() throws Exception {
Assume.assumeFalse("Test stories missing, checkout git submodule", getStory() == Story.MISSING);
Story story = getStory().clone();
// Mutate cases in base class. // Mutate cases in base class.
for (Case caze : story.getCases()) { for (Case caze : story2.getCases()) {
hpackWriter.writeHeaders(caze.getHeaders()); hpackWriter.writeHeaders(caze.getHeaders());
caze.setWire(bytesOut.readByteString()); caze.setWire(bytesOut.readByteString());
} }
testDecoder(story); testDecoder(story2);
} }
} }

View File

@@ -20,8 +20,8 @@ import java.util.ArrayDeque;
import java.util.Deque; import java.util.Deque;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import okio.Buffer; import okio.Buffer;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@@ -29,7 +29,7 @@ public final class ServerSentEventIteratorTest {
/** Either {@link Event} or {@link Long} items for events and retry changes, respectively. */ /** Either {@link Event} or {@link Long} items for events and retry changes, respectively. */
private final Deque<Object> callbacks = new ArrayDeque<>(); private final Deque<Object> callbacks = new ArrayDeque<>();
@After public void after() { @AfterEach public void after() {
assertThat(callbacks).isEmpty(); assertThat(callbacks).isEmpty();
} }

View File

@@ -18,10 +18,12 @@ package okhttp3
import org.junit.jupiter.api.extension.ExtensionContext import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.params.provider.Arguments import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.ArgumentsProvider import org.junit.jupiter.params.provider.ArgumentsProvider
import kotlin.jvm.Throws
abstract class SimpleProvider: ArgumentsProvider { abstract class SimpleProvider: ArgumentsProvider {
override fun provideArguments(context: ExtensionContext) = override fun provideArguments(context: ExtensionContext) =
arguments().map { Arguments.of(it) }.stream() arguments().map { Arguments.of(it) }.stream()
@Throws(Exception::class)
abstract fun arguments(): List<Any> abstract fun arguments(): List<Any>
} }

View File

@@ -3,6 +3,7 @@ apply plugin: 'kotlin-kapt'
dependencies { dependencies {
implementation project(':okhttp') implementation project(':okhttp')
implementation project(':mockwebserver-deprecated') implementation project(':mockwebserver-deprecated')
implementation project(':okhttp-testing-support')
implementation project(':okhttp-tls') implementation project(':okhttp-tls')
implementation deps.animalSniffer implementation deps.animalSniffer
implementation deps.moshi implementation deps.moshi

View File

@@ -15,18 +15,45 @@
*/ */
package okhttp3 package okhttp3
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ArgumentsSource
import java.io.File import java.io.File
import java.lang.reflect.InvocationTargetException import java.lang.reflect.InvocationTargetException
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
@RunWith(Parameterized::class) private val prefix = if (File("samples").exists()) "" else "../../"
@Ignore
class AllMainsTest(val className: String) { private fun mainFiles(): List<File> {
@Test val directories = listOf(
fun runMain() { "$prefix/samples/guide/src/main/java/okhttp3/guide",
"$prefix/samples/guide/src/main/java/okhttp3/recipes",
"$prefix/samples/guide/src/main/java/okhttp3/recipes/kt"
).map { File(it) }
return directories.flatMap {
it.listFiles().orEmpty().filter { f -> f.isFile }.toList()
}
}
internal class MainTestProvider : SimpleProvider() {
override fun arguments(): List<Any> {
val mainFiles = mainFiles()
return mainFiles.map {
val suffix = it.path.replace("${prefix}samples/guide/src/main/java/", "")
suffix.replace("(.*)\\.java".toRegex()) { mr ->
mr.groupValues[1].replace('/', '.')
}.replace("(.*)\\.kt".toRegex()) { mr ->
mr.groupValues[1].replace('/', '.') + "Kt"
}
}.sorted()
}
}
@Disabled("Don't run by default")
class AllMainsTest {
@ParameterizedTest
@ArgumentsSource(MainTestProvider::class)
fun runMain(className: String) {
val mainMethod = Class.forName(className) val mainMethod = Class.forName(className)
.methods.find { it.name == "main" } .methods.find { it.name == "main" }
try { try {
@@ -57,34 +84,4 @@ class AllMainsTest(val className: String) {
else -> false else -> false
} }
} }
companion object {
private val prefix = if (File("samples").exists()) "" else "../../"
@JvmStatic
@Parameterized.Parameters(name = "{0}")
fun data(): List<String> {
val mainFiles = mainFiles()
return mainFiles.map {
val suffix = it.path.replace("${prefix}samples/guide/src/main/java/", "")
suffix.replace("(.*)\\.java".toRegex()) { mr ->
mr.groupValues[1].replace('/', '.')
}.replace("(.*)\\.kt".toRegex()) { mr ->
mr.groupValues[1].replace('/', '.') + "Kt"
}
}.sorted()
}
private fun mainFiles(): List<File> {
val directories = listOf(
"$prefix/samples/guide/src/main/java/okhttp3/guide",
"$prefix/samples/guide/src/main/java/okhttp3/recipes",
"$prefix/samples/guide/src/main/java/okhttp3/recipes/kt"
).map { File(it) }
return directories.flatMap {
it.listFiles().orEmpty().filter { f -> f.isFile }.toList()
}
}
}
} }