Skip to content

Commit

Permalink
Fix samples and add test (square#5228)
Browse files Browse the repository at this point in the history
* Fix samples

* support different paths

* Simplify
  • Loading branch information
yschimke authored and squarejesse committed Jun 24, 2019
1 parent 0dd201b commit 26949cf
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
2 changes: 2 additions & 0 deletions samples/guide/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Samples
=======
Binary file added samples/guide/docs/images/logo-square.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public void run() throws Exception {
.setType(MultipartBody.FORM)
.addFormDataPart("title", "Square Logo")
.addFormDataPart("image", "logo-square.png",
RequestBody.create(new File("website/static/logo-square.png"), MEDIA_TYPE_PNG))
RequestBody.create(
new File("docs/images/logo-square.png"),
MEDIA_TYPE_PNG))
.build();

Request request = new Request.Builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package okhttp3.recipes;

import java.util.concurrent.TimeUnit;
import okhttp3.WebSocket;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;

Expand Down
64 changes: 64 additions & 0 deletions samples/guide/src/test/kotlin/okhttp3/AllMainsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2019 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3

import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import java.io.File
import java.lang.reflect.InvocationTargetException

@RunWith(Parameterized::class)
class AllMainsTest(val className: String) {
@Test
fun runMain() {
val mainMethod = Class.forName(className).methods.find { it.name == "main" }
try {
mainMethod?.invoke(null, arrayOf<String>())
} catch (ite: InvocationTargetException) {
if (!expectedFailure(className, ite.cause!!)) {
throw ite.cause!!
}
}
}

private fun expectedFailure(className: String, cause: Throwable): Boolean {
return when (className) {
"okhttp3.recipes.CheckHandshake" -> true // by design
"okhttp3.recipes.RequestBodyCompression" -> true // expired token
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 {
it.path.substring("$prefix/samples/guide/src/main/java".length, it.path.length - 5).replace('/', '.')
}.sorted()
}

private fun mainFiles(): List<File> {
return File("$prefix/samples/guide/src/main/java/okhttp3").listFiles()?.flatMap {
it?.listFiles()?.toList().orEmpty()
}.orEmpty()
}
}
}

0 comments on commit 26949cf

Please sign in to comment.