JOAL v2.6.0-rc-20250712
JOAL, OpenAL® 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.openal.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.util.locks.SingletonInstance;
38
39import org.junit.Assume;
40import org.junit.Before;
41import org.junit.BeforeClass;
42import org.junit.After;
43import org.junit.AfterClass;
44import org.junit.FixMethodOrder;
45import org.junit.Rule;
46import org.junit.rules.TestName;
47import org.junit.runners.MethodSorters;
48import org.junit.runners.model.FrameworkMethod;
49import org.junit.runners.model.TestClass;
50
51@FixMethodOrder(MethodSorters.NAME_ASCENDING)
52public abstract class UITestCase {
53 @Rule public TestName _unitTestName = new TestName();
54
55 public static final String SINGLE_INSTANCE_LOCK_FILE = "UITestCase.lock";
56 public static final int SINGLE_INSTANCE_LOCK_PORT = 59999;
57
58 public static final long SINGLE_INSTANCE_LOCK_TO = 6*60*1000; // wait up to 6 mins
59 public static final long SINGLE_INSTANCE_LOCK_POLL = 1000; // poll every 1s
60
61 private static volatile SingletonInstance singletonInstance;
62
63 private static volatile boolean testSupported = true;
64
65 private static volatile int maxMethodNameLen = 0;
66
67 private static final synchronized void initSingletonInstance() {
68 if( null == singletonInstance ) {
69 // singletonInstance = SingletonInstance.createFileLock(SINGLE_INSTANCE_LOCK_POLL, SINGLE_INSTANCE_LOCK_FILE);
70 singletonInstance = SingletonInstance.createServerSocket(SINGLE_INSTANCE_LOCK_POLL, SINGLE_INSTANCE_LOCK_PORT);
71 if(!singletonInstance.tryLock(SINGLE_INSTANCE_LOCK_TO)) {
72 throw new RuntimeException("Fatal: Could not lock single instance: "+singletonInstance.getName());
73 }
74 }
75 }
76
77 public static boolean isTestSupported() {
78 return testSupported;
79 }
80
81 public static void setTestSupported(final boolean v) {
82 System.err.println("setTestSupported: "+v);
83 testSupported = v;
84 }
85
86 public int getMaxTestNameLen() {
87 if(0 == maxMethodNameLen) {
88 int ml = 0;
89 final TestClass tc = new TestClass(getClass());
90 final List<FrameworkMethod> testMethods = tc.getAnnotatedMethods(org.junit.Test.class);
91 for(final Iterator<FrameworkMethod> iter=testMethods.iterator(); iter.hasNext(); ) {
92 final int l = iter.next().getName().length();
93 if( ml < l ) { ml = l; }
94 }
95 maxMethodNameLen = ml;
96 }
97 return maxMethodNameLen;
98 }
99
100 public final String getTestMethodName() {
101 return _unitTestName.getMethodName();
102 }
103
104 public final String getSimpleTestName(final String separator) {
105 return getClass().getSimpleName()+separator+getTestMethodName();
106 }
107
108 public final String getFullTestName(final String separator) {
109 return getClass().getName()+separator+getTestMethodName();
110 }
111
112 @BeforeClass
113 public static void oneTimeSetUp() {
114 // one-time initialization code
115 initSingletonInstance();
116 }
117
118 @AfterClass
119 public static void oneTimeTearDown() {
120 // one-time cleanup code
121 System.gc(); // force cleanup
122 singletonInstance.unlock();
123 }
124
125 @Before
126 public void setUp() {
127 System.err.print("++++ UITestCase.setUp: "+getFullTestName(" - "));
128 if(!testSupported) {
129 System.err.println(" - "+unsupportedTestMsg);
130 Assume.assumeTrue(testSupported); // abort
131 }
132 System.err.println();
133 }
134
135 @After
136 public void tearDown() {
137 System.err.println("++++ UITestCase.tearDown: "+getFullTestName(" - "));
138 }
139
140 public static void waitForKey(final String preMessage) {
141 final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
142 System.err.println(preMessage+"> Press enter to continue");
143 try {
144 System.err.println(stdin.readLine());
145 } catch (final IOException e) { }
146 }
147
148 static final String unsupportedTestMsg = "Test not supported on this platform.";
149}
150
static void setTestSupported(final boolean v)
Definition: UITestCase.java:81
final String getSimpleTestName(final String separator)
static void waitForKey(final String preMessage)
final String getFullTestName(final String separator)