001: /*
002: * $Id: ExternalPageViewer.java 459321 2006-02-14 21:07:44Z jonl $
003: * $Revision: 459321 $ $Date: 2006-02-14 22:07:44 +0100 (Tue, 14 Feb 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.util.tester;
019:
020: import java.io.File;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.UnsupportedEncodingException;
024: import java.net.URL;
025: import java.util.ArrayList;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: /**
030: * This class is experimental only and may not yet work in your environment
031: *
032: * @author Ingram Chen
033: */
034: public class ExternalPageViewer {
035: private static final List browserPaths = new ArrayList();
036:
037: // TODO Post 1.2: General: We could use JNLP to launch browser (see http://www.croftsoft.com/library/tutorials/browser/), but why not use Swing HTMLArea??
038: static {
039: registerBrowserPath("C:/Program Files/Mozilla Firefox/firefox.exe");
040: registerBrowserPath("C:/Program Files/Internet Explorer/iexplore.exe");
041: }
042:
043: private final WicketTester tester;
044:
045: /**
046: *
047: * @param tester
048: */
049: public ExternalPageViewer(final WicketTester tester) {
050: this .tester = tester;
051: }
052:
053: /**
054: * register addtional browser path for viewInBrowser()
055: *
056: * @param path
057: */
058: public static final void registerBrowserPath(String path) {
059: browserPaths.add(path);
060: }
061:
062: /**
063: * open a web browser and see lastet rendered WebPage.
064: */
065: public final void viewInBrowser() {
066: int webRootPathIndex = getThisClassFileURL().getPath().indexOf(
067: "/WEB-INF");
068:
069: // obtain webRootPath, for example:
070: // D:/eclipse/workspace/MyProject/MyWebRoot
071: String webRootPath = getThisClassFileURL().getPath().substring(
072: 0, webRootPathIndex);
073:
074: File temp = new File(webRootPath + "/"
075: + getTemperaryDumpHtmlFileName());
076: FileOutputStream out = null;
077: try {
078: out = new FileOutputStream(temp);
079: out.write(tester.getServletResponse().getDocument()
080: .getBytes(getHtmlEncoding()));
081: out.flush();
082: } catch (UnsupportedEncodingException e) {
083: throw convertoUnexpect(e);
084: } catch (IOException e) {
085: throw convertoUnexpect(e);
086: } finally {
087: if (out != null) {
088: try {
089: out.close();
090: } catch (IOException e) {
091: throw convertoUnexpect(e);
092: }
093: }
094: }
095:
096: if (!new File(getBrowserPath()).exists()) {
097: throw new IllegalStateException("No browser found at "
098: + getBrowserPath());
099: }
100: // try {
101: throw new RuntimeException("Not yet supported");
102: // new ProcessBuilder(getBrowserPath(),
103: // temp.toURL().toString()).start();
104: // } catch (MalformedURLException e) {
105: // throw convertoUnexpect(e);
106: // } catch (IOException e) {
107: // throw convertoUnexpect(e);
108: // }
109: }
110:
111: /**
112: *
113: * @return path
114: */
115: private String getBrowserPath() {
116: Iterator iter = browserPaths.iterator();
117: while (iter.hasNext()) {
118: String path = (String) iter.next();
119: if (new File(path).exists()) {
120: return path;
121: }
122: }
123: throw new IllegalStateException(
124: "No browser found, please add definition");
125: }
126:
127: /**
128: * define a temperary file name that stores source of last rendered page.
129: * This file is used by external browser
130: *
131: * @return String
132: */
133: protected String getTemperaryDumpHtmlFileName() {
134: // this pattern will hide from eclipe and ignore by cvs
135: return ".del-wicketTestDump.html";
136: }
137:
138: /**
139: * set default encoding for writing temperary file.
140: *
141: * @return String
142: */
143: protected String getHtmlEncoding() {
144: return "UTF-8";
145: }
146:
147: /**
148: *
149: * @return URL
150: */
151: private URL getThisClassFileURL() {
152: URL url = this .getClass().getClassLoader().getResource(
153: this .getClass().getName().replace('.', '/') + ".class");
154: return url;
155: }
156:
157: /**
158: *
159: * @param e
160: * @return RuntimeException
161: */
162: private RuntimeException convertoUnexpect(Exception e) {
163: return new RuntimeException("tester: unexpect", e);
164: }
165: }
|