01: package org.vraptor.test;
02:
03: import javax.servlet.ServletContext;
04:
05: import org.vraptor.config.ConfigException;
06: import org.vraptor.core.Controller;
07: import org.vraptor.core.ControllerFactory;
08: import org.vraptor.scope.ApplicationContext;
09: import org.vraptor.scope.DefaultApplicationContext;
10:
11: /**
12: * A test engine.
13: *
14: * @author Guilherme Silveira
15: *
16: */
17: public class TestEngine {
18:
19: private final Controller controller;
20:
21: private final ApplicationContext applicationContext;
22:
23: /**
24: * Creates a new test engine.
25: *
26: * @throws TestConfigException
27: */
28: private TestEngine() throws TestConfigException {
29: this (new MockedServletContext());
30: }
31:
32: public TestEngine(ServletContext context) {
33: this .applicationContext = new DefaultApplicationContext(context);
34: try {
35: this .controller = new ControllerFactory()
36: .configure(context);
37: } catch (ConfigException e) {
38: throw new TestConfigException(e);
39: }
40: }
41:
42: /**
43: * Loads vraptor configuration files and setups the engine
44: *
45: * @return the new engine
46: * @throws ConfigException
47: * some configuration problem was found
48: */
49: public static TestEngine createEngine() throws TestConfigException {
50: return new TestEngine();
51: }
52:
53: public static TestEngine createEngine(ServletContext context)
54: throws TestConfigException {
55: return new TestEngine(context);
56: }
57:
58: /**
59: * Creates a new test session
60: *
61: * @return a test session
62: */
63: public TestSession createSession() {
64: return new DefaultTestSession(controller);
65: }
66:
67: /**
68: * Returns the application context
69: *
70: * @return the context
71: */
72: public ApplicationContext getApplicationContext() {
73: return applicationContext;
74: }
75:
76: }
|