View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.bcel.verifier;
21  
22  import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
23  
24  import java.io.IOException;
25  
26  import org.apache.bcel.verifier.tests.TestReturn01Creator;
27  import org.apache.bcel.verifier.tests.TestReturn03BooleanCreator;
28  import org.apache.bcel.verifier.tests.TestReturn03ByteCreator;
29  import org.apache.bcel.verifier.tests.TestReturn03DoubleCreator;
30  import org.apache.bcel.verifier.tests.TestReturn03FloatCreator;
31  import org.apache.bcel.verifier.tests.TestReturn03IntCreator;
32  import org.apache.bcel.verifier.tests.TestReturn03LongCreator;
33  import org.apache.bcel.verifier.tests.TestReturn03ObjectCreator;
34  import org.apache.bcel.verifier.tests.TestReturn03UnknownCreator;
35  import org.junit.jupiter.api.Test;
36  
37  class VerifierReturnTest extends AbstractVerifierTest {
38  
39      @Test
40      void testInvalidReturn() throws IOException, ClassNotFoundException {
41          new TestReturn01Creator().create();
42          assertVerifyRejected("TestReturn01", "Verification of a void method that returns an object must fail.");
43          new TestReturn03IntCreator().create();
44          assertVerifyRejected("TestReturn03Int", "Verification of an int method that returns null int must fail.");
45          new TestReturn03FloatCreator().create();
46          assertVerifyRejected("TestReturn03Float", "Verification of an int method that returns null float must fail.");
47          new TestReturn03DoubleCreator().create();
48          assertVerifyRejected("TestReturn03Double", "Verification of an int method that returns null double must fail.");
49          new TestReturn03LongCreator().create();
50          assertVerifyRejected("TestReturn03Long", "Verification of an int method that returns null long must fail.");
51          new TestReturn03ByteCreator().create();
52          assertVerifyRejected("TestReturn03Byte", "Verification of an int method that returns null byte must fail.");
53          new TestReturn03BooleanCreator().create();
54          assertVerifyRejected("TestReturn03Boolean", "Verification of an int method that returns null boolean must fail.");
55          new TestReturn03ObjectCreator().create();
56          assertVerifyRejected("TestReturn03Object", "Verification of an int method that returns null Object must fail.");
57          final TestReturn03UnknownCreator testReturn03UnknownCreator = new TestReturn03UnknownCreator();
58          assertThrowsExactly(IllegalArgumentException.class, testReturn03UnknownCreator::create, "Invalid type <unknown object>");
59      }
60  
61      @Test
62      void testValidReturn() throws ClassNotFoundException {
63          assertVerifyOK("TestReturn02", "Verification of a method that returns a newly created object must pass.");
64          assertVerifyOK("TestArray01", "Verification of a method that returns an array must pass.");
65      }
66  }