commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject cvs commit: jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/converters URLConverterTestCase.java
Date Fri, 04 Apr 2003 15:52:47 GMT
bayard      2003/04/04 07:52:47

  Added:       beanutils/src/java/org/apache/commons/beanutils/converters
                        URLConverter.java
               beanutils/src/test/org/apache/commons/beanutils/converters
                        URLConverterTestCase.java
  Log:
  Converts Strings to URLs.
  
  Revision  Changes    Path
  1.1                  jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/URLConverter.java
  
  Index: URLConverter.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/URLConverter.java,v
1.1 2003/04/04 15:52:47 bayard Exp $
   * $Revision: 1.1 $
   * $Date: 2003/04/04 15:52:47 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 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", "Commons", 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/>.
   *
   */
  
  
  package org.apache.commons.beanutils.converters;
  
  
  import org.apache.commons.beanutils.ConversionException;
  import org.apache.commons.beanutils.Converter;
  
  import java.net.URL;
  import java.net.MalformedURLException;
  
  
  
  /**
   * <p>Standard {@link Converter} implementation that converts an incoming
   * String into a <code>java.net.URL</code> object, optionally using a
   * default value or throwing a {@link ConversionException} if a conversion
   * error occurs.</p>
   *
   * @author Henri Yandell
   * @version $Revision: 1.1 $ $Date: 2003/04/04 15:52:47 $
   * @since 1.3
   */
  
  public final class URLConverter implements Converter {
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Create a {@link Converter} that will throw a {@link ConversionException}
       * if a conversion error occurs.
       */
      public URLConverter() {
  
          this.defaultValue = null;
          this.useDefault = false;
  
      }
  
  
      /**
       * Create a {@link Converter} that will return the specified default value
       * if a conversion error occurs.
       *
       * @param defaultValue The default value to be returned
       */
      public URLConverter(Object defaultValue) {
  
          this.defaultValue = defaultValue;
          this.useDefault = true;
  
      }
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The default value specified to our Constructor, if any.
       */
      private Object defaultValue = null;
  
  
      /**
       * Should we return the default value on conversion errors?
       */
      private boolean useDefault = true;
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Convert the specified input object into an output object of the
       * specified type.
       *
       * @param type Data type to which this value should be converted
       * @param value The input value to be converted
       *
       * @exception ConversionException if conversion cannot be performed
       *  successfully
       */
      public Object convert(Class type, Object value) {
  
          if (value == null) {
              if (useDefault) {
                  return (defaultValue);
              } else {
                  throw new ConversionException("No value specified");
              }
          }
  
          if (value instanceof URL) {
              return (value);
          }
  
          try {
              return new URL(value.toString());
          } catch(MalformedURLException murle) {
              if (useDefault) {
                  return (defaultValue);
              } else {
                  throw new ConversionException(murle);
              }
          }
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/converters/URLConverterTestCase.java
  
  Index: URLConverterTestCase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/converters/URLConverterTestCase.java,v
1.1 2003/04/04 15:52:47 bayard Exp $
   * $Revision: 1.1 $
   * $Date: 2003/04/04 15:52:47 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 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", "Commons", 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/>.
   *
   */
  
  package org.apache.commons.beanutils.converters;
  
  import junit.framework.TestSuite;
  import junit.framework.TestCase;
  
  import org.apache.commons.beanutils.Converter;
  
  import java.net.URL;
  
  
  /**
   * Test Case for the URLConverter class.
   *
   * @author Henri Yandell
   * @version $Revision: 1.1 $ $Date: 2003/04/04 15:52:47 $
   */
  
  public class URLConverterTestCase extends TestCase {
  
      private Converter converter = null;
  
      // ------------------------------------------------------------------------
  
      public URLConverterTestCase(String name) {
          super(name);
      }
      
      // ------------------------------------------------------------------------
  
      public void setUp() throws Exception {
          converter = makeConverter();
      }
  
      public static TestSuite suite() {
          return new TestSuite(URLConverterTestCase.class);        
      }
  
      public void tearDown() throws Exception {
          converter = null;
      }
  
      // ------------------------------------------------------------------------
      
      protected Converter makeConverter() {
          return new URLConverter();
      }
      
      protected Class getExpectedType() {
          return URL.class;
      }
  
      // ------------------------------------------------------------------------
  
      public void testSimpleConversion() throws Exception {
          String[] message= { 
              "from String",
              "from String",
              "from String",
              "from String",
              "from String",
              "from String",
              "from String",
              "from String",
          };
          
          Object[] input = { 
              "http://www.apache.org",
              "http://www.apache.org/",
              "ftp://cvs.apache.org",
              "file://project.xml",
              "http://208.185.179.12",
              "http://www.apache.org:9999/test/thing",
              "http://user:admin@www.apache.org:50/one/two.three",
              "http://notreal.apache.org",
          };
          
          URL[] expected = { 
              new URL("http://www.apache.org"),
              new URL("http://www.apache.org/"),
              new URL("ftp://cvs.apache.org"),
              new URL("file://project.xml"),
              new URL("http://208.185.179.12"),
              new URL("http://www.apache.org:9999/test/thing"),
              new URL("http://user:admin@www.apache.org:50/one/two.three"),
              new URL("http://notreal.apache.org")
          };
          
          for(int i=0;i<expected.length;i++) {
              assertEquals(message[i] + " to URL",expected[i],converter.convert(URL.class,input[i]));
              assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
          }
      }
      
  }
  
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message