Статьи

Tomcat Context JUnit @Rule

Первый черновик JUnit @Rule, который создает тестовый контекст. Это можно использовать с правилом контекста Spring для этой публикации, чтобы создать полный контекст Spring для интеграционных тестов.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
  
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.lang.reflect.Method;
import java.sql.Driver;
import java.sql.DriverManager;
  
  
/**
 * Creates an context for tests using an Apache Tomcat server.xml.
 *
 *
 * @author alex.collins
 */
public class TomcatContextRule implements TestRule {
  
    public static final Logger LOGGER = Logger.getLogger(CatalinaContextRule.class);
  
    /**
     * Creates all the sub-contexts for a name.
     */
    public static void createSubContexts(Context ctx, String name) {
        String subContext = '';
        for (String x : name.substring(0, name.lastIndexOf('/')).split('/')) {
            subContext += x;
            try {
                ctx.createSubcontext(subContext);
            } catch (NamingException e) {
                // nop
            }
            subContext += '/';
        }
    }
  
    private final File serverXml;
  
    public TomcatContextRule(File serverXml, Object target) {
        if (serverXml == null || !serverXml.isFile()) {throw new IllegalArgumentException();}
        if (target == null) {throw new IllegalArgumentException();}
        this.serverXml = serverXml;
    }
  
    public Statement apply(final Statement statement, Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                createInitialContext();
                try {
                    statement.evaluate();
                } finally {
                    destroyInitialContext();
                }
            }
        };
    }
  
    private void createInitialContext() throws Exception {
  
        LOGGER.info('creating context');
  
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, org.apache.naming.java.javaURLContextFactory.class.getName());
        System.setProperty(Context.URL_PKG_PREFIXES, 'org.apache.naming');
  
        final InitialContext ic = new InitialContext();
  
        createSubContexts(ic, 'java:/comp/env');
  
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        final DocumentBuilder builder = factory.newDocumentBuilder();
        final Document document = builder.parse(serverXml);
  
        // create Environment
        {
            final NodeList envs = document.getElementsByTagName('Environment');
            for (int i = 0; i < envs.getLength(); i++) {
                final Element env = (Element)envs.item(i); // must be Element
                final String name = 'java:comp/env/' + env.getAttribute('name');
                final Object instance = Class.forName(env.getAttribute('type')).getConstructor(String.class)
                        .newInstance(env.getAttribute('value'));
  
                LOGGER.info('binding ' + name + ' <' + instance + '>');
  
                createSubContexts(ic, name);
  
                ic.bind(name, instance);
            }
        }
  
        // Resource
        {
            final NodeList resources = document.getElementsByTagName('Resource');
            for (int i = 0; i < resources.getLength(); i++) {
                final Element resource = (Element)resources.item(i); // must be Element
                final String name = 'java:comp/env/' + resource.getAttribute('name');
                final Class<?> type = Class.forName(resource.getAttribute('type'));
  
                final Object instance;
                if (type.equals(DataSource.class)) {
                    {
                        @SuppressWarnings('unchecked') // this mus be driver?
                        final Class<? extends Driver> driverClass = (Class<? extends Driver>) Class.forName(resource.getAttribute('driverClassName'));
  
                        DriverManager.registerDriver(driverClass.newInstance());
                    }
  
                    final BasicDataSource dataSource = new BasicDataSource();
                    // find all the bean attributes and set them use some reflection
                    for (Method method : dataSource.getClass().getMethods()) {
  
                        if (!method.getName().matches('^set.*')) {continue;}
  
                        final String x = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4);
  
                        if (!resource.hasAttribute(x)) {continue;}
                        Class<?> y = method.getParameterTypes()[0]; // might be primitive
  
                        if (y.isPrimitive()) {
                            if (y.getName().equals('boolean')) y = Boolean.class;
                            if (y.getName().equals('byte')) y = Byte.class;
                            if (y.getName().equals('char')) y = Character.class;
                            if (y.getName().equals('double')) y = Double.class;
                            if (y.getName().equals('float')) y = Float.class;
                            if (y.getName().equals('int')) y = Integer.class;
                            if (y.getName().equals('long')) y = Long.class;
                            if (y.getName().equals('short')) y = Short.class;
                            if (y.getName().equals('void')) y = Void.class;
                        }
  
                        method.invoke(dataSource, y.getConstructor(String.class).newInstance(resource.getAttribute(x)));
                    }
  
                    instance = dataSource;
                } else {
                    // not supported, yet...
                    throw new AssertionError('type ' + type + ' not supported');
                }
  
                LOGGER.info('binding ' + name + ' <' + instance + '>');
  
                createSubContexts(ic, name);
  
                ic.bind(name, instance);
            }
        }
    }
  
    private void destroyInitialContext() {
        System.clearProperty(Context.INITIAL_CONTEXT_FACTORY);
        System.clearProperty(Context.URL_PKG_PREFIXES);
  
        LOGGER.info('context destroyed');
    }
  
}

Например:

1
2
3
@Rule
   public TestRule rules = RuleChain.outerRule(new CatalinaContextRule(new File(getClass().getResource('/server.xml').getFile()), this))
           .around(new ContextRule(new String[] {'/applicationContext.xml'}, this));

Этот код на Github .

Ссылка: Tomcat Context JUnit @Rule от нашего партнера JCG Алекса Коллинза в блоге Алекса Коллинза .