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.

TestUtil.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. *
  23. */
  24. package com.github.dcevm.test;
  25. import junit.framework.Assert;
  26. import com.github.dcevm.HotSwapTool;
  27. /**
  28. * Utility methods for unit testing.
  29. *
  30. * @author Thomas Wuerthinger
  31. */
  32. public class TestUtil {
  33. public static final boolean LIGHT = true;
  34. public static int assertException(Class exceptionClass, Runnable run) {
  35. try {
  36. run.run();
  37. } catch (Throwable t) {
  38. if (t.getClass().equals(exceptionClass)) {
  39. return t.getStackTrace()[0].getLineNumber();
  40. }
  41. Assert.assertTrue("An exception of type " + t.getClass().getSimpleName() + " instead of " + exceptionClass.getSimpleName() + " has been thrown!", false);
  42. }
  43. Assert.assertTrue("No exception has been thrown!", false);
  44. return -1;
  45. }
  46. public static int assertUnsupportedWithLight(Runnable run) {
  47. if (TestUtil.LIGHT) {
  48. return assertUnsupported(run);
  49. }
  50. run.run();
  51. return -1;
  52. }
  53. public static int assertUnsupported(Runnable run) {
  54. return assertException(UnsupportedOperationException.class, run);
  55. }
  56. public static void assertUnsupportedToVersion(final Class clazz, final int version) {
  57. TestUtil.assertUnsupported(new Runnable() {
  58. @Override
  59. public void run() {
  60. HotSwapTool.toVersion(clazz, version);
  61. }
  62. });
  63. }
  64. public static void assertUnsupportedToVersionWithLight(final Class clazz, final int version) {
  65. TestUtil.assertUnsupportedWithLight(new Runnable() {
  66. @Override
  67. public void run() {
  68. HotSwapTool.toVersion(clazz, version);
  69. }
  70. });
  71. }
  72. }