001: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.plugins.pdftest;
003:
004: import java.io.IOException;
005: import java.net.URL;
006: import java.util.Collections;
007: import java.util.List;
008:
009: import com.canoo.webtest.boundary.FileBoundary;
010: import com.canoo.webtest.boundary.UrlBoundary;
011: import com.canoo.webtest.plugins.pdftest.htmlunit.PDFEncryptionPermission;
012: import com.canoo.webtest.plugins.pdftest.htmlunit.PDFPage;
013: import com.canoo.webtest.plugins.pdftest.htmlunit.pdfbox.PdfBoxPDFPage;
014: import com.canoo.webtest.self.ContextStub;
015: import com.canoo.webtest.steps.BaseStepTestCase;
016: import com.canoo.webtest.steps.Step;
017: import com.gargoylesoftware.htmlunit.Page;
018: import com.gargoylesoftware.htmlunit.SubmitMethod;
019: import com.gargoylesoftware.htmlunit.WebResponse;
020: import com.gargoylesoftware.htmlunit.WebResponseData;
021: import com.gargoylesoftware.htmlunit.WebResponseImpl;
022:
023: /**
024: * @author Etienne Studer
025: * @author Marc Guillemot
026: */
027: public abstract class AbstractBaseVerifyPdfTestCase extends
028: BaseStepTestCase {
029: private PDFPage pdfPageStub;
030:
031: public void testNoCurrentResponse() throws IOException {
032: assertStepRejectsNullResponse(getMinimallyConfiguredStep());
033: }
034:
035: protected void tearDown() throws Exception {
036: final Page page = getContext().getCurrentResponse();
037: if (page != null)
038: page.cleanUp();
039: if (pdfPageStub != null)
040: pdfPageStub.cleanUp();
041: super .tearDown();
042: }
043:
044: /**
045: * Gets an instance of the step under test with minimal configuration
046: * allowing verifyParameters() to be successfull.
047: * @return default is same as getStep()
048: */
049: protected Step getMinimallyConfiguredStep() {
050: return getStep();
051: }
052:
053: /**
054: * Creates a context with the default file for pdf tests
055: * @see com.canoo.webtest.steps.BaseStepTestCase#createContext()
056: */
057: protected ContextStub createContext() {
058: return new PdfContextStub(PdfTestResources.DEFAULT_FILE);
059: }
060:
061: public void testNonPdf() throws IOException {
062: final Step step = getMinimallyConfiguredStep();
063: getContext().setDefaultResponse("", "text/plain");
064: assertErrorOnExecute(
065: step,
066: "Current response is not a PDF page but has following mime type text/plain",
067: "");
068: }
069:
070: protected PDFPage getPDFPageStub() {
071: if (pdfPageStub == null)
072: pdfPageStub = createPDFPageStub();
073:
074: return pdfPageStub;
075: }
076:
077: private PDFPage createPDFPageStub() {
078: final byte[] pdfBytes = FileBoundary
079: .getBytes(PdfTestResources.DEFAULT_FILE);
080: final WebResponseData data = new WebResponseData(pdfBytes, 200,
081: "OK", Collections.EMPTY_LIST);
082: final URL url = UrlBoundary
083: .tryCreateUrl(ContextStub.SOME_BASE_URL);
084: final WebResponse webResp = new WebResponseImpl(data, url,
085: SubmitMethod.GET, 1);
086: return new PdfBoxPDFPage(webResp, null) {
087: public boolean isEncrypted() {
088: return true;
089: }
090:
091: public String getText(String s1, String s2, String s3,
092: String s4) {
093: throw new RuntimeException("Dummy Error");
094: }
095:
096: public List getBookmarks() {
097: throw new RuntimeException("Dummy Error");
098: }
099:
100: public List getLinks() {
101: throw new RuntimeException("Dummy Error");
102: }
103:
104: public boolean hasPermission(
105: final PDFEncryptionPermission permission) {
106: throw new RuntimeException("Dummy Error");
107: }
108: };
109: }
110: }
|