public void testRegisterCorrectCommand() {
   CommandFactory.registerCommand("test", Test.class);
   Assert.assertEquals(1, CommandFactory.getCommands().size());
   Assert.assertEquals(Test.class, CommandFactory.getCommands().get("test"));
   CommandFactory.unregisterCommand(Test.class, "test");
   Assert.assertEquals(0, CommandFactory.getCommands().size());
 }
  public void testNonExistingCommand() throws BadCommandException {
    ProtocolProviderServiceIrcImpl provider =
        EasyMock.createMock(ProtocolProviderServiceIrcImpl.class);
    IrcConnection connection = EasyMock.createMock(IrcConnection.class);
    EasyMock.replay(provider, connection);

    try {
      CommandFactory factory = new CommandFactory(provider, connection);
      factory.createCommand("test");
      Assert.fail();
    } catch (UnsupportedCommandException e) {
    }
  }
 public void testUnregisterOneAmongMultipleSameType() {
   CommandFactory.registerCommand("test", Test.class);
   CommandFactory.registerCommand("bla", Test.class);
   Assert.assertEquals(Test.class, CommandFactory.getCommands().get("test"));
   Assert.assertEquals(Test.class, CommandFactory.getCommands().get("bla"));
   Assert.assertEquals(2, CommandFactory.getCommands().size());
   CommandFactory.unregisterCommand(Test.class, "test");
   Assert.assertEquals(1, CommandFactory.getCommands().size());
   Assert.assertEquals(Test.class, CommandFactory.getCommands().get("bla"));
   CommandFactory.unregisterCommand(Test.class, null);
 }
  public void testCreateNullCommandName() throws UnsupportedCommandException, BadCommandException {
    ProtocolProviderServiceIrcImpl provider =
        EasyMock.createMock(ProtocolProviderServiceIrcImpl.class);
    IrcConnection connection = EasyMock.createMock(IrcConnection.class);
    EasyMock.replay(provider, connection);

    CommandFactory.registerCommand("test", Test.class);
    try {
      CommandFactory factory = new CommandFactory(provider, connection);
      factory.createCommand(null);
      Assert.fail();
    } catch (IllegalArgumentException e) {
    }
    CommandFactory.unregisterCommand(Test.class, null);
  }
 public void testRegisterNullType() {
   try {
     CommandFactory.registerCommand("test", null);
     Assert.fail();
   } catch (IllegalArgumentException e) {
   }
 }
 public void testRegisterNullCommand() {
   try {
     CommandFactory.registerCommand(null, Test.class);
     Assert.fail();
   } catch (IllegalArgumentException e) {
   }
 }
  public void testBadCommand() throws UnsupportedCommandException {
    ProtocolProviderServiceIrcImpl provider =
        EasyMock.createMock(ProtocolProviderServiceIrcImpl.class);
    IrcConnection connection = EasyMock.createMock(IrcConnection.class);
    EasyMock.replay(provider, connection);

    CommandFactory.registerCommand("test", BadImplementation.class);
    try {
      CommandFactory factory = new CommandFactory(provider, connection);
      factory.createCommand("test");
      Assert.fail();
    } catch (BadCommandException e) {
    } finally {
      CommandFactory.unregisterCommand(BadImplementation.class, null);
    }
  }
  public void testExistingCommand() throws UnsupportedCommandException, BadCommandException {
    ProtocolProviderServiceIrcImpl provider =
        EasyMock.createMock(ProtocolProviderServiceIrcImpl.class);
    IrcConnection connection = EasyMock.createMock(IrcConnection.class);
    EasyMock.replay(provider, connection);

    CommandFactory.registerCommand("test", Test.class);
    try {
      CommandFactory factory = new CommandFactory(provider, connection);
      Command cmd = factory.createCommand("test");
      Assert.assertNotNull(cmd);
      Assert.assertTrue(cmd instanceof Test);
    } finally {
      CommandFactory.unregisterCommand(Test.class, null);
    }
  }
 public void testRegisterMultipleCommandsForType() {
   CommandFactory.registerCommand("test", Test.class);
   CommandFactory.registerCommand("bla", Test.class);
   Assert.assertEquals(Test.class, CommandFactory.getCommands().get("test"));
   Assert.assertEquals(Test.class, CommandFactory.getCommands().get("bla"));
   Assert.assertEquals(2, CommandFactory.getCommands().size());
   CommandFactory.unregisterCommand(Test.class, null);
   Assert.assertEquals(0, CommandFactory.getCommands().size());
 }
  public void testUnregisterMultipleAmongOtherTypes() {
    Command anotherType =
        new Command() {

          @Override
          public void execute(String source, String line) {}

          @Override
          public String help() {
            return null;
          }
        };
    CommandFactory.registerCommand("test", Test.class);
    CommandFactory.registerCommand("foo", anotherType.getClass());
    CommandFactory.registerCommand("bla", Test.class);
    Assert.assertEquals(Test.class, CommandFactory.getCommands().get("test"));
    Assert.assertEquals(Test.class, CommandFactory.getCommands().get("bla"));
    Assert.assertNotNull(CommandFactory.getCommands().get("foo"));
    Assert.assertEquals(3, CommandFactory.getCommands().size());
    CommandFactory.unregisterCommand(Test.class, null);
    Assert.assertEquals(1, CommandFactory.getCommands().size());
    Assert.assertNotSame(Test.class, CommandFactory.getCommands().get("foo"));
    CommandFactory.unregisterCommand(anotherType.getClass(), null);
  }
 public void testCommandsAvailable() {
   Assert.assertNotNull(CommandFactory.getCommands());
 }
 public void testRandomInitialize_0() throws Exception {
   Variable vx;
   Class[] types = {CommandGene.VoidClass, CommandGene.VoidClass, CommandGene.IntegerClass};
   Class[][] argTypes = {{}, {}, {}};
   int[] minDepths = new int[] {3, 4, 1};
   int[] maxDepths = new int[] {3, 4, 1};
   CommandGene[][] nodeSets = {
     {
       CMD_SUB_V_V, // 0
       CMD_CONST1, // 1
       new StoreTerminal(m_gpconf, "mem0", CommandGene.IntegerClass), // 2
       new StoreTerminal(m_gpconf, "mem1", CommandGene.IntegerClass), // 3
     },
     {
       vx = Variable.create(m_gpconf, "X", CommandGene.IntegerClass), // 0
       new AddAndStore(m_gpconf, CommandGene.IntegerClass, "mem2"), // 1
       CMD_FOR, // 2
       new TransferMemory(m_gpconf, "mem2", "mem1"), // 3
       new TransferMemory(m_gpconf, "mem1", "mem0"), // 4
       new ReadTerminal(m_gpconf, CommandGene.IntegerClass, "mem0"), // 5
       new ReadTerminal(m_gpconf, CommandGene.IntegerClass, "mem1"), // 6
       CMD_SUB_V_V_V, // 7
       new Increment(m_gpconf, CommandGene.IntegerClass, -1), // 8
     },
     {}
   };
   // Add commands working with internal memory.
   // ------------------------------------------
   nodeSets[2] =
       CommandFactory.createReadOnlyCommands(
           nodeSets[2], m_gpconf, CommandGene.IntegerClass, "mem", 1, 2, !true);
   // Execute the functionality to test.
   // ----------------------------------
   rn.setNextIntSequence(new int[] {0, 2, 1, 3, 1, 2, 8, 0, 7, 1, 5, 6, 4, 3});
   m_gpconf.setPopulationSize(1);
   GPGenotype gen =
       GPGenotype.randomInitialGenotype(
           m_gpconf,
           types,
           argTypes,
           nodeSets,
           minDepths,
           maxDepths,
           200,
           new boolean[] {true, true, false},
           false);
   GPPopulation pop = gen.getGPPopulation();
   assertEquals(m_gpconf.getPopulationSize(), pop.size());
   // Evaluate program 1
   // ------------------
   IGPProgram p = pop.getGPProgram(0);
   assertEquals(8, p.getChromosome(0).size());
   assertEquals(CMD_SUB_V_V, p.getChromosome(0).getNode(0));
   assertEquals(StoreTerminal.class, p.getChromosome(0).getNode(1).getClass());
   assertSame(CMD_CONST1, p.getChromosome(0).getNode(2));
   assertEquals(SubProgram.class, p.getChromosome(0).getNode(3).getClass());
   //    assertEquals(StoreTerminal.class, p.getChromosome(0).getNode(3).getClass());
   assertSame(CMD_CONST1, p.getChromosome(0).getNode(4));
   // Evaluate program 2
   // ------------------
   int node = 0;
   assertEquals(9, p.getChromosome(1).size());
   assertEquals(CMD_FOR, p.getChromosome(1).getNode(node++));
   assertEquals(Increment.class, p.getChromosome(1).getNode(node++).getClass());
   assertEquals(Variable.class, p.getChromosome(1).getNode(node++).getClass());
   assertEquals(CMD_SUB_V_V_V, p.getChromosome(1).getNode(node++));
   assertEquals(AddAndStore.class, p.getChromosome(1).getNode(node++).getClass());
   assertEquals(ReadTerminal.class, p.getChromosome(1).getNode(node++).getClass());
   assertEquals(ReadTerminal.class, p.getChromosome(1).getNode(node++).getClass());
   assertEquals(TransferMemory.class, p.getChromosome(1).getNode(node++).getClass());
   assertEquals(TransferMemory.class, p.getChromosome(1).getNode(node++).getClass());
   // Evaluate program 3
   // ------------------
   assertEquals(1, p.getChromosome(2).size());
   assertEquals(ReadTerminal.class, p.getChromosome(2).getNode(0).getClass());
   assertEquals(0.0, computeFitness(p, vx), DELTA);
 }