logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From psm...@apache.org
Subject cvs commit: jakarta-log4j-sandbox/tests/src/java/org/apache/log4j/chainsaw CyclicBufferListTest.java
Date Mon, 26 May 2003 06:49:04 GMT
psmith      2003/05/25 23:49:04

  Modified:    src/java/org/apache/log4j/chainsaw CyclicBufferList.java
  Added:       tests/src/java/org/apache/log4j/chainsaw
                        CyclicBufferListTest.java
  Log:
  fixed a couple of issues in the CyclicBufferList and added a Test case class.
  
  Certainly wouldn't want to be relying on CyclicBufferList as part
  of Life support systems at this point...
  
  Revision  Changes    Path
  1.2       +62 -9     jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/CyclicBufferList.java
  
  Index: CyclicBufferList.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/CyclicBufferList.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CyclicBufferList.java	26 May 2003 04:44:45 -0000	1.1
  +++ CyclicBufferList.java	26 May 2003 06:49:03 -0000	1.2
  @@ -49,10 +49,14 @@
   
   package org.apache.log4j.chainsaw;
   
  +import com.sun.rsasign.s;
  +
   import org.apache.log4j.helpers.CyclicBuffer;
   import org.apache.log4j.spi.LoggingEvent;
   
   import java.util.AbstractList;
  +import java.util.ArrayList;
  +import java.util.Arrays;
   import java.util.List;
   
   
  @@ -84,23 +88,60 @@
         throw new IllegalArgumentException(
           "The maxSize argument (" + maxSize + ") is not a positive integer.");
       }
  -
       this.maxSize = maxSize;
  -    ea = new Object[maxSize];
  -    first = 0;
  -    last = 0;
  -    numElems = 0;
  +    clear();
  +  }
  +
  +  public CyclicBufferList() {
  +    this(5000);
  +  }
  +
  +  /**
  +   * Check if the given index is in range.  If not, throw an appropriate
  +   * runtime exception.
  +   */
  +  private void RangeCheck(int index) {
  +    if ((index >= ea.length) || (index < 0)) {
  +      throw new IndexOutOfBoundsException(
  +        "Index: " + index + ", Size: " + ea.length);
  +    }
  +  }
  +
  +  /**
  +   * Removes the element at the specified position in this list.
  +   * Shifts any subsequent elements to the left (subtracts one from their
  +   * indices).
  +   *
  +   * @param index the index of the element to removed.
  +   * @return the element that was removed from the list.
  +   * @throws    IndexOutOfBoundsException if index out of range <tt>(index
  +   *      &lt; 0 || index &gt;= size())</tt>.
  +   */
  +  public Object remove(int index) {
  +    Object oldValue = ea[index];
  +
  +    List list = new ArrayList(Arrays.asList(ea));
  +    list.remove(index);
  +    ea = list.toArray(ea);
  +    numElems = ea.length;
  +    
  +    numElems--;
  +    if (--last <= 0) {
  +      last = numElems;
  +    }
  +
  +    if (first == maxSize) {
  +      first = 0;
  +    }
  +    return oldValue;
     }
   
     public Object set(int index, Object element) {
       Object previous = ea[index];
       ea[index] = element;
  +
       return previous;
     }
  -  
  -  public CyclicBufferList() {
  -    this(5000);
  -  }
   
     /**
        Add an <code>event</code> as the last event in the buffer.
  @@ -118,6 +159,7 @@
       } else if (++first == maxSize) {
         first = 0;
       }
  +
       return true;
     }
   
  @@ -208,4 +250,15 @@
         last = loopLen;
       }
     }
  +  /* (non-Javadoc)
  +   * @see java.util.Collection#clear()
  +   */
  +  public void clear() {
  +    ea = new Object[maxSize];
  +    first = 0;
  +    last = 0;
  +    numElems = 0;
  +    
  +  }
  +
   }
  
  
  
  1.1                  jakarta-log4j-sandbox/tests/src/java/org/apache/log4j/chainsaw/CyclicBufferListTest.java
  
  Index: CyclicBufferListTest.java
  ===================================================================
  package org.apache.log4j.chainsaw;
  
  import junit.framework.TestCase;
  
  /**
   * @author Paul Smith <psmith@apache.org>
   *
   */
  public class CyclicBufferListTest extends TestCase {
  
    /**
     * Constructor for CyclicBufferListTest.
     * @param arg0
     */
    public CyclicBufferListTest(String arg0) {
      super(arg0);
    }
  
    public void testRemoveint() {
      
      Object[] stuff = new String[]{
         "One",
         "Two",
         "Three",
         "Four",
         "Five",
         "Six",
         "Seven",
         "Eight",
         "Nine",
         "Ten",
             };
             
       CyclicBufferList buf = new CyclicBufferList(10);
       for (int i = 0; i < stuff.length; i++) {
        Object object = stuff[i];
        buf.add(object);
      }
      assertTrue("Max Size should be 10, =" + buf.getMaxSize(), buf.getMaxSize()==10);
      assertTrue("Size should be 10, =" + buf.size(), buf.size()==10);
      buf.remove(5);
      assertTrue("Size should be 9,=" + buf.size(), buf.size()==9);
      assertTrue("Removing index 5 should remove 'Six'", !buf.contains("Six"));
      assertTrue("Removing index 5 should still contain 'Five'", buf.contains("Five"));
      assertTrue("Removing index 5 should still contain 'Seven'", buf.contains("Seven"));
      assertTrue("Max Size should still be 10, =" + buf.getMaxSize(), buf.getMaxSize()==10);
      assertTrue("Size should be 9, =" + buf.size(), buf.size()==9);
      
      /**
       * now lets add some more to the buffer, and ensure that One and Two disappear
       */
      buf.add("Eleven"); //this should not remove anything just yet..
      assertTrue("Max Size should be 10, =" + buf.getMaxSize(), buf.getMaxSize()==10);
      assertTrue("Size should be 10, =" + buf.size(), buf.size()==10);
      assertTrue("Adding 'Eleven' should still have One", buf.contains("One"));
      assertTrue("Adding 'Eleven' should still have Two", buf.contains("Two"));
      
      buf.add("Twelve");
  
      assertTrue("Max Size should be 10, =" + buf.getMaxSize(), buf.getMaxSize()==10);
      assertTrue("Size should be 10, =" + buf.size(), buf.size()==10);
  
      assertTrue("Adding 'Twelve' should NOT have One", !buf.contains("One"));
      assertTrue("Adding 'Twelve' should still have Two", buf.contains("Two"));
  
      buf.add("Thirteen");
  
      assertTrue("Max Size should be 10, =" + buf.getMaxSize(), buf.getMaxSize()==10);
      assertTrue("Size should be 10, =" + buf.size(), buf.size()==10);
  
      assertTrue("Adding 'Thirteen' should NOT have One", !buf.contains("One"));
      assertTrue("Adding 'Thirteen' should NOT have Two", !buf.contains("Two"));
      assertTrue("Adding 'Twelve' should still have Three", buf.contains("Three"));
      
    }
  
  }
  
  
  

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


Mime
View raw message