| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.config; |
| 2 | |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 3 | import static org.junit.Assert.assertNotNull; |
| 4 | |
| 5 | import org.apache.log4j.Logger; |
| 6 | import org.junit.runner.RunWith; |
| 7 | import org.junit.runner.notification.RunNotifier; |
| 8 | import org.junit.runners.model.InitializationError; |
| 9 | import org.springframework.beans.factory.annotation.Autowired; |
| 10 | import org.springframework.context.ApplicationContext; |
| 11 | import org.springframework.test.annotation.DirtiesContext; |
| 12 | import org.springframework.test.context.ContextConfiguration; |
| 13 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 14 | |
| 15 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 16 | import net.jcip.annotations.NotThreadSafe; |
| 17 | |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 18 | /** |
| 19 | * @author hanl |
| 20 | * @date 09/03/2016 |
| 21 | */ |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 22 | @NotThreadSafe |
| 23 | @RunWith(BeanConfigTest.SpringExtendedSetupListener.class) |
| 24 | @ContextConfiguration("classpath:test-config.xml") |
| 25 | @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) |
| 26 | public abstract class BeanConfigTest { |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 27 | |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 28 | 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 | |
| margaretha | e6f079d | 2017-10-09 15:57:27 +0200 | [diff] [blame] | 82 | protected ContextHolder getContext () { |
| 83 | return helper().getContext(); |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 86 | protected TestHelper helper () { |
| 87 | try { |
| 88 | return TestHelper.newInstance(this.context); |
| 89 | } |
| 90 | catch (Exception e) { |
| 91 | return null; |
| 92 | } |
| 93 | } |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 94 | } |