Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- docker run --rm obytes/runner-java run -l java -c "public class Sayara {
- public static final int MAX_FUEL_IN_LITERS = 50;
- public static final int MILEAGE_IN_L_PER_100KM = 10;
- private String carType;
- private int fuelAmount;
- public Sayara(String carType) {
- this.carType = carType;
- this.fuelAmount = 0;
- }
- private int calculateFuelConsumption(int distance) {
- return (int) (distance/100.0) * MILEAGE_IN_L_PER_100KM;
- }
- private int getRemainingDistance() {
- return (fuelAmount * 100) / MILEAGE_IN_L_PER_100KM;
- }
- public boolean drive(int distance) {
- boolean drove = false;
- if (getRemainingDistance() != 0) {
- fuelAmount -= calculateFuelConsumption(1);
- drove = true;
- }
- return drove;
- }
- }" -t cw -f "
- import junit.framework.*;
- import org.junit.Test;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import org.junit.Assert.*;
- public class Challenge11Test extends TestCase{
- public void testConstructorWithParams() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
- Sayara sayara = new Sayara(\"BMW\");
- Class<?> sayaraClass = sayara.getClass();
- Field fuelAmountField = sayaraClass.getDeclaredField(\"fuelAmount\");
- fuelAmountField.setAccessible(true);
- assertEquals(fuelAmountField.get(sayara), 0);
- Field carTypeField = sayaraClass.getDeclaredField(\"carType\");
- carTypeField.setAccessible(true);
- assertEquals(carTypeField.get(sayara), \"BMW\");
- }
- public void testMaxFuel() {
- Sayara sayara = new Sayara(\"R4\");
- assertEquals(sayara.MAX_FUEL_IN_LITERS, 50);
- }
- public void testFuelAmountIsPrivate() throws NoSuchFieldException, SecurityException {
- Sayara sayara = new Sayara(\"BMW\");
- Class<?> sayaraClass = sayara.getClass();
- Field field = sayaraClass.getDeclaredField(\"fuelAmount\");
- //https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.reflect.Modifier.PRIVATE
- assertEquals(field.getModifiers(), 2);
- }
- public void testFuelAmountIsZero() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
- Sayara sayara = new Sayara(\"BMW\");
- Class<?> sayaraClass = sayara.getClass();
- Field field = sayaraClass.getDeclaredField(\"fuelAmount\");
- field.setAccessible(true);
- assertEquals(field.get(sayara), 0);
- }
- public void testCalculateFuelConsumptionIsPrivate() throws NoSuchMethodException, SecurityException {
- Sayara sayara = new Sayara(\"R19\");
- Class<?> sayaraClass = sayara.getClass();
- Method method = sayaraClass.getDeclaredMethod(\"calculateFuelConsumption\", int.class);
- assertEquals(method.getModifiers(), 2);
- }
- public void testCalculateFuelConsumption() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
- Sayara sayara = new Sayara(\"R21\");
- Class<?> sayaraClass = sayara.getClass();
- Method method = sayaraClass.getDeclaredMethod(\"calculateFuelConsumption\", int.class);
- method.setAccessible(true);
- int distance = 440;
- assertEquals(method.invoke(sayara, distance), 40);
- }
- public void testGetRemainingDistanceIsPrivate() throws NoSuchMethodException, SecurityException {
- Sayara sayara = new Sayara(\"R19\");
- Class<?> sayaraClass = sayara.getClass();
- Method method = sayaraClass.getDeclaredMethod(\"getRemainingDistance\", null);
- assertEquals(method.getModifiers(), 2);
- }
- public void testGetRemainingDistance() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
- Sayara sayara = new Sayara(\"R19\");
- Class<?> sayaraClass = sayara.getClass();
- Method method = sayaraClass.getDeclaredMethod(\"getRemainingDistance\", null);
- method.setAccessible(true);
- assertEquals(method.invoke(sayara, null), 0);
- }
- public void testDriveIsPublic() throws NoSuchMethodException, SecurityException {
- Sayara sayara = new Sayara(\"R19\");
- Class<?> sayaraClass = sayara.getClass();
- Method method = sayaraClass.getDeclaredMethod(\"drive\", int.class);
- assertEquals(method.getModifiers(), 1);
- }
- }"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement