package org.apache.bcel;

import java.util.Iterator;

import org.apache.bcel.AbstractTestCase;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.util.InstructionFinder;
import org.apache.bcel.util.InstructionFinder.CodeConstraint;

public class InstructionFinderTestCase extends AbstractTestCase
{
	public void testSearchAll() throws Exception
	{
		JavaClass clazz = getTestClass("org.apache.bcel.util.InstructionFinder");
		Method[] methods = clazz.getMethods();
		Method searchM = null;
		for (Method m : methods)
		{
			if (m.getName().equals("search") && (m.getArgumentTypes().length == 3))
			{
				searchM = m;
				break;
			}
		}
		
		if (searchM == null)
			throw new Exception("search method not found");
		
		byte[] bytes = searchM.getCode().getCode();
		InstructionList il = new InstructionList(bytes);
		InstructionFinder finder = new InstructionFinder(il);
		Iterator it = finder.search(".*", il.getStart(), null);
		
		int cnt = 0;
		while (it.hasNext())
		{
			it.next();
			cnt++;
		}
		
		assertEquals(1, cnt);
	}
}

@@ -180,16 +180,67 @@
 ethod;%0D%0A
+import org.apache.bcel.generic.InstructionHandle;%0D%0A
 import o
@@ -1122,82 +1122,175 @@
 %0D%0A%09%09
-int cnt = 0;%0D%0A%09%09while (it.hasNext())%0D%0A%09%09%7B%0D%0A%09%09%09it.next();%0D%0A%09%09%09cnt++
+InstructionHandle%5B%5D ihs = (InstructionHandle%5B%5D)it.next();%0D%0A%09%09int size = 0;%0D%0A%09%09for (InstructionHandle ih : ihs)%0D%0A%09%09%7B%0D%0A%09%09%09size += ih.getInstruction().getLength()
 ;%0D%0A%09%09%7D%0D%0A
 %09%09%0D%0A
@@ -1285,20 +1285,16 @@
 ;%0D%0A%09%09%7D%0D%0A
-%09%09%0D%0A
 %09%09assert
@@ -1304,16 +1304,32 @@
 als(
-1, cnt);
+bytes.length, size);%0D%0A%09%09
 %0D%0A%09%7D

improve the test