/*
 * Copyright 2006 Amazon Technologies, Inc. or its affiliates.
 * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.amazon.carbonado.filter;

import junit.framework.TestCase;
import junit.framework.TestSuite;

import com.amazon.carbonado.stored.Order;

/**
 * 
 *
 * @author Brian S O'Neill
 */
public class TestFilterSuppliedValues extends TestCase {
    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }

    public static TestSuite suite() {
        return new TestSuite(TestFilterSuppliedValues.class);
    }

    public TestFilterSuppliedValues(String name) {
        super(name);
    }

    public void testSuppliedValuesFor() {
        Filter<Order> open = Filter.getOpenFilter(Order.class);
        Filter<Order> f1 = open.and("orderTotal", RelOp.EQ);
        Filter<Order> f2 = open.and("orderNumber", RelOp.EQ, "20");

        Filter<Order> f = f1.and(f2).bind();
        FilterValues<Order> fv = f.initialFilterValues();

        Object[] values = fv.getSuppliedValuesFor(f);
        assertEquals(0, values.length);

        values = fv.with(10).getSuppliedValuesFor(f);
        assertEquals(1, values.length);
        assertEquals(new Integer(10), values[0]);

        // Again, with reverse arrangement.
        f = f2.and(f1).bind();
        fv = f.initialFilterValues();

        values = fv.getSuppliedValuesFor(f);
        assertEquals(0, values.length);

        values = fv.with(10).getSuppliedValuesFor(f);
        assertEquals(1, values.length);
        assertEquals(new Integer(10), values[0]);

        // Now test with some values set and some blank.
        Filter<Order> f3 = open.and("orderComments", RelOp.GT);

        f = f1.and(f2).and(f3).bind();
        fv = f.initialFilterValues();

        values = fv.getSuppliedValuesFor(f);
        assertEquals(0, values.length);

        values = fv.with(10).getSuppliedValuesFor(f);
        assertEquals(1, values.length);
        assertEquals(new Integer(10), values[0]);

        values = fv.with(10).with("hello").getSuppliedValuesFor(f);
        assertEquals(2, values.length);
        assertEquals(new Integer(10), values[0]);
        assertEquals("hello", values[1]);

        // Arrange differently.
        f = f2.and(f1).and(f3).bind();
        fv = f.initialFilterValues();

        values = fv.getSuppliedValuesFor(f);
        assertEquals(0, values.length);

        values = fv.with(10).getSuppliedValuesFor(f);
        assertEquals(1, values.length);
        assertEquals(new Integer(10), values[0]);

        values = fv.with(10).with("hello").getSuppliedValuesFor(f);
        assertEquals(2, values.length);
        assertEquals(new Integer(10), values[0]);
        assertEquals("hello", values[1]);

        // Arrange differently again.
        f = f3.and(f1).and(f2).bind();
        fv = f.initialFilterValues();

        values = fv.getSuppliedValuesFor(f);
        assertEquals(0, values.length);

        values = fv.with("hello").getSuppliedValuesFor(f);
        assertEquals(1, values.length);
        assertEquals("hello", values[0]);

        values = fv.with("hello").with(5).getSuppliedValuesFor(f);
        assertEquals(2, values.length);
        assertEquals("hello", values[0]);
        assertEquals(new Integer(5), values[1]);
    }
}

@@ -4068,12 +4068,929 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    public void testSuppliedValuesFor_multipleReferences() %7B%0D%0A        // Test with property filter duplication.%0D%0A        Filter%3COrder%3E oneProp = Filter.filterFor(Order.class, %22orderTotal != ?%22);%0D%0A        oneProp = oneProp.bind();%0D%0A%0D%0A        // Note that reduce is not called on the filter, as it would reduce the%0D%0A        // filter back to one term.%0D%0A        Filter%3COrder%3E f = oneProp.and(oneProp);%0D%0A%0D%0A        // Note that values are only for oneProp.%0D%0A        FilterValues%3COrder%3E fv = oneProp.initialFilterValues();%0D%0A%0D%0A        Object%5B%5D values = fv.getSuppliedValuesFor(f);%0D%0A        assertEquals(0, values.length);%0D%0A%0D%0A        // Supply one actual value...%0D%0A        values = fv.with(10).getSuppliedValuesFor(f);%0D%0A        // ...which is duplicated as required by f.%0D%0A        assertEquals(2, values.length);%0D%0A        assertEquals(new Integer(10), values%5B0%5D);%0D%0A        assertEquals(new Integer(10), values%5B1%5D);%0D%0A    %7D%0D
 %0A%7D%0D%0A

Fix for getting supplied filter values when duplicate property filters exist.