> It looks like the check in CompositeView.modelToView() for throwing a
> BadLocationException was slightly to high. It looks like this method
> should never return null if the position doesn't map. This patch makes
> it so.

Ok, I found my little test program and transformed it into a Mauve test.
I'm not sure if I should actually check this test in as it's a weird
corner case that at least stretches the spec.
CompositeView.modelToView() returns null exactly when: the passed in
bias is Position.Bias.Backwards and the pos is <= getStartOffset(). It
is arguable if that is correct, because the spec says this method throws
a BadLocationException when the pos argument is not a valid position in
this view. pos <= start offset with backwards translates to pos <
startoffset normal which clearly is not valid.

I'm not sure if we should go so far and be conform in this corner case
here. I'm attaching the mauve test.

/Roman

/* modelToView.java -- Tests CompositeView.modelToView()
   Copyright (C) 2006 Roman Kennke ([EMAIL PROTECTED])
This file is part of Mauve.

Mauve is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Mauve 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
General Public License for more details.

You should have received a copy of the GNU General Public License
along with Mauve; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

*/

// Tags: JDK1.2

package gnu.testlet.javax.swing.text.CompositeView;

import java.awt.Rectangle;
import java.awt.Shape;

import javax.swing.text.BadLocationException;
import javax.swing.text.CompositeView;
import javax.swing.text.Position;

import junit.framework.TestCase;

public class modelToView extends TestCase
{

  private CompositeView compView;

  public void setUp()
  {
    compView = new TestCompositeView();
  }

  public void tearDown()
  {
    compView = null;
  }

  public void testReturnNullCase()
  {
    // Use other start and end offset.
    compView = new TestCompositeView(2, 10);
    try
      {
        Shape a = compView.modelToView(2, new Rectangle(0, 0, 10, 10),
                                       Position.Bias.Backward);
        assertNull(a);
      }
    catch (BadLocationException ex)
      {
        fail();
      }
  }
}
/* TestCompositeView.java -- A CompositeView for testing
   Copyright (C) 2006 Roman Kennke ([EMAIL PROTECTED])
This file is part of Mauve.

Mauve is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Mauve 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
General Public License for more details.

You should have received a copy of the GNU General Public License
along with Mauve; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

*/

// Tags: JDK1.2

package gnu.testlet.javax.swing.text.CompositeView;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;

import javax.swing.text.CompositeView;
import javax.swing.text.Element;
import javax.swing.text.View;

public class TestCompositeView extends CompositeView
{

  private int start;
  private int end;

  public TestCompositeView()
  {
    super(null);
    start = 0;
    end = 1;
  }

  public TestCompositeView(int s, int e)
  {
    this();
    start = s;
    end = e;
  }

  public TestCompositeView(Element element)
  {
    super(element);
  }

  protected void childAllocation(int index, Rectangle a)
  {
    // TODO Auto-generated method stub
  }

  protected View getViewAtPoint(int x, int y, Rectangle r)
  {
    // TODO Auto-generated method stub
    return null;
  }

  protected boolean isAfter(int x, int y, Rectangle r)
  {
    // TODO Auto-generated method stub
    return false;
  }

  protected boolean isBefore(int x, int y, Rectangle r)
  {
    // TODO Auto-generated method stub
    return false;
  }

  public float getPreferredSpan(int axis)
  {
    // TODO Auto-generated method stub
    return 0;
  }

  public void paint(Graphics g, Shape s)
  {
    // TODO Auto-generated method stub

  }

  public int getStartOffset()
  {
    if (getElement() != null)
      return super.getStartOffset();
    return start;
  }

  public int getEndOffset()
  {
    if (getElement() != null)
      return super.getEndOffset();
    return end;
  }
}

Reply via email to