bug 259422: DocLineComparator behaves ambiguous with empty file
diff --git a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/DocLineComparator.java b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/DocLineComparator.java
index 85f1cbe..52dff2f 100644
--- a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/DocLineComparator.java
+++ b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/DocLineComparator.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2006 IBM Corporation and others.
+ * Copyright (c) 2000, 2009 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -45,30 +45,27 @@
 		fIgnoreWhiteSpace= ignoreWhiteSpace;
 
 		fLineOffset= 0;
-		if (region != null) {
-			fLength= region.getLength();
-			int start= region.getOffset();
+		if (region == null) {
+			region = new Region(0, fDocument.getLength());
+		}
+		fLength= region.getLength();
+		int start= region.getOffset();
+		try {
+			fLineOffset= fDocument.getLineOfOffset(start);
+		} catch (BadLocationException ex) {
+			// silently ignored
+		}
+
+		if (fLength == 0)
+			fLineCount= 0;
+		else {
+			int endLine= fDocument.getNumberOfLines();
 			try {
-				fLineOffset= fDocument.getLineOfOffset(start);
+				endLine= fDocument.getLineOfOffset(start + fLength);
 			} catch (BadLocationException ex) {
 				// silently ignored
 			}
-
-			if (fLength == 0)
-				fLineCount= 0;
-			else {
-				int endLine= fDocument.getNumberOfLines();
-				try {
-					endLine= fDocument.getLineOfOffset(start + fLength);
-				} catch (BadLocationException ex) {
-					// silently ignored
-				}
-				fLineCount= endLine - fLineOffset + 1;
-			}
-
-		} else {
-			fLength= document.getLength();
-			fLineCount= fDocument.getNumberOfLines();
+			fLineCount= endLine - fLineOffset + 1;
 		}
 	}
 
diff --git a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/DocLineComparatorTest.java b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/DocLineComparatorTest.java
index 264d050..b72cc3f 100644
--- a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/DocLineComparatorTest.java
+++ b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/DocLineComparatorTest.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2006 IBM Corporation and others.
+ * Copyright (c) 2000, 2009 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -17,6 +17,7 @@
 import org.eclipse.compare.rangedifferencer.IRangeComparator;
 import org.eclipse.jface.text.Document;
 import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.Region;
 
 public class DocLineComparatorTest extends TestCase {
 	
@@ -70,4 +71,54 @@
 
 		Assert.assertTrue(comp1.rangesEqual(0, comp2, 0));
 	}
+
+	public void testNoContent() {
+		IDocument doc= new Document();
+		
+		IRangeComparator comp1= new DocLineComparator(doc, null, true);
+		IRangeComparator comp2= new DocLineComparator(doc, new Region(0, doc.getLength()), true);
+
+		Assert.assertTrue(comp1.rangesEqual(0, comp2, 0));
+		Assert.assertEquals(comp1.getRangeCount(), comp2.getRangeCount());
+		Assert.assertEquals(0, comp2.getRangeCount());
+	}
+	
+	public void testOneLine() {
+		IDocument doc = new Document();
+		doc.set("line1"); //$NON-NLS-1$
+		
+		IRangeComparator comp1= new DocLineComparator(doc, null, true);
+		IRangeComparator comp2= new DocLineComparator(doc, new Region(0, doc.getLength()), true);
+
+		Assert.assertEquals(comp1.getRangeCount(), comp2.getRangeCount());
+		Assert.assertEquals(1, comp2.getRangeCount());
+	}
+	
+	public void testTwoLines() {
+		IDocument doc = new Document();
+		doc.set("line1\nline2"); //$NON-NLS-1$
+		
+		IRangeComparator comp1= new DocLineComparator(doc, null, true);
+		IRangeComparator comp2= new DocLineComparator(doc, new Region(0, doc.getLength()), true);
+
+		Assert.assertEquals(comp1.getRangeCount(), comp2.getRangeCount());
+		Assert.assertEquals(2, comp2.getRangeCount());
+		
+		IRangeComparator comp3= new DocLineComparator(doc, new Region(0, "line1".length()), true);
+		Assert.assertEquals(1, comp3.getRangeCount());
+		
+		comp3= new DocLineComparator(doc, new Region(0, "line1".length()+1), true);
+		Assert.assertEquals(2, comp3.getRangeCount()); // two lines
+	}
+	
+	public void testBug259422() {
+		IDocument doc = new Document();
+		doc.set(""); //$NON-NLS-1$
+		
+		IRangeComparator comp1= new DocLineComparator(doc, null, true);
+		IRangeComparator comp2= new DocLineComparator(doc, new Region(0, doc.getLength()), true);
+
+		Assert.assertEquals(comp1.getRangeCount(), comp2.getRangeCount());
+	}
+	
 }