Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8269871: CellEditEvent: must not throw NPE in accessors
Reviewed-by: aghaisas
  • Loading branch information
Jeanette Winzenburg committed Sep 6, 2021
1 parent 2267b11 commit 78ae4a8
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 20 deletions.
Expand Up @@ -290,8 +290,8 @@ public TableColumn(String text) {
**************************************************************************/

private EventHandler<CellEditEvent<S,T>> DEFAULT_EDIT_COMMIT_HANDLER = t -> {
int index = t.getTablePosition().getRow();
List<S> list = t.getTableView().getItems();
int index = t.getTablePosition() != null ? t.getTablePosition().getRow() : -1;
List<S> list = t.getTableView() != null ? t.getTableView().getItems() : null;
if (list == null || index < 0 || index >= list.size()) return;
S rowData = list.get(index);
ObservableValue<T> ov = getCellObservableValue(rowData);
Expand Down Expand Up @@ -795,7 +795,7 @@ public CellEditEvent(TableView<S> table, TablePosition<S,T> pos,
* @return The TableView control upon which this event occurred.
*/
public TableView<S> getTableView() {
return pos.getTableView();
return pos != null ? pos.getTableView() : null;
}

/**
Expand All @@ -804,7 +804,7 @@ public TableView<S> getTableView() {
* @return The TableColumn that the edit occurred in.
*/
public TableColumn<S,T> getTableColumn() {
return pos.getTableColumn();
return pos != null ? pos.getTableColumn() : null;
}

/**
Expand Down Expand Up @@ -853,10 +853,10 @@ public T getOldValue() {
* @return the value for the row
*/
public S getRowValue() {
List<S> items = getTableView().getItems();
List<S> items = getTableView() != null ? getTableView().getItems() : null;
if (items == null) return null;

int row = pos.getRow();
int row = pos != null ? pos.getRow() : -1;
if (row < 0 || row >= items.size()) return null;

return items.get(row);
Expand Down
Expand Up @@ -285,7 +285,9 @@ public TreeTableColumn(String text) {

private EventHandler<TreeTableColumn.CellEditEvent<S,T>> DEFAULT_EDIT_COMMIT_HANDLER =
t -> {
ObservableValue<T> ov = getCellObservableValue(t.getRowValue());
TreeItem<S> rowValue = t.getRowValue();
if (rowValue == null) return;
ObservableValue<T> ov = getCellObservableValue(rowValue);
if (ov instanceof WritableValue) {
((WritableValue)ov).setValue(t.getNewValue());
}
Expand Down Expand Up @@ -772,7 +774,7 @@ public CellEditEvent(TreeTableView<S> table, TreeTablePosition<S,T> pos,
* @return The TableView control upon which this event occurred.
*/
public TreeTableView<S> getTreeTableView() {
return pos.getTreeTableView();
return pos != null ? pos.getTreeTableView() : null;
}

/**
Expand All @@ -781,7 +783,7 @@ public TreeTableView<S> getTreeTableView() {
* @return The TreeTableColumn that the edit occurred in.
*/
public TreeTableColumn<S,T> getTableColumn() {
return pos.getTableColumn();
return pos != null ? pos.getTableColumn() : null;
}

/**
Expand Down Expand Up @@ -830,12 +832,10 @@ public T getOldValue() {
* @return the row value
*/
public TreeItem<S> getRowValue() {
// List<S> items = getTreeTableView().getItems();
// if (items == null) return null;

TreeTableView<S> treeTable = getTreeTableView();
int row = pos.getRow();
if (row < 0 || row >= treeTable.getExpandedItemCount()) return null;
int row = pos != null ? pos.getRow() : -1;
int expandedItemCount = treeTable != null ? treeTable.getExpandedItemCount() : 0;
if (row < 0 || row >= expandedItemCount) return null;

return treeTable.getTreeItem(row);
}
Expand Down
@@ -0,0 +1,190 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package test.javafx.scene.control;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import static javafx.scene.control.TableColumn.*;
import static org.junit.Assert.*;

import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;

/**
* Test cell edit event for TableColumn: must not throw NPE in accessors (JDK-8269871).
*/
public class CellEditEventOfTableColumnTest {

private TableView<String> table;
private TableColumn<String, String> editingColumn;

//---------------- default commit handler

@Test
public void testDefaultOnCommitHandlerTablePositionWithNullTable() {
String edited = "edited";
TablePosition<String, String> pos = new TablePosition<>(null, 1, editingColumn);
CellEditEvent<String, String> event = new CellEditEvent<>(table, pos, editCommitEvent(), edited);
Event.fireEvent(editingColumn, event);
}

@Test
public void testDefaultOnCommitHandlerNullTablePosition() {
String edited = "edited";
CellEditEvent<String, String> event = new CellEditEvent<>(table, null, editCommitEvent(), edited);
Event.fireEvent(editingColumn, event);
}

//---------------- accessors in CellEditEvent

@Test
public void testNullTablePositionGetTableView() {
CellEditEvent<String, String> ev = new CellEditEvent<>(table, null, editAnyEvent(), null);
assertNull("table must be null for null pos", ev.getTableView());
}

@Test
public void testNullTablePositionGetTableColumn() {
CellEditEvent<String, String> ev = new CellEditEvent<>(table, null, editAnyEvent(), null);
assertNull("column must be null for null pos", ev.getTableColumn());
}

@Test
public void testNullTablePositionGetOldValue() {
CellEditEvent<String, String> ev = new CellEditEvent<>(table, null, editAnyEvent(), null);
assertNull("oldValue must be null for null pos", ev.getOldValue());
}

@Test
public void testNullTablePositionGetRowValue() {
CellEditEvent<String, String> ev = new CellEditEvent<>(table, null, editAnyEvent(), null);
assertNull("rowValue must be null for null pos", ev.getRowValue());
}

@Test
public void testNullTablePositionGetNewValue() {
String editedValue = "edited";
CellEditEvent<String, String> ev = new CellEditEvent<>(table, null, editAnyEvent(), editedValue);
assertEquals("editedValue must be available for null pos", editedValue, ev.getNewValue());
}

@Test
public void testTablePositionWithNullTable() {
String editedValue = "edited";
TablePosition<String, String> pos = new TablePosition<>(null, 1, editingColumn);
CellEditEvent<String, String> ev = new CellEditEvent<>(table, pos, editAnyEvent(), editedValue);
assertNull("rowValue must be null for null pos", ev.getRowValue());
}

//---------- event source

@Ignore("JDK-8271474")
@Test
public void testNullTable() {
new CellEditEvent<Object, Object>(null, // null table must not throw NPE
new TablePosition<>(null, -1, null), editAnyEvent(), null);
}

@Test
public void testCellEditEventDifferentSource() {
assertCellEditEvent(new TableView<>());
}

@Test
public void testCellEditEventSameSource() {
assertCellEditEvent(table);
}

@Ignore("JDK-8271474")
@Test
public void testCellEditEventNullSource() {
assertCellEditEvent(null);
}

/**
* Creates a CellEditEvent with the given source and TablePosition
* having default values and asserts its state.
*/
private void assertCellEditEvent(TableView<String> source) {
int editingRow = 1;
String editedValue = "edited";
String rowValue = table.getItems().get(editingRow);
String oldValue = editingColumn.getCellData(editingRow);
TablePosition<String, String> pos = new TablePosition<>(table, editingRow, editingColumn);
CellEditEvent<String, String> event = new CellEditEvent<>(source, pos, editAnyEvent(), editedValue);
if (source != null) {
assertEquals(source, event.getSource());
}
assertCellEditEventState(event, table, editingColumn, pos, editedValue, oldValue, rowValue);
}

/**
* Asserts state of the CellEditEvent.
*/
private <S, T> void assertCellEditEventState(CellEditEvent<S, T> event,
TableView<S> table, TableColumn<S, T> tableColumn, TablePosition<S, T> pos,
T newValue, T oldValue, S rowValue) {
assertEquals(newValue, event.getNewValue());
assertEquals(oldValue, event.getOldValue());
assertEquals(rowValue, event.getRowValue());
assertEquals(tableColumn, event.getTableColumn());
assertEquals(pos, event.getTablePosition());
assertEquals(table, event.getTableView());
}

//------------ init

@Before public void setup() {
Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> {
if (throwable instanceof RuntimeException) {
throw (RuntimeException)throwable;
} else {
Thread.currentThread().getThreadGroup().uncaughtException(thread, throwable);
}
});

ObservableList<String> model = FXCollections.observableArrayList("Four", "Five", "Fear");
table = new TableView<String>(model);
editingColumn = new TableColumn<>("TEST");
editingColumn.setCellValueFactory(e -> new SimpleStringProperty(e.getValue()));
table.getColumns().addAll(editingColumn);
}

@After
public void cleanup() {
Thread.currentThread().setUncaughtExceptionHandler(null);
}

}

1 comment on commit 78ae4a8

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.