Skip to content
Snippets Groups Projects
Commit 67416358 authored by Arghya Ghosh's avatar Arghya Ghosh
Browse files

Added one more test for more coverage and added unit_tests.html

parent 26d4d7b1
Branches
No related tags found
No related merge requests found
......@@ -8,7 +8,7 @@ public class PowerHandlerTest {
private PowerHandler powerHandler;
private ExceptionHandlerTest testHandler;
//Serves as custom handler for exceptions we throw at it. Allows to test the Powerhandler class which implements an exception
private class ExceptionHandlerTest implements Thread.UncaughtExceptionHandler {
class ExceptionHandlerTest implements Thread.UncaughtExceptionHandler {
Throwable receivedException = null;
/**
......
package powerutility;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class PowerSurgeTest {
@Before
public void setUp() throws Exception {
}
@Test
public void testPowerSurgeCanBeCaught() {
try {
throw new PowerSurge();
} catch (PowerSurge e) {
assertNotNull("Exception should be caught", e);
}
}
}
\ No newline at end of file
<! Starter for the testing, will update as I make more tests >
<!AttendentTests Line 3-116>
package com.thelocalmarketplace.hardware;
import static org.junit.Assert.assertEquals;
import java.math.BigDecimal;
import java.util.Currency;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
import com.thelocalmarketplace.hardware.AttendantStation;
import com.thelocalmarketplace.hardware.SelfCheckoutStationBronze;
import powerutility.PowerGrid;
@SuppressWarnings("javadoc")
public class AttendantStationTest {
private AttendantStation supervisionStation;
private SelfCheckoutStationBronze station;
private Currency CAD = Currency.getInstance(Locale.CANADA);
@Before
public void setup() {
SelfCheckoutStationBronze.resetConfigurationToDefaults();
supervisionStation = new AttendantStation();
}
@Test
public void testCreate() {
assertEquals(0, supervisionStation.supervisedStationCount());
assertEquals(0, supervisionStation.supervisedStations().size());
}
@Test
public void testAddAndRemove() {
SelfCheckoutStationBronze.configureCurrency(CAD);
SelfCheckoutStationBronze.configureBanknoteDenominations(new BigDecimal[] { BigDecimal.ONE });
SelfCheckoutStationBronze.configureCoinDenominations(new BigDecimal[] { BigDecimal.ONE });
SelfCheckoutStationBronze.configureScaleMaximumWeight(10.0);
SelfCheckoutStationBronze.configureScaleSensitivity(1.0);
station = new SelfCheckoutStationBronze();
supervisionStation.add(station);
assertEquals(1, supervisionStation.supervisedStationCount());
assertEquals(1, supervisionStation.supervisedStations().size());
supervisionStation.remove(station);
assertEquals(0, supervisionStation.supervisedStationCount());
assertEquals(0, supervisionStation.supervisedStations().size());
}
@Test(expected = IllegalArgumentException.class)
public void testBadAdd() {
supervisionStation.add(null);
}
@Test(expected = IllegalStateException.class)
public void testBadAdd2() {
SelfCheckoutStationBronze.configureCurrency(CAD);
SelfCheckoutStationBronze.configureBanknoteDenominations(new BigDecimal[] { BigDecimal.ONE });
SelfCheckoutStationBronze.configureCoinDenominations(new BigDecimal[] { BigDecimal.ONE });
SelfCheckoutStationBronze.configureScaleMaximumWeight(10.0);
SelfCheckoutStationBronze.configureScaleSensitivity(1.0);
station = new SelfCheckoutStationBronze();
supervisionStation.add(station);
supervisionStation.add(station);
}
@Test
public void testRemoveNull() {
supervisionStation.remove(null);
}
@Test
public void testPlugIn() {
supervisionStation.unplug();
supervisionStation.plugIn(PowerGrid.instance());
assertEquals(true, supervisionStation.keyboard.isPluggedIn());
assertEquals(false, supervisionStation.keyboard.isPoweredUp());
}
@Test
public void testUnplug() {
supervisionStation.unplug();
assertEquals(false, supervisionStation.keyboard.isPluggedIn());
assertEquals(false, supervisionStation.keyboard.isPoweredUp());
}
@Test
public void testTurnOn() {
supervisionStation.plugIn(PowerGrid.instance());
supervisionStation.turnOn();
assertEquals(true, supervisionStation.keyboard.isPluggedIn());
assertEquals(true, supervisionStation.keyboard.isPoweredUp());
}
@Test
public void testTurnOff() {
supervisionStation.plugIn(PowerGrid.instance());
supervisionStation.turnOn();
supervisionStation.turnOff();
assertEquals(true, supervisionStation.keyboard.isPluggedIn());
assertEquals(false, supervisionStation.keyboard.isPoweredUp());
}
}
<!Starter tests for PowerGrid. To be update>
package powerutility;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@SuppressWarnings("javadoc")
public class PowerGridTest {
// For use as a Java application: needed as JUnit captures the exceptions
// otherwise
public static void main(String[] args) {
PowerGridTest t = new PowerGridTest();
t.setup();
try {
t.testForPowerFailure();
fail();
}
catch(NoPowerException e) {}
t.teardown();
t.setup();
try {
t.testForPowerSurge();
fail();
}
catch(PowerSurge e) {}
t.teardown();
t.setup();
try {
t.testForPowerSurge2();
fail();
}
catch(PowerSurge e) {}
t.teardown();
t.setup();
try {
t.testForPowerOutage();
fail();
}
catch(NoPowerException e) {}
t.teardown();
t.setup();
t.testForPowerRestore();
t.teardown();
}
@Before
public void setup() {
PowerGrid.instance().forcePowerRestore();
}
@After
public void teardown() {
PowerGrid.reconnectToMains();
PowerGrid.instance().forcePowerRestore();
}
@Test(expected = NoPowerException.class)
public void testForPowerFailure() {
PowerGrid.disconnect();
PowerGrid.instance().hasPower();
}
@Test(expected = PowerSurge.class)
public void testForPowerSurge2() {
PowerGrid.engageFaultyPowerSource();
PowerGrid.instance().hasPower();
}
@Test(expected = PowerSurge.class)
public void testForPowerSurge() {
PowerGrid.engageUninterruptiblePowerSource();
PowerGrid.instance().forcePowerSurge();
PowerGrid.instance().hasPower();
}
@Test
public void testForPowerRestore() {
PowerGrid pg = PowerGrid.instance();
PowerGrid.engageUninterruptiblePowerSource();
pg.forcePowerRestore();
assertTrue(pg.hasPower());
}
@Test(expected = NoPowerException.class)
public void testForPowerOutage() {
PowerGrid pg = PowerGrid.instance();
PowerGrid.engageUninterruptiblePowerSource();
pg.forcePowerOutage();
pg.hasPower();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment