|
| 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