Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. docker run --rm obytes/runner-java run -l java -c "public class Sayara {
  2. public static final int MAX_FUEL_IN_LITERS = 50;
  3. public static final int MILEAGE_IN_L_PER_100KM = 10;
  4.  
  5. private String carType;
  6. private int fuelAmount;
  7.  
  8. public Sayara(String carType) {
  9. this.carType = carType;
  10. this.fuelAmount = 0;
  11. }
  12.  
  13. private int calculateFuelConsumption(int distance) {
  14. return (int) (distance/100.0) * MILEAGE_IN_L_PER_100KM;
  15. }
  16.  
  17. private int getRemainingDistance() {
  18. return (fuelAmount * 100) / MILEAGE_IN_L_PER_100KM;
  19. }
  20.  
  21. public boolean drive(int distance) {
  22. boolean drove = false;
  23. if (getRemainingDistance() != 0) {
  24. fuelAmount -= calculateFuelConsumption(1);
  25. drove = true;
  26. }
  27.  
  28. return drove;
  29. }
  30. }" -t cw -f "
  31. import junit.framework.*;
  32. import org.junit.Test;
  33.  
  34. import java.lang.reflect.Field;
  35. import java.lang.reflect.InvocationTargetException;
  36. import java.lang.reflect.Method;
  37.  
  38. import org.junit.Assert.*;
  39.  
  40.  
  41. public class Challenge11Test extends TestCase{
  42.  
  43. public void testConstructorWithParams() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
  44. Sayara sayara = new Sayara(\"BMW\");
  45. Class<?> sayaraClass = sayara.getClass();
  46. Field fuelAmountField = sayaraClass.getDeclaredField(\"fuelAmount\");
  47. fuelAmountField.setAccessible(true);
  48. assertEquals(fuelAmountField.get(sayara), 0);
  49.  
  50. Field carTypeField = sayaraClass.getDeclaredField(\"carType\");
  51. carTypeField.setAccessible(true);
  52. assertEquals(carTypeField.get(sayara), \"BMW\");
  53.  
  54. }
  55.  
  56. public void testMaxFuel() {
  57. Sayara sayara = new Sayara(\"R4\");
  58. assertEquals(sayara.MAX_FUEL_IN_LITERS, 50);
  59. }
  60.  
  61. public void testFuelAmountIsPrivate() throws NoSuchFieldException, SecurityException {
  62. Sayara sayara = new Sayara(\"BMW\");
  63. Class<?> sayaraClass = sayara.getClass();
  64. Field field = sayaraClass.getDeclaredField(\"fuelAmount\");
  65.  
  66. //https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.lang.reflect.Modifier.PRIVATE
  67. assertEquals(field.getModifiers(), 2);
  68. }
  69.  
  70. public void testFuelAmountIsZero() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
  71. Sayara sayara = new Sayara(\"BMW\");
  72. Class<?> sayaraClass = sayara.getClass();
  73. Field field = sayaraClass.getDeclaredField(\"fuelAmount\");
  74. field.setAccessible(true);
  75.  
  76. assertEquals(field.get(sayara), 0);
  77. }
  78.  
  79. public void testCalculateFuelConsumptionIsPrivate() throws NoSuchMethodException, SecurityException {
  80.  
  81. Sayara sayara = new Sayara(\"R19\");
  82. Class<?> sayaraClass = sayara.getClass();
  83. Method method = sayaraClass.getDeclaredMethod(\"calculateFuelConsumption\", int.class);
  84. assertEquals(method.getModifiers(), 2);
  85. }
  86.  
  87. public void testCalculateFuelConsumption() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  88.  
  89. Sayara sayara = new Sayara(\"R21\");
  90. Class<?> sayaraClass = sayara.getClass();
  91. Method method = sayaraClass.getDeclaredMethod(\"calculateFuelConsumption\", int.class);
  92.  
  93. method.setAccessible(true);
  94. int distance = 440;
  95. assertEquals(method.invoke(sayara, distance), 40);
  96. }
  97.  
  98. public void testGetRemainingDistanceIsPrivate() throws NoSuchMethodException, SecurityException {
  99.  
  100. Sayara sayara = new Sayara(\"R19\");
  101. Class<?> sayaraClass = sayara.getClass();
  102. Method method = sayaraClass.getDeclaredMethod(\"getRemainingDistance\", null);
  103. assertEquals(method.getModifiers(), 2);
  104. }
  105.  
  106. public void testGetRemainingDistance() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  107.  
  108. Sayara sayara = new Sayara(\"R19\");
  109. Class<?> sayaraClass = sayara.getClass();
  110. Method method = sayaraClass.getDeclaredMethod(\"getRemainingDistance\", null);
  111. method.setAccessible(true);
  112.  
  113. assertEquals(method.invoke(sayara, null), 0);
  114. }
  115.  
  116. public void testDriveIsPublic() throws NoSuchMethodException, SecurityException {
  117.  
  118. Sayara sayara = new Sayara(\"R19\");
  119. Class<?> sayaraClass = sayara.getClass();
  120. Method method = sayaraClass.getDeclaredMethod(\"drive\", int.class);
  121. assertEquals(method.getModifiers(), 1);
  122. }
  123. }"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement