001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.grid;
016:
017: import java.util.Arrays;
018: import java.util.List;
019:
020: import org.apache.tapestry.ComponentResources;
021: import org.apache.tapestry.beaneditor.BeanModel;
022: import org.apache.tapestry.beaneditor.PropertyModel;
023: import org.apache.tapestry.internal.test.InternalBaseTestCase;
024: import org.apache.tapestry.ioc.Messages;
025: import org.apache.tapestry.services.BeanModelSource;
026: import org.testng.annotations.AfterClass;
027: import org.testng.annotations.BeforeClass;
028: import org.testng.annotations.Test;
029:
030: public class ListGridDataSourceTest extends InternalBaseTestCase {
031: // Just arbitrary numbers ...
032:
033: private static final int FRED = 99;
034:
035: private static final int BARNEY = 23;
036:
037: private static final int WILMA = 107;
038:
039: private static final int BETTY = 298;
040:
041: // Arrays.asList returns an unmodifiable list
042:
043: private final List _raw = Arrays.asList(new Datum(FRED, "Fred"),
044: new Datum(BARNEY, "Barney"), new Datum(WILMA, "Wilma"),
045: new Datum(BETTY, null));
046:
047: private final ListGridDataSource _source = new ListGridDataSource(
048: _raw);
049:
050: private BeanModel _model;
051:
052: @BeforeClass
053: public void setup() {
054: BeanModelSource source = getService(BeanModelSource.class);
055:
056: ComponentResources resources = mockComponentResources();
057: Messages messages = mockMessages();
058:
059: train_getMessages(resources, messages);
060: stub_contains(messages, false);
061:
062: replay();
063:
064: _model = source.create(Datum.class, false, resources);
065:
066: verify();
067: }
068:
069: @AfterClass
070: public void cleanup() {
071: _model = null;
072: }
073:
074: @Test
075: public void sort_on_number_ascending() {
076: sort("id", true, BARNEY, FRED, WILMA, BETTY);
077: }
078:
079: @Test
080: public void sort_on_number_descending() {
081: sort("id", false, BETTY, WILMA, FRED, BARNEY);
082: }
083:
084: @Test
085: public void sort_on_string_value_ascending() {
086: // Nulls sort first
087:
088: // Without a secondary sort column, it's kind of arbitrary whether WILMA or BETTY is sorted
089: // first.
090:
091: sort("value", true, BETTY, BARNEY, FRED, WILMA);
092: }
093:
094: @Test
095: public void sort_on_string_value_descending() {
096: sort("value", false, WILMA, FRED, BARNEY, BETTY);
097: }
098:
099: private void sort(String propertyName, boolean ascending,
100: int... ids) {
101: PropertyModel propertyModel = _model.get(propertyName);
102:
103: int availableRows = _source.getAvailableRows();
104:
105: _source.prepare(0, availableRows - 1, propertyModel, ascending);
106:
107: for (int i = 0; i < ids.length; i++) {
108: Datum row = (Datum) _source.getRowValue(i);
109:
110: assertEquals(row.getId(), ids[i], "Id for Datum #" + i);
111: }
112: }
113: }
|