[jsword-svn] r1405 - in trunk/common/src: main/java/org/crosswire/common/history test/java test/java/org/crosswire/common test/java/org/crosswire/common/diff test/java/org/crosswire/common/history

dmsmith at www.crosswire.org dmsmith at www.crosswire.org
Thu Jun 14 09:22:53 MST 2007


Author: dmsmith
Date: 2007-06-14 09:22:52 -0700 (Thu, 14 Jun 2007)
New Revision: 1405

Added:
   trunk/common/src/test/java/org/crosswire/common/history/
   trunk/common/src/test/java/org/crosswire/common/history/AllTests.java
   trunk/common/src/test/java/org/crosswire/common/history/HistoryTest.java
Modified:
   trunk/common/src/main/java/org/crosswire/common/history/History.java
   trunk/common/src/test/java/AllTests.java
   trunk/common/src/test/java/org/crosswire/common/diff/AllTests.java
   trunk/common/src/test/java/org/crosswire/common/diff/BitapTest.java
   trunk/common/src/test/java/org/crosswire/common/diff/CommonalityTest.java
   trunk/common/src/test/java/org/crosswire/common/diff/DiffCleanupTest.java
   trunk/common/src/test/java/org/crosswire/common/diff/DiffTest.java
   trunk/common/src/test/java/org/crosswire/common/diff/DifferenceEngineTest.java
   trunk/common/src/test/java/org/crosswire/common/diff/DifferenceTest.java
   trunk/common/src/test/java/org/crosswire/common/diff/LineMapTest.java
   trunk/common/src/test/java/org/crosswire/common/diff/MatchTest.java
   trunk/common/src/test/java/org/crosswire/common/diff/PatchEntryTest.java
   trunk/common/src/test/java/org/crosswire/common/diff/PatchTest.java
   trunk/common/src/test/java/org/crosswire/common/diff/diff_match_patch_test.java
Log:
Fixed a bug with History.
Add JUnit for History.

Modified: trunk/common/src/main/java/org/crosswire/common/history/History.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/history/History.java	2007-06-14 11:17:36 UTC (rev 1404)
+++ trunk/common/src/main/java/org/crosswire/common/history/History.java	2007-06-14 16:22:52 UTC (rev 1405)
@@ -55,6 +55,7 @@
     /**
      * Make a particular element in the navigation list the current
      * item in history.
+     * 
      * @param index the index of item to make the last one in the back list,
      *          -1 (or lower) will put everything in the forward list.
      *          Indexes beyond the end of the list will put everything
@@ -63,8 +64,10 @@
     public Object select(int index)
     {
         int i = index;
+
         // Adjust to be 1 based
         int size = nav.size();
+
         if (i > size)
         {
             i = size;
@@ -73,28 +76,64 @@
         {
             i = 1;
         }
-        backCount = i;
-        fireHistoryChanged();
+
+        // Only fire history changes when there is a change.
+        if (i != backCount)
+        {
+            backCount = i;
+            fireHistoryChanged();
+        }
+
         return getCurrent();
     }
 
     /**
-     * Add an element to history. If there is any "forward" list, the element
-     * replaces it.
-     * @param obj
+     * Add an element to history. If the element is in the forward list,
+     * then it replaces everything in the forward list upto it.
+     * Otherwise, it replaces the forward list.
+     * 
+     * @param obj the object to add
      */
     public void add(Object obj)
     {
-        // everything after backCount is blown away.
-        if (nav.size() > 0)
+        Object current = getCurrent();
+
+        // Don't add null objects or the same object.
+        if (obj == null || obj.equals(current))
         {
-            nav.subList(backCount, nav.size()).clear();
+            return;
         }
-        // then we add it
-        nav.add(obj);
-        backCount++; // or nav.size();
+
+        // If we are adding the next element, then just advance
+        // otherwise ...
+//        Object next = peek(1);
+//        if (!obj.equals(next))
+//        {
+            int size = nav.size();
+            if (size > backCount)
+            {
+                int pos = backCount;
+                while (pos < size && !obj.equals(nav.get(pos)))
+                {
+                    pos++;
+                }
+                // At this point pos either == size or the element at pos matches what we are navigating to.
+                nav.subList(backCount, Math.min(pos++, size)).clear();
+            }
+
+            // If it matches, then we don't have to do anything more
+            if (!obj.equals(peek(1)))
+            {
+                // then we add it
+                nav.add(backCount, obj);
+            }
+//        }
+
+        backCount++;
+
         // and remember when we saw it
         visit(obj);
+
         fireHistoryChanged();
     }
 
