Bug 545565 - ListLineTracker returns wrong results after content set to
null

If tracked text is set to null the tracked content length was not
updated.

Change-Id: I26ed7b21879ff77a1e3b7828a4e4a4d969608cb8
Signed-off-by: Paul Pazderski <paul-eclipse@ppazderski.de>
diff --git a/org.eclipse.text.tests/META-INF/MANIFEST.MF b/org.eclipse.text.tests/META-INF/MANIFEST.MF
index 9048492..ae8dbd8 100644
--- a/org.eclipse.text.tests/META-INF/MANIFEST.MF
+++ b/org.eclipse.text.tests/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2

 Bundle-Name: %Plugin.name

 Bundle-SymbolicName: org.eclipse.text.tests

-Bundle-Version: 3.12.100.qualifier

+Bundle-Version: 3.12.200.qualifier

 Bundle-Vendor: %Plugin.providerName

 Bundle-Localization: plugin

 Export-Package: 

diff --git a/org.eclipse.text.tests/pom.xml b/org.eclipse.text.tests/pom.xml
index 4c70468..bbfd947 100644
--- a/org.eclipse.text.tests/pom.xml
+++ b/org.eclipse.text.tests/pom.xml
@@ -19,7 +19,7 @@
   </parent>
   <groupId>org.eclipse.text</groupId>
   <artifactId>org.eclipse.text.tests</artifactId>
-  <version>3.12.100-SNAPSHOT</version>
+  <version>3.12.200-SNAPSHOT</version>
   <packaging>eclipse-test-plugin</packaging>
   <properties>
   	<testSuite>${project.artifactId}</testSuite>
diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/LineTrackerTest3.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/LineTrackerTest3.java
index aabd126..17d388f 100644
--- a/org.eclipse.text.tests/src/org/eclipse/text/tests/LineTrackerTest3.java
+++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/LineTrackerTest3.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2008 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -544,4 +544,63 @@
 		} catch (BadLocationException e) {
 		}
 	}
+
+	/**
+	 * Test for Bug 545565. Some ListLineTracker methods yield wrong results after tracker content
+	 * was set to <code>null</code>.
+	 * 
+	 * @throws BadLocationException if test failed
+	 */
+	@Test
+	public void testBug545565_setNull() throws BadLocationException {
+		int initialContentLength= fText.getLength();
+		set(null);
+		assertEquals("Tracker not empty.", 1, fTracker.getNumberOfLines());
+		assertEquals("Tracker not empty.", 0, fTracker.getLineLength(0));
+		try {
+			fTracker.getLineInformationOfOffset(5);
+			fail("No exception for bad location.");
+		} catch (BadLocationException e) {
+			// expected
+		}
+		try {
+			fTracker.getLineInformationOfOffset(initialContentLength);
+			fail("No exception for bad location.");
+		} catch (BadLocationException e) {
+			// expected
+		}
+		try {
+			fTracker.getLineNumberOfOffset(5);
+			fail("No exception for bad location.");
+		} catch (BadLocationException e) {
+			// expected
+		}
+		try {
+			fTracker.getLineNumberOfOffset(initialContentLength);
+			fail("No exception for bad location.");
+		} catch (BadLocationException e) {
+			// expected
+		}
+		try {
+			fTracker.getNumberOfLines(5, 3);
+			fail("No exception for bad location.");
+		} catch (BadLocationException e) {
+			// expected
+		}
+	}
+
+	/**
+	 * Check if ListLineTracker and TreeLineTracker return same result for same input in context of
+	 * Bug 545565.
+	 * 
+	 * @throws BadLocationException if test fails
+	 */
+	@Test
+	public void testBug545565_compareTrackerResult() throws BadLocationException {
+		set(null);
+		int lineFromListTracker= fTracker.getLineNumberOfOffset(0);
+		replace(0, 0, null);
+		int lineFromTreeTracker= fTracker.getLineNumberOfOffset(0);
+		assertEquals("Trackers returned different lines for same offset.", lineFromTreeTracker, lineFromListTracker);
+	}
 }
diff --git a/org.eclipse.text/src/org/eclipse/jface/text/ListLineTracker.java b/org.eclipse.text/src/org/eclipse/jface/text/ListLineTracker.java
index a82c20b..56c7ede 100644
--- a/org.eclipse.text/src/org/eclipse/jface/text/ListLineTracker.java
+++ b/org.eclipse.text/src/org/eclipse/jface/text/ListLineTracker.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2008 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -330,6 +330,8 @@
 		if (text != null) {
 			fTextLength= text.length();
 			createLines(text, 0, 0);
+		} else {
+			fTextLength= 0;
 		}
 	}