/**
 * Copyright (C) 2003 - 2009
 * Computational Intelligence Research Group (CIRG@UP)
 * Department of Computer Science
 * University of Pretoria
 * South Africa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package net.sourceforge.cilib.entity.topologies;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.is;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import net.sourceforge.cilib.ec.Individual;
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.entity.EntityType;
import net.sourceforge.cilib.entity.Particle;
import net.sourceforge.cilib.entity.Topology;
import net.sourceforge.cilib.entity.comparator.AscendingFitnessComparator;
import net.sourceforge.cilib.entity.comparator.DescendingFitnessComparator;
import net.sourceforge.cilib.entity.comparator.SocialBestFitnessComparator;
import net.sourceforge.cilib.problem.MaximisationFitness;
import net.sourceforge.cilib.problem.MinimisationFitness;
import net.sourceforge.cilib.pso.PSO;

import net.sourceforge.cilib.pso.particle.StandardParticle;
import org.junit.Assert;
import org.junit.Test;

/**
 *
 * @author Gary Pampara
 */
public class AbstractTopologyTest {

    @Test
    public void comparatorBestEntity() {
        Particle i1 = new StandardParticle();
        Particle i2 = new StandardParticle();
        Particle i3 = new StandardParticle();

        i1.getProperties().put(EntityType.FITNESS, new MinimisationFitness(0.0));
        i2.getProperties().put(EntityType.FITNESS, new MinimisationFitness(1.0));
        i3.getProperties().put(EntityType.FITNESS, new MinimisationFitness(0.5));

        i1.getProperties().put(EntityType.Particle.BEST_FITNESS, new MinimisationFitness(0.0));
        i2.getProperties().put(EntityType.Particle.BEST_FITNESS, new MinimisationFitness(1.0));
        i3.getProperties().put(EntityType.Particle.BEST_FITNESS, new MinimisationFitness(0.5));

        Topology<Particle> topology = new GBestTopology<Particle>();
        topology.add(i1);
        topology.add(i2);
        topology.add(i3);

        Particle socialBest = topology.getBestEntity(new SocialBestFitnessComparator());
        topology.clearBestEntity();
        Particle mostFit = topology.getBestEntity(new AscendingFitnessComparator());
        topology.clearBestEntity();
        Particle leastFit = topology.getBestEntity(new DescendingFitnessComparator());
        topology.clearBestEntity();
        Particle other = topology.getBestEntity();

        Assert.assertThat(socialBest, is(other));
        Assert.assertThat(mostFit, is(i1));
        Assert.assertThat(leastFit, is(i2));
    }

    @Test
    public void compareBestFitnessEntity() {
        Particle p1 = new StandardParticle();
        Particle p2 = new StandardParticle();

        p1.getProperties().put(EntityType.FITNESS, new MaximisationFitness(400.0));
        p2.getProperties().put(EntityType.FITNESS, new MaximisationFitness(0.0));

        Topology<Particle> topology = new GBestTopology<Particle>();
        topology.add(p1);
        topology.add(p2);

        Particle best = topology.getBestEntity();

        Assert.assertThat(best, is(p1));
    }


    /**
     * Test the setter method for the IoC container
     *
     */
    @Test
    public void testIoCSetterMethod() {

        try {
            PSO p = new PSO();

            Method m = p.getClass().getMethod("setTopology", new Class[] {Topology.class});

            Topology<Individual> top1 = new LBestTopology<Individual>();
            Topology<Particle> top2 = new VonNeumannTopology<Particle>();
            Topology<Entity> top3 = new HypercubeTopology<Entity>();

            m.invoke(p, top1);
            assertTrue(p.getTopology() instanceof LBestTopology);

            m.invoke(p, top2);
            assertTrue(p.getTopology() instanceof VonNeumannTopology);

            m.invoke(p, top3);
            assertTrue(p.getTopology() instanceof HypercubeTopology);

        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testGeneric() {
        PSO p = new PSO();
        List<Particle> list = p.getTopology().asList();

        assertEquals(p.getTopology().size(), list.size());

        for (Particle particle : p.getTopology()) {
            assertTrue(list.contains(particle));
        }
    }

}

@@ -949,319 +949,35 @@
 org.
-junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertTrue;%0Aimport static org.hamcrest.CoreMatchers.is;%0A%0Aimport java.lang.reflect.InvocationTargetException;%0Aimport java.lang.reflect.Method;%0Aimport java.util.List;%0A%0Aimport net.sourceforge.cilib.ec.Individual;%0Aimport net.sourceforge.cilib.entity.Entity;
+hamcrest.CoreMatchers.is;%0A%0A
 %0Aimp
@@ -1459,46 +1459,8 @@
 ess;
-%0Aimport net.sourceforge.cilib.pso.PSO;
 %0A%0Aim
@@ -1513,24 +1513,144 @@
 rdParticle;%0A
+import org.jmock.Mockery;%0Aimport org.jmock.integration.junit4.JMock;%0Aimport org.jmock.integration.junit4.JUnit4Mockery;%0A
 import org.j
@@ -1684,16 +1684,49 @@
 it.Test;
+%0Aimport org.junit.runner.RunWith;
 %0A%0A/**%0A *
@@ -1754,16 +1754,38 @@
 ara%0A */%0A
+@RunWith(JMock.class)%0A
 public c
@@ -1811,16 +1811,67 @@
 gyTest %7B
+%0A    private Mockery context = new JUnit4Mockery();
 %0A%0A    @T
@@ -3845,1502 +3845,6 @@
  %7D%0A%0A
-%0A    /**%0A     * Test the setter method for the IoC container%0A     *%0A     */%0A    @Test%0A    public void testIoCSetterMethod() %7B%0A%0A        try %7B%0A            PSO p = new PSO();%0A%0A            Method m = p.getClass().getMethod(%22setTopology%22, new Class%5B%5D %7BTopology.class%7D);%0A%0A            Topology%3CIndividual%3E top1 = new LBestTopology%3CIndividual%3E();%0A            Topology%3CParticle%3E top2 = new VonNeumannTopology%3CParticle%3E();%0A            Topology%3CEntity%3E top3 = new HypercubeTopology%3CEntity%3E();%0A%0A            m.invoke(p, top1);%0A            assertTrue(p.getTopology() instanceof LBestTopology);%0A%0A            m.invoke(p, top2);%0A            assertTrue(p.getTopology() instanceof VonNeumannTopology);%0A%0A            m.invoke(p, top3);%0A            assertTrue(p.getTopology() instanceof HypercubeTopology);%0A%0A        %7D catch (SecurityException e) %7B%0A            e.printStackTrace();%0A        %7D catch (NoSuchMethodException e) %7B%0A            e.printStackTrace();%0A        %7D catch (IllegalArgumentException e) %7B%0A            e.printStackTrace();%0A        %7D catch (IllegalAccessException e) %7B%0A            e.printStackTrace();%0A        %7D catch (InvocationTargetException e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void testGeneric() %7B%0A        PSO p = new PSO();%0A        List%3CParticle%3E list = p.getTopology().asList();%0A%0A        assertEquals(p.getTopology().size(), list.size());%0A%0A        for (Particle particle : p.getTopology()) %7B%0A            assertTrue(list.contains(particle));%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Remove useless tests.