@@ -152,6 +191,21 @@
     }
 
     /**
+     * Get the current item in the "back" list
+     * @param i the distance to travel
+     * @return the requested item in the navigation list.
+     */
+    private Object peek(int i)
+    {
+        int size = nav.size();
+        if (size > 0 && backCount > 0 && backCount + i <= size)
+        {
+            return nav.get(backCount + i - 1);
+        }
+        return null;
+    }
+
+    /**
      * Add a listener for history events.
      * @param li the interested listener
      */

Modified: trunk/common/src/test/java/AllTests.java
===================================================================
--- trunk/common/src/test/java/AllTests.java	2007-06-14 11:17:36 UTC (rev 1404)
+++ trunk/common/src/test/java/AllTests.java	2007-06-14 16:22:52 UTC (rev 1405)
@@ -46,6 +46,7 @@
         suite.addTest(org.crosswire.common.util.AllTests.suite());
         suite.addTest(org.crosswire.common.progress.AllTests.suite());
         suite.addTest(org.crosswire.common.diff.AllTests.suite());
+        suite.addTest(org.crosswire.common.history.AllTests.suite());
         suite.addTest(org.crosswire.common.xml.AllTests.suite());
 
         return suite;

Modified: trunk/common/src/test/java/org/crosswire/common/diff/AllTests.java
===================================================================
--- trunk/common/src/test/java/org/crosswire/common/diff/AllTests.java	2007-06-14 11:17:36 UTC (rev 1404)
+++ trunk/common/src/test/java/org/crosswire/common/diff/AllTests.java	2007-06-14 16:22:52 UTC (rev 1405)
@@ -17,7 +17,7 @@
  * Copyright: 2005
  *     The copyright to this program is held by it's authors.
  *
- * ID: $Id: AllTests.java 763 2005-07-27 23:26:43Z dmsmith $
+ * ID: $Id$
  */
 package org.crosswire.common.diff;
 


Property changes on: trunk/common/src/test/java/org/crosswire/common/diff/AllTests.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native


Property changes on: trunk/common/src/test/java/org/crosswire/common/diff/BitapTest.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native


Property changes on: trunk/common/src/test/java/org/crosswire/common/diff/CommonalityTest.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native


Property changes on: trunk/common/src/test/java/org/crosswire/common/diff/DiffCleanupTest.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native


Property changes on: trunk/common/src/test/java/org/crosswire/common/diff/DiffTest.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native


Property changes on: trunk/common/src/test/java/org/crosswire/common/diff/DifferenceEngineTest.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native


Property changes on: trunk/common/src/test/java/org/crosswire/common/diff/DifferenceTest.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native


Property changes on: trunk/common/src/test/java/org/crosswire/common/diff/LineMapTest.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native


Property changes on: trunk/common/src/test/java/org/crosswire/common/diff/MatchTest.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native


Property changes on: trunk/common/src/test/java/org/crosswire/common/diff/PatchEntryTest.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native


Property changes on: trunk/common/src/test/java/org/crosswire/common/diff/PatchTest.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native


