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.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 org.dcevm.test;
  25. import junit.framework.Assert;
  26. import org.dcevm.HotSwapTool;
  27. import static org.dcevm.test.util.HotSwapTestHelper.__toVersion__;
  28. /**
  29. * Utility methods for unit testing.
  30. *
  31. * @author Thomas Wuerthinger
  32. */
  33. public class TestUtil {
  34. public static final boolean LIGHT = true;
  35. public static int assertException(Class exceptionClass, Runnable run) {
  36. try {
  37. run.run();
  38. } catch (Throwable t) {
  39. if (t.getClass().equals(exceptionClass)) {
  40. return t.getStackTrace()[0].getLineNumber();
  41. }
  42. Assert.assertTrue("An exception of type " + t.getClass().getSimpleName() + " instead of " + exceptionClass.getSimpleName() + " has been thrown!", false);
  43. }
  44. Assert.assertTrue("No exception has been thrown!", false);
  45. return -1;
  46. }
  47. public static int assertUnsupportedWithLight(Runnable run) {
  48. if (TestUtil.LIGHT) {
  49. return assertUnsupported(run);
  50. }
  51. run.run();
  52. return -1;
  53. }
  54. public static int assertUnsupported(Runnable run) {
  55. return assertException(UnsupportedOperationException.class, run);
  56. }
  57. public static void assertUnsupportedToVersion(final Class clazz, final int version) {
  58. TestUtil.assertUnsupported(new Runnable() {
  59. @Override
  60. public void run() {
  61. HotSwapTool.toVersion(clazz, version);
  62. }
  63. });
  64. }
  65. public static void assertUnsupportedToVersionWithLight(final Class clazz, final int version) {
  66. TestUtil.assertUnsupportedWithLight(new Runnable() {
  67. @Override
  68. public void run() {
  69. HotSwapTool.toVersion(clazz, version);
  70. }
  71. });
  72. }
  73. }