Skip to content

Commit 26949cf

Browse files
yschimkesquarejesse
authored andcommitted
Fix samples and add test (#5228)
* Fix samples * support different paths * Simplify
1 parent 0dd201b commit 26949cf

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

samples/guide/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Samples
2+
=======
3.65 KB
Loading

samples/guide/src/main/java/okhttp3/recipes/PostMultipart.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public void run() throws Exception {
4040
.setType(MultipartBody.FORM)
4141
.addFormDataPart("title", "Square Logo")
4242
.addFormDataPart("image", "logo-square.png",
43-
RequestBody.create(new File("website/static/logo-square.png"), MEDIA_TYPE_PNG))
43+
RequestBody.create(
44+
new File("docs/images/logo-square.png"),
45+
MEDIA_TYPE_PNG))
4446
.build();
4547

4648
Request request = new Request.Builder()

samples/guide/src/main/java/okhttp3/recipes/WebSocketEcho.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package okhttp3.recipes;
22

33
import java.util.concurrent.TimeUnit;
4-
import okhttp3.WebSocket;
54
import okhttp3.OkHttpClient;
65
import okhttp3.Request;
76
import okhttp3.Response;
7+
import okhttp3.WebSocket;
88
import okhttp3.WebSocketListener;
99
import okio.ByteString;
1010

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (C) 2019 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package okhttp3
17+
18+
import org.junit.Test
19+
import org.junit.runner.RunWith
20+
import org.junit.runners.Parameterized
21+
import java.io.File
22+
import java.lang.reflect.InvocationTargetException
23+
24+
@RunWith(Parameterized::class)
25+
class AllMainsTest(val className: String) {
26+
@Test
27+
fun runMain() {
28+
val mainMethod = Class.forName(className).methods.find { it.name == "main" }
29+
try {
30+
mainMethod?.invoke(null, arrayOf<String>())
31+
} catch (ite: InvocationTargetException) {
32+
if (!expectedFailure(className, ite.cause!!)) {
33+
throw ite.cause!!
34+
}
35+
}
36+
}
37+
38+
private fun expectedFailure(className: String, cause: Throwable): Boolean {
39+
return when (className) {
40+
"okhttp3.recipes.CheckHandshake" -> true // by design
41+
"okhttp3.recipes.RequestBodyCompression" -> true // expired token
42+
else -> false
43+
}
44+
}
45+
46+
companion object {
47+
private val prefix = if (File("samples").exists()) "" else "../../"
48+
49+
@JvmStatic
50+
@Parameterized.Parameters(name = "{0}")
51+
fun data(): List<String> {
52+
val mainFiles = mainFiles()
53+
return mainFiles.map {
54+
it.path.substring("$prefix/samples/guide/src/main/java".length, it.path.length - 5).replace('/', '.')
55+
}.sorted()
56+
}
57+
58+
private fun mainFiles(): List<File> {
59+
return File("$prefix/samples/guide/src/main/java/okhttp3").listFiles()?.flatMap {
60+
it?.listFiles()?.toList().orEmpty()
61+
}.orEmpty()
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)