001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket;
018:
019: import java.io.File;
020: import java.io.FileOutputStream;
021: import java.io.IOException;
022: import java.io.OutputStream;
023:
024: import org.apache.wicket.protocol.http.MockHttpServletResponse;
025: import org.apache.wicket.protocol.http.WebRequestCycle;
026: import org.apache.wicket.util.resource.FileResourceStream;
027: import org.apache.wicket.util.resource.IResourceStream;
028: import org.slf4j.Logger;
029: import org.slf4j.LoggerFactory;
030:
031: /**
032: * Tests resources.
033: */
034: public class ResourceTest extends WicketTestCase {
035: private static final Logger log = LoggerFactory
036: .getLogger(ResourceTest.class);
037: private static final String TEST_STRING = "Hello, World!";
038:
039: /**
040: * Tests a resource that should be marked as being cacheable.
041: */
042: public void testCacheableResource() {
043: String testFileLastModified;
044: final File testFile;
045: try {
046: testFile = File.createTempFile(
047: ResourceTest.class.getName(), null);
048: OutputStream out = new FileOutputStream(testFile);
049: out.write(TEST_STRING.getBytes());
050: out.close();
051: } catch (IOException e) {
052: throw new RuntimeException(e);
053: }
054: testFileLastModified = MockHttpServletResponse
055: .formatDate(testFile.lastModified());
056: tester.setupRequestAndResponse();
057: WebRequestCycle cycle = tester.createRequestCycle();
058: Resource resource = new Resource() {
059:
060: private static final long serialVersionUID = 1L;
061:
062: public IResourceStream getResourceStream() {
063: return new FileResourceStream(
064: new org.apache.wicket.util.file.File(testFile));
065: }
066: };
067: resource.onResourceRequested();
068: tester.processRequestCycle(cycle);
069:
070: log.debug(tester.getLastModifiedFromResponseHeader());
071: assertEquals(testFileLastModified, tester
072: .getLastModifiedFromResponseHeader());
073: assertEquals(TEST_STRING.length(), tester
074: .getContentLengthFromResponseHeader());
075: }
076:
077: /**
078: * tests a resource that is not cacheable.
079: */
080: public void testNonCacheableResource() {
081: String testFileLastModified;
082: final File testFile;
083: try {
084: testFile = File.createTempFile(
085: ResourceTest.class.getName(), null);
086: OutputStream out = new FileOutputStream(testFile);
087: out.write(TEST_STRING.getBytes());
088: out.close();
089: } catch (IOException e) {
090: throw new RuntimeException(e);
091: }
092: testFileLastModified = MockHttpServletResponse
093: .formatDate(testFile.lastModified());
094: tester.setupRequestAndResponse();
095: WebRequestCycle cycle = tester.createRequestCycle();
096: Resource resource = new Resource() {
097: /**
098: *
099: */
100: private static final long serialVersionUID = 1L;
101:
102: public IResourceStream getResourceStream() {
103: return new FileResourceStream(
104: new org.apache.wicket.util.file.File(testFile));
105: }
106: };
107: resource.setCacheable(false);
108: resource.onResourceRequested();
109: tester.processRequestCycle(cycle);
110:
111: assertNull(tester.getLastModifiedFromResponseHeader());
112: assertEquals(TEST_STRING.length(), tester
113: .getContentLengthFromResponseHeader());
114: }
115: }
|