001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.binding.tests;
032:
033: import javax.swing.ListModel;
034:
035: import junit.framework.TestCase;
036:
037: import com.jgoodies.binding.adapter.AbstractTableAdapter;
038: import com.jgoodies.binding.list.ArrayListModel;
039:
040: /**
041: * A test case for class {@link AbstractTableAdapter}.
042: *
043: * @author Karsten Lentzsch
044: * @version $Revision: 1.8 $
045: */
046: public final class AbstractTableAdapterTest extends TestCase {
047:
048: private static final String[] COLUMN_NAMES = { "Title", "Artist" };
049:
050: // Constructor Tests ******************************************************
051:
052: public void testConstructorRejectsNullListModel() {
053: try {
054: new ExampleTableModel(null);
055: } catch (NullPointerException e) {
056: // The expected behavior.
057: }
058: try {
059: new ExampleTableModel(null, COLUMN_NAMES);
060: } catch (NullPointerException e) {
061: // The expected behavior.
062: }
063: }
064:
065: public void testConstructorAcceptsNullColumnNames() {
066: try {
067: new ExampleTableModel(new ArrayListModel<Object>());
068: } catch (NullPointerException e) {
069: fail("AbstractTableAdapter(ListModel) is correct if the ListModel is not null.");
070: }
071: try {
072: new ExampleTableModel(new ArrayListModel<Object>(), null);
073: } catch (NullPointerException e) {
074: fail("AbstractTableAdapter(ListModel, String[]) must accept a null columnName argument.");
075: }
076: }
077:
078: // Helper Code ************************************************************
079:
080: /**
081: * An example TableModel that presents an Album's title and artist.
082: */
083: private static final class ExampleTableModel extends
084: AbstractTableAdapter<Object> {
085:
086: private ExampleTableModel(ListModel listModel) {
087: super (listModel);
088: }
089:
090: private ExampleTableModel(ListModel listModel,
091: String[] columnNames) {
092: super (listModel, columnNames);
093: }
094:
095: public Object getValueAt(int rowIndex, int columnIndex) {
096: Object row = getRow(rowIndex);
097: switch (columnIndex) {
098: case 0:
099: return "Title of " + row;
100: case 1:
101: return "Artist of " + row;
102: default:
103: throw new IllegalStateException("Unknown column");
104: }
105: }
106:
107: }
108:
109: }
|