001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.unit;
007:
008: import java.io.InputStream;
009: import java.net.MalformedURLException;
010: import java.net.URL;
011: import java.sql.Connection;
012: import java.sql.DriverManager;
013: import java.sql.SQLException;
014: import java.sql.Statement;
015: import java.util.Enumeration;
016: import java.util.HashMap;
017: import java.util.Properties;
018: import java.util.Set;
019:
020: import javax.servlet.RequestDispatcher;
021: import javax.servlet.Servlet;
022: import javax.servlet.ServletContext;
023: import javax.servlet.ServletContextEvent;
024: import javax.servlet.ServletException;
025:
026: import org.h2.server.web.DbStarter;
027: import org.h2.test.TestBase;
028:
029: /**
030: * Tests the DbStarter servlet.
031: * This test simulates a minimum servlet container environment.
032: */
033: public class TestServlet extends TestBase {
034:
035: /**
036: * Minimum ServletContext implementation.
037: * Most methods are not implemented.
038: */
039: private static class TestServletContext implements ServletContext {
040:
041: private Properties initParams = new Properties();
042: private HashMap attributes = new HashMap();
043:
044: public void setAttribute(String key, Object value) {
045: attributes.put(key, value);
046: }
047:
048: public Object getAttribute(String key) {
049: return attributes.get(key);
050: }
051:
052: private void setInitParameter(String key, String value) {
053: initParams.setProperty(key, value);
054: }
055:
056: public String getInitParameter(String key) {
057: return initParams.getProperty(key);
058: }
059:
060: public Enumeration getAttributeNames() {
061: throw new UnsupportedOperationException();
062: }
063:
064: public ServletContext getContext(String string) {
065: throw new UnsupportedOperationException();
066: }
067:
068: public Enumeration getInitParameterNames() {
069: throw new UnsupportedOperationException();
070: }
071:
072: public int getMajorVersion() {
073: throw new UnsupportedOperationException();
074: }
075:
076: public String getMimeType(String string) {
077: throw new UnsupportedOperationException();
078: }
079:
080: public int getMinorVersion() {
081: throw new UnsupportedOperationException();
082: }
083:
084: public RequestDispatcher getNamedDispatcher(String string) {
085: throw new UnsupportedOperationException();
086: }
087:
088: public String getRealPath(String string) {
089: throw new UnsupportedOperationException();
090: }
091:
092: public RequestDispatcher getRequestDispatcher(String string) {
093: throw new UnsupportedOperationException();
094: }
095:
096: public URL getResource(String string)
097: throws MalformedURLException {
098: throw new UnsupportedOperationException();
099: }
100:
101: public InputStream getResourceAsStream(String string) {
102: throw new UnsupportedOperationException();
103: }
104:
105: public Set getResourcePaths(String string) {
106: throw new UnsupportedOperationException();
107: }
108:
109: public String getServerInfo() {
110: throw new UnsupportedOperationException();
111: }
112:
113: public Servlet getServlet(String string)
114: throws ServletException {
115: throw new UnsupportedOperationException();
116: }
117:
118: public String getServletContextName() {
119: throw new UnsupportedOperationException();
120: }
121:
122: public Enumeration getServletNames() {
123: throw new UnsupportedOperationException();
124: }
125:
126: public Enumeration getServlets() {
127: throw new UnsupportedOperationException();
128: }
129:
130: public void log(String string) {
131: throw new UnsupportedOperationException();
132: }
133:
134: public void log(Exception exception, String string) {
135: throw new UnsupportedOperationException();
136: }
137:
138: public void log(String string, Throwable throwable) {
139: throw new UnsupportedOperationException();
140: }
141:
142: public void removeAttribute(String string) {
143: throw new UnsupportedOperationException();
144: }
145:
146: }
147:
148: public void test() throws Exception {
149: if (config.networked || config.memory) {
150: return;
151: }
152: DbStarter listener = new DbStarter();
153:
154: TestServletContext context = new TestServletContext();
155: context.setInitParameter("db.url", getURL("servlet", true));
156: context.setInitParameter("db.user", getUser());
157: context.setInitParameter("db.password", getPassword());
158: context.setInitParameter("db.tcpServer", "-tcpPort 8888");
159:
160: ServletContextEvent event = new ServletContextEvent(context);
161: listener.contextInitialized(event);
162:
163: Connection conn1 = listener.getConnection();
164: Connection conn1a = (Connection) context
165: .getAttribute("connection");
166: check(conn1 == conn1a);
167: Statement stat1 = conn1.createStatement();
168: stat1.execute("CREATE TABLE T(ID INT)");
169:
170: Connection conn2 = DriverManager.getConnection(
171: "jdbc:h2:tcp://localhost:8888/" + baseDir + "/servlet",
172: getUser(), getPassword());
173: Statement stat2 = conn2.createStatement();
174: stat2.execute("SELECT * FROM T");
175: stat2.execute("DROP TABLE T");
176:
177: try {
178: stat1.execute("SELECT * FROM T");
179: error();
180: } catch (SQLException e) {
181: checkNotGeneralException(e);
182: }
183:
184: conn2.close();
185:
186: listener.contextDestroyed(event);
187:
188: // listener must be stopped
189: try {
190: conn2 = DriverManager.getConnection(
191: "jdbc:h2:tcp://localhost:8888/" + baseDir
192: + "/servlet", getUser(), getPassword());
193: error();
194: } catch (SQLException e) {
195: checkNotGeneralException(e);
196: }
197:
198: // connection must be closed
199: try {
200: stat1.execute("SELECT * FROM DUAL");
201: error();
202: } catch (SQLException e) {
203: checkNotGeneralException(e);
204: }
205:
206: }
207:
208: }
|