Skip to content

Commit

Permalink
Working on #2
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Sep 13, 2015
1 parent 1bd9461 commit 4c84e11
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/java/jlifx/packet/PacketService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import jlifx.bulb.GatewayBulb;
import jlifx.bulb.IBulb;

public final class PacketService {
public class PacketService {
private PacketWriter packetWriter = new UdpTcpPacketWriter();

public void setPacketWriter(PacketWriter packetWriter) {
Expand Down
18 changes: 10 additions & 8 deletions src/test/java/jlifx/bulb/AbstractJLifxTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.easymock.EasyMockSupport;

import jlifx.commandline.AbstractBulbCommand;
import jlifx.packet.PacketService;

public class AbstractJLifxTestCase extends EasyMockSupport {
public static final byte[] TEST_MAC_ADDRESS_1 = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
Expand All @@ -17,6 +18,14 @@ protected IBulb getMockedBulb() {
return mockedBulb;
}

protected GatewayBulb getMockedGatewayBulb() {
return createMock(GatewayBulb.class);
}

protected PacketService getMockedPacketService() {
return createMock(PacketService.class);
}

protected PrintStream getPrintStream() {
return new PrintStream(new ByteArrayOutputStream());
}
Expand All @@ -25,11 +34,4 @@ protected void executeCommand(AbstractBulbCommand command, IBulb bulb, String...
command.execute(Collections.singletonList(bulb), commandArgs, getPrintStream());
}

protected void startTestBulb() {

}

protected void stopTestBulb() {

}
}
}
42 changes: 42 additions & 0 deletions src/test/java/jlifx/bulb/BulbTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jlifx.bulb;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import jlifx.packet.PacketService;

public class BulbTest extends AbstractJLifxTestCase {

@Test
public void testBulbOperations() throws Exception {
GatewayBulb gatewayBulb = getMockedGatewayBulb();
Bulb bulb = new Bulb(TEST_MAC_ADDRESS_1, gatewayBulb);

assertArrayEquals(TEST_MAC_ADDRESS_1, bulb.getMacAddress());
assertEquals(gatewayBulb, bulb.getGatewayBulb());
assertTrue(bulb.equals(bulb));
}

@Test
public void testSwitchBulbOnOff() throws Exception {
GatewayBulb gatewayBulb = getMockedGatewayBulb();
PacketService packetService = getMockedPacketService();
expect(gatewayBulb.getPacketService()).andReturn(packetService);
expectLastCall().times(2);
Bulb bulb = new Bulb(TEST_MAC_ADDRESS_1, gatewayBulb);
packetService.sendPowerManagementPacket(bulb, true);
packetService.sendPowerManagementPacket(bulb, false);
replayAll();

bulb.switchOn();
bulb.switchOff();

verifyAll();
}

}

0 comments on commit 4c84e11

Please sign in to comment.