// Copyright 2007 The Apache Software Foundation
//
// 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 org.apache.tapestry.internal.grid;

import java.util.Arrays;
import java.util.List;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.beaneditor.BeanModel;
import org.apache.tapestry.beaneditor.PropertyModel;
import org.apache.tapestry.internal.test.InternalBaseTestCase;
import org.apache.tapestry.ioc.Messages;
import org.apache.tapestry.services.BeanModelSource;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class ListGridDataSourceTest extends InternalBaseTestCase
{
    // Just arbitrary numbers ...

    private static final int FRED = 99;

    private static final int BARNEY = 23;

    private static final int WILMA = 107;

    private static final int BETTY = 298;

    // Arrays.asList returns an unmodifiable list

    private final List _raw = Arrays.asList(
            new Datum(FRED, "Fred"),
            new Datum(BARNEY, "Barney"),
            new Datum(WILMA, null),
            new Datum(BETTY, null));

    private final ListGridDataSource _source = new ListGridDataSource(_raw);

    private BeanModel _model;

    @BeforeClass
    public void setup()
    {
        BeanModelSource source = getService(BeanModelSource.class);

        ComponentResources resources = mockComponentResources();
        Messages messages = mockMessages();

        train_getMessages(resources, messages);
        stub_contains(messages, false);

        replay();

        _model = source.create(Datum.class, false, resources);

        verify();
    }

    @AfterClass
    public void cleanup()
    {
        _model = null;
    }

    @Test
    public void sort_on_number_ascending()
    {
        sort("id", true, BARNEY, FRED, WILMA, BETTY);
    }

    @Test
    public void sort_on_number_descending()
    {
        sort("id", false, BETTY, WILMA, FRED, BARNEY);
    }

    @Test
    public void sort_on_string_value_ascending()
    {
        // Nulls sort first

        // Without a secondary sort column, it's kind of arbitrary whether WILMA or BETTY is sorted
        // first.

        sort("value", true, WILMA, BETTY, BARNEY, FRED);
    }

    @Test
    public void sort_on_string_value_descending()
    {
        sort("value", false, FRED, BARNEY, WILMA, BETTY);
    }

    private void sort(String propertyName, boolean ascending, int... ids)
    {
        PropertyModel propertyModel = _model.get(propertyName);

        int availableRows = _source.getAvailableRows();

        _source.prepare(0, availableRows - 1, propertyModel, ascending);

        for (int i = 0; i < ids.length; i++)
        {
            Datum row = (Datum) _source.getRowValue(i);

            assertEquals(row.getId(), ids[i], "Id for Datum #" + i);
        }
    }
}

@@ -1597,20 +1597,23 @@
 (WILMA, 
-null
+%22Wilma%22
 ),%0A     
@@ -2737,23 +2737,16 @@
 %22, true,
- WILMA,
  BETTY, 
@@ -2757,16 +2757,23 @@
 EY, FRED
+, WILMA
 );%0A    %7D
@@ -2868,16 +2868,23 @@
 , false,
+ WILMA,
  FRED, B
@@ -2889,23 +2889,16 @@
  BARNEY,
- WILMA,
  BETTY);

Fix a broken test whose behavior varied arbitrary (due to comparison of null to null).