You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/10/16 14:55:50 UTC

[commons-bcel] branch master updated: Clean up test case

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-bcel.git


The following commit(s) were added to refs/heads/master by this push:
     new f4c665ac Clean up test case
f4c665ac is described below

commit f4c665acbb304f1c761115535263ae53da1612ae
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Oct 16 10:55:46 2022 -0400

    Clean up test case
    
    Make it obvious that we do not expect exceptions for the given test
    fixtures.
---
 .../bcel/classfile/ConstantPoolTestCase.java       | 60 +++++++++++-----------
 1 file changed, 31 insertions(+), 29 deletions(-)

diff --git a/src/test/java/org/apache/bcel/classfile/ConstantPoolTestCase.java b/src/test/java/org/apache/bcel/classfile/ConstantPoolTestCase.java
index 03ee161e..8ee490c4 100644
--- a/src/test/java/org/apache/bcel/classfile/ConstantPoolTestCase.java
+++ b/src/test/java/org/apache/bcel/classfile/ConstantPoolTestCase.java
@@ -17,9 +17,13 @@
 
 package org.apache.bcel.classfile;
 
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
+import java.io.IOException;
+import java.util.stream.IntStream;
+
 import org.apache.bcel.AbstractTestCase;
 import org.apache.bcel.Const;
 import org.apache.bcel.generic.ConstantPoolGen;
@@ -29,6 +33,14 @@ import org.apache.bcel.util.ClassPath;
 import org.apache.bcel.util.ClassPathRepository;
 import org.junit.jupiter.api.Test;
 
+class ClassWithDoubleConstantPoolItem {
+    double d = 42; // here is the key; we need a double constant value
+}
+
+class ClassWithLongConstantPoolItem {
+    long l = 42; // here is the key; we need a double constant value
+}
+
 public class ConstantPoolTestCase extends AbstractTestCase {
 
     private InstructionHandle[] getInstructionHandles(final JavaClass clazz, final ConstantPoolGen cp, final Method method) {
@@ -53,44 +65,34 @@ public class ConstantPoolTestCase extends AbstractTestCase {
     }
 
     @Test
-    public void testTooManyConstants() throws ClassNotFoundException {
-        final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.SimpleClassWithDefaultConstructor");
-        final ConstantPoolGen cp = new ConstantPoolGen(clazz.getConstantPool());
-        int i = cp.getSize();
-        while (i < Const.MAX_CP_ENTRIES - 1) {
-            cp.addLong(i);
-            i = cp.getSize(); // i += 2
+    public void testDoubleConstantWontThrowClassFormatException() throws ClassNotFoundException, IOException {
+        try (final ClassPath cp = new ClassPath("target/test-classes/org/apache/bcel/classfile")) {
+            final JavaClass c = new ClassPathRepository(cp).loadClass("ClassWithDoubleConstantPoolItem");
+
+            final ConstantPool pool = c.getConstantPool();
+            IntStream.range(0, pool.getLength()).forEach(i -> assertDoesNotThrow(() -> pool.getConstant(i)));
         }
-        assertThrows(IllegalStateException.class, () -> cp.addLong(0));
     }
 
     @Test
-    public void doubleConstantWontThrowClassFormatException() throws ClassNotFoundException {
-        ClassPath cp = new ClassPath("target/test-classes/org/apache/bcel/classfile");
-        JavaClass c = new ClassPathRepository(cp).loadClass("ClassWithDoubleConstantPoolItem");
+    public void testLongConstantWontThrowClassFormatException() throws ClassNotFoundException, IOException {
+        try (final ClassPath cp = new ClassPath("target/test-classes/org/apache/bcel/classfile")) {
+            final JavaClass c = new ClassPathRepository(cp).loadClass("ClassWithLongConstantPoolItem");
 
-        ConstantPool pool = c.getConstantPool();
-        for (int i = 1; i < pool.getLength(); ++i) {
-            pool.getConstant(i);
+            final ConstantPool pool = c.getConstantPool();
+            IntStream.range(0, pool.getLength()).forEach(i -> assertDoesNotThrow(() -> pool.getConstant(i)));
         }
     }
 
     @Test
-    public void longConstantWontThrowClassFormatException() throws ClassNotFoundException {
-        ClassPath cp = new ClassPath("target/test-classes/org/apache/bcel/classfile");
-        JavaClass c = new ClassPathRepository(cp).loadClass("ClassWithLongConstantPoolItem");
-
-        ConstantPool pool = c.getConstantPool();
-        for (int i = 1; i < pool.getLength(); ++i) {
-            pool.getConstant(i);
+    public void testTooManyConstants() throws ClassNotFoundException {
+        final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.SimpleClassWithDefaultConstructor");
+        final ConstantPoolGen cp = new ConstantPoolGen(clazz.getConstantPool());
+        int i = cp.getSize();
+        while (i < Const.MAX_CP_ENTRIES - 1) {
+            cp.addLong(i);
+            i = cp.getSize(); // i += 2
         }
+        assertThrows(IllegalStateException.class, () -> cp.addLong(0));
     }
 }
-
-class ClassWithDoubleConstantPoolItem {
-    double d = 42; // here is the key; we need a double constant value
-}
-
-class ClassWithLongConstantPoolItem {
-    long l = 42; // here is the key; we need a double constant value
-}