replace "String.indexOf(String) >= 1" with "String.contains(String)"

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1795296 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2017-05-16 10:53:15 +00:00
parent f701e81a64
commit 0b767886bf
14 changed files with 53 additions and 59 deletions

View File

@ -18,6 +18,9 @@
package org.apache.poi.ss.excelant; package org.apache.poi.ss.excelant;
import static org.apache.poi.POITestCase.assertContains;
import static org.apache.poi.POITestCase.assertNotContained;
import java.io.File; import java.io.File;
import java.io.PrintStream; import java.io.PrintStream;
import java.net.URL; import java.net.URL;
@ -119,20 +122,14 @@ public abstract class BuildFileTest extends TestCase {
* Assert that the given substring is in the log messages. * Assert that the given substring is in the log messages.
*/ */
public void assertLogContaining(String substring) { public void assertLogContaining(String substring) {
String realLog = getLog(); assertContains(getLog(), substring);
assertTrue("expecting log to contain \"" + substring + "\" log was \""
+ realLog + "\"",
realLog.indexOf(substring) >= 0);
} }
/** /**
* Assert that the given substring is not in the log messages. * Assert that the given substring is not in the log messages.
*/ */
public void assertLogNotContaining(String substring) { public void assertLogNotContaining(String substring) {
String realLog = getLog(); assertNotContained(getLog(), substring);
assertFalse("didn't expect log to contain \"" + substring + "\" log was \""
+ realLog + "\"",
realLog.indexOf(substring) >= 0);
} }
/** /**
@ -152,11 +149,7 @@ public abstract class BuildFileTest extends TestCase {
* @since Ant1.7 * @since Ant1.7
*/ */
public void assertOutputContaining(String message, String substring) { public void assertOutputContaining(String message, String substring) {
String realOutput = getOutput(); assertContains("output: " + message, getOutput(), substring);
String realMessage = (message != null)
? message
: "expecting output to contain \"" + substring + "\" output was \"" + realOutput + "\"";
assertTrue(realMessage, realOutput.indexOf(substring) >= 0);
} }
/** /**
@ -167,11 +160,7 @@ public abstract class BuildFileTest extends TestCase {
* @since Ant1.7 * @since Ant1.7
*/ */
public void assertOutputNotContaining(String message, String substring) { public void assertOutputNotContaining(String message, String substring) {
String realOutput = getOutput(); assertNotContained(getOutput(), substring);
String realMessage = (message != null)
? message
: "expecting output to not contain \"" + substring + "\" output was \"" + realOutput + "\"";
assertFalse(realMessage, realOutput.indexOf(substring) >= 0);
} }
/** /**
@ -218,10 +207,10 @@ public abstract class BuildFileTest extends TestCase {
*/ */
public void assertDebuglogContaining(String substring) { public void assertDebuglogContaining(String substring) {
String realLog = getFullLog(); String realLog = getFullLog();
assertTrue("expecting debug log to contain \"" + substring assertContains("expecting debug log to contain \"" + substring
+ "\" log was \"" + "\" log was \""
+ realLog + "\"", + realLog + "\"",
realLog.indexOf(substring) >= 0); realLog, substring);
} }
/** /**
@ -397,7 +386,7 @@ public abstract class BuildFileTest extends TestCase {
executeTarget(target); executeTarget(target);
} catch (org.apache.tools.ant.BuildException ex) { } catch (org.apache.tools.ant.BuildException ex) {
buildException = ex; buildException = ex;
if ((null != contains) && (ex.getMessage().indexOf(contains) == -1)) { if ((null != contains) && (!ex.getMessage().contains(contains))) {
fail("Should throw BuildException because '" + cause + "' with message containing '" + contains + "' (actual message '" + ex.getMessage() + "' instead)"); fail("Should throw BuildException because '" + cause + "' with message containing '" + contains + "' (actual message '" + ex.getMessage() + "' instead)");
} }
return; return;

View File

@ -40,6 +40,7 @@ public abstract class HeaderFooter implements org.apache.poi.ss.usermodel.Header
String _center = ""; String _center = "";
String _right = ""; String _right = "";
// FIXME: replace outer goto. just eww.
outer: outer:
while (text.length() > 1) { while (text.length() > 1) {
if (text.charAt(0) != '&') { if (text.charAt(0) != '&') {
@ -50,30 +51,30 @@ outer:
int pos = text.length(); int pos = text.length();
switch (text.charAt(1)) { switch (text.charAt(1)) {
case 'L': case 'L':
if (text.indexOf("&C") >= 0) { if (text.contains("&C")) {
pos = Math.min(pos, text.indexOf("&C")); pos = Math.min(pos, text.indexOf("&C"));
} }
if (text.indexOf("&R") >= 0) { if (text.contains("&R")) {
pos = Math.min(pos, text.indexOf("&R")); pos = Math.min(pos, text.indexOf("&R"));
} }
_left = text.substring(2, pos); _left = text.substring(2, pos);
text = text.substring(pos); text = text.substring(pos);
break; break;
case 'C': case 'C':
if (text.indexOf("&L") >= 0) { if (text.contains("&L")) {
pos = Math.min(pos, text.indexOf("&L")); pos = Math.min(pos, text.indexOf("&L"));
} }
if (text.indexOf("&R") >= 0) { if (text.contains("&R")) {
pos = Math.min(pos, text.indexOf("&R")); pos = Math.min(pos, text.indexOf("&R"));
} }
_center = text.substring(2, pos); _center = text.substring(2, pos);
text = text.substring(pos); text = text.substring(pos);
break; break;
case 'R': case 'R':
if (text.indexOf("&C") >= 0) { if (text.contains("&C")) {
pos = Math.min(pos, text.indexOf("&C")); pos = Math.min(pos, text.indexOf("&C"));
} }
if (text.indexOf("&L") >= 0) { if (text.contains("&L")) {
pos = Math.min(pos, text.indexOf("&L")); pos = Math.min(pos, text.indexOf("&L"));
} }
_right = text.substring(2, pos); _right = text.substring(2, pos);
@ -288,7 +289,7 @@ outer:
// Firstly, do the easy ones which are static // Firstly, do the easy ones which are static
for (MarkupTag mt : MarkupTag.values()) { for (MarkupTag mt : MarkupTag.values()) {
String seq = mt.getRepresentation(); String seq = mt.getRepresentation();
while ((pos = text.indexOf(seq)) > -1) { while ((pos = text.indexOf(seq)) >= 0) {
text = text.substring(0, pos) + text.substring(pos + seq.length()); text = text.substring(0, pos) + text.substring(pos + seq.length());
} }
} }

View File

@ -176,10 +176,7 @@ public class AreaReference {
} }
// Check for the , as a sign of non-coniguous // Check for the , as a sign of non-coniguous
if(reference.indexOf(',') == -1) { return !reference.contains(",");
return true;
}
return false;
} }
public static AreaReference getWholeRow(SpreadsheetVersion version, String start, String end) { public static AreaReference getWholeRow(SpreadsheetVersion version, String start, String end) {
@ -388,6 +385,7 @@ public class AreaReference {
String partA = reference.substring(0, delimiterPos); String partA = reference.substring(0, delimiterPos);
String partB = reference.substring(delimiterPos+1); String partB = reference.substring(delimiterPos+1);
if(partB.indexOf(SHEET_NAME_DELIMITER) >= 0) { if(partB.indexOf(SHEET_NAME_DELIMITER) >= 0) {
// partB contains SHEET_NAME_DELIMITER
// TODO - are references like "Sheet1!A1:Sheet1:B2" ever valid? // TODO - are references like "Sheet1!A1:Sheet1:B2" ever valid?
// FormulaParser has code to handle that. // FormulaParser has code to handle that.

View File

@ -408,7 +408,7 @@ public class CellReference {
boolean isQuoted = reference.charAt(0) == SPECIAL_NAME_DELIMITER; boolean isQuoted = reference.charAt(0) == SPECIAL_NAME_DELIMITER;
if(!isQuoted) { if(!isQuoted) {
// sheet names with spaces must be quoted // sheet names with spaces must be quoted
if (reference.indexOf(' ') == -1) { if (! reference.contains(" ")) {
return reference.substring(0, indexOfSheetNameDelimiter); return reference.substring(0, indexOfSheetNameDelimiter);
} else { } else {
throw new IllegalArgumentException("Sheet names containing spaces must be quoted: (" + reference + ")"); throw new IllegalArgumentException("Sheet names containing spaces must be quoted: (" + reference + ")");

View File

@ -119,7 +119,7 @@ public abstract class POIXMLRelation {
* @return the filename including the suffix * @return the filename including the suffix
*/ */
public String getFileName(int index) { public String getFileName(int index) {
if(_defaultName.indexOf("#") == -1) { if(! _defaultName.contains("#")) {
// Generic filename in all cases // Generic filename in all cases
return getDefaultFileName(); return getDefaultFileName();
} }

View File

@ -278,7 +278,9 @@ public final class OOXMLLite {
if (loc == null) continue; if (loc == null) continue;
String jar = loc.toString(); String jar = loc.toString();
if(jar.indexOf(ptrn) != -1) map.put(cls.getName(), cls); if (jar.contains(ptrn)) {
map.put(cls.getName(), cls);
}
} }
return map; return map;
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {

View File

@ -111,7 +111,7 @@ public class XSSFComment implements Comment {
boolean visible = false; boolean visible = false;
if(_vmlShape != null){ if(_vmlShape != null){
String style = _vmlShape.getStyle(); String style = _vmlShape.getStyle();
visible = style != null && style.indexOf("visibility:visible") != -1; visible = style != null && style.contains("visibility:visible");
} }
return visible; return visible;
} }

View File

@ -124,7 +124,7 @@ public final class RecipientChunks implements ChunkGroupWithProperties {
if (recipientEmailChunk != null) { if (recipientEmailChunk != null) {
String email = recipientEmailChunk.getValue(); String email = recipientEmailChunk.getValue();
int cne = email.indexOf("/CN="); int cne = email.indexOf("/CN=");
if (cne == -1) { if (cne < 0) {
// Normal smtp address // Normal smtp address
return email; return email;
} else { } else {
@ -136,7 +136,7 @@ public final class RecipientChunks implements ChunkGroupWithProperties {
// Might be in the name field, check there // Might be in the name field, check there
if (recipientNameChunk != null) { if (recipientNameChunk != null) {
String name = recipientNameChunk.getValue(); String name = recipientNameChunk.getValue();
if (name.indexOf('@') > -1) { if (name.contains("@")) {
// Strip leading and trailing quotes if needed // Strip leading and trailing quotes if needed
if (name.startsWith("'") && name.endsWith("'")) { if (name.startsWith("'") && name.endsWith("'")) {
return name.substring(1, name.length() - 1); return name.substring(1, name.length() - 1);
@ -149,8 +149,9 @@ public final class RecipientChunks implements ChunkGroupWithProperties {
// encoded as a SMTP destination in there. // encoded as a SMTP destination in there.
if (recipientSearchChunk != null) { if (recipientSearchChunk != null) {
String search = recipientSearchChunk.getAs7bitString(); String search = recipientSearchChunk.getAs7bitString();
if (search.indexOf("SMTP:") != -1) { int idx = search.indexOf("SMTP:");
return search.substring(search.indexOf("SMTP:") + 5); if (idx >= 0) {
return search.substring(idx + 5);
} }
} }

View File

@ -140,7 +140,7 @@ public final class POIFSChunkParser {
// Name in the wrong format // Name in the wrong format
return; return;
} }
if(entryName.indexOf('_') == -1) { if(! entryName.contains("_")) {
// Name in the wrong format // Name in the wrong format
return; return;
} }

View File

@ -64,9 +64,9 @@ public final class TestOutlookTextExtractor {
assertContains(text, "From: Kevin Roast\n"); assertContains(text, "From: Kevin Roast\n");
assertContains(text, "To: Kevin Roast <kevin.roast@alfresco.org>\n"); assertContains(text, "To: Kevin Roast <kevin.roast@alfresco.org>\n");
assertEquals(-1, text.indexOf("CC:")); assertNotContained(text, "CC:");
assertEquals(-1, text.indexOf("BCC:")); assertNotContained(text, "BCC:");
assertEquals(-1, text.indexOf("Attachment:")); assertNotContained(text, "Attachment:");
assertContains(text, "Subject: Test the content transformer\n"); assertContains(text, "Subject: Test the content transformer\n");
Calendar cal = LocaleUtil.getLocaleCalendar(2007, 5, 14, 9, 42, 55); Calendar cal = LocaleUtil.getLocaleCalendar(2007, 5, 14, 9, 42, 55);
SimpleDateFormat f = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss Z", Locale.ROOT); SimpleDateFormat f = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss Z", Locale.ROOT);
@ -89,8 +89,8 @@ public final class TestOutlookTextExtractor {
assertContains(text, "From: Travis Ferguson\n"); assertContains(text, "From: Travis Ferguson\n");
assertContains(text, "To: travis@overwrittenstack.com\n"); assertContains(text, "To: travis@overwrittenstack.com\n");
assertEquals(-1, text.indexOf("CC:")); assertNotContained(text, "CC:");
assertEquals(-1, text.indexOf("BCC:")); assertNotContained(text, "BCC:");
assertContains(text, "Subject: test message\n"); assertContains(text, "Subject: test message\n");
assertContains(text, "Date: Fri, 6 Jul 2007 05:27:17 +0000\n"); assertContains(text, "Date: Fri, 6 Jul 2007 05:27:17 +0000\n");
assertContains(text, "This is a test message."); assertContains(text, "This is a test message.");
@ -193,7 +193,7 @@ public final class TestOutlookTextExtractor {
"'Paul Holmes-Higgin' <paul.hh@alfresco.com>; 'Mike Farman' <mikef@alfresco.com>\n"); "'Paul Holmes-Higgin' <paul.hh@alfresco.com>; 'Mike Farman' <mikef@alfresco.com>\n");
assertContains(text, "CC: nickb@alfresco.com; " + assertContains(text, "CC: nickb@alfresco.com; " +
"nick.burch@alfresco.com; 'Roy Wetherall' <roy.wetherall@alfresco.com>\n"); "nick.burch@alfresco.com; 'Roy Wetherall' <roy.wetherall@alfresco.com>\n");
assertEquals(-1, text.indexOf("BCC:")); assertNotContained(text, "BCC:");
assertContains(text, "Subject: This is a test message please ignore\n"); assertContains(text, "Subject: This is a test message please ignore\n");
assertContains(text, "Date: Mon, 11 Jan 2010 16:2"); // Exact times differ slightly assertContains(text, "Date: Mon, 11 Jan 2010 16:2"); // Exact times differ slightly
assertContains(text, "The quick brown fox jumps over the lazy dog"); assertContains(text, "The quick brown fox jumps over the lazy dog");
@ -216,8 +216,8 @@ public final class TestOutlookTextExtractor {
assertContains(text, "From: Nicolas1"); assertContains(text, "From: Nicolas1");
assertContains(text, "To: 'nicolas1.23456@free.fr'"); assertContains(text, "To: 'nicolas1.23456@free.fr'");
assertEquals(-1, text.indexOf("CC:")); assertNotContained(text, "CC:");
assertEquals(-1, text.indexOf("BCC:")); assertNotContained(text, "BCC:");
assertContains(text, "Subject: test"); assertContains(text, "Subject: test");
assertContains(text, "Date: Wed, 22 Apr"); assertContains(text, "Date: Wed, 22 Apr");
assertContains(text, "Attachment: test-unicode.doc\n"); assertContains(text, "Attachment: test-unicode.doc\n");

View File

@ -162,7 +162,7 @@ public final class TestProblems extends HWPFTestCase {
String text = para.text(); String text = para.text();
totalLength += text.length(); totalLength += text.length();
if (text.indexOf("{delete me}") > -1) { if (text.contains("{delete me}")) {
para.delete(); para.delete();
deletedLength = text.length(); deletedLength = text.length();
} }

View File

@ -64,7 +64,7 @@ public final class TestEscherGraphics {
@Test @Test
public void testGetFont() { public void testGetFont() {
Font f = graphics.getFont(); Font f = graphics.getFont();
if (f.toString().indexOf("dialog") == -1 && f.toString().indexOf("Dialog") == -1) { if (! f.toString().contains("dialog") && ! f.toString().contains("Dialog")) {
assertEquals("java.awt.Font[family=Arial,name=Arial,style=plain,size=10]", f.toString()); assertEquals("java.awt.Font[family=Arial,name=Arial,style=plain,size=10]", f.toString());
} }
} }
@ -72,7 +72,7 @@ public final class TestEscherGraphics {
@Test @Test
public void testGetFontMetrics() { public void testGetFontMetrics() {
Font f = graphics.getFont(); Font f = graphics.getFont();
if (f.toString().indexOf("dialog") != -1 || f.toString().indexOf("Dialog") != -1) { if (f.toString().contains("dialog") || f.toString().contains("Dialog")) {
return; return;
} }
FontMetrics fontMetrics = graphics.getFontMetrics(graphics.getFont()); FontMetrics fontMetrics = graphics.getFontMetrics(graphics.getFont());

View File

@ -17,6 +17,8 @@
package org.apache.poi.poifs.filesystem; package org.apache.poi.poifs.filesystem;
import static org.apache.poi.POITestCase.assertContains;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -45,8 +47,8 @@ public class TestOfficeXMLException extends TestCase {
fail("expected exception was not thrown"); fail("expected exception was not thrown");
} catch(OfficeXmlFileException e) { } catch(OfficeXmlFileException e) {
// expected during successful test // expected during successful test
assertTrue(e.getMessage().indexOf("The supplied data appears to be in the Office 2007+ XML") > -1); assertContains(e.getMessage(), "The supplied data appears to be in the Office 2007+ XML");
assertTrue(e.getMessage().indexOf("You are calling the part of POI that deals with OLE2 Office Documents") > -1); assertContains(e.getMessage(), "You are calling the part of POI that deals with OLE2 Office Documents");
} }
} }
public void test2003XMLException() throws IOException public void test2003XMLException() throws IOException
@ -58,8 +60,8 @@ public class TestOfficeXMLException extends TestCase {
fail("expected exception was not thrown"); fail("expected exception was not thrown");
} catch(NotOLE2FileException e) { } catch(NotOLE2FileException e) {
// expected during successful test // expected during successful test
assertTrue(e.getMessage().indexOf("The supplied data appears to be a raw XML file") > -1); assertContains(e.getMessage(), "The supplied data appears to be a raw XML file");
assertTrue(e.getMessage().indexOf("Formats such as Office 2003 XML") > -1); assertContains(e.getMessage(), "Formats such as Office 2003 XML");
} }
} }

View File

@ -22,6 +22,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.apache.poi.POITestCase.assertContains;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -118,7 +119,7 @@ public class TestOle10Native {
Ole10Native.createFromEmbeddedOleObject(fs); Ole10Native.createFromEmbeddedOleObject(fs);
fail("Should have thrown exception because OLENative lacks a length parameter"); fail("Should have thrown exception because OLENative lacks a length parameter");
} catch (Ole10NativeException e) { } catch (Ole10NativeException e) {
assertTrue(e.getMessage().indexOf("declared data length") > -1); assertContains(e.getMessage(), "declared data length");
} }
} }