Home | History | Annotate | Download | only in verifier
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *   http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  */
     18 
     19 package org.apache.bcel.verifier;
     20 
     21 import org.apache.bcel.Repository;
     22 import org.apache.bcel.classfile.JavaClass;
     23 
     24 import junit.framework.TestCase;
     25 
     26 public abstract class AbstractVerifierTestCase extends TestCase {
     27 
     28     public static final String TEST_PACKAGE = AbstractVerifierTestCase.class.getPackage().getName() + ".tests.";
     29 
     30     /**
     31      * Asserts that the verification of the given class is OK. If it isn't it throws an AssertionFailedError with the given message.
     32      *
     33      * @param classname simple classname of the class to verify
     34      * @param message   message displayed if assertion fails
     35      */
     36     public void assertVerifyOK(final String classname, final String message) {
     37         final String testClassname = TEST_PACKAGE + classname;
     38         assertTrue(message, doAllPasses(testClassname));
     39     }
     40 
     41     /**
     42      * Asserts that the verification of the given class is rejected.
     43      * If it isn't it throws an AssertionFailedError with the given message.
     44      *
     45      * @param classname simple classname of the class to verify
     46      * @param message   message displayed if assertion fails
     47      */
     48     public void assertVerifyRejected(final String classname, final String message) {
     49         final String testClassname = TEST_PACKAGE + classname;
     50         assertFalse(message, doAllPasses(testClassname));
     51     }
     52 
     53     /**
     54      * Executes all the verification on the given class.
     55      *
     56      * @param classname name of the class to verify
     57      * @return false if the verification fails, true otherwise
     58      */
     59     public boolean doAllPasses(final String classname) {
     60         int nbMethods = 0;
     61 
     62         try {
     63             final JavaClass jc = Repository.lookupClass(classname);
     64             nbMethods = jc.getMethods().length;
     65         } catch (final ClassNotFoundException e) {
     66             fail(e.getMessage());
     67             return false;
     68         }
     69 
     70         final Verifier verifier = VerifierFactory.getVerifier(classname);
     71         VerificationResult result = verifier.doPass1();
     72         if (result.getStatus() != VerificationResult.VERIFIED_OK) {
     73             return false;
     74         }
     75 
     76         result = verifier.doPass2();
     77         if (result.getStatus() != VerificationResult.VERIFIED_OK) {
     78             return false;
     79         }
     80 
     81         for (int i = nbMethods; --i >= 0;) {
     82             result = verifier.doPass3a(i);
     83             if (result.getStatus() != VerificationResult.VERIFIED_OK) {
     84                 return false;
     85             }
     86             result = verifier.doPass3b(i);
     87             if (result.getStatus() != VerificationResult.VERIFIED_OK) {
     88                 return false;
     89             }
     90         }
     91 
     92         return true;
     93     }
     94 
     95 }
     96