| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.config; |
| 2 | |
| 3 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 4 | import de.ids_mannheim.korap.web.service.BootableBeanInterface; |
| 5 | import de.ids_mannheim.korap.web.service.CollectionLoader; |
| 6 | import de.ids_mannheim.korap.web.service.PolicyLoader; |
| 7 | import de.ids_mannheim.korap.web.service.UserLoader; |
| 8 | import org.junit.Ignore; |
| 9 | import org.junit.Test; |
| 10 | |
| 11 | import java.util.ArrayList; |
| 12 | import java.util.HashSet; |
| 13 | import java.util.List; |
| 14 | import java.util.Set; |
| 15 | |
| 16 | import static org.junit.Assert.assertEquals; |
| 17 | import static org.junit.Assert.assertNotEquals; |
| 18 | |
| 19 | /** |
| 20 | * @author hanl |
| 21 | * @date 12/02/2016 |
| 22 | */ |
| 23 | public class LoaderTest extends BeanConfigTest { |
| 24 | |
| 25 | @Test |
| 26 | @Ignore |
| 27 | public void testConfigOrder () { |
| 28 | System.out.println("done ..."); |
| 29 | |
| 30 | List s = new ArrayList<>(); |
| 31 | s.add("new"); |
| 32 | s.add("new2"); |
| 33 | } |
| 34 | |
| 35 | |
| 36 | @Override |
| 37 | public void initMethod () throws KustvaktException {} |
| 38 | |
| 39 | |
| 40 | @Test |
| 41 | public void runBootInterfaces () { |
| 42 | Set<Class<? extends BootableBeanInterface>> set = new HashSet<>(); |
| 43 | set.add(CollectionLoader.class); |
| 44 | set.add(PolicyLoader.class); |
| 45 | set.add(UserLoader.class); |
| 46 | |
| 47 | List<BootableBeanInterface> list = new ArrayList<>(set.size()); |
| 48 | for (Class cl : set) { |
| 49 | BootableBeanInterface iface; |
| 50 | try { |
| 51 | iface = (BootableBeanInterface) cl.newInstance(); |
| 52 | list.add(iface); |
| 53 | } |
| 54 | catch (InstantiationException | IllegalAccessException e) { |
| 55 | // do nothing |
| 56 | } |
| 57 | } |
| 58 | assertEquals(set.size(), list.size()); |
| 59 | List tracer = new ArrayList(); |
| 60 | System.out.println("Found boot loading interfaces: " + list); |
| 61 | while (!set.isEmpty()) { |
| 62 | out_loop: for (BootableBeanInterface iface : new ArrayList<>(list)) { |
| 63 | try { |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 64 | for (Class cl : iface.getDependencies()) { |
| 65 | if (set.contains(cl)) |
| 66 | continue out_loop; |
| 67 | } |
| Michael Hanl | f8fcc7a | 2016-06-03 17:41:07 +0200 | [diff] [blame^] | 68 | System.out.println("Running boot instructions from class " |
| 69 | + iface.getClass().getSimpleName()); |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 70 | set.remove(iface.getClass()); |
| 71 | list.remove(iface); |
| 72 | iface.load(helper().getContext()); |
| 73 | tracer.add(iface.getClass()); |
| 74 | } |
| 75 | catch (KustvaktException e) { |
| 76 | // don't do anything! |
| 77 | System.out.println("An error occurred in class " |
| 78 | + iface.getClass().getSimpleName() + "!\n" + e); |
| 79 | throw new RuntimeException( |
| 80 | "Boot loading interface failed ..."); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | assertEquals(0, tracer.indexOf(UserLoader.class)); |
| 85 | assertNotEquals(0, tracer.indexOf(CollectionLoader.class)); |
| 86 | assertNotEquals(0, tracer.indexOf(PolicyLoader.class)); |
| 87 | } |
| 88 | |
| 89 | } |