BoundedArchive extends AbstractArchive { @Override public boolean addCheckedIndividual(Individual individual) { if (!contains(individual) && individuals.size() >= capacity) { throw new IndexOutOfBoundsException(); } return super.addCheckedIndividual(individual); } @Inject  BoundedArchive(int capacity); void setCapacity(int capacity); int getCapacity(); @Override boolean addCheckedIndividual(Individual individual); @Override boolean addCheckedIndividuals(Collection<? extends Individual> c);  }
@Test public void addCheckedIndividual() { Individual individual1 = mock(Individual.class); BoundedArchive archive = new BoundedArchive(1) { @Override protected boolean updateWithNondominated(Collection<Individual> candidates) { individuals.addAll(candidates); return true; } }; archive.addCheckedIndividual(individual1); Assert.assertTrue(archive.contains(individual1)); Assert.assertEquals(1, archive.size()); }
@Test public void addCheckedIndividualTwice() { Individual individual1 = mock(Individual.class); BoundedArchive archive = new BoundedArchive(1) { @Override protected boolean updateWithNondominated(Collection<Individual> candidates) { individuals.addAll(candidates); return true; } }; archive.addCheckedIndividual(individual1); archive.addCheckedIndividual(individual1); Assert.assertTrue(archive.contains(individual1)); Assert.assertEquals(1, archive.size()); }
@Test(expected = IndexOutOfBoundsException.class) public void addCheckedIndividualDespiteArchiveFull() { Individual individual1 = mock(Individual.class); Individual individual2 = mock(Individual.class); BoundedArchive archive = new BoundedArchive(1) { @Override protected boolean updateWithNondominated(Collection<Individual> candidates) { individuals.addAll(candidates); return true; } }; archive.addCheckedIndividual(individual1); Assert.assertTrue(archive.contains(individual1)); Assert.assertEquals(1, archive.size()); archive.addCheckedIndividual(individual2); }