Property changes on: trunk/common/src/test/java/org/crosswire/common/diff/diff_match_patch_test.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Added: trunk/common/src/test/java/org/crosswire/common/history/AllTests.java
===================================================================
--- trunk/common/src/test/java/org/crosswire/common/history/AllTests.java	                        (rev 0)
+++ trunk/common/src/test/java/org/crosswire/common/history/AllTests.java	2007-06-14 16:22:52 UTC (rev 1405)
@@ -0,0 +1,44 @@
+/**
+ * Distribution License:
+ * JSword is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License, version 2.1 as published by
+ * the Free Software Foundation. This program is distributed in the hope
+ * that it will be useful, but WITHOUT ANY WARRANTY; without even the
+ * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * The License is available on the internet at:
+ *       http://www.gnu.org/copyleft/lgpl.html
+ * or by writing to:
+ *      Free Software Foundation, Inc.
+ *      59 Temple Place - Suite 330
+ *      Boston, MA 02111-1307, USA
+ *
+ * Copyright: 2005
+ *     The copyright to this program is held by it's authors.
+ *
+ * ID: $Id$
+ */
+package org.crosswire.common.history;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * JUnit Test.
+ * 
+ * @see gnu.lgpl.License for license details.
+ *      The copyright to this program is held by it's authors.
+ * @author DM Smith [dmsmith555 at yahoo dot com]
+ */
+public class AllTests
+{
+    public static Test suite()
+    {
+        TestSuite suite = new TestSuite("Test for org.crosswire.common.history"); //$NON-NLS-1$
+        //$JUnit-BEGIN$
+        suite.addTest(new TestSuite(HistoryTest.class));
+        //$JUnit-END$
+        return suite;
+    }
+}
\ No newline at end of file


