You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by re...@apache.org on 2001/01/26 07:54:57 UTC

cvs commit: jakarta-slide/src/tests/client TestCookie.java TestDOMUtils.java

remm        01/01/25 22:54:57

  Added:       src/tests/client TestCookie.java TestDOMUtils.java
  Log:
  - Readd the two Webdav client tests.
  
  Revision  Changes    Path
  1.1                  jakarta-slide/src/tests/client/TestCookie.java
  
  Index: TestCookie.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-slide/src/tests/client/TestCookie.java,v 1.1 2001/01/26 06:54:57 remm Exp $
   * $Revision: 1.1 $
   * $Date: 2001/01/26 06:54:57 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  package client;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import junit.ui.TestRunner;
  
  import org.apache.webdav.lib.Cookie;
  import org.apache.webdav.lib.Header;
  import org.apache.webdav.lib.WebdavException;
  
  /**
   * Test cases for Cookie
   *
   * @author BC Holmes
   * @version $Revision: 1.1 $
   */
  public class TestCookie extends TestCase {
  
      private static final String DOMAIN_NAME = "www.apache.org";
  
      private String[] testName = { "custno", "name", "name" };
      private String[] testValue = { "12345", "John", "Doe, John" };
      private String[] testDomain = { "www.apache.org", ".apache.org",
          ".apache.org" };
  
      public TestCookie(String name) {
          super(name);
      }
  
      public static Test suite() {
          return new TestSuite(TestCookie.class);
      }
  
  /*
      public static void main(String[] args) {
          new TestRunner().start(new String[] { TestCookie.class.getName() },
                                 new StandardTestSuiteLoader());
      }
  */
  
      /**
       * Test basic parse (with various spacings
       */
      public void testParse1() throws Exception {
          String headerValue = "custno = 12345; comment=test; version=1," +
              " name=John; version=1; max-age=600; secure; domain=.apache.org";
          Cookie[] cookies = Cookie.parse(DOMAIN_NAME, new Header(
              "set-cookie", headerValue));
          checkResultsOfParse(cookies, 2, 0);
      }
  
  
      protected void checkResultsOfParse(
          Cookie[] cookies, int length, int offset) throws Exception {
  
          assert("number of cookies should be " + length + ", but is " +
                 cookies.length + " instead.", cookies.length == length);
  
          for (int i = 0; i < cookies.length; i++) {
  
              assert("Name of cookie " + i + " should be \"" +
                     testName[i+offset] + "\", but is " + cookies[i].getName() +
                     " instead.",
                     testName[i+offset].equals(cookies[i].getName()));
              assert("Value of cookie " + i + " should be \"" +
                     testValue[i+offset] + "\", but is " +
                     cookies[i].getValue() + " instead.",
                     testValue[i+offset].equals(cookies[i].getValue()));
              assert("Domain of cookie " + i + " should be \"" +
                     testDomain[i+offset] + "\", but is " +
                     cookies[i].getDomain() + " instead.",
                     testDomain[i+offset].equalsIgnoreCase(
                         cookies[i].getDomain()));
          }
      }
  
      /**
       * Test no spaces
       */
      public void testParse2() throws Exception {
          String headerValue = "custno=12345;comment=test; version=1," +
              "name=John;version=1;max-age=600;secure;domain=.apache.org";
          Cookie[] cookies = Cookie.parse(DOMAIN_NAME, new Header(
              "set-cookie", headerValue));
          checkResultsOfParse(cookies, 2, 0);
      }
  
      /**
       * Test parse with quoted text
       */
      public void testParse3() throws Exception {
          String headerValue =
              "name=\"Doe, John\";version=1;max-age=600;secure;domain=.apache.org";
          Cookie[] cookies = Cookie.parse(DOMAIN_NAME, new Header(
              "set-cookie", headerValue));
          checkResultsOfParse(cookies, 1, 2);
      }
  
      /**
       * Test security error
       */
      public void testSecurityError() throws Exception {
          String headerValue = "custno=12345;comment=test; version=1," +
              "name=John;version=1;max-age=600;secure;domain=jakarta.apache.org";
          Exception exception = null;
          try {
              Cookie[] cookies = Cookie.parse(DOMAIN_NAME, new Header(
                  "set-cookie", headerValue));
          } catch (WebdavException e) {
              exception = e;
          }
  
          assert("Webdav exception should have been caught", exception != null);
      }
  }
  
  
  
  1.1                  jakarta-slide/src/tests/client/TestDOMUtils.java
  
  Index: TestDOMUtils.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-slide/src/tests/client/TestDOMUtils.java,v 1.1 2001/01/26 06:54:57 remm Exp $
   * $Revision: 1.1 $
   * $Date: 2001/01/26 06:54:57 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package client;
  
  import java.io.StringReader;
  
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.FactoryConfigurationError;
  import javax.xml.parsers.ParserConfigurationException;
  
  import org.apache.webdav.lib.util.DOMUtils;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.NodeList;
  
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  import junit.framework.*;
  
  import junit.textui.TestRunner;
  
  
  
  /**
   * Testcases for DOMUtils
   *
   * @author B.C. Holmes
   */
  public class TestDOMUtils extends TestCase {
  
      protected String xml1 = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
          "<D:multistatus xmlns:D=\"DAV:\"><D:response>"+
              "<D:href>/foo/index.html</D:href>" +
                  "<D:propstat>" +
                  "<D:prop><D:resourcetype/></D:prop>" +
                  "<D:status>HTTP/1.1 200 OK</D:status>" +
                  "</D:propstat>" +
              "</D:response></D:multistatus>";
  
      protected String xml2 = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
          "<multistatus xmlns=\"DAV:\"><response>"+
              "<href>/foo/index.html</href>" +
                  "<propstat>" +
                  "<prop><resourcetype/></prop>" +
                  "<status>HTTP/1.1 200 OK</status>" +
                  "</propstat>" +
              "</response></multistatus>";
  
      protected String xml3 = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
          "<multistatus xmlns:=\"DAV:\"><response>"+
              "<href>/foo/index.html</href>" +
                  "<propstat>" +
                  "<prop><resourcetype/></prop>" +
                  "<status>HTTP/1.1 200 OK</status>" +
                  "</propstat>" +
              "</response></multistatus>";
  
      public TestDOMUtils(String name) {
          super(name);
      }
  
      public static Test suite() {
          return new TestSuite(TestDOMUtils.class);
      }
  
  /*
      public static void main(String[] args) {
          new TestRunner().start(new String[] { TestDOMUtils.class.getName() },
                                 new StandardTestSuiteLoader());
      }
  */
  /*
      public void testIsDOM2Compliant() throws Exception {
          DocumentBuilderFactory factory =
              DocumentBuilderFactory.newInstance();
          factory.setNamespaceAware(true);
          DocumentBuilder builder = factory.newDocumentBuilder();
          Document document = builder.newDocument();
  
          System.out.println("Result: " + DocumentWrapper.isDOM2Compliant(document));
      }
  */
  
      public void testGetTextValue() throws Exception {
          String xml = "<root><customer>Joe Schmoe</customer>" +
                             "<customer>Jane Doe</customer></root>";
  
          Document document = parse(xml);
          Element root = document.getDocumentElement();
          Element child = (Element) root.getFirstChild();
          assert("Text value should be \"Joe Schmoe\", and is \"" +
                 DOMUtils.getTextValue(child) + "\" instead",
                 "Joe Schmoe".equals(DOMUtils.getTextValue(child)));
      }
  
      public void testFindDavPrefix1() throws Exception {
          Document document = parse(xml1);
          assert("DAV Prefix should be \"D:\", and is \"" +
                 DOMUtils.findDavPrefix(document) + "\" instead",
                 "D:".equals(DOMUtils.findDavPrefix(document)));
      }
  
      public void testFindDavPrefix2() throws Exception {
          Document document = parse(xml2);
          assert("DAV Prefix should be \"\", and is \"" +
                 DOMUtils.findDavPrefix(document) + "\" instead",
                 "".equals(DOMUtils.findDavPrefix(document)));
      }
  
      public void testFindDavPrefix3() throws Exception {
          try {
              Document document = parse(xml3);
              assert("DAV Prefix should be \"\", and is \"" +
                     DOMUtils.findDavPrefix(document) + "\" instead",
                     "".equals(DOMUtils.findDavPrefix(document)));
          } catch (SAXException e) {
              // this parser doesn't like the "xmlns:" attribute
          }
      }
  
      public void testParseStatus() throws Exception {
          Document document = parse(xml1);
          String prefix = DOMUtils.findDavPrefix(document);
          NodeList list = document.getElementsByTagName(prefix + "status");
          String statusText = DOMUtils.getTextValue(list.item(0));
          int status = DOMUtils.parseStatus(statusText);
          assert("Status should be \"200\" and is \"" +
                 status + "\" instead", status == 200);
      }
  
      public void testGetElementNamespaceURI() throws Exception {
          Document document = parse(xml1);
          String namespace = DOMUtils.getElementNamespaceURI(
              document.getDocumentElement());
          assert("Namespace should be \"DAV:\" and is \"" +
                 namespace + "\" instead",
                 namespace != null && namespace.equals("DAV:"));
      }
  
      protected Document parse(String xml) throws Exception {
          DocumentBuilderFactory factory =
              DocumentBuilderFactory.newInstance();
          factory.setNamespaceAware(true);
          DocumentBuilder builder = factory.newDocumentBuilder();
          return builder.parse(new InputSource(new StringReader(xml)));
      }
  }