/**************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 complex.toolkit.awtgrid;
import java.lang.reflect.Method;
import com.sun.star.awt.grid.GridDataEvent;
import com.sun.star.awt.grid.XMutableGridDataModel;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.IndexOutOfBoundsException;
import static org.junit.Assert.*;
import static complex.toolkit.Assert.*;
/** test for the css.awt.grid.XMutableGridData
interface
*
* @author frank.schoenheit@oracle.com
*/
public class TMutableGridDataModel
{
public TMutableGridDataModel( final XMutableGridDataModel i_dataModel )
{
m_dataModel = i_dataModel;
m_listener = new GridDataListener();
m_dataModel.addGridDataListener( m_listener );
}
/*
* tests the XMutableGridDataModel.addRow method
*/
public void testAddRow() throws IndexOutOfBoundsException
{
m_dataModel.addRow( m_rowHeadings[0], m_rowValues[0] );
GridDataEvent event = m_listener.assertSingleRowInsertionEvent();
m_listener.reset();
assertEquals( "row insertion: wrong FirstRow (1)", 0, event.FirstRow );
assertEquals( "row insertion: wrong LastRow (1)", 0, event.LastRow );
impl_assertRowData( 0 );
m_dataModel.addRow( m_rowHeadings[1], m_rowValues[1] );
event = m_listener.assertSingleRowInsertionEvent();
m_listener.reset();
assertEquals( "row insertion: wrong FirstRow (2)", 1, event.FirstRow );
assertEquals( "row insertion: wrong LastRow (2)", 1, event.LastRow );
impl_assertRowData( 1 );
}
/**
* tests the XMutableGridDataModel.addRows method
*/
public void testAddRows() throws IndexOutOfBoundsException, IllegalArgumentException
{
assertEquals( "precondition not met: call this directly after testAddRow, please!", 2, m_dataModel.getRowCount() );
m_dataModel.addRows(
new Object[] { m_rowHeadings[2], m_rowHeadings[3], m_rowHeadings[4] },
new Object[][] { m_rowValues[2], m_rowValues[3], m_rowValues[4] } );
GridDataEvent event = m_listener.assertSingleRowInsertionEvent();
assertEquals( "row insertion: wrong FirstRow (1)", 2, event.FirstRow );
assertEquals( "row insertion: wrong LastRow (1)", 4, event.LastRow );
m_listener.reset();
assertEquals( "data model's row count is not adjusted when adding rows", m_rowValues.length, m_dataModel.getRowCount() );
assertEquals( "data model's column count is not adjusted when adding rows", m_rowValues[0].length, m_dataModel.getColumnCount() );
for ( int row=0; row rowCount is expected to throw",
m_dataModel, "insertRow",
new Class[] { Integer.class, Object.class, Object[].class },
new Object[] { expectedRowCount + 1, "", new Object[] { "1", 2, 3 } },
IndexOutOfBoundsException.class );
assertException( "inserting a row at a position < 0 is expected to throw",
m_dataModel, "insertRow",
new Class[] { Integer.class, Object.class, Object[].class },
new Object[] { -1, "", new Object[] { "1", 2, 3 } },
IndexOutOfBoundsException.class );
// remove the row, to create the situation expected by the next test
m_dataModel.removeRow( insertionPos );
m_listener.reset();
}
/**
* tests the XMutableGridDataModel.insertRows method
*/
public void testInsertRows() throws IndexOutOfBoundsException, IllegalArgumentException
{
int expectedRowCount = m_rowValues.length;
assertEquals( "precondition not met: call this directly after testInsertRow, please!", expectedRowCount, m_dataModel.getRowCount() );
// inserting some rows somewhere between the other rows
final int insertionPos = 3;
final Object[] rowHeadings = new Object[] { "A", "B", "C" };
final Object[][] rowData = new Object[][] {
new Object[] { "A", "B", "C", "D", "E" },
new Object[] { "J", "I", "H", "G", "F" },
new Object[] { "K", "L", "M", "N", "O" }
};
final int insertedRowCount = rowData.length;
assertEquals( "invalid test data", rowHeadings.length, insertedRowCount );
m_dataModel.insertRows( insertionPos, rowHeadings, rowData );
expectedRowCount += insertedRowCount;
final GridDataEvent event = m_listener.assertSingleRowInsertionEvent();
assertEquals( "inserting multiple rows results in wrong FirstRow being notified",
insertionPos, event.FirstRow );
assertEquals( "inserting multiple rows results in wrong LastRow being notified",
insertionPos + insertedRowCount - 1, event.LastRow );
m_listener.reset();
for ( int row=0; row= insertionPos ) && ( row < insertionPos + insertedRowCount )
? rowData[ row - insertionPos ]
: m_rowValues[ row - insertedRowCount ];
assertArrayEquals( "row number " + row + " has wrong content content after inserting multiple rows",
expectedRowData, actualRowData );
final Object actualHeading = m_dataModel.getRowHeading(row);
final Object expectedHeading =
( row < insertionPos )
? m_rowHeadings[ row ]
: ( row >= insertionPos ) && ( row < insertionPos + insertedRowCount )
? rowHeadings[ row - insertionPos ]
: m_rowHeadings[ row - insertedRowCount ];
assertEquals( "row " + row + " has a wrong heading after invoking insertRows",
expectedHeading, actualHeading );
}
// exceptions
assertException( "inserting multiple rows at a position > rowCount is expected to throw an IndexOutOfBoundsException",
m_dataModel, "insertRows",
new Class[] { Integer.class, Object[].class, Object[][].class },
new Object[] { expectedRowCount + 1, new Object[0], new Object[][] { } },
IndexOutOfBoundsException.class );
assertException( "inserting multiple rows at a position < 0 is expected to throw an IndexOutOfBoundsException",
m_dataModel, "insertRows",
new Class[] { Integer.class, Object[].class, Object[][].class },
new Object[] { -1, new Object[0], new Object[][] { } },
IndexOutOfBoundsException.class );
assertException( "inserting multiple rows with inconsistent array lengths is expected to throw an IllegalArgumentException",
m_dataModel, "insertRows",
new Class[] { Integer.class, Object[].class, Object[][].class },
new Object[] { 0, new Object[0], new Object[][] { new Object[0] } },
IllegalArgumentException.class );
// remove the row, to create the situation expected by the next test
for ( int i=0; i