aboutsummaryrefslogtreecommitdiff
path: root/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/ParameterValueParsing.java
blob: 130c186775e60f6c74ac86fc6b521b6b4b08fe68 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * Copyright 2021 Google 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 com.google.testing.junit.testparameterinjector.junit5;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.common.primitives.Primitives;
import com.google.common.primitives.UnsignedLong;
import com.google.common.reflect.TypeToken;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.reflect.Array;
import java.lang.reflect.ParameterizedType;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nullable;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

/** A helper class for parsing parameter values from strings. */
final class ParameterValueParsing {

  @SuppressWarnings("unchecked")
  static <E extends Enum<E>> Enum<?> parseEnum(String str, Class<?> enumType) {
    return Enum.valueOf((Class<E>) enumType, str);
  }

  static boolean isValidYamlString(String yamlString) {
    try {
      new Yaml(new SafeConstructor(new LoaderOptions())).load(yamlString);
      return true;
    } catch (RuntimeException e) {
      return false;
    }
  }

  static Object parseYamlStringToJavaType(String yamlString, Class<?> javaType) {
    return parseYamlObjectToJavaType(parseYamlStringToObject(yamlString), TypeToken.of(javaType));
  }

  static Object parseYamlStringToObject(String yamlString) {
    return new Yaml(new SafeConstructor(new LoaderOptions())).load(yamlString);
  }

  private static UnsignedLong parseYamlSignedLongToUnsignedLong(long number) {
    checkState(number >= 0, "%s should be greater than or equal to zero", number);
    return UnsignedLong.fromLongBits(number);
  }

  @SuppressWarnings({"unchecked"})
  static Object parseYamlObjectToJavaType(Object parsedYaml, TypeToken<?> javaType) {
    // Pass along null so we don't have to worry about it below
    if (parsedYaml == null) {
      return null;
    }

    YamlValueTransformer yamlValueTransformer =
        new YamlValueTransformer(parsedYaml, javaType.getRawType());

    yamlValueTransformer
        .ifJavaType(String.class)
        .supportParsedType(String.class, self -> self)
        // Also support other primitives because it's easy to accidentally write e.g. a number when
        // a string was intended in YAML
        .supportParsedType(Boolean.class, Object::toString)
        .supportParsedType(Integer.class, Object::toString)
        .supportParsedType(Long.class, Object::toString)
        .supportParsedType(Double.class, Object::toString);

    yamlValueTransformer.ifJavaType(Boolean.class).supportParsedType(Boolean.class, self -> self);

    yamlValueTransformer.ifJavaType(Integer.class).supportParsedType(Integer.class, self -> self);

    yamlValueTransformer
        .ifJavaType(Long.class)
        .supportParsedType(Long.class, self -> self)
        .supportParsedType(Integer.class, Integer::longValue);

    yamlValueTransformer
        .ifJavaType(UnsignedLong.class)
        .supportParsedType(Long.class, self -> parseYamlSignedLongToUnsignedLong(self.longValue()))
        .supportParsedType(
            Integer.class, self -> parseYamlSignedLongToUnsignedLong(self.longValue()))
        // UnsignedLong::valueOf(BigInteger) will validate that BigInteger is in the valid range and
        // throws otherwise.
        .supportParsedType(BigInteger.class, UnsignedLong::valueOf);

    yamlValueTransformer
        .ifJavaType(BigInteger.class)
        .supportParsedType(Long.class, self -> BigInteger.valueOf(self.longValue()))
        .supportParsedType(Integer.class, self -> BigInteger.valueOf(self.longValue()))
        .supportParsedType(BigInteger.class, self -> self);

    yamlValueTransformer
        .ifJavaType(Float.class)
        .supportParsedType(Float.class, self -> self)
        .supportParsedType(Double.class, Double::floatValue)
        .supportParsedType(Integer.class, Integer::floatValue)
        .supportParsedType(String.class, Float::valueOf);

    yamlValueTransformer
        .ifJavaType(Double.class)
        .supportParsedType(Double.class, self -> self)
        .supportParsedType(Integer.class, Integer::doubleValue)
        .supportParsedType(Long.class, Long::doubleValue)
        .supportParsedType(String.class, Double::valueOf);

    yamlValueTransformer
        .ifJavaType(Enum.class)
        .supportParsedType(
            String.class, str -> ParameterValueParsing.parseEnum(str, javaType.getRawType()));

    yamlValueTransformer
        .ifJavaType(byte[].class)
        .supportParsedType(byte[].class, self -> self)
        // Uses String based charset because StandardCharsets was not introduced until later
        // versions of Android
        // See https://developer.android.com/reference/java/nio/charset/StandardCharsets.
        .supportParsedType(String.class, s -> s.getBytes(Charset.forName("UTF-8")));

    if (ByteStringReflection.MAYBE_BYTE_STRING_CLASS.isPresent()) {
      yamlValueTransformer
          .ifJavaType((Class<Object>) ByteStringReflection.MAYBE_BYTE_STRING_CLASS.get())
          .supportParsedType(String.class, ByteStringReflection::copyFromUtf8)
          .supportParsedType(byte[].class, ByteStringReflection::copyFrom);
    }

    // Added mainly for protocol buffer parsing
    yamlValueTransformer
        .ifJavaType(List.class)
        .supportParsedType(
            List.class,
            list ->
                Lists.transform(
                    list,
                    e ->
                        parseYamlObjectToJavaType(
                            e, getGenericParameterType(javaType, /* parameterIndex= */ 0))));
    yamlValueTransformer
        .ifJavaType(Map.class)
        .supportParsedType(Map.class, map -> parseYamlMapToJavaMap(map, javaType));

    return yamlValueTransformer.transformedJavaValue();
  }

