blob: 7603c18b4c750ad381bd41ec6f2e1220e46354b1 [file] [log] [blame]
Michael Hanle56bb892016-05-25 17:34:41 +02001package de.ids_mannheim.korap.config;
2
margaretha45667922018-01-25 21:23:03 +01003import static org.junit.Assert.assertNotNull;
4
5import org.apache.log4j.Logger;
6import org.junit.runner.RunWith;
7import org.junit.runner.notification.RunNotifier;
8import org.junit.runners.model.InitializationError;
9import org.springframework.beans.factory.annotation.Autowired;
10import org.springframework.context.ApplicationContext;
11import org.springframework.test.annotation.DirtiesContext;
12import org.springframework.test.context.ContextConfiguration;
13import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14
15import de.ids_mannheim.korap.exceptions.KustvaktException;
16import net.jcip.annotations.NotThreadSafe;
17
Michael Hanle56bb892016-05-25 17:34:41 +020018/**
19 * @author hanl
20 * @date 09/03/2016
21 */
margaretha45667922018-01-25 21:23:03 +010022@NotThreadSafe
23@RunWith(BeanConfigTest.SpringExtendedSetupListener.class)
24@ContextConfiguration("classpath:test-config.xml")
25@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
26public abstract class BeanConfigTest {
Michael Hanle56bb892016-05-25 17:34:41 +020027
margaretha45667922018-01-25 21:23:03 +010028 private static Logger jlog = Logger.getLogger(BeanConfigTest.class);
29 @Autowired
30 protected ApplicationContext context;
31
32 public void init () throws Exception {
33 assertNotNull("Application context must not be null!", this.context);
34 jlog.debug("running one-time before init for class "
35 + this.getClass().getSimpleName() + " ...");
36 BeansFactory.setKustvaktContext(getContext());
37 assertNotNull(BeansFactory.getKustvaktContext());
38 initMethod();
39 }
40
41 public abstract void initMethod () throws KustvaktException;
42
43 public void close () {
44 BeansFactory.closeApplication();
45 }
46
47 public static class SpringExtendedSetupListener
48 extends SpringJUnit4ClassRunner {
49
50 private BeanConfigTest instanceSetupListener;
51
52
53 public SpringExtendedSetupListener (Class<?> clazz)
54 throws InitializationError {
55 super(clazz);
56 }
57
58
59 @Override
60 protected Object createTest () throws Exception {
61 Object test = super.createTest();
62 // Note that JUnit4 will call this createTest() multiple times for each
63 // test method, so we need to ensure to call "beforeClassSetup" only once.
64 if (test instanceof BeanConfigTest
65 && instanceSetupListener == null) {
66 instanceSetupListener = (BeanConfigTest) test;
67 instanceSetupListener.init();
68 }
69 return test;
70 }
71
72
73 @Override
74 public void run (RunNotifier notifier) {
75 super.run(notifier);
76 if (instanceSetupListener != null) {
77 instanceSetupListener.close();
78 }
79 }
80 }
81
margarethae6f079d2017-10-09 15:57:27 +020082 protected ContextHolder getContext () {
83 return helper().getContext();
Michael Hanle56bb892016-05-25 17:34:41 +020084 }
85
Michael Hanle56bb892016-05-25 17:34:41 +020086 protected TestHelper helper () {
87 try {
88 return TestHelper.newInstance(this.context);
89 }
90 catch (Exception e) {
91 return null;
92 }
93 }
Michael Hanle56bb892016-05-25 17:34:41 +020094}