JOCL v2.6.0-rc-20250722
JOCL, OpenCL® API Binding for Java™ (public API).
UITestCase.java
Go to the documentation of this file.
1/**
2 * Copyright 2010, 2014 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.opencl.test.util;
30
31import java.io.BufferedReader;
32import java.io.IOException;
33import java.io.InputStreamReader;
34import java.util.Iterator;
35import java.util.List;
36
37import com.jogamp.common.os.Platform;
38import com.jogamp.common.util.locks.SingletonInstance;
39import com.jogamp.opencl.CLPlatform;
40
41import org.junit.Assume;
42import org.junit.Before;
43import org.junit.BeforeClass;
44import org.junit.After;
45import org.junit.AfterClass;
46import org.junit.FixMethodOrder;
47import org.junit.Rule;
48import org.junit.rules.TestName;
49import org.junit.runners.MethodSorters;
50import org.junit.runners.model.FrameworkMethod;
51import org.junit.runners.model.TestClass;
52
53@FixMethodOrder(MethodSorters.NAME_ASCENDING)
54public abstract class UITestCase {
55 @Rule public TestName _unitTestName = new TestName();
56
57 public static final String SINGLE_INSTANCE_LOCK_FILE = "UITestCase.lock";
58 public static final int SINGLE_INSTANCE_LOCK_PORT = 59999;
59
60 public static final long SINGLE_INSTANCE_LOCK_TO = 6*60*1000; // wait up to 6 mins
61 public static final long SINGLE_INSTANCE_LOCK_POLL = 1000; // poll every 1s
62
63 private static volatile SingletonInstance singletonInstance;
64
65 private static volatile boolean testSupported = true;
66
67 private static volatile int maxMethodNameLen = 0;
68
69 private static final synchronized void initSingletonInstance() {
70 if( null == singletonInstance ) {
71 // singletonInstance = SingletonInstance.createFileLock(SINGLE_INSTANCE_LOCK_POLL, SINGLE_INSTANCE_LOCK_FILE);
72 singletonInstance = SingletonInstance.createServerSocket(SINGLE_INSTANCE_LOCK_POLL, SINGLE_INSTANCE_LOCK_PORT);
73 if(!singletonInstance.tryLock(SINGLE_INSTANCE_LOCK_TO)) {
74 throw new RuntimeException("Fatal: Could not lock single instance: "+singletonInstance.getName());
75 }
76 }
77 }
78
79 public static boolean isTestSupported() {
80 return testSupported;
81 }
82
83 public static void setTestSupported(final boolean v) {
84 System.err.println("setTestSupported: "+v);
85 testSupported = v;
86 }
87
88 public int getMaxTestNameLen() {
89 if(0 == maxMethodNameLen) {
90 int ml = 0;
91 final TestClass tc = new TestClass(getClass());
92 final List<FrameworkMethod> testMethods = tc.getAnnotatedMethods(org.junit.Test.class);
93 for(final Iterator<FrameworkMethod> iter=testMethods.iterator(); iter.hasNext(); ) {
94 final int l = iter.next().getName().length();
95 if( ml < l ) { ml = l; }
96 }
97 maxMethodNameLen = ml;
98 }
99 return maxMethodNameLen;
100 }
101
102 public final String getTestMethodName() {
103 return _unitTestName.getMethodName();
104 }
105
106 public final String getSimpleTestName(final String separator) {
107 return getClass().getSimpleName()+separator+getTestMethodName();
108 }
109
110 public final String getFullTestName(final String separator) {
111 return getClass().getName()+separator+getTestMethodName();
112 }
113
114 @BeforeClass
115 public static void oneTimeSetUp() {
116 // one-time initialization code
117 initSingletonInstance();
118 }
119
120 @AfterClass
121 public static void oneTimeTearDown() {
122 // one-time cleanup code
123 System.gc(); // force cleanup
124 singletonInstance.unlock();
125 }
126
127 @Before
128 public void setUp() {
129 System.err.print("++++ UITestCase.setUp: "+getFullTestName(" - "));
130 final boolean isOpenCLUnavailable = !CLPlatform.isAvailable();
131 final boolean abortTest = isOpenCLUnavailable || !testSupported;
132 if( abortTest ) {
133 if( isOpenCLUnavailable ) {
134 System.err.print(" - CL not supported on this machine");
135 }
136 if( !testSupported ) {
137 System.err.print(" - "+unsupportedTestMsg);
138 }
139 System.err.println("");
140 Assume.assumeTrue(false); // abort
141 }
142 System.err.println();
143 }
144
145 @After
146 public void tearDown() {
147 System.err.println("++++ UITestCase.tearDown: "+getFullTestName(" - "));
148 }
149
150 public static void waitForKey(final String preMessage) {
151 final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
152 System.err.println(preMessage+"> Press enter to continue");
153 try {
154 System.err.println(stdin.readLine());
155 } catch (final IOException e) { }
156 }
157
158 static final String unsupportedTestMsg = "Test not supported on this platform.";
159}
160
CLPlatfrorm representing a OpenCL implementation (e.g.
Definition: CLPlatform.java:99
static boolean isAvailable()
static void setTestSupported(final boolean v)
Definition: UITestCase.java:83
final String getFullTestName(final String separator)
static void waitForKey(final String preMessage)
final String getSimpleTestName(final String separator)