Advertisement
IvetValcheva

тест №3

Nov 30th, 2022
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. import core.Data;
  2.  
  3. import interfaces.Entity;
  4. import interfaces.Repository;
  5. import model.BaseEntity;
  6. import model.Invoice;
  7. import model.StoreClient;
  8. import model.User;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11.  
  12. import java.lang.reflect.Constructor;
  13. import java.lang.reflect.InvocationTargetException;
  14. import java.util.ArrayList;
  15. import java.util.Comparator;
  16. import java.util.List;
  17. import java.util.Random;
  18. import java.util.stream.Collectors;
  19.  
  20. import static org.junit.Assert.*;
  21.  
  22. public class DataTest3 {
  23.     private Repository data;
  24.     private int parentId;
  25.     private Entity savedEntity;
  26.     private int[] statusesCount;
  27.  
  28.     @Before
  29.     public void setUp() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
  30.         this.data = new Data();
  31.         Constructor<?>[] constructors = {getConstructor(Invoice.class), getConstructor(StoreClient.class), getConstructor(User.class)};
  32.         Random random = new Random();
  33.         BaseEntity.Status[] statuses = BaseEntity.Status.values();
  34.         this.statusesCount = new int[statuses.length];
  35.         List<Integer> parentIds = new ArrayList<>();
  36.         parentIds.add(0);
  37.         for (int i = 0; i < 25; i++) {
  38.             Constructor<?> constructor = constructors[random.nextInt(constructors.length)];
  39.             int nextInt = random.nextInt(i + 1);
  40.             Entity entity = (Entity) constructor.newInstance(i, parentIds.get(nextInt));
  41.             parentIds.add(i);
  42.             if (entity.getId() == 13) {
  43.                 this.parentId = entity.getParentId();
  44.                 savedEntity = entity;
  45.             }
  46.             int enumIndex = random.nextInt(statuses.length);
  47.             entity.setStatus(statuses[enumIndex]);
  48.             statusesCount[enumIndex]++;
  49.             this.data.add(entity);
  50.         }
  51.     }
  52.  
  53.     private Constructor<?> getConstructor(Class<?> clazz) throws NoSuchMethodException {
  54.         return clazz.getDeclaredConstructor(int.class, int.class);
  55.     }
  56.    
  57.     @Test(expected = IllegalStateException.class)
  58.     public void getIdWhenNull() {
  59.         Repository repository = new Data();
  60.         repository.peekMostRecent();
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement