Fixed TestRocketOptimizationFunction unit tests

This commit is contained in:
Daniel_M_Williams 2015-12-25 13:33:46 -05:00
parent 53b20eb999
commit 711f71e2c8
4 changed files with 29 additions and 16 deletions

View File

@ -1,6 +1,5 @@
package net.sf.openrocket.document; package net.sf.openrocket.document;
import java.util.Collection;
import java.util.EventListener; import java.util.EventListener;
import java.util.EventObject; import java.util.EventObject;
import java.util.List; import java.util.List;
@ -13,7 +12,6 @@ import net.sf.openrocket.aerodynamics.BarrowmanCalculator;
import net.sf.openrocket.aerodynamics.WarningSet; import net.sf.openrocket.aerodynamics.WarningSet;
import net.sf.openrocket.formatting.RocketDescriptor; import net.sf.openrocket.formatting.RocketDescriptor;
import net.sf.openrocket.masscalc.MassCalculator; import net.sf.openrocket.masscalc.MassCalculator;
import net.sf.openrocket.motor.MotorConfiguration;
import net.sf.openrocket.rocketcomponent.FlightConfiguration; import net.sf.openrocket.rocketcomponent.FlightConfiguration;
import net.sf.openrocket.rocketcomponent.FlightConfigurationId; import net.sf.openrocket.rocketcomponent.FlightConfigurationId;
import net.sf.openrocket.rocketcomponent.Rocket; import net.sf.openrocket.rocketcomponent.Rocket;
@ -147,6 +145,10 @@ public class Simulation implements ChangeSource, Cloneable {
this.name = name; this.name = name;
this.options = options; this.options = options;
FlightConfigurationId fcid = rocket.getSelectedConfiguration().getFlightConfigurationID();
options.setFlightConfigurationId(fcid);
options.addChangeListener(new ConditionListener()); options.addChangeListener(new ConditionListener());
if (extensions != null) { if (extensions != null) {
@ -280,6 +282,13 @@ public class Simulation implements ChangeSource, Cloneable {
status = Status.OUTDATED; status = Status.OUTDATED;
} }
} }
// if the id hasn't been set yet, skip.
if ( options.getId().hasError() ){
log.warn(" simulationOptions lacks a valid id. Skipping.");
status = Status.CANT_RUN;
return status;
}
FlightConfiguration config = rocket.getFlightConfiguration(options.getId()); FlightConfiguration config = rocket.getFlightConfiguration(options.getId());

View File

@ -568,7 +568,9 @@ public class Rocket extends RocketComponent {
public FlightConfiguration createFlightConfiguration( final FlightConfigurationId fcid) { public FlightConfiguration createFlightConfiguration( final FlightConfigurationId fcid) {
checkState(); checkState();
if( fcid.hasError() ){ if( null == fcid ){
throw new NullPointerException("Attempted to create a flightConfiguration from a null key!");
}else if( fcid.hasError() ){
throw new NullPointerException("Attempted to create a flightConfiguration from an error key!"); throw new NullPointerException("Attempted to create a flightConfiguration from an error key!");
}else if( configSet.containsKey(fcid)){ }else if( configSet.containsKey(fcid)){
return this.configSet.get(fcid); return this.configSet.get(fcid);
@ -815,6 +817,7 @@ public class Rocket extends RocketComponent {
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
buf.append(String.format("====== Dumping %d Configurations from rocket: \n", this.getConfigurationCount(), this.getName())); buf.append(String.format("====== Dumping %d Configurations from rocket: \n", this.getConfigurationCount(), this.getName()));
final String fmt = " [%-12s]: %s\n"; final String fmt = " [%-12s]: %s\n";
buf.append(String.format(fmt, " *SELECTED* ", selectedConfiguration.getName() ));
for( FlightConfiguration config : this.configSet.values() ){ for( FlightConfiguration config : this.configSet.values() ){
String shortKey = config.getId().toShortKey(); String shortKey = config.getId().toShortKey();
if( this.selectedConfiguration.equals( config)){ if( this.selectedConfiguration.equals( config)){

View File

@ -51,7 +51,7 @@ public class SimulationOptions implements ChangeSource, Cloneable {
protected final Preferences preferences = Application.getPreferences(); protected final Preferences preferences = Application.getPreferences();
private final Rocket rocket; private final Rocket rocket;
private FlightConfigurationId configId = new FlightConfigurationId(); private FlightConfigurationId configId = FlightConfigurationId.ERROR_FCID;
/* /*
* NOTE: When adding/modifying parameters, they must also be added to the * NOTE: When adding/modifying parameters, they must also be added to the
@ -439,7 +439,6 @@ public class SimulationOptions implements ChangeSource, Cloneable {
if (this.rocket == src.rocket) { if (this.rocket == src.rocket) {
this.configId = src.configId; this.configId = src.configId;
} else { } else {
if (src.rocket.hasMotors(src.configId)) { if (src.rocket.hasMotors(src.configId)) {
// First check for exact match: // First check for exact match:
if (this.rocket.containsFlightConfigurationID(src.configId)) { if (this.rocket.containsFlightConfigurationID(src.configId)) {
@ -462,7 +461,7 @@ public class SimulationOptions implements ChangeSource, Cloneable {
this.configId = matchID; this.configId = matchID;
} }
} else { } else {
this.configId = null; this.configId = FlightConfigurationId.ERROR_FCID;
} }
} }
@ -588,7 +587,7 @@ public class SimulationOptions implements ChangeSource, Cloneable {
*/ */
@Override @Override
public int hashCode() { public int hashCode() {
if (configId == null) if (configId.hasError())
return rocket.hashCode(); return rocket.hashCode();
return rocket.hashCode() + configId.hashCode(); return rocket.hashCode() + configId.hashCode();
} }

View File

@ -3,6 +3,15 @@ package net.sf.openrocket.optimization.rocketoptimization;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.auto.Mock;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.Test;
import org.junit.runner.RunWith;
import net.sf.openrocket.document.Simulation; import net.sf.openrocket.document.Simulation;
import net.sf.openrocket.optimization.general.OptimizationException; import net.sf.openrocket.optimization.general.OptimizationException;
import net.sf.openrocket.optimization.general.Point; import net.sf.openrocket.optimization.general.Point;
@ -13,14 +22,6 @@ import net.sf.openrocket.unit.Value;
import net.sf.openrocket.util.Pair; import net.sf.openrocket.util.Pair;
import net.sf.openrocket.util.BaseTestCase.BaseTestCase; import net.sf.openrocket.util.BaseTestCase.BaseTestCase;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.auto.Mock;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(JMock.class) @RunWith(JMock.class)
public class TestRocketOptimizationFunction extends BaseTestCase { public class TestRocketOptimizationFunction extends BaseTestCase {
@ -222,12 +223,13 @@ public class TestRocketOptimizationFunction extends BaseTestCase {
@Test @Test
public void testNewSimulationInstance() { public void testNewSimulationNames() {
final Rocket rocket = new Rocket(); final Rocket rocket = new Rocket();
rocket.setName("Foobar"); rocket.setName("Foobar");
final Simulation simulation = new Simulation(rocket); final Simulation simulation = new Simulation(rocket);
simulation.setName("MySim"); simulation.setName("MySim");
RocketOptimizationFunction function = new RocketOptimizationFunction(simulation, RocketOptimizationFunction function = new RocketOptimizationFunction(simulation,
parameter, goal, domain, modifier1, modifier2); parameter, goal, domain, modifier1, modifier2);