Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/src/org/sbml/jsbml/AbstractTreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ public void firePropertyChange(String propertyName, Object oldValue,
changeType = 0; // element added
} else if ((oldValue != null) && (newValue == null)) {
changeType = 1; // element removed
} else if ((oldValue != null) && !oldValue.equals(newValue)) {
} else if ((oldValue != null) &&
(!oldValue.equals(newValue) || oldValue != newValue)) {
changeType = 2; // real property change
}
if (-1 < changeType) {
Expand Down
67 changes: 67 additions & 0 deletions core/test/org/sbml/jsbml/test/PropertyChangeNaNTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* ----------------------------------------------------------------------------
* This file is part of JSBML. Please visit <http://sbml.org/Software/JSBML>
* for the latest version of JSBML and more information about SBML.
*
* Copyright (C) 2009-2022 jointly by the following organizations:
* 1. The University of Tuebingen, Germany
* 2. EMBL European Bioinformatics Institute (EBML-EBI), Hinxton, UK
* 3. The California Institute of Technology, Pasadena, CA, USA
* 4. The University of California, San Diego, La Jolla, CA, USA
* 5. The Babraham Institute, Cambridge, UK
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation. A copy of the license agreement is provided
* in the file named "LICENSE.txt" included with this software distribution
* and also available online as <http://sbml.org/Software/JSBML/License>.
* ----------------------------------------------------------------------------
*/

package org.sbml.jsbml.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.sbml.jsbml.Parameter;
import org.sbml.jsbml.util.SimpleTreeNodeChangeListener;
import org.sbml.jsbml.util.TreeNodeChangeEvent;

/**
* Tests for property-change events when working with NaN values.
*
* This is a regression test for PR #273:
* when a double property is internally stored as NaN and is explicitly
* set to NaN again, a property-change event should be fired.
*/
public class PropertyChangeNaNTest {

private static class CountingListener extends SimpleTreeNodeChangeListener {
int count = 0;

public void propertyChange(TreeNodeChangeEvent evt) {
count++;
}
}

@Test
public void firesPropertyChangeWhenUndefinedDoubleSetToNaN() {
// Parameter value defaults to "undefined", which is internally represented as NaN.
Parameter p = new Parameter(3, 1);

CountingListener listener = new CountingListener();
p.addTreeNodeChangeListener(listener);

// Sanity check: value is NaN before we start.
assertTrue(Double.isNaN(p.getValue()));

// Explicitly set the value to NaN again.
listener.count = 0;
p.setValue(Double.NaN);

// With the fix in AbstractTreeNode.firePropertyChange, this should now
// be treated as a real property change and fire exactly one event.
assertEquals(1, listener.count);
}
}