home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / edtplug / classes / netscape / test / plugin / composer / TestDocument.java < prev    next >
Encoding:
Java Source  |  1998-04-08  |  4.2 KB  |  149 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. package netscape.test.plugin.composer;
  20.  
  21. import java.io.*;
  22. import java.awt.*;
  23. import java.net.*;
  24. import netscape.plugin.composer.*;
  25. /** An implementation of Document which is used for testing.
  26.  */
  27.  
  28. class TestDocument extends Document {
  29.     public TestDocument(Test test, String document, URL base, String workDirectory,
  30.         URL workDirectoryURL, URL documentURL, String eventName){
  31.         this.test = test;
  32.         this.text = document;
  33.         this.workDirectory = workDirectory;
  34.         this.base = base;
  35.         this.workDirectoryURL = workDirectoryURL;
  36.         this.documentURL = documentURL;
  37.         this.eventName = eventName;
  38.     }
  39.     public URL getBase(){
  40.         return base;
  41.     }
  42.     public File getWorkDirectory(){
  43.         try {
  44.             return new File(workDirectory);
  45.         } catch ( Throwable e ) {
  46.             e.printStackTrace();
  47.             return null;
  48.         }
  49.     }
  50.     public URL getWorkDirectoryURL(){
  51.         return workDirectoryURL;
  52.     }
  53.  
  54.     public Reader getInput() throws IOException {
  55.         if ( in != null ) {
  56.             throw new IOException("Must close existing input stream first.");
  57.         }
  58.         in = new TestDocumentReader(this, text.toCharArray());
  59.         return in;
  60.     }
  61.     public Writer getOutput() throws IOException {
  62.         doOutputCheck();
  63.         out = new TestDocumentWriter(this);
  64.         return out;
  65.     }
  66.     private void doOutputCheck() throws IOException {
  67.         if ( out != null ) {
  68.             throw new IOException("Must close existing output stream first.");
  69.         }
  70.     }
  71.  
  72.     public String getText() {
  73.         return text;
  74.     }
  75.  
  76.     public void setText(String text) throws IOException {
  77.         doOutputCheck();
  78.         internalSetText(text);
  79.     }
  80.  
  81.     private void internalSetText(String text) {
  82.         this.text = text;
  83.         test.newText(text);
  84.     }
  85.  
  86.     void inputDone(TestDocumentReader in) {
  87.         if ( in == this.in ) {
  88.             this.in = null;
  89.         }
  90.         else {
  91.             // Do nothing.
  92.         }
  93.     }
  94.     void outputDone(TestDocumentWriter out, String text) {
  95.         if ( out == this.out ) {
  96.             this.out = null;
  97.             internalSetText(text);
  98.         }
  99.         else {
  100.             // Do nothing.
  101.         }
  102.     }
  103.  
  104.     public String getEventName(){
  105.         return eventName;
  106.     }
  107.     public URL getDocumentURL(){
  108.         return documentURL;
  109.     }
  110.     public void redirectDocumentOpen(String newURL){
  111.         test.reportRedirectDocumentOpen(eventName, newURL);
  112.     }
  113.  
  114.     private Test test;
  115.     private String workDirectory;
  116.     private URL workDirectoryURL;
  117.     private URL documentURL;
  118.  
  119.     private String text;
  120.     private URL base;
  121.     private TestDocumentWriter out;
  122.     private TestDocumentReader in;
  123.     private String eventName;
  124. }
  125.  
  126. class TestDocumentReader extends CharArrayReader {
  127.     public TestDocumentReader(TestDocument document, char[] text){
  128.         super(text);
  129.         this.document = document;
  130.     }
  131.  
  132.     public void close() {
  133.         document.inputDone(this);
  134.         super.close();
  135.     }
  136.     TestDocument document;
  137. }
  138.  
  139. class TestDocumentWriter extends CharArrayWriter {
  140.     public TestDocumentWriter(TestDocument document){
  141.         this.document = document;
  142.     }
  143.     public void close() {
  144.         document.outputDone(this, toString());
  145.         super.close();
  146.     }
  147.     TestDocument document;
  148. }
  149.