You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

POITestCase.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi;
  16. import static org.hamcrest.CoreMatchers.containsString;
  17. import static org.hamcrest.CoreMatchers.endsWith;
  18. import static org.hamcrest.CoreMatchers.hasItem;
  19. import static org.hamcrest.CoreMatchers.not;
  20. import static org.hamcrest.CoreMatchers.startsWith;
  21. import static org.hamcrest.MatcherAssert.assertThat;
  22. import static org.junit.jupiter.api.Assertions.assertNotNull;
  23. import static org.junit.jupiter.api.Assertions.assertTrue;
  24. import java.io.File;
  25. import java.lang.reflect.Field;
  26. import java.security.AccessController;
  27. import java.security.PrivilegedActionException;
  28. import java.security.PrivilegedExceptionAction;
  29. import java.util.Locale;
  30. import java.util.Map;
  31. import java.util.Set;
  32. import javax.imageio.ImageIO;
  33. import org.apache.poi.util.Internal;
  34. import org.apache.poi.util.SuppressForbidden;
  35. import org.mockito.internal.matchers.apachecommons.ReflectionEquals;
  36. /**
  37. * Util class for POI JUnit TestCases, which provide additional features
  38. */
  39. @Internal
  40. @SuppressWarnings("java:S2187")
  41. public final class POITestCase {
  42. private POITestCase() {
  43. }
  44. public static void assertStartsWith(String string, String prefix) {
  45. assertNotNull(string);
  46. assertNotNull(prefix);
  47. assertThat(string, startsWith(prefix));
  48. }
  49. public static void assertStartsWith(String message, String string, String prefix) {
  50. assertNotNull(message, string);
  51. assertNotNull(message, prefix);
  52. assertThat(message, string, startsWith(prefix));
  53. }
  54. public static void assertEndsWith(String string, String suffix) {
  55. assertNotNull(string);
  56. assertNotNull(suffix);
  57. assertThat(string, endsWith(suffix));
  58. }
  59. public static void assertContains(String haystack, String needle) {
  60. assertNotNull(haystack);
  61. assertNotNull(needle);
  62. assertThat(haystack, containsString(needle));
  63. }
  64. public static void assertContains(String message, String haystack, String needle) {
  65. assertNotNull(message, haystack);
  66. assertNotNull(message, needle);
  67. assertThat(message, haystack, containsString(needle));
  68. }
  69. public static void assertContainsIgnoreCase(String haystack, String needle, Locale locale) {
  70. assertNotNull(haystack);
  71. assertNotNull(needle);
  72. String hay = haystack.toLowerCase(locale);
  73. String n = needle.toLowerCase(locale);
  74. assertTrue(hay.contains(n), "Unable to find expected text '" + needle + "' in text:\n" + haystack);
  75. }
  76. public static void assertContainsIgnoreCase(String haystack, String needle) {
  77. assertContainsIgnoreCase(haystack, needle, Locale.ROOT);
  78. }
  79. public static void assertNotContained(String haystack, String needle) {
  80. assertNotNull(haystack);
  81. assertNotNull(needle);
  82. assertThat(haystack, not(containsString(needle)));
  83. }
  84. /**
  85. * @param map haystack
  86. * @param key needle
  87. */
  88. public static <T> void assertContains(Map<T, ?> map, T key) {
  89. assertTrue(map.containsKey(key), "Unable to find " + key + " in " + map);
  90. }
  91. public static <T> void assertNotContained(Set<T> set, T element) {
  92. assertThat("Set should not contain " + element, set, not(hasItem(element)));
  93. }
  94. /**
  95. * Utility method to get the value of a private/protected field.
  96. * Only use this method in test cases!!!
  97. */
  98. @SuppressWarnings({"unused", "unchecked"})
  99. @SuppressForbidden("For test usage only")
  100. public static <R,T> R getFieldValue(final Class<? super T> clazz, final T instance, final Class<R> fieldType, final String fieldName) {
  101. assertTrue(clazz.getName().startsWith("org.apache.poi."), "Reflection of private fields is only allowed for POI classes.");
  102. try {
  103. return AccessController.doPrivileged((PrivilegedExceptionAction<R>) () -> {
  104. Field f = clazz.getDeclaredField(fieldName);
  105. f.setAccessible(true);
  106. return (R) f.get(instance);
  107. });
  108. } catch (PrivilegedActionException pae) {
  109. throw new RuntimeException("Cannot access field '" + fieldName + "' of class " + clazz, pae.getException());
  110. }
  111. }
  112. /**
  113. * Utility method to shallow compare all fields of the objects
  114. * Only use this method in test cases!!!
  115. */
  116. public static void assertReflectEquals(final Object expected, Object actual) {
  117. // as long as ReflectionEquals is provided by Mockito, use it ... otherwise use commons.lang for the tests
  118. // JaCoCo Code Coverage adds its own field, don't look at this one here
  119. assertTrue(new ReflectionEquals(expected, "$jacocoData").matches(actual));
  120. }
  121. /**
  122. * Ensures that the temporary directory is defined and exists and
  123. * ensures ImageIO uses this directory for cache-files
  124. */
  125. public static void setImageIOCacheDir() {
  126. final String tmpDirProperty = System.getProperty("java.io.tmpdir");
  127. if(tmpDirProperty == null || "".equals(tmpDirProperty)) {
  128. return;
  129. }
  130. // ensure that temp-dir exists because ImageIO requires it
  131. File tmpDir = new File(tmpDirProperty);
  132. if(!tmpDir.exists() && !tmpDir.mkdirs()) {
  133. throw new IllegalStateException("Could not create temporary directory " + tmpDirProperty + ", full path " + tmpDir.getAbsolutePath());
  134. }
  135. ImageIO.setCacheDirectory(tmpDir);
  136. }
  137. }