Skip to content

Commit

Permalink
Merge pull request #83 from gayanW/java-10_81
Browse files Browse the repository at this point in the history
Move/Refactor ReflectionTest -> StackWalkerTest
  • Loading branch information
cyrille-artho authored Jun 14, 2018
2 parents 0fc5a7b + 0b347c4 commit b1f8b9d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 65 deletions.
71 changes: 71 additions & 0 deletions src/tests/gov/nasa/jpf/test/java/lang/StackWalkerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2014, United States Government, as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The Java Pathfinder core (jpf-core) platform is 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 gov.nasa.jpf.test.java.lang;

import gov.nasa.jpf.util.test.TestJPF;

import org.junit.Test;

import java.util.List;
import java.util.stream.Collectors;

public class StackWalkerTest extends TestJPF {

static class MyClass {
void bar(){
foo();
}

/**
* Traverse from the top frame of the stack which is the {@code foo} method
* and assert that the declaring class of each stack frame is valid.
* Reflection frames are ignored, see: {@link java.lang.StackWalker.Option#SHOW_REFLECT_FRAMES}
*/
void foo (){
final StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
List<Class<?>> callerClasses = walker.walk(s -> s
.limit(4)
.map(StackWalker.StackFrame::getDeclaringClass)
.collect(Collectors.toList()));

Class<?> callerCls = callerClasses.get(0); // foo()
System.out.println("-- declaring class of the top frame of the stack = " + callerCls);
assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.java.lang.StackWalkerTest$MyClass"));

callerCls = callerClasses.get(1); // bar()
System.out.println("-- StackFrame[1].getDeclaringClass = " + callerCls);
assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.java.lang.StackWalkerTest$MyClass"));

callerCls = callerClasses.get(2); // callIt()
System.out.println("-- StackFrame[2].getDeclaringClass = " + callerCls);
assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.java.lang.StackWalkerTest"));
}
}

void callIt(){
MyClass o = new MyClass();
o.bar();
}

@Test
public void testCallerClass() {
if (verifyNoPropertyViolation()){
callIt();
}
}
}
65 changes: 0 additions & 65 deletions src/tests/gov/nasa/jpf/test/vm/reflection/ReflectionTest.java

This file was deleted.

0 comments on commit b1f8b9d

Please sign in to comment.