Skip to content

Commit

Permalink
#2 Improving test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Jun 21, 2015
1 parent 1966525 commit 3911ce9
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 32 deletions.
3 changes: 1 addition & 2 deletions src/main/java/jlifx/bulb/DiscoveryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private static GatewayBulb waitForReply(DatagramSocket socket) throws IOExceptio
socket.setSoTimeout(500);
int retries = 3;
byte[] byteArray = new byte[128];
while (retries > 0) {
while (retries-- > 0) {
DatagramPacket answer = new DatagramPacket(byteArray, byteArray.length);
try {
socket.receive(answer);
Expand All @@ -76,7 +76,6 @@ private static GatewayBulb waitForReply(DatagramSocket socket) throws IOExceptio
Packet packet = Packet.fromDatagramPacket(answer);
return new GatewayBulb(answer.getAddress(), packet.getGatewayMac(), packet.getTargetMac());
}
retries--;
}
return result;
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/jlifx/commandline/command/ScanCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ public boolean execute(String[] args, PrintStream out) throws IOException {
out.println("");
return false;
} else {
out.println("Found LIFX gateway bulb:");
out.println("IP address : " + gatewayBulb.getInetAddress().getHostAddress());
out.println("GW MAC address : " + gatewayBulb.getMacAddressAsString());
out.println("MAC address : " + gatewayBulb.getGatewayMacAddressAsString());
showGatewayBulbInfo(out, gatewayBulb);
Collection<IBulb> allBulbs = DiscoveryService.discoverAllBulbs(gatewayBulb);
out.println("Found " + allBulbs.size() + " bulb(s) in network:");
for (IBulb bulb : allBulbs) {
Expand All @@ -33,4 +30,11 @@ public boolean execute(String[] args, PrintStream out) throws IOException {
return true;
}

private void showGatewayBulbInfo(PrintStream out, GatewayBulb gatewayBulb) {
out.println("Found LIFX gateway bulb:");
out.println("IP address : " + gatewayBulb.getInetAddress().getHostAddress());
out.println("GW MAC address : " + gatewayBulb.getMacAddressAsString());
out.println("MAC address : " + gatewayBulb.getGatewayMacAddressAsString());
}

}
8 changes: 8 additions & 0 deletions src/main/java/jlifx/packet/Packet.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public Packet(byte[] targetMac, byte[] gatewayMac, byte[] timestamp, byte type)
setType(type);
}

public Packet(Packet packet) {
setTargetMac(packet.getTargetMac());
setGatewayMac(packet.getGatewayMac());
setTimestamp(packet.getTimestamp());
setType(packet.getType());
setPayload(packet.getPayload());
}

public byte[] getTargetMac() {
return targetMac;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/jlifx/packet/WifiInfoResponsePacket.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package jlifx.packet;

public class WifiInfoResponsePacket {
private final Packet packet;
public class WifiInfoResponsePacket extends Packet {

public WifiInfoResponsePacket(Packet packet) {
this.packet = packet;
super(packet);
}

public float getSignalStrength() {
return 0; // TODO
}
}

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

import java.io.PrintStream;
import java.util.Collections;

import jlifx.commandline.AbstractBulbCommand;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.easymock.EasyMockSupport;

public class AbstractJLifxTestCase extends EasyMockSupport {
private final IBulb mockedBulb = createMock(IBulb.class);

protected IBulb getMockedBulb() {
return mockedBulb;
}

protected PrintStream getPrintStream() {
return new PrintStream(new ByteArrayOutputStream());
}

protected void executeCommand(AbstractBulbCommand command, IBulb bulb, String... commandArgs) throws Exception {
command.execute(Collections.singletonList(bulb), commandArgs, getPrintStream());
}
}
14 changes: 4 additions & 10 deletions src/test/java/jlifx/commandline/command/BlinkCommandTest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
package jlifx.commandline.command;

import java.io.PrintStream;
import java.util.Collections;

import jlifx.bulb.AbstractJLifxTestCase;
import jlifx.bulb.IBulb;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.easymock.EasyMock;
import org.easymock.EasyMockSupport;
import org.junit.Test;

public class BlinkCommandTest extends EasyMockSupport {
public class BlinkCommandTest extends AbstractJLifxTestCase {

@Test
public void testBlinkTwoTimes() throws Exception {
BlinkCommand command = new BlinkCommand();
IBulb bulb = createMock(IBulb.class);
IBulb bulb = getMockedBulb();
bulb.switchOn();
EasyMock.expectLastCall().times(2);
bulb.switchOff();
EasyMock.expectLastCall().times(2);
replayAll();

command.execute(Collections.singletonList(bulb), new String[] {"-times", "2"}, new PrintStream(
new ByteArrayOutputStream()));
executeCommand(new BlinkCommand(), bulb, "times", "2");

verifyAll();
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/jlifx/commandline/command/DimCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package jlifx.commandline.command;

import static org.easymock.EasyMock.expectLastCall;
import jlifx.bulb.AbstractJLifxTestCase;
import jlifx.bulb.IBulb;

import org.junit.Test;

public class DimCommandTest extends AbstractJLifxTestCase {

@Test
public void testDimBulb() throws Exception {
DimCommand command = new DimCommand();
IBulb bulb = getMockedBulb();
bulb.setDim(0.5F);
expectLastCall().once();
replayAll();

executeCommand(command, bulb, "dim", "0.5");

verifyAll();
}

}
27 changes: 27 additions & 0 deletions src/test/java/jlifx/commandline/command/RainbowCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package jlifx.commandline.command;

import static org.easymock.EasyMock.isA;

import java.awt.Color;

import jlifx.bulb.AbstractJLifxTestCase;
import jlifx.bulb.IBulb;

import org.easymock.EasyMock;
import org.junit.Test;

public class RainbowCommandTest extends AbstractJLifxTestCase {

@Test
public void testTimed() throws Exception {
IBulb bulb = getMockedBulb();
bulb.colorize(isA(Color.class), EasyMock.gt(0), EasyMock.eq(1.0F));
EasyMock.expectLastCall().atLeastOnce();
replayAll();

executeCommand(new RainbowCommand(), bulb, "duration", "2");

verifyAll();
}

}
22 changes: 22 additions & 0 deletions src/test/java/jlifx/commandline/command/SwitchCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package jlifx.commandline.command;

import static org.easymock.EasyMock.expectLastCall;
import jlifx.bulb.AbstractJLifxTestCase;
import jlifx.bulb.IBulb;

import org.junit.Test;

public class SwitchCommandTest extends AbstractJLifxTestCase {

@Test
public void testSwitchOn() throws Exception {
IBulb bulb = getMockedBulb();
bulb.switchOn();
expectLastCall().once();
replayAll();

executeCommand(new SwitchCommand(), bulb, "switch", "on");

verifyAll();
}
}
43 changes: 31 additions & 12 deletions src/test/java/jlifx/packet/PacketServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class PacketServiceTest extends EasyMockSupport {
private static final byte[] TEST_MAC_ADDRESS_1 = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
private static final byte[] TEST_MAC_ADDRESS_2 = new byte[] {0x06, 0x05, 0x04, 0x03, 0x02, 0x01};

private PacketWriter buildSimplePacketWriterMock(Class<? extends Packet> packetClass) throws UnknownHostException,
private PacketWriter buildPacketWriterMock(Class<? extends Packet> packetClass) throws UnknownHostException,
IOException {
PacketWriter result = createMock(PacketWriter.class);
result.connect(InetAddress.getLocalHost());
Expand All @@ -31,11 +31,20 @@ private PacketWriter buildSimplePacketWriterMock(Class<? extends Packet> packetC
return result;
}

private PacketWriter buildPacketWriterMockWithResponse(Class<? extends Packet> packetClass, Packet responsePacket)
throws UnknownHostException, IOException {
PacketWriter result = createMock(PacketWriter.class);
List<Packet> returnValue = Collections.singletonList(responsePacket);
EasyMock.expect(result.sendPacketAndWaitForResponse(isA(GatewayBulb.class), isA(packetClass)))
.andReturn(returnValue);
return result;
}

@Test
public void testSendPowerManagementPacket() throws Exception {
GatewayBulb bulb = new GatewayBulb(InetAddress.getLocalHost(), TEST_MAC_ADDRESS_1, TEST_MAC_ADDRESS_2);
PacketService packetService = new PacketService();
packetService.setPacketWriter(buildSimplePacketWriterMock(PowerManagementPacket.class));
packetService.setPacketWriter(buildPacketWriterMock(PowerManagementPacket.class));
replayAll();

packetService.sendPowerManagementPacket(bulb, true);
Expand All @@ -47,7 +56,7 @@ public void testSendPowerManagementPacket() throws Exception {
public void testSendColorManagementPacket() throws Exception {
GatewayBulb bulb = new GatewayBulb(InetAddress.getLocalHost(), TEST_MAC_ADDRESS_1, TEST_MAC_ADDRESS_2);
PacketService packetService = new PacketService();
packetService.setPacketWriter(buildSimplePacketWriterMock(ColorManagementPacket.class));
packetService.setPacketWriter(buildPacketWriterMock(ColorManagementPacket.class));
replayAll();

packetService.sendColorManagementPacket(bulb, Color.BLUE, 3, 0.5F);
Expand All @@ -59,7 +68,7 @@ public void testSendColorManagementPacket() throws Exception {
public void testSendSetDimAbsolutePacket() throws Exception {
GatewayBulb bulb = new GatewayBulb(InetAddress.getLocalHost(), TEST_MAC_ADDRESS_1, TEST_MAC_ADDRESS_2);
PacketService packetService = new PacketService();
packetService.setPacketWriter(buildSimplePacketWriterMock(SetDimAbsolutePacket.class));
packetService.setPacketWriter(buildPacketWriterMock(SetDimAbsolutePacket.class));
replayAll();

packetService.sendSetDimAbsolutePacket(bulb, 0.5F);
Expand All @@ -71,19 +80,29 @@ public void testSendSetDimAbsolutePacket() throws Exception {
public void testSendStatusRequestPacket() throws Exception {
GatewayBulb bulb = new GatewayBulb(InetAddress.getLocalHost(), TEST_MAC_ADDRESS_1, TEST_MAC_ADDRESS_2);
PacketService packetService = new PacketService();
PacketWriter packetWriter = createMock(PacketWriter.class);
List<Packet> returnValue = Collections.singletonList(new StatusResponsePacket(new Packet()));
EasyMock.expect(
packetWriter.sendPacketAndWaitForResponse(isA(GatewayBulb.class), isA(StatusRequestPacket.class)))
.andReturn(
returnValue);
packetService.setPacketWriter(packetWriter);
packetService.setPacketWriter(buildPacketWriterMockWithResponse(StatusRequestPacket.class,
new StatusResponsePacket(new Packet())));
replayAll();

List<StatusResponsePacket> result = packetService.sendStatusRequestPacket(bulb);

verifyAll();
assertNotNull(result);
assertEquals(1, result.size());
}

@Test
public void testSendWifiInfoRequestPacket() throws Exception {
GatewayBulb bulb = new GatewayBulb(InetAddress.getLocalHost(), TEST_MAC_ADDRESS_1, TEST_MAC_ADDRESS_2);
PacketService packetService = new PacketService();
packetService.setPacketWriter(buildPacketWriterMockWithResponse(WifiInfoRequestPacket.class,
new WifiInfoResponsePacket(new Packet())));
replayAll();

List<Packet> result = packetService.sendWifiInfoRequestPacket(bulb);

verifyAll();
assertNotNull(result);
assertEquals(1, result.size());
}
}

0 comments on commit 3911ce9

Please sign in to comment.