Property changes on: trunk/common/src/test/java/org/crosswire/common/history/AllTests.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Added: trunk/common/src/test/java/org/crosswire/common/history/HistoryTest.java
===================================================================
--- trunk/common/src/test/java/org/crosswire/common/history/HistoryTest.java	                        (rev 0)
+++ trunk/common/src/test/java/org/crosswire/common/history/HistoryTest.java	2007-06-14 16:22:52 UTC (rev 1405)
@@ -0,0 +1,122 @@
+/**
+ * Distribution License:
+ * JSword is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License, version 2.1 as published by
+ * the Free Software Foundation. This program is distributed in the hope
+ * that it will be useful, but WITHOUT ANY WARRANTY; without even the
+ * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * The License is available on the internet at:
+ *       http://www.gnu.org/copyleft/lgpl.html
+ * or by writing to:
+ *      Free Software Foundation, Inc.
+ *      59 Temple Place - Suite 330
+ *      Boston, MA 02111-1307, USA
+ *
+ * Copyright: 2007
+ *     The copyright to this program is held by it's authors.
+ *
+ * ID: $Id$
+ */
+package org.crosswire.common.history;
+
+import junit.framework.TestCase;
+
+/**
+ * JUnit Test.
+ * 
+ * @see gnu.lgpl.License for license details.
+ *      The copyright to this program is held by it's authors.
+ * @author DM Smith [dmsmith555 at yahoo dot com]
+ */
+public class HistoryTest extends TestCase
+{
+    public void testAdd()
+    {
+        History history = new History();
+        assertEquals(null, history.getCurrent());
+        history.add("a"); //$NON-NLS-1$
+        assertEquals("a", history.getCurrent()); //$NON-NLS-1$
+        history.add("b"); //$NON-NLS-1$
+        assertEquals("[a, b]", history.getPreviousList().toString()); //$NON-NLS-1$
+        // re-adding the current element won't change the list
+        history.add("b"); //$NON-NLS-1$
+        assertEquals("[a, b]", history.getPreviousList().toString()); //$NON-NLS-1$
+        history.add("c"); //$NON-NLS-1$
+        assertEquals("[a, b, c]", history.getPreviousList().toString()); //$NON-NLS-1$
+    }
+
+    public void testGo()
+    {
+        History history = new History();
+        assertEquals(null, history.getCurrent());
+        history.add("a"); //$NON-NLS-1$
+        history.add("b"); //$NON-NLS-1$
+        history.add("c"); //$NON-NLS-1$
+        history.add("d"); //$NON-NLS-1$
+        assertEquals("[a, b, c, d]", history.getPreviousList().toString()); //$NON-NLS-1$
+        history.go(-1);
+        assertEquals("[a, b, c]", history.getPreviousList().toString()); //$NON-NLS-1$
+        assertEquals("[d]", history.getNextList().toString()); //$NON-NLS-1$
+
+        history.go(-2);
+        assertEquals("[a]", history.getPreviousList().toString()); //$NON-NLS-1$
+        assertEquals("[b, c, d]", history.getNextList().toString()); //$NON-NLS-1$
+
+        history.go(3);
+        assertEquals("[a, b, c, d]", history.getPreviousList().toString()); //$NON-NLS-1$
+
+        history.go(-10);
+        assertEquals("[a]", history.getPreviousList().toString()); //$NON-NLS-1$
+        assertEquals("[b, c, d]", history.getNextList().toString()); //$NON-NLS-1$
+
+        history.go(10);
+        assertEquals("[a, b, c, d]", history.getPreviousList().toString()); //$NON-NLS-1$
+    }
+
+    public void testNav()
+    {
+        History history = new History();
+        assertEquals(null, history.getCurrent());
+        history.add("a"); //$NON-NLS-1$
+        history.add("b"); //$NON-NLS-1$
+        history.add("c"); //$NON-NLS-1$
+        history.add("d"); //$NON-NLS-1$
+        history.add("e"); //$NON-NLS-1$
+        history.add("f"); //$NON-NLS-1$
+        history.add("g"); //$NON-NLS-1$
+        history.add("h"); //$NON-NLS-1$
+        history.add("i"); //$NON-NLS-1$
+        assertEquals("[a, b, c, d, e, f, g, h, i]", history.getPreviousList().toString()); //$NON-NLS-1$
+
+        history.go(-5);
+        assertEquals("[a, b, c, d]", history.getPreviousList().toString()); //$NON-NLS-1$
+        assertEquals("[e, f, g, h, i]", history.getNextList().toString()); //$NON-NLS-1$
+        assertEquals("d", history.getCurrent()); //$NON-NLS-1$
+
+        // Adding the current does not change anything
+        history.add("d"); //$NON-NLS-1$
+        assertEquals("[a, b, c, d]", history.getPreviousList().toString()); //$NON-NLS-1$
+        assertEquals("[e, f, g, h, i]", history.getNextList().toString()); //$NON-NLS-1$
+
+        // Adding the next splits the list
+        history.add("e"); //$NON-NLS-1$
+        assertEquals("[a, b, c, d, e]", history.getPreviousList().toString()); //$NON-NLS-1$
+        assertEquals("[f, g, h, i]", history.getNextList().toString()); //$NON-NLS-1$
+
+        // Adding the next splits the list
+        history.add("h"); //$NON-NLS-1$
+        assertEquals("[a, b, c, d, e, h]", history.getPreviousList().toString()); //$NON-NLS-1$
+        assertEquals("[i]", history.getNextList().toString()); //$NON-NLS-1$
+
+        history.go(-5);
+        assertEquals("[a]", history.getPreviousList().toString()); //$NON-NLS-1$
+        assertEquals("[b, c, d, e, h, i]", history.getNextList().toString()); //$NON-NLS-1$
+
+        history.add("e"); //$NON-NLS-1$
+        assertEquals("[a, e]", history.getPreviousList().toString()); //$NON-NLS-1$
+        assertEquals("[h, i]", history.getNextList().toString()); //$NON-NLS-1$
+
+    }
+}


Property changes on: trunk/common/src/test/java/org/crosswire/common/history/HistoryTest.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native




More information about the jsword-svn mailing list