
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import org.apache.commons.jxpath.DynamicPropertyHandler;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathIntrospector;

public class QueryBug
{

	// =======================================================
	// Simple bean class with one property
	// =======================================================

	public static class SimpleBean
	{
		Integer index;

		public SimpleBean(int i) {
			index = new Integer(i);
		}

		public Integer getIndex() {
			return index;
		}

		public String toString() {
			return "SimpleBean-"+index;
		}
	}

	// =======================================================
	// Main
	// =======================================================

	public static void main (String [] args)
	throws Exception
	{
		// create an array of simple beans
		SimpleBean [] ary = new SimpleBean[8];
		for (int i=0; i<ary.length; i++)
			ary[i] = new SimpleBean(i);

		// create an empty context and the array as a variable
		JXPathContext ctx = JXPathContext.newContext(null);
		ctx.getVariables().declareVariable("array", ary);

		// this query should return all beans with an index
		// greater than 2, but it doesn't.
		System.err.println("$array[index > 2]");
		Iterator i = ctx.iterate("$array[index > 2]");
		while (i.hasNext()) {
			System.err.println("\t"+i.next());
		}
		System.err.println();

		/*
		// I tried declaring the array in the context as a regular
		// element, and the query worked just fine.  Uncomment this
		// to see it in action.
		HashMap m = new HashMap();
		m.put("array", ary);

		JXPathContext ctx = JXPathContext.newContext(m);

		System.err.println("array[index > 2]");
		Iterator i = ctx.iterate("array[index > 2]");
		while (i.hasNext()) {
			System.err.println("\t"+i.next());
		}
		System.err.println();
		*/
	}

}
