Advertisement
IvetValcheva

Тест №23

Nov 30th, 2022
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. import interfaces.Buffer;
  2. import core.Loader;
  3. import interfaces.Entity;
  4. import model.BaseEntity;
  5. import model.Invoice;
  6. import model.StoreClient;
  7. import model.User;
  8. import org.junit.Before;
  9. import org.junit.Test;
  10.  
  11. import java.lang.reflect.Constructor;
  12. import java.lang.reflect.InvocationTargetException;
  13. import java.util.List;
  14. import java.util.Random;
  15.  
  16. import static org.junit.Assert.*;
  17.  
  18. public class LoaderTest23 {
  19.  
  20.     private Buffer buffer;
  21.     private Entity savedEntity;
  22.     private int[] statusesCount;
  23.  
  24.     @Before
  25.     public void setUp() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
  26.         this.buffer = new Loader();
  27.         Constructor<?>[] constructors = {getConstructor(Invoice.class), getConstructor(StoreClient.class), getConstructor(User.class)};
  28.         Random random = new Random();
  29.         int boundIndex = random.nextInt(25);
  30.         BaseEntity.Status[] statuses = BaseEntity.Status.values();
  31.         this.statusesCount = new int[statuses.length];
  32.         for (int i = 0; i < 25; i++) {
  33.             Constructor<?> constructor = constructors[random.nextInt(constructors.length)];
  34.             Entity entity = (Entity) constructor.newInstance(i, i + 13);
  35.             if (i == boundIndex) {
  36.                 this.savedEntity = entity;
  37.             }
  38.             int enumIndex = random.nextInt(statuses.length);
  39.             entity.setStatus(statuses[enumIndex]);
  40.             statusesCount[enumIndex]++;
  41.             this.buffer.add(entity);
  42.         }
  43.     }
  44.  
  45.     private Constructor<?> getConstructor(Class<?> clazz) throws NoSuchMethodException {
  46.         return clazz.getDeclaredConstructor(int.class, int.class);
  47.     }
  48.    
  49.      @Test
  50.     public void sizeAfterRemove() {
  51.         int expectedCount = 25 - statusesCount[BaseEntity.Status.SOLD.ordinal()];
  52.         this.buffer.removeSold();
  53.         assertEquals(expectedCount, this.buffer.entitiesCount());
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement