Implement a trivial HTTP server to test http file loads, and what do you

know, I find that selection doesn't work.  Hmm.  Well, let's hear it for
test first.

Naturally, the next step is to fix the new NavigationTest.testHttpLoad().

A test/automated/src/test/HttpNavigationTest.txt

- data file for testHttpLoad().

A test/automated/src/classes/org/mozilla/util/THTTPD.java

- trivial HTTP server that suits my needs.

M test/automated/src/classes/org/mozilla/webclient/NavigationTest.java

- added commented out testHttpLoad().  It breaks.

M test/automated/src/classes/org/mozilla/webclient/WebclientTestCase.java

- remove emacs mode line.
This commit is contained in:
edburns%acm.org 2004-06-18 13:53:14 +00:00
Родитель 1f106fb784
Коммит 3d944e6dd3
5 изменённых файлов: 295 добавлений и 15 удалений

Просмотреть файл

@ -144,12 +144,6 @@
<classpath refid="test.classpath"/>
<formatter type="plain" usefile="false"/>
<test name="org.mozilla.webclient.BrowserControlFactoryTest"/>
<test name="org.mozilla.webclient.ProfileManagerTest"/>
<test name="org.mozilla.webclient.BookmarksTest"/>
<test name="org.mozilla.webclient.PreferencesTest"/>
<test name="org.mozilla.webclient.impl.wrapper_native.WrapperFactoryImplTest"/>
<test name="org.mozilla.webclient.impl.WebclientFactoryImplTest"/>
<test name="org.mozilla.webclient.NavigationTest"/>
<!-- non running
<test name="org.mozilla.webclient.wrapper_native.gtk.TestGtkBrowserControlCanvas"/>

Просмотреть файл

@ -0,0 +1,216 @@
/*
* $Id: THTTPD.java,v 1.1 2004-06-18 13:53:13 edburns%acm.org Exp $
*/
/*
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Sun
* Microsystems, Inc. Portions created by Sun are
* Copyright (C) 1999 Sun Microsystems, Inc. All
* Rights Reserved.
*
* Contributor(s): Ed Burns &lt;edburns@acm.org&gt;
*/
package org.mozilla.util;
import java.io.File;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
// THTTPD.java
public class THTTPD extends Object {
public final static int PORT = 5243;
public static class ServerThread extends Thread {
protected File root = null;
protected boolean keepRunning = true;
protected int maxRequests = -1;
protected int numRequests = 0;
protected int count = 0;
public final static int REQUEST_GET = 2;
public final static int REQUEST_POST = 2;
public ServerThread(String name, File root,
int maxRequests) {
super(name);
this.root = root;
this.maxRequests = maxRequests;
keepRunning = true;
}
protected int soTimeout = -1;
public int getSoTimeout() {
return soTimeout;
}
public void setSoTimeout(int newSoTimeout) {
soTimeout = newSoTimeout;
}
public void stopRunning() {
keepRunning = false;
this.interrupt();
}
public void run() {
ServerSocket serverSocket = null;
Socket socket = null;
BufferedReader
responseReader = null,
requestReader = null;
BufferedWriter
responseWriter = null;
String
requestLine = null,
curLine = null;
File responseFile = null;
StringBuffer responseString = null;
V();
while (keepRunning) {
if ((-1 != maxRequests) && numRequests < maxRequests) {
break;
}
numRequests++;
try {
serverSocket = new ServerSocket(PORT);
if (-1 != getSoTimeout()) {
serverSocket.setSoTimeout(getSoTimeout());
}
socket = serverSocket.accept();
requestReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
requestLine = requestReader.readLine();
while (null != (curLine = requestReader.readLine())) {
if (curLine.trim().length() == 0) {
break;
}
}
switch (getRequestMethod(requestLine)) {
case REQUEST_GET:
responseWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
if (null !=
(responseFile = getFileForRequestURI(getRequestURI(requestLine)))) {
curLine = "HTTP/1.0 200 OK\r\n";
responseWriter.write(curLine, 0,
curLine.length());
responseReader = new BufferedReader(new InputStreamReader(new FileInputStream(responseFile)));
responseString = new StringBuffer();
while (null != (curLine = responseReader.readLine())) {
responseString.append(curLine);
}
curLine = "Content-type: text/plain\r\nContent-Length: " + responseString.length() + "\r\n\r\n";
responseWriter.write(curLine, 0,
curLine.length());
responseWriter.write(responseString.toString(),
0, responseString.length());
responseWriter.flush();
responseWriter.close();
}
break;
}
socket.close();
serverSocket.close();
}
catch (Exception e) {
System.out.println("Exception: " + e + " " +
e.getMessage());
stopRunning();
}
}
V();
}
protected int getRequestMethod(String requestLine) {
int result = REQUEST_GET;
return result;
}
protected String getRequestURI(String requestLine) {
String result = null;
int space2, space1 = requestLine.indexOf(" ");
if (-1 == space1) {
return result;
}
space2 = requestLine.indexOf(" ", ++space1);
if (-1 == space2) {
return result;
}
result = requestLine.substring(space1, space2);
return result;
}
protected File getFileForRequestURI(String requestURI) {
File result = new File(root, requestURI);
if (!result.exists() || result.isDirectory()) {
result = null;
}
return result;
}
public synchronized void P() {
while (count <= 0) {
try { wait(); } catch (InterruptedException ex) {}
}
--count;
}
public synchronized void V() {
++count;
notifyAll();
}
}
public static void printUsage() {
}
public static void main(String args[]) {
// validate args
if ((args.length < 2) ||
(null == args[0] || 0 == args[0].length()) ||
(null == args[1] || 0 == args[1].length()) ||
(!args[0].equals("-root"))) {
printUsage();
return;
}
File root = new File(args[1]);
if (!root.exists() || !root.isDirectory()) {
printUsage();
}
Object toNotify = new Object();
ServerThread server = new ServerThread("THTTPD-MainThread", root, -1);
server.start();
server.P();
server.P();
}
}

