Initial import
[jpf-core.git] / src / tests / java8 / DefaultMethodTest.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package java8;
19
20 import gov.nasa.jpf.annotation.MJI;
21 import gov.nasa.jpf.util.test.TestJPF;
22 import gov.nasa.jpf.vm.MJIEnv;
23 import gov.nasa.jpf.vm.NativePeer;
24 import gov.nasa.jpf.vm.Verify;
25 import org.junit.Test;
26
27 /**
28  * regression test for Java 8 default methods
29  */
30 public class DefaultMethodTest extends TestJPF {
31   
32   //------------------------------------------ non-ambiguous recursive lookup
33   
34   interface A1 {
35     default int getValue(){
36       return 42;
37     }
38   } 
39   
40   interface B1 extends A1 {
41     // nothing
42   }
43   
44   static class C1 implements A1 {
45     // nothing
46   }
47   
48   static class D1 extends C1 {
49     // nothing
50   } 
51   
52   @Test
53   public void testSingleMethod (){
54     if (verifyNoPropertyViolation()){
55       D1 o = new D1();
56       int result = o.getValue();
57       System.out.println(result);
58       assertTrue (result == 42);
59     }
60   }
61   
62   //------------------------------------------ ambiguity resolution
63   
64   interface B2 {
65     default int getValue(){
66       return 3;
67     }
68   }
69   
70   static class D2 implements A1, B2 {
71     @Override
72     public int getValue(){
73       return A1.super.getValue() + B2.super.getValue();
74     }
75   }
76   
77   @Test
78   public void testExplicitDelegation (){
79     if (verifyNoPropertyViolation()){
80       D2 o = new D2();
81       int result = o.getValue();
82       System.out.println(result);
83       assertTrue (result == 45);
84     }    
85   }
86
87   //------------------------------------------- overloaded methods
88
89   interface E1 {
90     default int foo (String s1, String s2){
91       System.out.println("this is E1.foo(String,String)");
92       return 42;
93     }
94
95     default int foo (Object o1, Object o2){
96       System.out.println("this is E1.foo(Object,Object)");
97       return 0;
98     }
99   }
100
101   static class F implements E1 {
102     String getId() {
103       return "whatever";
104     }
105
106     void bar (){
107       int r = foo("blah", getId());
108       assertTrue(r == 42);
109     }
110   }
111
112   @Test
113   public void testOverloadedDefaults(){
114     if (verifyNoPropertyViolation()){
115       F o = new F();
116       o.bar();
117     }
118   }
119
120   //----------------------------------------------- native peer for interface
121
122   interface G1 {
123     default int foo (){  // should be intercepted by peer
124       System.out.println("this is bytecode G1.foo()");
125       return -1;
126     }
127   }
128
129   static class H implements G1 {
130     void bar (){
131       int r = foo();
132       //assertTrue(r == 42);
133     }
134   }
135
136   @Test
137   public void testInterfacePeer(){
138     if (verifyNoPropertyViolation()){
139       H o = new H();
140       o.bar();
141     }
142   }
143
144   // <2do> how to test IncompatibleClassChangeError without explicit classfile restore?
145 }
146