You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by hi...@apache.org on 2007/01/14 22:04:20 UTC

svn commit: r496150 - in /harmony/enhanced/classlib/trunk/modules/swing/src: main/java/common/javax/swing/text/AbstractDocument.java test/api/java/common/javax/swing/text/AbstractDocument_BranchElementTest.java

Author: hindessm
Date: Sun Jan 14 13:04:20 2007
New Revision: 496150

URL: http://svn.apache.org/viewvc?view=rev&rev=496150
Log:
Applying patches from "[#HARMONY-2756] [classlib][swing]
j.s.text.AbstractDocument.BranchElement.getElementIndex may return -1
instead of 0 in RI".

Modified:
    harmony/enhanced/classlib/trunk/modules/swing/src/main/java/common/javax/swing/text/AbstractDocument.java
    harmony/enhanced/classlib/trunk/modules/swing/src/test/api/java/common/javax/swing/text/AbstractDocument_BranchElementTest.java

Modified: harmony/enhanced/classlib/trunk/modules/swing/src/main/java/common/javax/swing/text/AbstractDocument.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/swing/src/main/java/common/javax/swing/text/AbstractDocument.java?view=diff&rev=496150&r1=496149&r2=496150
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/swing/src/main/java/common/javax/swing/text/AbstractDocument.java (original)
+++ harmony/enhanced/classlib/trunk/modules/swing/src/main/java/common/javax/swing/text/AbstractDocument.java Sun Jan 14 13:04:20 2007
@@ -14,10 +14,6 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
-/**
- * @author Alexey A. Ivanov
- * @version $Revision$
- */
 package javax.swing.text;
 
 import java.awt.font.TextAttribute;
