summaryrefslogtreecommitdiff
path: root/test/java/com/google/devtools/build/android/desugar/runtime/ThrowableExtensionTestUtility.java
blob: 489bd7a7dc1826c0e2d44d6964a48dc81bdc97f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// 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 com.google.devtools.build.android.desugar.runtime;

import static com.google.common.truth.Truth.assertThat;

import java.lang.reflect.Method;

/**
 * A utility class for testing ThrowableExtension. It uses reflection to get the strategy name, so
 * as to avoid dependency on the runtime library. This is beneficial, because we can test whether
 * the runtime library is on the classpath.
 */
public class ThrowableExtensionTestUtility {

  private static final String SYSTEM_PROPERTY_EXPECTED_STRATEGY = "expected.strategy";

  public static String getTwrStrategyClassNameSpecifiedInSystemProperty() {
    String className = unquote(System.getProperty(SYSTEM_PROPERTY_EXPECTED_STRATEGY));
    assertThat(className).isNotEmpty();
    return className;
  }

  private static final String THROWABLE_EXTENSION_CLASS_NAME =
      "com.google.devtools.build.android.desugar.runtime.ThrowableExtension";

  private static boolean isStrategyOfClass(String className) {
    return getStrategyClassName().equals(className);
  }

  public static String getStrategyClassName() {
    try {
      Class<?> klass = Class.forName(THROWABLE_EXTENSION_CLASS_NAME);
      Method method = klass.getMethod("getStrategy");
      Object strategy = method.invoke(null);
      return strategy.getClass().getName();
    } catch (Throwable e) {
      throw new AssertionError(e);
    }
  }

  public static boolean isMimicStrategy() {
    return isStrategyOfClass(THROWABLE_EXTENSION_CLASS_NAME + "$MimicDesugaringStrategy");
  }

  public static boolean isNullStrategy() {
    return isStrategyOfClass(THROWABLE_EXTENSION_CLASS_NAME + "$NullDesugaringStrategy");
  }

  public static boolean isReuseStrategy() {
    return isStrategyOfClass(THROWABLE_EXTENSION_CLASS_NAME + "$ReuseDesugaringStrategy");
  }

  private static String unquote(String s) {
    if (s.startsWith("'") || s.startsWith("\"")) {
      assertThat(s).endsWith(s.substring(0, 1));
      return s.substring(1, s.length() - 1);
    } else {
      return s;
    }
  }
}