geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject cvs commit: incubator-geronimo/modules/common/src/test/org/apache/geronimo/common/propertyeditor URLArrayEditorTest.java
Date Thu, 28 Aug 2003 10:16:39 GMT
jdillon     2003/08/28 03:16:39

  Modified:    modules/common/src/java/org/apache/geronimo/common/propertyeditor
                        PropertyEditors.java StringArrayEditor.java
  Added:       modules/common/src/java/org/apache/geronimo/common/propertyeditor
                        ArrayPropertyEditorSupport.java URLArrayEditor.java
               modules/common/src/test/org/apache/geronimo/common/propertyeditor
                        URLArrayEditorTest.java
  Log:
   o Added URLArrayEditor
   o Created support class for editing array types
  
  Revision  Changes    Path
  1.2       +2 -1      incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/propertyeditor/PropertyEditors.java
  
  Index: PropertyEditors.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/propertyeditor/PropertyEditors.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PropertyEditors.java	28 Aug 2003 09:40:53 -0000	1.1
  +++ PropertyEditors.java	28 Aug 2003 10:16:39 -0000	1.2
  @@ -86,6 +86,7 @@
           // Have to explicity register this because it does not follow the
           // naming convention used by PropertyManagerEditor to discover editors
           registerEditor(String[].class, StringArrayEditor.class);
  +        registerEditor(java.net.URL[].class, URLArrayEditor.class);
       }
       
       /**
  
  
  
  1.2       +6 -38     incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/propertyeditor/StringArrayEditor.java
  
  Index: StringArrayEditor.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/propertyeditor/StringArrayEditor.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StringArrayEditor.java	28 Aug 2003 09:40:53 -0000	1.1
  +++ StringArrayEditor.java	28 Aug 2003 10:16:39 -0000	1.2
  @@ -56,9 +56,7 @@
   
   package org.apache.geronimo.common.propertyeditor;
   
  -import java.util.LinkedList;
   import java.util.List;
  -import java.util.StringTokenizer;
   
   /**
    * A property editor for String[].
  @@ -66,45 +64,15 @@
    * @version $Revision$ $Date$
    */
   public class StringArrayEditor
  -    extends TextPropertyEditorSupport
  +    extends ArrayPropertyEditorSupport
   {
  -    /**
  -     * Returns a String[] by spliting up the input string where 
  -     * elements are separated by commas.
  -     *
  -     * @return a URL object
  -     *
  -     * @throws PropertyEditorException   An MalformedURLException occured.
  -     */
  -    public void setAsText(String text)
  +    protected Object convertValue(final String value) throws Exception
       {
  -        if (text == null || text.length() == 0) {
  -            setValue(null);
  -        }
  -        else {
  -            StringTokenizer stok = new StringTokenizer(text, ",");
  -            List list = new LinkedList();
  -            
  -            while (stok.hasMoreTokens()) {
  -                list.add(stok.nextToken());
  -            }
  -            
  -            setValue((String[])list.toArray(new String[list.size()]));
  -        }
  +        return value;
       }
       
  -    public String getAsText()
  +    protected Object createArray(final List list)
       {
  -        String[] strings = (String[])getValue();
  -        if (strings == null || strings.length == 0) {
  -            return null; 
  -        }
  -        
  -        StringBuffer result = new StringBuffer(strings[0]);
  -        for (int i = 1; i < strings.length; i++) {
  -            result.append(",").append(strings[i]); 
  -        }
  -        
  -        return result.toString();
  +        return list.toArray(new String[list.size()]);
       }
   }
  
  
  
  1.1                  incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/propertyeditor/ArrayPropertyEditorSupport.java
  
  Index: ArrayPropertyEditorSupport.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" 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",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * 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.geronimo.common.propertyeditor;
  
  import java.util.LinkedList;
  import java.util.List;
  import java.util.StringTokenizer;
  
  /**
   * Property editor support for array types.
   *
   * @version $Revision: 1.1 $ $Date: 2003/08/28 10:16:39 $
   */
  public abstract class ArrayPropertyEditorSupport
      extends TextPropertyEditorSupport
  {
      protected abstract Object convertValue(final String value) throws Exception;
      protected abstract Object createArray(final List list);
      
      public void setAsText(String text)
      {
          if (text == null || text.length() == 0) {
              setValue(null);
          }
          else {
              StringTokenizer stok = new StringTokenizer(text, ",");
              List list = new LinkedList();
              
              try {
                  while (stok.hasMoreTokens()) {
                      Object obj = convertValue(stok.nextToken());
                      list.add(obj);
                  }
              }
              catch (Exception e) {
                  throw new PropertyEditorException(e);
              }
              
              setValue(createArray(list));
          }
      }
      
      public String getAsText()
      {
          Object[] objects = (Object[])getValue();
          if (objects == null || objects.length == 0) {
              return null; 
          }
          
          StringBuffer result = new StringBuffer(String.valueOf(objects[0]));
          for (int i = 1; i < objects.length; i++) {
              result.append(",").append(objects[i]); 
          }
          
          return result.toString();
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/propertyeditor/URLArrayEditor.java
  
  Index: URLArrayEditor.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" 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",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * 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.geronimo.common.propertyeditor;
  
  import java.util.List;
  
  import java.net.URL;
  
  import org.apache.geronimo.common.Strings;
  
  /**
   * A property editor for URL[].
   *
   * @version $Revision: 1.1 $ $Date: 2003/08/28 10:16:39 $
   */
  public class URLArrayEditor
      extends ArrayPropertyEditorSupport
  {
      protected Object convertValue(final String value) throws Exception
      {
          return Strings.toURL(value);
      }
      
      protected Object createArray(final List list)
      {
          return list.toArray(new URL[list.size()]);
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/common/src/test/org/apache/geronimo/common/propertyeditor/URLArrayEditorTest.java
  
  Index: URLArrayEditorTest.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" 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",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * 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.geronimo.common.propertyeditor;
  
  import java.net.URL;
  
  import java.beans.PropertyEditor;
  
  import junit.framework.TestCase;
  
  /**
   * Unit test for {@link URLArrayEditor} class.
   *
   * @version $Revision: 1.1 $ $Date: 2003/08/28 10:16:39 $
   */
  public class URLArrayEditorTest
      extends TestCase
  {
      PropertyEditor editor;
      
      protected void setUp()
      {
          editor = PropertyEditors.findEditor(URL[].class);
      }
      
      public void testGetValue_Simple()
      {
          String input = "http://apache.org";
          
          editor.setAsText(input);
          Object output = editor.getValue();
          
          assertNotNull(output);
          assertEquals(URL[].class, output.getClass());
          
          URL[] urls = (URL[])output;
          assertEquals(1, urls.length);
          assertEquals(input, urls[0].toString());
      }
      
      public void testGetValue_2URLs()
      {
          String input = "http://apache.org, http://google.com";
          
          editor.setAsText(input);
          Object output = editor.getValue();
          
          assertNotNull(output);
          assertEquals(URL[].class, output.getClass());
          
          URL[] urls = (URL[])output;
          assertEquals(2, urls.length);
          assertEquals("http://apache.org", urls[0].toString());
          assertEquals("http://google.com", urls[1].toString());
      }
  }
  
  
  

Mime
View raw message