/*******************************************************************************
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.apache.kato.tck.scenario142.javaruntime;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.tools.diagnostics.image.CorruptDataException;
import javax.tools.diagnostics.runtime.java.JavaLocation;
import javax.tools.diagnostics.runtime.java.JavaMethod;
import javax.tools.diagnostics.runtime.java.JavaRuntime;
import javax.tools.diagnostics.runtime.java.JavaStackFrame;
import javax.tools.diagnostics.runtime.java.JavaThread;
import org.apache.kato.tck.harness.TCKJavaRuntimeTestcase;
import org.apache.kato.tck.scenario142.javaruntime.SetupJavaRuntimeThreads;
/**
*
*
*/
public class TestJavaRuntimeThreads extends TCKJavaRuntimeTestcase {
/**
* Test has at least one thread
*
*/
public void testHasThreads() {
JavaRuntime runtime=getJavaRuntime();
Iterator threadIterator=runtime.getThreads().iterator();
assertNotNull(threadIterator);
assertTrue(threadIterator.hasNext());
}
/**
* Test has configured threads
* @throws CorruptDataException
*
*/
public void testHasExpectedThreads() throws CorruptDataException {
List allThreads = getJavaRuntime().getThreads();
assertNotNull(getThread(allThreads,SetupJavaRuntimeThreads.DTFJ_TCK_T1));
assertNotNull(getThread(allThreads,SetupJavaRuntimeThreads.DTFJ_TCK_T2));
assertNotNull(getThread(allThreads,SetupJavaRuntimeThreads.DTFJ_TCK_T3));
}
public void testExpectedThreadState() throws CorruptDataException {
List allThreads = getJavaRuntime().getThreads();
JavaThread t1=getThread(allThreads,SetupJavaRuntimeThreads.DTFJ_TCK_T1);
int state=t1.getState();
assertEquals(JavaThread.STATE_ALIVE | JavaThread.STATE_WAITING | JavaThread.STATE_SLEEPING,
(state & (JavaThread.STATE_ALIVE | JavaThread.STATE_WAITING | JavaThread.STATE_SLEEPING)));
}
public void testHasThreadStack() throws CorruptDataException {
List allThreads = getJavaRuntime().getThreads();
JavaThread t1=getThread(allThreads,SetupJavaRuntimeThreads.DTFJ_TCK_T1);
Iterator i=t1.getStackFrames().iterator();
assertTrue(i.hasNext());
}
public void testThreadStackFrameLocation() throws CorruptDataException {
List allThreads = getJavaRuntime().getThreads();
JavaThread t1=getThread(allThreads,SetupJavaRuntimeThreads.DTFJ_TCK_T1);
Iterator i=t1.getStackFrames().iterator();
JavaStackFrame frame=(JavaStackFrame) i.next();
JavaLocation location=frame.getLocation();
assertNotNull(location);
}
public void testThreadStackFrameLocationMethod() throws CorruptDataException {
List allThreads = getJavaRuntime().getThreads();
JavaThread t1=getThread(allThreads,SetupJavaRuntimeThreads.DTFJ_TCK_T1);
Iterator i=t1.getStackFrames().iterator();
JavaStackFrame frame=(JavaStackFrame) i.next();
JavaLocation location=frame.getLocation();
JavaMethod method=location.getMethod();
assertNotNull(method);
}
public void testThreadStackFrameLocationMethodName() throws CorruptDataException {
List allThreads = getJavaRuntime().getThreads();
JavaThread t1=getThread(allThreads,SetupJavaRuntimeThreads.DTFJ_TCK_T1);
Iterator i=t1.getStackFrames().iterator();
JavaStackFrame frame=(JavaStackFrame) i.next();
JavaLocation location=frame.getLocation();
JavaMethod method=location.getMethod();
String name=method.getName();
assertEquals(name,"sleep");
}
private List getThreads() {
JavaRuntime runtime=getJavaRuntime();
Iterator threadIterator=runtime.getThreads().iterator();
List allThreads=new LinkedList();
while(threadIterator.hasNext()) {
JavaThread thread=(JavaThread) threadIterator.next();
allThreads.add(thread);
}
return allThreads;
}
public void testThreadGroupMembership() throws CorruptDataException {
List allThreads = getJavaRuntime().getThreads();
JavaThread t1=getThread(allThreads, SetupJavaRuntimeThreads.DTFJ_TCK_T1);
}
private JavaThread getThread(List allThreads, String threadname) throws CorruptDataException {
Iterator i=allThreads.iterator();
while(i.hasNext()) {
JavaThread thread=(JavaThread) i.next();
String name=thread.getName();
if(name.equals(threadname)) return thread;
}
return null;
}
}