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.util.List;
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_AAAA;
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 {
@Test public void testGoogleDotComEncoding() {
@@ -75,7 +75,7 @@ public class DnsRecordCodecTest {
+ "0000000100000e7364666c6b686673646c6b6a64660265650000010001c01b00060001000007070038026e"
+ "7303746c64c01b0a686f73746d61737465720d6565737469696e7465726e6574c01b5adb12c100000e1000"
+ "0003840012750000000e10"));
fail();
fail("");
} catch (UnknownHostException uhe) {
assertThat(uhe.getMessage()).isEqualTo("sdflkhfsdlkjdf.ee: NXDOMAIN");
}

View File

@@ -15,31 +15,29 @@
*/
package okhttp3.internal.http2;
import java.util.Collection;
import java.util.List;
import okhttp3.SimpleProvider;
import okhttp3.internal.http2.hpackjson.Story;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import static okhttp3.internal.http2.hpackjson.HpackJsonUtil.storiesForCurrentDraft;
@RunWith(Parameterized.class)
public class HpackDecodeInteropTest extends HpackDecodeTestBase {
public HpackDecodeInteropTest(Story story) {
super(story);
@ParameterizedTest
@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}")
public static Collection<Story[]> createStories() throws Exception {
static class StoriesTestProvider extends SimpleProvider {
@NotNull @Override public List<Object> arguments() throws Exception {
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.
*/
protected static Collection<Story[]> createStories(String[] interopTests)
protected static List<Object> createStories(String[] interopTests)
throws Exception {
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) {
List<Story> stories = HpackJsonUtil.readStories(interopTestName);
for (Story story : stories) {
result.add(new Story[] {story});
result.add(story);
}
}
return result;
@@ -55,19 +55,6 @@ public class HpackDecodeTestBase {
private final Buffer bytesIn = new Buffer();
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 {
for (Case testCase : story.getCases()) {
bytesIn.write(testCase.getWire());
@@ -88,8 +75,4 @@ public class HpackDecodeTestBase {
assertThat(new LinkedHashSet<>(observed)).overridingErrorMessage(message).isEqualTo(
new LinkedHashSet<>(expected));
}
protected Story getStory() {
return story;
}
}

View File

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

View File

@@ -20,8 +20,8 @@ import java.util.ArrayDeque;
import java.util.Deque;
import javax.annotation.Nullable;
import okio.Buffer;
import org.junit.After;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
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. */
private final Deque<Object> callbacks = new ArrayDeque<>();
@After public void after() {
@AfterEach public void after() {
assertThat(callbacks).isEmpty();
}

View File

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

View File

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

View File

@@ -15,18 +15,45 @@
*/
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.lang.reflect.InvocationTargetException
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
@RunWith(Parameterized::class)
@Ignore
class AllMainsTest(val className: String) {
@Test
fun runMain() {
private val prefix = if (File("samples").exists()) "" else "../../"
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()
}
}
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)
.methods.find { it.name == "main" }
try {
@@ -57,34 +84,4 @@ class AllMainsTest(val className: String) {
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()
}
}
}
}