blob: 192c5613b62cb2b6693df287bc913583cae06a8c [file] [log] [blame]
Michael Hanle56bb892016-05-25 17:34:41 +02001package de.ids_mannheim.korap.config;
2
3import de.ids_mannheim.korap.exceptions.KustvaktException;
4import de.ids_mannheim.korap.web.service.BootableBeanInterface;
5import de.ids_mannheim.korap.web.service.CollectionLoader;
6import de.ids_mannheim.korap.web.service.PolicyLoader;
7import de.ids_mannheim.korap.web.service.UserLoader;
8import org.junit.Ignore;
9import org.junit.Test;
10
11import java.util.ArrayList;
12import java.util.HashSet;
13import java.util.List;
14import java.util.Set;
15
16import static org.junit.Assert.assertEquals;
17import static org.junit.Assert.assertNotEquals;
18
19/**
20 * @author hanl
21 * @date 12/02/2016
22 */
23public 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 Hanle56bb892016-05-25 17:34:41 +020064 for (Class cl : iface.getDependencies()) {
65 if (set.contains(cl))
66 continue out_loop;
67 }
Michael Hanlf8fcc7a2016-06-03 17:41:07 +020068 System.out.println("Running boot instructions from class "
69 + iface.getClass().getSimpleName());
Michael Hanle56bb892016-05-25 17:34:41 +020070 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}