001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestElements.java 3669 2007-02-26 13:51:23Z gbevin $
007: */
008: package com.uwyn.rife.cmf.elements;
009:
010: import java.io.*;
011: import java.net.InetAddress;
012: import java.net.Socket;
013: import java.net.URL;
014: import java.util.Arrays;
015:
016: import com.meterware.httpunit.*;
017: import com.uwyn.rife.cmf.Content;
018: import com.uwyn.rife.cmf.MimeType;
019: import com.uwyn.rife.cmf.dam.ContentImage;
020: import com.uwyn.rife.cmf.dam.ContentManager;
021: import com.uwyn.rife.cmf.dam.ContentQueryManager;
022: import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContentFactory;
023: import com.uwyn.rife.cmf.dam.contentstores.TestsuiteDatabaseContentStores;
024: import com.uwyn.rife.database.Datasource;
025: import com.uwyn.rife.database.Datasources;
026: import com.uwyn.rife.resources.ResourceFinderClasspath;
027: import com.uwyn.rife.tools.FileUtils;
028: import com.uwyn.rife.tools.HttpUtils;
029:
030: public class TestElements extends TestsuiteDatabaseContentStores {
031: private Datasource mDatasource = null;
032:
033: public TestElements(String datasourceName, int siteType, String name) {
034: super (datasourceName, siteType, name);
035:
036: mDatasource = Datasources.getRepInstance().getDatasource(
037: datasourceName);
038: }
039:
040: public void setUp() throws Exception {
041: super .setUp();
042: DatabaseContentFactory.getInstance(mDatasource).install();
043: }
044:
045: public void tearDown() throws Exception {
046: DatabaseContentFactory.getInstance(mDatasource).remove();
047: super .tearDown();
048: }
049:
050: public void testServeContentRaw() throws Exception {
051: int size = (int) (65535 * 5.8);
052: byte[] binary = new byte[size];
053: for (int i = 0; i < size; i++) {
054: binary[i] = (byte) (i % 255);
055: }
056:
057: ContentManager manager = DatabaseContentFactory
058: .getInstance(mDatasource);
059: Content content = new Content(MimeType.RAW,
060: new ByteArrayInputStream(binary)).name("mycoollib.so");
061: manager.storeContent("/rawdata", content, null);
062:
063: setupSite("site/cmf.xml");
064:
065: WebConversation conversation = new WebConversation();
066: WebRequest request = null;
067: WebResponse response = null;
068:
069: request = new GetMethodWebRequest(
070: "http://localhost:8181/serve/rawdata");
071: response = conversation.getResponse(request);
072:
073: assertEquals("application/octet-stream", response
074: .getContentType());
075: assertEquals(size, response.getContentLength());
076:
077: assertTrue(Arrays.equals(binary, FileUtils.readBytes(response
078: .getInputStream())));
079:
080: request = new GetMethodWebRequest(
081: "http://localhost:8181/serve/rawdata/mycoollib.so");
082: response = conversation.getResponse(request);
083:
084: assertEquals("application/octet-stream", response
085: .getContentType());
086: assertEquals(size, response.getContentLength());
087:
088: assertTrue(Arrays.equals(binary, FileUtils.readBytes(response
089: .getInputStream())));
090: }
091:
092: public void testServeContentImage() throws Exception {
093: ContentManager manager = DatabaseContentFactory
094: .getInstance(mDatasource);
095:
096: URL image_resource_gif = ResourceFinderClasspath.getInstance()
097: .getResource("uwyn.gif");
098: byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
099:
100: Content content = new Content(MimeType.IMAGE_PNG,
101: data_image_gif).name("uwyn.png");
102: manager.storeContent("/imagegif", content, null);
103:
104: setupSite("site/cmf.xml");
105:
106: WebConversation conversation = new WebConversation();
107: WebRequest request = null;
108: WebResponse response = null;
109:
110: URL image_resource_png = ResourceFinderClasspath.getInstance()
111: .getResource("uwyn.png");
112: byte[] data_image_png = FileUtils.readBytes(image_resource_png);
113:
114: request = new GetMethodWebRequest(
115: "http://localhost:8181/serve/imagegif");
116: response = conversation.getResponse(request);
117:
118: assertEquals(MimeType.IMAGE_PNG.toString(), response
119: .getContentType());
120: assertEquals(data_image_png.length, response.getContentLength());
121: assertTrue(Arrays.equals(data_image_png, FileUtils
122: .readBytes(response.getInputStream())));
123:
124: request = new GetMethodWebRequest(
125: "http://localhost:8181/serve/imagegif/uwyn.png");
126: response = conversation.getResponse(request);
127:
128: assertEquals(MimeType.IMAGE_PNG.toString(), response
129: .getContentType());
130: assertEquals(data_image_png.length, response.getContentLength());
131: assertTrue(Arrays.equals(data_image_png, FileUtils
132: .readBytes(response.getInputStream())));
133: }
134:
135: public void testServeContentImageEmbedded() throws Exception {
136: ContentManager manager = DatabaseContentFactory
137: .getInstance(mDatasource);
138:
139: URL image_resource_gif = ResourceFinderClasspath.getInstance()
140: .getResource("uwyn.gif");
141: byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
142:
143: Content content = new Content(MimeType.IMAGE_PNG,
144: data_image_gif);
145: manager.storeContent("/imagegif", content, null);
146:
147: setupSite("site/cmf.xml");
148:
149: WebConversation conversation = new WebConversation();
150: WebRequest request = null;
151: WebResponse response = null;
152:
153: request = new GetMethodWebRequest(
154: "http://localhost:8181/serve_embedded_image");
155: response = conversation.getResponse(request);
156: assertEquals(
157: "The content \"<img src=\"/serve/imagegif\" width=\"88\" height=\"33\" alt=\"\" />\" is served embedded.\n",
158: response.getText());
159:
160: request = new GetMethodWebRequest("http://localhost:8181"
161: + response.getImages()[0].getSource());
162: response = conversation.getResponse(request);
163:
164: URL image_resource_png = ResourceFinderClasspath.getInstance()
165: .getResource("uwyn.png");
166: assertTrue(Arrays.equals(FileUtils
167: .readBytes(image_resource_png), FileUtils
168: .readBytes(response.getInputStream())));
169: }
170:
171: public void testServeContentText() throws Exception {
172: ContentManager manager = DatabaseContentFactory
173: .getInstance(mDatasource);
174:
175: String data = "<i>cool beans</i><p>hot <a href=\"http://uwyn.com\">chili</a></p>";
176: Content content = new Content(MimeType.APPLICATION_XHTML, data)
177: .fragment(true).name("mytext.html");
178: manager.storeContent("/textxhtml", content, null);
179:
180: setupSite("site/cmf.xml");
181:
182: WebConversation conversation = new WebConversation();
183: WebRequest request = null;
184: WebResponse response = null;
185:
186: request = new GetMethodWebRequest(
187: "http://localhost:8181/serve/textxhtml");
188: request.setHeaderField("Accept-Encoding", "");
189: response = conversation.getResponse(request);
190:
191: assertEquals(0, response.getContentType().indexOf(
192: MimeType.APPLICATION_XHTML.toString()));
193: assertEquals(data.length(), response.getContentLength());
194: assertEquals(data, response.getText());
195:
196: request = new GetMethodWebRequest(
197: "http://localhost:8181/serve/textxhtml");
198: request.setHeaderField("Accept-Encoding", "gzip");
199: response = conversation.getResponse(request);
200:
201: assertEquals(0, response.getContentType().indexOf(
202: MimeType.APPLICATION_XHTML.toString()));
203: assertTrue(data.length() < response.getContentLength());
204: assertEquals(data, response.getText());
205:
206: request = new GetMethodWebRequest(
207: "http://localhost:8181/serve/textxhtml/mytext.html");
208: request.setHeaderField("Accept-Encoding", "");
209: response = conversation.getResponse(request);
210:
211: assertEquals(0, response.getContentType().indexOf(
212: MimeType.APPLICATION_XHTML.toString()));
213: assertEquals(data.length(), response.getContentLength());
214: assertEquals(data, response.getText());
215:
216: request = new GetMethodWebRequest(
217: "http://localhost:8181/serve/textxhtml/mytext.html");
218: request.setHeaderField("Accept-Encoding", "gzip");
219: response = conversation.getResponse(request);
220:
221: assertEquals(0, response.getContentType().indexOf(
222: MimeType.APPLICATION_XHTML.toString()));
223: assertTrue(data.length() < response.getContentLength());
224: assertEquals(data, response.getText());
225: }
226:
227: public void testServeContentTextEmbedded() throws Exception {
228: ContentManager manager = DatabaseContentFactory
229: .getInstance(mDatasource);
230:
231: String data = "<i>cool beans</i><p>hot <a href=\"http://uwyn.com\">chili</a></p>";
232: Content content = new Content(MimeType.APPLICATION_XHTML, data)
233: .fragment(true);
234: manager.storeContent("/textxhtml", content, null);
235:
236: setupSite("site/cmf.xml");
237:
238: WebConversation conversation = new WebConversation();
239: WebRequest request = null;
240: WebResponse response = null;
241:
242: request = new GetMethodWebRequest(
243: "http://localhost:8181/serve_embedded_xhtml");
244: response = conversation.getResponse(request);
245: assertEquals(
246: "The content \"<i>cool beans</i><p>hot <a href=\"http://uwyn.com\">chili</a></p>\" is served embedded.\n",
247: response.getText());
248: }
249:
250: public void testServeContentRepository() throws Exception {
251: ContentManager manager = DatabaseContentFactory
252: .getInstance(mDatasource);
253:
254: manager.createRepository("nondefault");
255:
256: String data = "<i>cool beans</i><p>hot <a href=\"http://uwyn.com\">chili</a></p>";
257: Content content = new Content(MimeType.APPLICATION_XHTML, data)
258: .fragment(true).name("mytext.html");
259: manager.storeContent("nondefault:/textxhtml", content, null);
260:
261: setupSite("site/cmf.xml");
262:
263: WebConversation conversation = new WebConversation();
264: WebRequest request = null;
265: WebResponse response = null;
266:
267: request = new GetMethodWebRequest(
268: "http://localhost:8181/serve_repository/textxhtml");
269: request.setHeaderField("Accept-Encoding", "");
270: response = conversation.getResponse(request);
271:
272: assertEquals(0, response.getContentType().indexOf(
273: MimeType.APPLICATION_XHTML.toString()));
274: assertEquals(data.length(), response.getContentLength());
275: assertEquals(data, response.getText());
276:
277: request = new GetMethodWebRequest(
278: "http://localhost:8181/serve_repository/textxhtml");
279: request.setHeaderField("Accept-Encoding", "gzip");
280: response = conversation.getResponse(request);
281:
282: assertEquals(0, response.getContentType().indexOf(
283: MimeType.APPLICATION_XHTML.toString()));
284: assertTrue(data.length() < response.getContentLength());
285: assertEquals(data, response.getText());
286:
287: request = new GetMethodWebRequest(
288: "http://localhost:8181/serve_repository/textxhtml/mytext.html");
289: request.setHeaderField("Accept-Encoding", "");
290: response = conversation.getResponse(request);
291:
292: assertEquals(0, response.getContentType().indexOf(
293: MimeType.APPLICATION_XHTML.toString()));
294: assertEquals(data.length(), response.getContentLength());
295: assertEquals(data, response.getText());
296:
297: request = new GetMethodWebRequest(
298: "http://localhost:8181/serve_repository/textxhtml/mytext.html");
299: request.setHeaderField("Accept-Encoding", "gzip");
300: response = conversation.getResponse(request);
301:
302: assertEquals(0, response.getContentType().indexOf(
303: MimeType.APPLICATION_XHTML.toString()));
304: assertTrue(data.length() < response.getContentLength());
305: assertEquals(data, response.getText());
306:
307: request = new GetMethodWebRequest(
308: "http://localhost:8181/serve/textxhtml");
309: try {
310: response = conversation.getResponse(request);
311: fail();
312: } catch (HttpNotFoundException e) {
313: assertEquals(404, e.getResponseCode());
314: }
315:
316: request = new GetMethodWebRequest(
317: "http://localhost:8181/serve/textxhtml/mytext.html");
318: try {
319: response = conversation.getResponse(request);
320: fail();
321: } catch (HttpNotFoundException e) {
322: assertEquals(404, e.getResponseCode());
323: }
324: }
325:
326: public void testServeContentUnknown() throws Exception {
327: setupSite("site/cmf.xml");
328:
329: WebConversation conversation = new WebConversation();
330: WebRequest request = null;
331:
332: request = new GetMethodWebRequest(
333: "http://localhost:8181/serve/imageunknown");
334: try {
335: conversation.getResponse(request);
336: fail();
337: } catch (HttpNotFoundException e) {
338: assertEquals(404, e.getResponseCode());
339: }
340: }
341:
342: public void testServeContentNoPathinfo() throws Exception {
343: setupSite("site/cmf.xml");
344:
345: WebConversation conversation = new WebConversation();
346: WebRequest request = null;
347:
348: request = new GetMethodWebRequest("http://localhost:8181/serve");
349: try {
350: conversation.getResponse(request);
351: fail();
352: } catch (HttpNotFoundException e) {
353: assertEquals(404, e.getResponseCode());
354: }
355: }
356:
357: public void testServeContentNoDatasource() throws Exception {
358: setupSite("site/cmf.xml");
359:
360: WebConversation conversation = new WebConversation();
361: WebRequest request = null;
362:
363: request = new GetMethodWebRequest(
364: "http://localhost:8181/serve_nodatasource/dummy");
365: try {
366: conversation.getResponse(request);
367: fail();
368: } catch (Throwable e) {
369: assertTrue(getLogSink().getInternalException() instanceof MissingDatasourceException);
370: assertEquals(".SERVE_NO_DATASOURCE",
371: ((MissingDatasourceException) getLogSink()
372: .getInternalException()).getId());
373: }
374: }
375:
376: public void testContentQueryManagerContentForHtml()
377: throws Exception {
378: ContentQueryManager manager = new ContentQueryManager<ContentImage>(
379: mDatasource, ContentImage.class);
380: manager.install();
381: try {
382: URL image_resource_gif = ResourceFinderClasspath
383: .getInstance().getResource("uwyn.gif");
384: byte[] data_image_gif = FileUtils
385: .readBytes(image_resource_gif);
386: ContentImage content = new ContentImage().name(
387: "the content name").image(data_image_gif);
388:
389: int id = manager.save(content);
390:
391: setupSite("site/cmf.xml");
392:
393: HttpUtils.Page page = HttpUtils
394: .retrievePage("http://localhost:8181/contentforhtml?id="
395: + id);
396:
397: assertEquals(
398: "<img src=\"/serve/contentimage/"
399: + id
400: + "/image\" width=\"88\" height=\"33\" alt=\"\" />"
401: + "<img src=\"/serve/contentimage/"
402: + id
403: + "/image\" width=\"88\" height=\"33\" alt=\"\" />",
404: page.getContent());
405: } finally {
406: manager.remove();
407: }
408: }
409:
410: public void testServeContentModifiedSince() throws Exception {
411: ContentManager manager = DatabaseContentFactory
412: .getInstance(mDatasource);
413:
414: String data = "<i>cool beans</i><p>hot <a href=\"http://uwyn.com\">chili</a></p>";
415: Content content = new Content(MimeType.APPLICATION_XHTML, data)
416: .fragment(true);
417: manager.storeContent("/textxhtml", content, null);
418:
419: setupSite("site/cmf.xml");
420:
421: // doing this with a raw sockets since there's some bug in JDK 1.4 that
422: // triggers an exception otherwise
423: StringBuffer buffer = new StringBuffer();
424: try {
425: InetAddress addr = InetAddress.getByName("localhost");
426: int port = 8181;
427:
428: Socket socket = new Socket(addr, port);
429: BufferedWriter writer = new BufferedWriter(
430: new OutputStreamWriter(socket.getOutputStream(),
431: "UTF-8"));
432: writer.write("GET /serve/textxhtml HTTP/1.1\r\n");
433: writer.write("Host: localhost\r\n");
434: writer.write("User-Agent: RIFE\r\n");
435: writer.write("Accept: text/html\r\n");
436: writer
437: .write("If-Modified-Since: 24 Aug 2204 15:14:06 GMT\r\n");
438: writer.write("\r\n");
439: writer.flush();
440:
441: BufferedReader reader = new BufferedReader(
442: new InputStreamReader(socket.getInputStream()));
443: String line;
444: while ((line = reader.readLine()) != null) {
445: buffer.append(line);
446: if (0 == line.length()) {
447: break;
448: }
449: }
450: writer.close();
451: reader.close();
452: socket.close();
453: } catch (Throwable e) {
454: e.printStackTrace();
455: }
456:
457: assertEquals(0, buffer.indexOf("HTTP/1.1 304 Not Modified"));
458: }
459: }
|