SelectionOperation implements Operation { public void setProperties(Properties p) { double valueD = 1.0; String key; int valueI = Integer.parseInt(Constants.DEFAULT_POPULATION_SIZE); key = Constants.POPULATION_SIZE; valueI = Integer.parseInt(p.getProperty(key, String.valueOf(valueI))); key = Constants.REPLACEMENT_TYPE; if (p.getProperty(key) != null) { if (p.getProperty(key).equals(Constants.STEADY_STATE)) { valueD = 2.0 / valueI; } else { if (p.getProperty(key).equals(Constants.GENERATIONAL)) { valueD = 1.0; } } } else { key = Constants.SELECTION_SIZE; valueD = Double.parseDouble(p.getProperty(key, "1.0")); if (valueD < 0.0 || valueD > 1.0) { System.err.println(this.getClass().getName() + ".setProperties() bad value for parameter -" + Constants.SELECTION_SIZE + ":" + valueD + " must be between 0.0 and 1.0"); throw new IllegalArgumentException(); } } this.size = (int) (valueD * valueI); this.selectedPopulation = new SimplePopulation(this.size); } SelectionOperation(int size);  SelectionOperation(Properties p);  SelectionOperation(); void setProperties(Properties p); @SuppressWarnings({"EmptyMethod"}) abstract void doOperation(Individual operand); abstract void doOperation(List<Individual> operands); Population getSelectedPopulation(); int getSize();  }
@Test public void testSetProperties() { System.out.println("* SelectionOperation: setProperties"); Properties p = GrammarCreator.getProperties(); SelectionOperation instance = new SelectionOperationMock(); p.setProperty(Constants.REPLACEMENT_TYPE, Constants.STEADY_STATE); p.setProperty(Constants.POPULATION_SIZE, "10"); instance.setProperties(p); assertEquals(instance.size,2); p = GrammarCreator.getProperties(); instance = new SelectionOperationMock(); p.setProperty(Constants.REPLACEMENT_TYPE, Constants.GENERATIONAL); p.setProperty(Constants.POPULATION_SIZE, "10"); instance.setProperties(p); assertEquals(instance.size,10); p = GrammarCreator.getProperties(); instance = new SelectionOperationMock(); p.setProperty(Constants.SELECTION_SIZE, "0.5"); p.setProperty(Constants.POPULATION_SIZE, "10"); p.remove(Constants.REPLACEMENT_TYPE); instance.setProperties(p); assertEquals(instance.size,5); p = GrammarCreator.getProperties(); instance = new SelectionOperationMock(); p.setProperty(Constants.SELECTION_SIZE, "-0.5"); p.setProperty(Constants.POPULATION_SIZE, "10"); p.remove(Constants.REPLACEMENT_TYPE); try { instance.setProperties(p); } catch(IllegalArgumentException e) { assertTrue(true); } assertEquals(instance.size,0); }