  private static Map<?, ?> parseYamlMapToJavaMap(Map<?, ?> map, TypeToken<?> javaType) {
    Map<Object, Object> returnedMap = new LinkedHashMap<>();
    for (Entry<?, ?> entry : map.entrySet()) {
      returnedMap.put(
          parseYamlObjectToJavaType(
              entry.getKey(), getGenericParameterType(javaType, /* parameterIndex= */ 0)),
          parseYamlObjectToJavaType(
              entry.getValue(), getGenericParameterType(javaType, /* parameterIndex= */ 1)));
    }
    return returnedMap;
  }

  private static TypeToken<?> getGenericParameterType(TypeToken<?> typeToken, int parameterIndex) {
    checkArgument(
        typeToken.getType() instanceof ParameterizedType,
        "Could not parse the generic parameter of type %s",
        typeToken);

    ParameterizedType parameterizedType = (ParameterizedType) typeToken.getType();
    return TypeToken.of(parameterizedType.getActualTypeArguments()[parameterIndex]);
  }

  private static final class YamlValueTransformer {
    private final Object parsedYaml;
    private final Class<?> javaType;
    @Nullable private Object transformedJavaValue;

    YamlValueTransformer(Object parsedYaml, Class<?> javaType) {
      this.parsedYaml = parsedYaml;
      this.javaType = javaType;
    }

    <JavaT> SupportedJavaType<JavaT> ifJavaType(Class<JavaT> supportedJavaType) {
      return new SupportedJavaType<>(supportedJavaType);
    }

    Object transformedJavaValue() {
      checkArgument(
          transformedJavaValue != null,
          "Could not map YAML value %s (class = %s) to java class %s",
          parsedYaml,
          parsedYaml.getClass(),
          javaType);
      return transformedJavaValue;
    }

    final class SupportedJavaType<JavaT> {

      private final Class<JavaT> supportedJavaType;

      private SupportedJavaType(Class<JavaT> supportedJavaType) {
        this.supportedJavaType = supportedJavaType;
      }

      @SuppressWarnings("unchecked")
      @CanIgnoreReturnValue
      <ParsedYamlT> SupportedJavaType<JavaT> supportParsedType(
          Class<ParsedYamlT> parsedYamlType, Function<ParsedYamlT, JavaT> transformation) {
        if (Primitives.wrap(supportedJavaType).isAssignableFrom(Primitives.wrap(javaType))) {
          if (Primitives.wrap(parsedYamlType).isInstance(parsedYaml)) {
            checkState(
                transformedJavaValue == null,
                "This case is already handled. This is a bug in"
                    + " testparameterinjector.TestParametersMethodProcessor.");
            try {
              transformedJavaValue = checkNotNull(transformation.apply((ParsedYamlT) parsedYaml));
            } catch (Exception e) {
              throw new IllegalArgumentException(
                  String.format(
                      "Could not map YAML value %s (class = %s) to java class %s",
                      parsedYaml, parsedYaml.getClass(), javaType),
                  e);
            }
          }
        }

        return this;
      }
    }
  }

  static String formatTestNameString(Optional<String> parameterName, @Nullable Object value) {
    Object unwrappedValue;
    Optional<String> customName;

    if (value instanceof TestParameterValue) {
      TestParameterValue tpValue = (TestParameterValue) value;
      unwrappedValue = tpValue.getWrappedValue();
      customName = tpValue.getCustomName();
    } else {
      unwrappedValue = value;
      customName = Optional.absent();
    }

    String result = customName.or(() -> valueAsString(unwrappedValue));
    if (parameterName.isPresent() && !customName.isPresent()) {
      if (unwrappedValue == null
          ||
          // Primitives are often ambiguous
          Primitives.unwrap(unwrappedValue.getClass()).isPrimitive()
          // Ambiguous String cases
          || unwrappedValue.equals("null")
          || (unwrappedValue instanceof CharSequence
              && CharMatcher.anyOf("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
                  .matchesNoneOf((CharSequence) unwrappedValue))) {
        // Prefix the parameter value with its field name. This is to avoid test names
        // such as myMethod_success[true,false,2]. Instead, it'll be
        // myMethod_success[dryRun=true,experimentFlag=false,retries=2].
        result = String.format("%s=%s", parameterName.get(), valueAsString(unwrappedValue));
      }
    }
    return result.trim().replaceAll("\\s+", " ");
  }

  private static String valueAsString(Object value) {
    if (value != null && value.getClass().isArray()) {
      StringBuilder resultBuider = new StringBuilder();
      resultBuider.append("[");
      for (int i = 0; i < Array.getLength(value); i++) {
        if (i > 0) {
          resultBuider.append(", ");
        }
        resultBuider.append(Array.get(value, i));
      }
      resultBuider.append("]");
      return resultBuider.toString();
    } else if (ByteStringReflection.isInstanceOfByteString(value)) {
      return Arrays.toString(ByteStringReflection.byteStringToByteArray(value));
    } else {
      return String.valueOf(value);
    }
  }

  private ParameterValueParsing() {}
}