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 package org.graalvm.compiler.test;
24
25 import java.io.PrintStream;
26 import java.io.PrintWriter;
27 import java.lang.reflect.Field;
28 import java.lang.reflect.Method;
29 import java.util.Arrays;
30
31 import org.junit.Assert;
32 import org.junit.internal.ComparisonCriteria;
33 import org.junit.internal.ExactComparisonCriteria;
34
35 import sun.misc.Unsafe;
36
37 /**
38 * Base class that contains common utility methods and classes useful in unit tests.
39 */
40 public class GraalTest {
41
42 public static final Unsafe UNSAFE;
43 static {
44 try {
45 Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
46 theUnsafe.setAccessible(true);
47 UNSAFE = (Unsafe) theUnsafe.get(Unsafe.class);
48 } catch (Exception e) {
49 throw new RuntimeException("exception while trying to get Unsafe", e);
50 }
348 * @param message the identifying message for the {@link AssertionError}
349 * @param objects arguments to the format string
350 */
351 public static void assertTrue(boolean condition, String message, Object... objects) {
352 if (!condition) {
353 fail(message, objects);
354 }
355 }
356
357 /**
358 * Asserts that a condition is false. If it isn't it throws an {@link AssertionError} with the
359 * given message produced by {@link String#format}.
360 *
361 * @param condition condition to be checked
362 * @param message the identifying message for the {@link AssertionError}
363 * @param objects arguments to the format string
364 */
365 public static void assertFalse(boolean condition, String message, Object... objects) {
366 assertTrue(!condition, message, objects);
367 }
368 }
|
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 package org.graalvm.compiler.test;
24
25 import static org.graalvm.compiler.debug.DebugContext.DEFAULT_LOG_STREAM;
26 import static org.graalvm.compiler.debug.DebugContext.NO_DESCRIPTION;
27 import static org.graalvm.compiler.debug.DebugContext.NO_GLOBAL_METRIC_VALUES;
28
29 import java.io.PrintStream;
30 import java.io.PrintWriter;
31 import java.lang.reflect.Field;
32 import java.lang.reflect.Method;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.List;
38
39 import org.graalvm.compiler.debug.DebugHandlersFactory;
40 import org.graalvm.compiler.debug.DebugContext;
41 import org.graalvm.compiler.debug.DebugDumpHandler;
42 import org.graalvm.compiler.options.OptionValues;
43 import org.junit.After;
44 import org.junit.Assert;
45 import org.junit.internal.ComparisonCriteria;
46 import org.junit.internal.ExactComparisonCriteria;
47
48 import sun.misc.Unsafe;
49
50 /**
51 * Base class that contains common utility methods and classes useful in unit tests.
52 */
53 public class GraalTest {
54
55 public static final Unsafe UNSAFE;
56 static {
57 try {
58 Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
59 theUnsafe.setAccessible(true);
60 UNSAFE = (Unsafe) theUnsafe.get(Unsafe.class);
61 } catch (Exception e) {
62 throw new RuntimeException("exception while trying to get Unsafe", e);
63 }
361 * @param message the identifying message for the {@link AssertionError}
362 * @param objects arguments to the format string
363 */
364 public static void assertTrue(boolean condition, String message, Object... objects) {
365 if (!condition) {
366 fail(message, objects);
367 }
368 }
369
370 /**
371 * Asserts that a condition is false. If it isn't it throws an {@link AssertionError} with the
372 * given message produced by {@link String#format}.
373 *
374 * @param condition condition to be checked
375 * @param message the identifying message for the {@link AssertionError}
376 * @param objects arguments to the format string
377 */
378 public static void assertFalse(boolean condition, String message, Object... objects) {
379 assertTrue(!condition, message, objects);
380 }
381
382 /**
383 * Gets the {@link DebugHandlersFactory}s available for a {@link DebugContext}.
384 */
385 protected Collection<DebugHandlersFactory> getDebugHandlersFactories() {
386 return Collections.emptyList();
387 }
388
389 /**
390 * Gets a {@link DebugContext} object corresponding to {@code options}, creating a new one if
391 * none currently exists. Debug contexts created by this method will have their
392 * {@link DebugDumpHandler}s closed in {@link #afterTest()}.
393 */
394 protected DebugContext getDebugContext(OptionValues options) {
395 List<DebugContext> cached = cachedDebugs.get();
396 if (cached == null) {
397 cached = new ArrayList<>();
398 cachedDebugs.set(cached);
399 }
400 for (DebugContext debug : cached) {
401 if (debug.getOptions() == options) {
402 return debug;
403 }
404 }
405 DebugContext debug = DebugContext.create(options, NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, DEFAULT_LOG_STREAM, getDebugHandlersFactories());
406 cached.add(debug);
407 return debug;
408 }
409
410 private final ThreadLocal<List<DebugContext>> cachedDebugs = new ThreadLocal<>();
411
412 @After
413 public void afterTest() {
414 List<DebugContext> cached = cachedDebugs.get();
415 if (cached != null) {
416 for (DebugContext debug : cached) {
417 debug.closeDumpHandlers(true);
418 }
419 }
420 }
421 }
|