Advertisement
NeverRIEght

ShopTest.java

May 4th, 2024 (edited)
1,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.42 KB | None | 0 0
  1. package com.epam.rd.autocode.queue;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.PrintStream;
  6. import java.lang.reflect.Method;
  7. import java.lang.reflect.Modifier;
  8. import java.util.Random;
  9. import java.util.Set;
  10. import java.util.stream.Stream;
  11.  
  12. import org.junit.jupiter.api.Assertions;
  13. import org.junit.jupiter.api.BeforeEach;
  14. import org.junit.jupiter.api.Test;
  15.  
  16. import com.epam.rd.autocode.queue.CashBox.State;
  17.  
  18. import spoon.Launcher;
  19. import spoon.SpoonAPI;
  20. import spoon.reflect.reference.CtTypeReference;
  21. import spoon.reflect.visitor.filter.TypeFilter;
  22.  
  23. import static org.junit.jupiter.api.Assertions.*;
  24.  
  25. /**
  26.  * @author D. Kolesnikov
  27.  */
  28. public class ShopTest {
  29.  
  30.     //////////////////////////////////////////////////////////////////////////////
  31.  
  32.     private static boolean isAllTestsMustFailed;
  33.  
  34.     private static Throwable complianceTestFailedCause;
  35.  
  36.     static {
  37.         try {
  38.             String testClassName = new Exception().getStackTrace()[0].getClassName();
  39.             String className = testClassName.substring(0, testClassName.lastIndexOf("Test"));
  40.             Class<?> c = Class.forName(className);
  41.  
  42.             java.lang.reflect.Method[] methods = {
  43.                     c.getDeclaredMethod("addBuyer", Buyer.class),
  44.                     c.getDeclaredMethod("getCashBox", int.class),
  45.                     c.getDeclaredMethod("setCashBoxState", int.class, CashBox.State.class),
  46.                     c.getDeclaredMethod("tact"),
  47.                     c.getDeclaredMethod("print")
  48.             };
  49.  
  50.             org.apache.bcel.classfile.JavaClass jc = org.apache.bcel.Repository.lookupClass(c);
  51.             for (java.lang.reflect.Method method : methods) {
  52.                 org.apache.bcel.classfile.Method m = jc.getMethod(method);
  53.                 org.apache.bcel.classfile.Code code = m.getCode();
  54.                 Assertions.assertTrue(code.getCode().length > 2, () -> m + " is not a stub");
  55.             }
  56.         } catch (Throwable t) {
  57.             isAllTestsMustFailed = true;
  58.             complianceTestFailedCause = t;
  59.             t.printStackTrace();
  60.         }
  61.     }
  62.  
  63.     {
  64.         if (isAllTestsMustFailed) {
  65.             Assertions.fail(() -> "Compliance test failed: " + complianceTestFailedCause.getMessage());
  66.         }
  67.     }
  68.  
  69.     //////////////////////////////////////////////////////////////////////////////
  70.  
  71.     private static final PrintStream STD_OUT = System.out;
  72.  
  73.     private Shop shop;
  74.  
  75.     @BeforeEach
  76.     void setUp() {
  77.         Buyer.resetNames();
  78.         shop = new Shop(5);
  79.     }
  80.  
  81.     @Test
  82.     void test1() {
  83.         shop.setCashBoxState(0, State.ENABLED);
  84.  
  85.         shop.addBuyer(Buyer.nextBuyer());
  86.         shop.addBuyer(Buyer.nextBuyer());
  87.         shop.addBuyer(Buyer.nextBuyer());
  88.  
  89.         shop.setCashBoxState(2, State.ENABLED);
  90.         shop.tact();
  91.  
  92.         //+B
  93.         //
  94.         //+C
  95.         //
  96.         //
  97.         assertEquals("+B-+C--", getState(shop));
  98.  
  99.         shop.addBuyer(Buyer.nextBuyer());
  100.         shop.addBuyer(Buyer.nextBuyer());
  101.  
  102.         //+BD
  103.         //
  104.         //+CE
  105.         //
  106.         //
  107.         assertEquals("+BD-+CE--", getState(shop));
  108.  
  109.         shop.setCashBoxState(4, State.ENABLED);
  110.         shop.tact();
  111.  
  112.         //+D
  113.         //
  114.         //+E
  115.         //
  116.         //+
  117.         assertEquals("+D-+E-+", getState(shop));
  118.  
  119.         shop.addBuyer(Buyer.nextBuyer());
  120.         shop.addBuyer(Buyer.nextBuyer());
  121.  
  122.         //+DG
  123.         //
  124.         //+E
  125.         //
  126.         //+F
  127.         assertEquals("+DG-+E-+F", getState(shop));
  128.  
  129.         String actual = getState(shop);
  130.         String expected = "+DG-+E-+F";
  131.         assertEquals(expected, actual);
  132.     }
  133.  
  134.     @Test
  135.     void test2() {
  136.         shop.setCashBoxState(0, State.ENABLED);
  137.  
  138.         shop.addBuyer(Buyer.nextBuyer());
  139.         shop.addBuyer(Buyer.nextBuyer());
  140.         shop.addBuyer(Buyer.nextBuyer());
  141.         shop.addBuyer(Buyer.nextBuyer());
  142.         shop.addBuyer(Buyer.nextBuyer());
  143.  
  144.         shop.setCashBoxState(2, State.ENABLED);
  145.  
  146.         //+ABCDE
  147.         //
  148.         //+
  149.         //
  150.         //
  151.         assertEquals("+ABCDE-+--", getState(shop));
  152.  
  153.         shop.addBuyer(Buyer.nextBuyer());
  154.         shop.addBuyer(Buyer.nextBuyer());
  155.         shop.addBuyer(Buyer.nextBuyer());
  156.         shop.addBuyer(Buyer.nextBuyer());
  157.  
  158.         //+ABCDE
  159.         //
  160.         //+FGHI
  161.         //
  162.         //
  163.         assertEquals("+ABCDE-+FGHI--", getState(shop));
  164.  
  165.  
  166.         shop.setCashBoxState(1, State.ENABLED);
  167.         shop.setCashBoxState(3, State.ENABLED);
  168.  
  169.         //+ABCDE
  170.         //+
  171.         //+FGHI
  172.         //+
  173.         //
  174.         assertEquals("+ABCDE++FGHI+-", getState(shop));
  175.  
  176.         shop.tact();
  177.  
  178.         //+BC
  179.         //+ED
  180.         //+GH
  181.         //+I
  182.         //
  183.         assertEquals("+BC+ED+GH+I-", getState(shop));
  184.  
  185.         String actual = getState(shop);
  186.         String expected = "+BC+ED+GH+I-";
  187.         assertEquals(expected, actual);
  188.     }
  189.  
  190.     @Test
  191.     void test3() {
  192.         shop.setCashBoxState(0, State.ENABLED);
  193.  
  194.         shop.addBuyer(Buyer.nextBuyer());
  195.         shop.addBuyer(Buyer.nextBuyer());
  196.         shop.addBuyer(Buyer.nextBuyer());
  197.         shop.addBuyer(Buyer.nextBuyer());
  198.         shop.addBuyer(Buyer.nextBuyer());
  199.  
  200.         shop.setCashBoxState(2, State.ENABLED);
  201.  
  202.         //+ABCDE
  203.         //
  204.         //+
  205.         //
  206.         //
  207.         assertEquals("+ABCDE-+--", getState(shop));
  208.  
  209.         shop.addBuyer(Buyer.nextBuyer());
  210.         shop.addBuyer(Buyer.nextBuyer());
  211.         shop.addBuyer(Buyer.nextBuyer());
  212.         shop.addBuyer(Buyer.nextBuyer());
  213.  
  214.         shop.setCashBoxState(2, State.IS_CLOSING);
  215.  
  216.         //+ABCDE
  217.         //
  218.         //|FGHI
  219.         //
  220.         //
  221.         assertEquals("+ABCDE-|FGHI--", getState(shop));
  222.  
  223.         shop.tact();
  224.  
  225.         //+BCDE
  226.         //
  227.         //|GHI
  228.         //
  229.         //
  230.         assertEquals("+BCDE-|GHI--", getState(shop));
  231.  
  232.         String actual = getState(shop);
  233.         String expected = "+BCDE-|GHI--";
  234.         assertEquals(expected, actual);
  235.     }
  236.  
  237.     @Test
  238.     void test4() {
  239.         shop.setCashBoxState(0, State.ENABLED);
  240.  
  241.         shop.addBuyer(Buyer.nextBuyer());
  242.         shop.addBuyer(Buyer.nextBuyer());
  243.         shop.addBuyer(Buyer.nextBuyer());
  244.         shop.addBuyer(Buyer.nextBuyer());
  245.         shop.addBuyer(Buyer.nextBuyer());
  246.  
  247.         shop.setCashBoxState(2, State.ENABLED);
  248.  
  249.         //+ABCDE
  250.         //
  251.         //+
  252.         //
  253.         //
  254.         assertEquals("+ABCDE-+--", getState(shop));
  255.  
  256.         shop.addBuyer(Buyer.nextBuyer());
  257.         shop.addBuyer(Buyer.nextBuyer());
  258.         shop.addBuyer(Buyer.nextBuyer());
  259.         shop.addBuyer(Buyer.nextBuyer());
  260.  
  261.         shop.setCashBoxState(2, State.IS_CLOSING);
  262.  
  263.         //+ABCDE
  264.         //
  265.         //|FGHI
  266.         //
  267.         //
  268.         assertEquals("+ABCDE-|FGHI--", getState(shop));
  269.  
  270.         shop.addBuyer(Buyer.nextBuyer());
  271.         shop.addBuyer(Buyer.nextBuyer());
  272.         shop.addBuyer(Buyer.nextBuyer());
  273.         shop.addBuyer(Buyer.nextBuyer());
  274.  
  275.         //+ABCDEJKLM
  276.         //
  277.         //|FGHI
  278.         //
  279.         //
  280.         assertEquals("+ABCDEJKLM-|FGHI--", getState(shop));
  281.  
  282.         shop.tact();
  283.  
  284.         //+BCDEJKLM
  285.         //
  286.         //|GHI
  287.         //
  288.         //
  289.         assertEquals("+BCDEJKLM-|GHI--", getState(shop));
  290.  
  291.         shop.setCashBoxState(3, State.ENABLED);
  292.  
  293.         //+BCDEJKLM
  294.         //
  295.         //|GHI
  296.         //+
  297.         //
  298.         assertEquals("+BCDEJKLM-|GHI+-", getState(shop));
  299.  
  300.         shop.tact();
  301.  
  302.         //+CDEJ
  303.         //
  304.         //|HI
  305.         //+MLK
  306.         //
  307.         assertEquals("+CDEJ-|HI+MLK-", getState(shop));
  308.  
  309.         String actual = getState(shop);
  310.         assertTrue(Set.of("+CDEJ-|HI+MLK-", "+CDE-|HI+MLKJ-").contains(actual));
  311.     }
  312.  
  313.     @Test
  314.     void test5() {
  315.         shop.setCashBoxState(0, State.ENABLED);
  316.  
  317.         shop.addBuyer(Buyer.nextBuyer()); //A
  318.         shop.addBuyer(Buyer.nextBuyer()); //B
  319.         shop.addBuyer(Buyer.nextBuyer()); //C
  320.         shop.addBuyer(Buyer.nextBuyer()); //D
  321.         shop.addBuyer(Buyer.nextBuyer()); //E
  322.  
  323.         shop.setCashBoxState(2, State.ENABLED);
  324.  
  325.         //+ABCDE
  326.         //
  327.         //+
  328.         //
  329.         //
  330.         assertEquals("+ABCDE-+--", getState(shop));
  331.  
  332.         shop.addBuyer(Buyer.nextBuyer()); //F
  333.         shop.addBuyer(Buyer.nextBuyer()); //G
  334.         shop.addBuyer(Buyer.nextBuyer()); //H
  335.         shop.addBuyer(Buyer.nextBuyer()); //I
  336.  
  337.         //+ABCDE
  338.         //
  339.         //+FGHI
  340.         //
  341.         //
  342.         assertEquals("+ABCDE-+FGHI--", getState(shop));
  343.  
  344.         shop.setCashBoxState(2, State.IS_CLOSING);
  345.  
  346.         //+ABCDE
  347.         //
  348.         //|FGHI
  349.         //
  350.         //
  351.         assertEquals("+ABCDE-|FGHI--", getState(shop));
  352.  
  353.         shop.addBuyer(Buyer.nextBuyer()); //J
  354.         shop.addBuyer(Buyer.nextBuyer()); //K
  355.         shop.addBuyer(Buyer.nextBuyer()); //L
  356.         shop.addBuyer(Buyer.nextBuyer()); //M
  357.  
  358.         //+ABCDEJKLM
  359.         //
  360.         //|FGHI
  361.         //
  362.         //
  363.         assertEquals("+ABCDEJKLM-|FGHI--", getState(shop));
  364.  
  365.         shop.tact();
  366.  
  367.         //+BCDEJKLM
  368.         //
  369.         //|GHI
  370.         //
  371.         //
  372.         assertEquals("+BCDEJKLM-|GHI--", getState(shop));
  373.  
  374.         shop.setCashBoxState(3, State.ENABLED);
  375.  
  376.         //+BCDEJKLM
  377.         //
  378.         //|GHI
  379.         //+
  380.         //
  381.         assertEquals("+BCDEJKLM-|GHI+-", getState(shop));
  382.  
  383.         shop.tact();
  384.  
  385.         //+CDEJ
  386.         //
  387.         //|HI
  388.         //+MLK
  389.         //
  390.         assertEquals("+CDEJ-|HI+MLK-", getState(shop));
  391.  
  392.         shop.addBuyer(Buyer.nextBuyer()); //N
  393.         shop.addBuyer(Buyer.nextBuyer()); //O
  394.         shop.addBuyer(Buyer.nextBuyer()); //P
  395.         shop.addBuyer(Buyer.nextBuyer()); //Q
  396.  
  397.         //+CDEJOQ
  398.         //
  399.         //|HI
  400.         //+MLKNP
  401.         //
  402.         assertEquals("+CDEJOQ-|HI+MLKNP-", getState(shop));
  403.  
  404.         String actual = getState(shop);
  405.         String expected = "+CDEJOQ-|HI+MLKNP-";
  406.         assertEquals(expected, actual);
  407.     }
  408.  
  409.     @Test
  410.     void test6() throws IOException {
  411.         shop.setCashBoxState(0, State.ENABLED);
  412.  
  413.         shop.addBuyer(Buyer.nextBuyer());
  414.         shop.addBuyer(Buyer.nextBuyer());
  415.         shop.addBuyer(Buyer.nextBuyer());
  416.         shop.addBuyer(Buyer.nextBuyer());
  417.  
  418.         shop.setCashBoxState(1, State.ENABLED);
  419.         shop.addBuyer(Buyer.nextBuyer());
  420.  
  421.         //+ABCD
  422.         //+E
  423.         //
  424.         //
  425.         //
  426.         assertEquals("+ABCD+E---", getState(shop));
  427.  
  428.         shop.setCashBoxState(0, State.IS_CLOSING);
  429.  
  430.         //|ABCD
  431.         //+E
  432.         //
  433.         //
  434.         //
  435.         assertEquals("|ABCD+E---", getState(shop));
  436.  
  437.         shop.addBuyer(Buyer.nextBuyer());
  438.  
  439.         //|ABCD
  440.         //+EF
  441.         //
  442.         //
  443.         //
  444.         assertEquals("|ABCD+EF---", getState(shop));
  445.  
  446.         shop.tact();
  447.  
  448.         //|BC
  449.         //+FD
  450.         //
  451.         //
  452.         //
  453.         assertEquals("|BC+FD---", getState(shop));
  454.  
  455.         String actual = getState(shop);
  456.         String expected = "|BC+FD---";
  457.         assertEquals(expected, actual);
  458.     }
  459.  
  460.     @Test
  461.     void complianceTestLambdaExpressionsAreRestrictedForUsing() {
  462.         Stream.of(Shop.class)
  463.                 .map(Class::getDeclaredMethods)
  464.                 .flatMap(Stream::of)
  465.                 .filter(m -> Modifier.isStatic(m.getModifiers()))
  466.                 .filter(m -> Modifier.isPrivate(m.getModifiers()))
  467.                 .map(Method::getName)
  468.                 .filter(name -> name.contains("lambda$"))
  469.                 .findAny()
  470.                 .ifPresent(m ->
  471.                         fail(() -> "Using of lambda expressions is restricted: " + m));
  472.     }
  473.  
  474.     @Test
  475.     void appShouldUseOnlyOptionalFromJavaUtilPackage() {
  476.         SpoonAPI spoon = new Launcher();
  477.         spoon.addInputResource("src/main/java/");
  478.         spoon.buildModel();
  479.  
  480.         spoon.getModel()
  481.                 .getElements(new TypeFilter<>(CtTypeReference.class))
  482.                 .stream()
  483.                 .filter(r -> r.toString().startsWith("java.util.stream"))
  484.                 .map(CtTypeReference::getQualifiedName)
  485.                 .findAny()
  486.                 .ifPresent(name ->
  487.                         fail(() -> "Using of stream API is restricted: " + name));
  488.     }
  489.  
  490.     @Test
  491.     void addBuyerShouldAddBuyerToProperCashBoxWhenThereAreMoreThanOneShortestQueues() {
  492.         shop = new Shop(3);
  493.         for (int j = 0; j < 3; j++) {
  494.             shop.getCashBox(j).setState(State.ENABLED);
  495.         }
  496.  
  497.         shop.addBuyer(Buyer.nextBuyer());
  498.         assertEquals(shop.getCashBox(0).getQueue().getLast().toString(), "A");
  499.  
  500.         shop.addBuyer(Buyer.nextBuyer());
  501.         assertEquals(shop.getCashBox(1).getQueue().getLast().toString(), "B");
  502.  
  503.         shop.addBuyer(Buyer.nextBuyer());
  504.         assertEquals(shop.getCashBox(2).getQueue().getLast().toString(), "C");
  505.  
  506.         shop.addBuyer(Buyer.nextBuyer());
  507.         assertEquals(shop.getCashBox(0).getQueue().getLast().toString(), "D");
  508.     }
  509.  
  510.     @Test
  511.     void printShouldWorkProperly() throws IOException {
  512.         shop = new Shop(3);
  513.         String actual = null;
  514.         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
  515.              PrintStream ps = new PrintStream(baos)) {
  516.             System.setOut(ps);
  517.  
  518.             shop.setCashBoxState(0, State.ENABLED);
  519.             shop.setCashBoxState(1, State.ENABLED);
  520.             shop.setCashBoxState(2, State.ENABLED);
  521.             shop.addBuyer(Buyer.nextBuyer());
  522.             shop.addBuyer(Buyer.nextBuyer());
  523.             shop.addBuyer(Buyer.nextBuyer());
  524.             shop.addBuyer(Buyer.nextBuyer());
  525.             shop.addBuyer(Buyer.nextBuyer());
  526.             shop.addBuyer(Buyer.nextBuyer());
  527.             shop.addBuyer(Buyer.nextBuyer());
  528.             shop.addBuyer(Buyer.nextBuyer());
  529.             shop.addBuyer(Buyer.nextBuyer());
  530.  
  531.             shop.setCashBoxState(0, State.IS_CLOSING);
  532.             shop.tact();
  533.             shop.addBuyer(Buyer.nextBuyer());
  534.  
  535.             shop.setCashBoxState(1, State.IS_CLOSING);
  536.             shop.addBuyer(Buyer.nextBuyer());
  537.             shop.addBuyer(Buyer.nextBuyer());
  538.             shop.tact();
  539.             shop.tact();
  540.  
  541.             shop.print();
  542.             actual = baos.toString();
  543.         } finally {
  544.             System.setOut(STD_OUT);
  545.         }
  546.  
  547.         actual = actual.replaceAll("\r", "").trim();
  548.         String expected = "#0[-]~#1[|]J~#2[+]KL".replace('~', '\n');
  549.         assertEquals(expected, actual);
  550.     }
  551.  
  552.     private static String getState(Shop shop) {
  553.         StringBuilder sb = new StringBuilder();
  554.         for (int j = 0; j < shop.getCashBoxCount(); j++) {
  555.             CashBox cashBox = shop.getCashBox(j);
  556.             State s = cashBox.getState();
  557.             sb.append(s == State.ENABLED ? '+' : (s == State.DISABLED ? '-' : '|'));
  558.             for (Buyer b : shop.getCashBox(j).getQueue()) {
  559.                 sb.append(b);
  560.             }
  561.         }
  562.         return sb.toString();
  563.     }
  564.  
  565.     @Test
  566.     void neverSetCashBoxStateTest() {
  567.         Shop shop1 = new Shop(1);
  568.         shop1.setCashBoxState(0, State.ENABLED);
  569.         assertEquals(State.ENABLED, shop1.getCashBox(0).getState());
  570.  
  571.         shop1.setCashBoxState(0, State.DISABLED);
  572.         assertEquals(State.DISABLED, shop1.getCashBox(0).getState());
  573.  
  574.         shop1.setCashBoxState(0, State.IS_CLOSING);
  575.         assertEquals(State.IS_CLOSING, shop1.getCashBox(0).getState());
  576.     }
  577.  
  578.     @Test
  579.     void neverCashBoxCountTest() {
  580.         Shop shop1 = new Shop(1);
  581.         assertEquals(1, shop1.getCashBoxCount());
  582.  
  583.         shop1 = new Shop(2);
  584.         assertEquals(2, shop1.getCashBoxCount());
  585.  
  586.         shop1 = new Shop(3);
  587.         assertEquals(3, shop1.getCashBoxCount());
  588.  
  589.         Random rand = new Random();
  590.         for (int k = 0; k < 100; k++) {
  591.             int randomValue = rand.nextInt(500) + 1;
  592.             shop1 = new Shop(randomValue);
  593.             assertEquals(randomValue, shop1.getCashBoxCount());
  594.             assertEquals(State.DISABLED, shop1.getCashBox(randomValue - 1).getState());
  595.  
  596.             Shop finalShop = shop1;
  597.             assertThrows(IndexOutOfBoundsException.class, () -> finalShop.getCashBox(randomValue));
  598.         }
  599.     }
  600.  
  601.     @Test
  602.     void neverAddBuyerCountTest() {
  603.         Random rand = new Random();
  604.         for (int k = 0; k < 100; k++) {
  605.             final int cashBoxesCount = rand.nextInt(200) + 1;
  606.             final int buyersCount = rand.nextInt(200) + 1;
  607.             final int minBuyersCount = buyersCount / cashBoxesCount;
  608.             final int maxBuyersCount = minBuyersCount + 1;
  609.  
  610.             Shop shop1 = new Shop(cashBoxesCount);
  611.  
  612.             for (int i = 0; i < cashBoxesCount; i++) {
  613.                 shop1.setCashBoxState(i, State.ENABLED);
  614.             }
  615.  
  616.             Buyer.resetNames();
  617.             for (int i = 0; i < buyersCount; i++) {
  618.                 shop1.addBuyer(Buyer.nextBuyer());
  619.             }
  620.  
  621.             int actualCountOfByers = 0;
  622.             for (int i = 0; i < cashBoxesCount; i++) {
  623.                 CashBox currentCashBox = shop1.getCashBox(i);
  624.                 int countOfBuyerAtCashBox = currentCashBox.getQueue().size();
  625.                 assertTrue(countOfBuyerAtCashBox >= minBuyersCount);
  626.                 assertTrue(countOfBuyerAtCashBox <= maxBuyersCount);
  627.                 actualCountOfByers += currentCashBox.getQueue().size();
  628.             }
  629.  
  630.             assertEquals(buyersCount, actualCountOfByers);
  631.         }
  632.     }
  633.  
  634.     @Test
  635.     void neverGetLeastLoadedCashBox() {
  636.         Shop shop1 = new Shop(2);
  637.  
  638.         shop1.setCashBoxState(0, State.ENABLED);
  639.         shop1.setCashBoxState(1, State.ENABLED);
  640.         assertEquals(shop1.getCashBox(0), shop1.getLeastLoadedCashbox());
  641.  
  642.         Buyer.resetNames();
  643.         shop1.addBuyer(Buyer.nextBuyer()); //A
  644.         assertEquals(shop1.getCashBox(1), shop1.getLeastLoadedCashbox());
  645.  
  646.         shop1.addBuyer(Buyer.nextBuyer()); //B
  647.         assertEquals(shop1.getCashBox(0), shop1.getLeastLoadedCashbox());
  648.  
  649.         shop1.addBuyer(Buyer.nextBuyer()); //C
  650.         assertEquals(shop1.getCashBox(1), shop1.getLeastLoadedCashbox());
  651.  
  652.         shop1.setCashBoxState(1, State.IS_CLOSING);
  653.         assertEquals(shop1.getCashBox(0), shop1.getLeastLoadedCashbox());
  654.  
  655.         shop1.setCashBoxState(1, State.DISABLED);
  656.         assertEquals(shop1.getCashBox(0), shop1.getLeastLoadedCashbox());
  657.  
  658.         shop1.addBuyer(Buyer.nextBuyer()); //D
  659.         shop1.addBuyer(Buyer.nextBuyer()); //E
  660.         shop1.setCashBoxState(1, State.ENABLED);
  661.         assertEquals(shop1.getCashBox(1), shop1.getLeastLoadedCashbox());
  662.  
  663.         shop1.addBuyer(Buyer.nextBuyer()); //F
  664.         shop1.addBuyer(Buyer.nextBuyer()); //G
  665.         assertEquals(shop1.getCashBox(1), shop1.getLeastLoadedCashbox());
  666.     }
  667.  
  668.     @Test
  669.     void neverToStringTest() {
  670.         Shop shop1 = new Shop(1);
  671.         assertEquals("#0[-]", shop1.toString());
  672.  
  673.         shop1 = new Shop(2);
  674.         assertEquals("#0[-]\n" + "#1[-]", shop1.toString());
  675.  
  676.         shop1 = new Shop(4);
  677.         assertEquals("""
  678.                #0[-]
  679.                #1[-]
  680.                #2[-]
  681.                #3[-]""", shop1.toString());
  682.  
  683.         shop1.setCashBoxState(0, State.ENABLED);
  684.         shop1.setCashBoxState(2, State.ENABLED);
  685.         assertEquals("""
  686.                #0[+]
  687.                #1[-]
  688.                #2[+]
  689.                #3[-]""", shop1.toString());
  690.  
  691.         shop1.setCashBoxState(1, State.IS_CLOSING);
  692.         shop1.setCashBoxState(3, State.DISABLED);
  693.         assertEquals("""
  694.                #0[+]
  695.                #1[|]
  696.                #2[+]
  697.                #3[-]""", shop1.toString());
  698.  
  699.         Buyer.resetNames();
  700.         shop1.addBuyer(Buyer.nextBuyer()); //A
  701.         assertEquals("""
  702.                #0[+]A
  703.                #1[|]
  704.                #2[+]
  705.                #3[-]""", shop1.toString());
  706.  
  707.         shop1.addBuyer(Buyer.nextBuyer()); //B
  708.         assertEquals("""
  709.                #0[+]A
  710.                #1[|]
  711.                #2[+]B
  712.                #3[-]""", shop1.toString());
  713.  
  714.         shop1.addBuyer(Buyer.nextBuyer()); //C
  715.         assertEquals("""
  716.                #0[+]AC
  717.                #1[|]
  718.                #2[+]B
  719.                #3[-]""", shop1.toString());
  720.  
  721.         shop1.setCashBoxState(2, State.IS_CLOSING);
  722.         shop1.addBuyer(Buyer.nextBuyer()); //D
  723.         assertEquals("""
  724.                #0[+]ACD
  725.                #1[|]
  726.                #2[|]B
  727.                #3[-]""", shop1.toString());
  728.     }
  729. }
  730.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement