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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.junit.Assert.assertFalse;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertTrue;
  19. import static org.junit.Assert.fail;
  20. import java.lang.reflect.Field;
  21. import java.lang.reflect.Method;
  22. import java.security.AccessController;
  23. import java.security.PrivilegedActionException;
  24. import java.security.PrivilegedExceptionAction;
  25. import java.util.Map;
  26. import org.apache.poi.util.SuppressForbidden;
  27. /**
  28. * Util class for POI JUnit TestCases, which provide additional features
  29. */
  30. public final class POITestCase {
  31. public static void assertContains(String haystack, String needle) {
  32. assertNotNull(haystack);
  33. assertTrue(
  34. "Unable to find expected text '" + needle + "' in text:\n" + haystack,
  35. haystack.contains(needle)
  36. );
  37. }
  38. public static void assertNotContained(String haystack, String needle) {
  39. assertNotNull(haystack);
  40. assertFalse(
  41. "Unexpectedly found text '" + needle + "' in text:\n" + haystack,
  42. haystack.contains(needle)
  43. );
  44. }
  45. /**
  46. * @param map haystack
  47. * @param key needle
  48. */
  49. public static <T> void assertContains(Map<T, ?> map, T key) {
  50. if (map.containsKey(key)) {
  51. return;
  52. }
  53. fail("Unable to find " + key + " in " + map);
  54. }
  55. /**
  56. * Utility method to get the value of a private/protected field.
  57. * Only use this method in test cases!!!
  58. */
  59. public static <R,T> R getFieldValue(final Class<? super T> clazz, final T instance, final Class<R> fieldType, final String fieldName) {
  60. assertTrue("Reflection of private fields is only allowed for POI classes.", clazz.getName().startsWith("org.apache.poi."));
  61. try {
  62. return AccessController.doPrivileged(new PrivilegedExceptionAction<R>() {
  63. @Override
  64. @SuppressWarnings("unchecked")
  65. @SuppressForbidden("For test usage only")
  66. public R run() throws Exception {
  67. Field f = clazz.getDeclaredField(fieldName);
  68. f.setAccessible(true);
  69. return (R) f.get(instance);
  70. }
  71. });
  72. } catch (PrivilegedActionException pae) {
  73. throw new RuntimeException("Cannot access field '" + fieldName + "' of class " + clazz, pae.getException());
  74. }
  75. }
  76. /**
  77. * Utility method to call a private/protected method.
  78. * Only use this method in test cases!!!
  79. */
  80. public static <R,T> R callMethod(final Class<? super T> clazz, final T instance, final Class<R> returnType, final String methodName,
  81. final Class<?>[] parameterTypes, final Object[] parameters) {
  82. assertTrue("Reflection of private methods is only allowed for POI classes.", clazz.getName().startsWith("org.apache.poi."));
  83. try {
  84. return AccessController.doPrivileged(new PrivilegedExceptionAction<R>() {
  85. @Override
  86. @SuppressWarnings("unchecked")
  87. @SuppressForbidden("For test usage only")
  88. public R run() throws Exception {
  89. Method m = clazz.getDeclaredMethod(methodName, parameterTypes);
  90. m.setAccessible(true);
  91. return (R) m.invoke(instance, parameters);
  92. }
  93. });
  94. } catch (PrivilegedActionException pae) {
  95. throw new RuntimeException("Cannot access method '" + methodName + "' of class " + clazz, pae.getException());
  96. }
  97. }
  98. }