Просмотреть файл

@ -1,5 +1,5 @@
/*
* $Id: NavigationTest.java,v 1.13 2004-06-16 14:37:34 edburns%acm.org Exp $
* $Id: NavigationTest.java,v 1.14 2004-06-18 13:53:13 edburns%acm.org Exp $
*/
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
@ -26,6 +26,8 @@
package org.mozilla.webclient;
import org.mozilla.util.THTTPD;
import junit.framework.TestSuite;
import junit.framework.TestResult;
import junit.framework.Test;
@ -80,9 +82,7 @@ public class NavigationTest extends WebclientTestCase {
// Testcases
//
/*************
public void testLoad() throws Exception {
public void testFileAndStreamLoad() throws Exception {
BrowserControl firstBrowserControl = null;
DocumentListener listener = null;
Selection selection = null;
@ -190,8 +190,6 @@ public class NavigationTest extends WebclientTestCase {
BrowserControlFactory.deleteBrowserControl(firstBrowserControl);
}
****************/
public void testStop() throws Exception {
DocumentListener listener = null;
BrowserControl firstBrowserControl = BrowserControlFactory.newBrowserControl();
@ -250,6 +248,77 @@ public class NavigationTest extends WebclientTestCase {
BrowserControlFactory.deleteBrowserControl(firstBrowserControl);
}
/**********
public void testHttpLoad() throws Exception {
BrowserControl firstBrowserControl = null;
DocumentListener listener = null;
Selection selection = null;
firstBrowserControl = BrowserControlFactory.newBrowserControl();
assertNotNull(firstBrowserControl);
BrowserControlCanvas canvas = (BrowserControlCanvas)
firstBrowserControl.queryInterface(BrowserControl.BROWSER_CONTROL_CANVAS_NAME);
eventRegistration = (EventRegistration2)
firstBrowserControl.queryInterface(BrowserControl.EVENT_REGISTRATION_NAME);
assertNotNull(canvas);
Frame frame = new Frame();
frame.setUndecorated(true);
frame.setBounds(0, 0, 640, 480);
frame.add(canvas, BorderLayout.CENTER);
frame.setVisible(true);
canvas.setVisible(true);
Navigation2 nav = (Navigation2)
firstBrowserControl.queryInterface(BrowserControl.NAVIGATION_NAME);
assertNotNull(nav);
final CurrentPage2 currentPage = (CurrentPage2)
firstBrowserControl.queryInterface(BrowserControl.CURRENT_PAGE_NAME);
assertNotNull(currentPage);
//
// try loading a file over HTTP
//
NavigationTest.keepWaiting = true;
final THTTPD.ServerThread serverThread =
new THTTPD.ServerThread("LocalHTTPD",
new File (getBrowserBinDir()), 1);
serverThread.setSoTimeout(15000);
serverThread.start();
serverThread.P();
eventRegistration.addDocumentLoadListener(listener = new DocumentListener() {
public void doEndCheck() {
currentPage.selectAll();
Selection selection = currentPage.getSelection();
NavigationTest.keepWaiting = false;
serverThread.stopRunning();
assertTrue(-1 != selection.toString().indexOf("This file was downloaded over HTTP."));
System.out.println("Selection is: " +
selection.toString());
}
});
String url = "http://localhost:5243/HttpNavigationTest.txt";
nav.loadURL(url);
// keep waiting until the previous load completes
while (NavigationTest.keepWaiting) {
Thread.currentThread().sleep(1000);
}
eventRegistration.removeDocumentLoadListener(listener);
frame.setVisible(false);
BrowserControlFactory.deleteBrowserControl(firstBrowserControl);
}
****************/
public static abstract class DocumentListener implements DocumentLoadListener {
public void eventDispatched(WebclientEvent event) {

Просмотреть файл

@ -1,8 +1,8 @@
/*
* $Id: WebclientTestCase.java,v 1.5 2004-04-23 13:17:51 edburns%acm.org Exp $
* $Id: WebclientTestCase.java,v 1.6 2004-06-18 13:53:13 edburns%acm.org Exp $
*/
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/*
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
@ -44,7 +44,7 @@ import junit.framework.TestCase;
*
* <B>Lifetime And Scope</B> <P>
*
* @version $Id: WebclientTestCase.java,v 1.5 2004-04-23 13:17:51 edburns%acm.org Exp $
* @version $Id: WebclientTestCase.java,v 1.6 2004-06-18 13:53:13 edburns%acm.org Exp $
*
* @see Blah
* @see Bloo

Просмотреть файл

@ -0,0 +1 @@
This file was downloaded over HTTP.