@@ -385,7 +381,7 @@
 
         @Override
         public int getElementIndex(final int offset) {
-            if (offset < 0 && elements.length > 0) {
+            if (elements.length <= 1) {
                 return 0;
             }
 
@@ -425,21 +421,10 @@
         }
 
         public Element positionToElement(final int offset) {
-            if (offset < 0) {
+            if (offset < getStartOffset() || offset > getEndOffset() - 1) {
                 return null;
             }
-
-            Element result = null;
-            for (int i = 0; i < elements.length; i++) {
-                Element child = elements[i];
-                if (child.getStartOffset() <= offset
-                    && offset < child.getEndOffset()) {
-
-                    result = child;
-                    break;
-                }
-            }
-            return result;
+            return elements[getElementIndex(offset)];
         }
 
         public void replace(final int index, final int length,

Modified: harmony/enhanced/classlib/trunk/modules/swing/src/test/api/java/common/javax/swing/text/AbstractDocument_BranchElementTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/swing/src/test/api/java/common/javax/swing/text/AbstractDocument_BranchElementTest.java?view=diff&rev=496150&r1=496149&r2=496150
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/swing/src/test/api/java/common/javax/swing/text/AbstractDocument_BranchElementTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/swing/src/test/api/java/common/javax/swing/text/AbstractDocument_BranchElementTest.java Sun Jan 14 13:04:20 2007
@@ -14,10 +14,6 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
-/**
- * @author Alexey A. Ivanov
- * @version $Revision$
- */
 package javax.swing.text;
 
 import java.util.Enumeration;
@@ -27,7 +23,6 @@
 
 /**
  * Tests AbstractDocument.BranchElement class.
- *
  */
 public class AbstractDocument_BranchElementTest extends BasicSwingTestCase {
     protected AbstractDocument doc;
@@ -131,14 +126,14 @@
     public void testGetElementIndex03() {
         BranchElement root = doc.new BranchElement(null, null);
         try {
-            assertEquals(-1, root.getElementIndex(-1));
+            assertEquals(0, root.getElementIndex(-1));
             if (!BasicSwingTestCase.isHarmony()) {
                 fail("NullPointerException should be thrown");
             }
         } catch (NullPointerException e) {
         }
         try {
-            assertEquals(-1, root.getElementIndex(0));
+            assertEquals(0, root.getElementIndex(0));
             if (!BasicSwingTestCase.isHarmony()) {
                 fail("NullPointerException should be thrown");
             }
@@ -187,6 +182,52 @@
         }
     }
 
+    /**
+     * Tests getElementIndex behavior when there are no child elements, but
+     * <code>getStartOffset</code> and <code>getEndOffset</code> are overridden
+     * to prevent <code>{@link NullPointerException}</code>.
+     */
+    // Regression for HARMONY-2756
+    public void testGetElementIndex06() {
+        par = doc.new BranchElement(null, null) {
+            private static final long serialVersionUID = 1L;
+
+            @Override
+            public int getStartOffset() {
+                return 10;
+            }
+
+            @Override
+            public int getEndOffset() {
+                return 20;
+            }
+        };
+        assertEquals(0, par.getElementIndex(-1));
+        assertEquals(0, par.getElementIndex(0));
+        assertEquals(0, par.getElementIndex(10));
+        assertEquals(0, par.getElementIndex(15));
+        assertEquals(0, par.getElementIndex(20));
+        assertEquals(0, par.getElementIndex(25));
+    }
+
+    /**
+     * Tests getElementIndex behavior when this BranchElement doesn't span
+     * all the document, it has child elements.
+     */
+    // Regression for HARMONY-2756
+    public void testGetElementIndex07() throws BadLocationException {
+        par = createBranchElement();
+
+        assertEquals(0, par.getElementIndex(-1));
+        assertEquals(0, par.getElementIndex(0));
+        assertEquals(0, par.getElementIndex(7));
+        assertEquals(1, par.getElementIndex(10));
+        assertEquals(2, par.getElementIndex(13));
+        assertEquals(3, par.getElementIndex(18));
+        assertEquals(3, par.getElementIndex(19));
+        assertEquals(3, par.getElementIndex(20));
+    }
+
     public void testIsLeaf() {
         assertFalse(bidi.isLeaf());
         assertFalse(par.isLeaf());
@@ -280,6 +321,21 @@
         assertNull(par.positionToElement(20));
     }
 
+    /**
+     * Tests <code>positionToElement</code> where the <code>BranchElement</code>
+     * tested doesn't span over the whole document. 
+     */
+    public void testPositionToElement_PartialBranch() throws BadLocationException {
+        par = createBranchElement();
+
+        assertNull(par.positionToElement(6));
+        assertSame(par.getElement(0), par.positionToElement(7));
+        assertSame(par.getElement(1), par.positionToElement(10));
+        assertSame(par.getElement(2), par.positionToElement(13));
+        assertSame(par.getElement(3), par.positionToElement(18));
+        assertNull(par.positionToElement(19));
+    }
+
     public void testToString() {
         assertEquals("BranchElement(bidi root) 0,15\n", bidi.toString());
         assertEquals("BranchElement(paragraph) 0,15\n", par.toString());
@@ -287,5 +343,21 @@
 
     private Element createLeaf(final int start, final int end) {
         return doc.new LeafElement(par, null, start, end);
+    }
+
+    /**
+     * Creates <code>BranchElement</code> which doesn't span over the whole document.
+     * <code>DefaultStyledDocument</code> is used to prepare the structure.
+     */
+    private BranchElement createBranchElement() throws BadLocationException {
+        final DefaultStyledDocument styledDoc = new DefaultStyledDocument(); 
+        doc = styledDoc;
+        doc.insertString(doc.getLength(), "1 line\nonetwothree\n3 line", null);
+        //                                 0123456 789012345678 901234
+        //                                 0          1          2
+        styledDoc.setCharacterAttributes(7, 3, SimpleAttributeSet.EMPTY, false);
+        styledDoc.setCharacterAttributes(10, 3, SimpleAttributeSet.EMPTY, false);
+        styledDoc.setCharacterAttributes(13, 5, SimpleAttributeSet.EMPTY, false);
+        return (BranchElement)doc.getDefaultRootElement().getElement(1);
     }
 }