View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3;
19  
20  import static org.junit.jupiter.api.Assumptions.assumeTrue;
21  
22  import java.text.ParseException;
23  import java.util.Arrays;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Locale;
27  import java.util.Map;
28  
29  /**
30   * Offers tests a way to skip problematic Locales with a JUnit assumption.
31   */
32  public class LocaleProblems {
33  
34      // needs a better name.
35      private static final Map<JavaVersion, List<String>> UNSUPPORTED_CAT_A;
36      static {
37          UNSUPPORTED_CAT_A = new HashMap<>();
38          // "cv_RU" is a problem on Java version: 26-beta, vendor: Eclipse Adoptium, runtime: /opt/hostedtoolcache/Java_Temurin-Hotspot_jdk/26.0.0-ea.27.0.ea/x64
39          // Same for "cv"
40          UNSUPPORTED_CAT_A.put(JavaVersion.JAVA_26, Arrays.asList("cv", "cv_RU"));
41      }
42  
43      // needs a better name.
44      private static final Map<JavaVersion, List<String>> UNSUPPORTED_CAT_B;
45      static {
46          UNSUPPORTED_CAT_B = new HashMap<>();
47          // "cv_RU_#Cyrl" is a problem on Java version: 26-beta, vendor: Eclipse Adoptium, runtime: /opt/hostedtoolcache/Java_Temurin-Hotspot_jdk/26.0.0-ea.27.0.ea/x64
48          UNSUPPORTED_CAT_B.put(JavaVersion.JAVA_26, Arrays.asList("cv", "cv_RU", "cv_RU_#Cyrl"));
49      }
50  
51      private static void assumeLocaleSupported(final Locale locale, final Map<JavaVersion, List<String>> unsupportedCatA, final ParseException e) {
52          final boolean supported = isSupported(locale, unsupportedCatA);
53          if (!supported) {
54              System.out.printf("Failing test assumption for locale '%s' on Java version %s.%n", locale, SystemUtils.JAVA_SPECIFICATION_VERSION_ENUM);
55              if (e != null) {
56                  // Not the whole stack trace, just the exception message.
57                  System.out.printf("\t%s%n", e);
58              }
59          }
60          assumeTrue(supported);
61      }
62  
63      public static void assumeLocaleSupportedA(final Locale locale) {
64          assumeLocaleSupported(locale, UNSUPPORTED_CAT_A, null);
65      }
66  
67      public static void assumeLocaleSupportedB(final Locale locale, final ParseException e) {
68          assumeLocaleSupported(locale, UNSUPPORTED_CAT_B, e);
69      }
70  
71      private static boolean isSupported(final Locale locale, final Map<JavaVersion, List<String>> unsupported) {
72          final List<String> list = unsupported.get(SystemUtils.JAVA_SPECIFICATION_VERSION_ENUM);
73          return list == null || !list.contains(locale.toString());
74      }
75  }