]> source.dussan.org Git - poi.git/commitdiff
replace "String.indexOf(String) >= 1" with "String.contains(String)"
authorJaven O'Neal <onealj@apache.org>
Tue, 16 May 2017 10:53:15 +0000 (10:53 +0000)
committerJaven O'Neal <onealj@apache.org>
Tue, 16 May 2017 10:53:15 +0000 (10:53 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1795296 13f79535-47bb-0310-9956-ffa450edef68

14 files changed:
src/excelant/testcases/org/apache/poi/ss/excelant/BuildFileTest.java
src/java/org/apache/poi/hssf/usermodel/HeaderFooter.java
src/java/org/apache/poi/ss/util/AreaReference.java
src/java/org/apache/poi/ss/util/CellReference.java
src/ooxml/java/org/apache/poi/POIXMLRelation.java
src/ooxml/java/org/apache/poi/util/OOXMLLite.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java
src/scratchpad/src/org/apache/poi/hsmf/datatypes/RecipientChunks.java
src/scratchpad/src/org/apache/poi/hsmf/parsers/POIFSChunkParser.java
src/scratchpad/testcases/org/apache/poi/hsmf/extractor/TestOutlookTextExtractor.java
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java
src/testcases/org/apache/poi/hssf/usermodel/TestEscherGraphics.java
src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java
src/testcases/org/apache/poi/poifs/filesystem/TestOle10Native.java

index ea2bb596cf68da7c76ddf6bdbb28604ede757418..2e0feae806efeea4eb1e083725e4107e5165511d 100644 (file)
@@ -18,6 +18,9 @@
 
 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.PrintStream;
 import java.net.URL;
@@ -119,20 +122,14 @@ public abstract class BuildFileTest extends TestCase {
      * Assert that the given substring is in the log messages.
      */
     public void assertLogContaining(String substring) {
-        String realLog = getLog();
-        assertTrue("expecting log to contain \"" + substring + "\" log was \""
-                + realLog + "\"",
-                realLog.indexOf(substring) >= 0);
+        assertContains(getLog(), substring);
     }
 
     /**
      * Assert that the given substring is not in the log messages.
      */
     public void assertLogNotContaining(String substring) {
-        String realLog = getLog();
-        assertFalse("didn't expect log to contain \"" + substring + "\" log was \""
-                + realLog + "\"",
-                realLog.indexOf(substring) >= 0);
+        assertNotContained(getLog(), substring);
     }
 
     /**
@@ -152,11 +149,7 @@ public abstract class BuildFileTest extends TestCase {
      * @since Ant1.7
      */
     public void assertOutputContaining(String message, String substring) {
-        String realOutput = getOutput();
-        String realMessage = (message != null)
-                ? message
-                : "expecting output to contain \"" + substring + "\" output was \"" + realOutput + "\"";
-        assertTrue(realMessage, realOutput.indexOf(substring) >= 0);
+        assertContains("output: " + message, getOutput(), substring);
     }
 
     /**
@@ -167,11 +160,7 @@ public abstract class BuildFileTest extends TestCase {
      * @since Ant1.7
      */
     public void assertOutputNotContaining(String message, String substring) {
-        String realOutput = getOutput();
-        String realMessage = (message != null)
-                ? message
-                : "expecting output to not contain \"" + substring + "\" output was \"" + realOutput + "\"";
-        assertFalse(realMessage, realOutput.indexOf(substring) >= 0);
+        assertNotContained(getOutput(), substring);
     }
 
     /**
@@ -218,10 +207,10 @@ public abstract class BuildFileTest extends TestCase {
      */
     public void assertDebuglogContaining(String substring) {
         String realLog = getFullLog();
-        assertTrue("expecting debug log to contain \"" + substring
+        assertContains("expecting debug log to contain \"" + substring
                 + "\" log was \""
                 + realLog + "\"",
-                realLog.indexOf(substring) >= 0);
+                realLog, substring);
     }
 
     /**
@@ -397,7 +386,7 @@ public abstract class BuildFileTest extends TestCase {
             executeTarget(target);
         } catch (org.apache.tools.ant.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)");
             }
             return;
index 94a4e3b318f5cd7892c0c43121e104647df8428f..38464490d6b3365237e52399fa2df3c92a22da80 100644 (file)
@@ -40,6 +40,7 @@ public abstract class HeaderFooter implements org.apache.poi.ss.usermodel.Header
                String _center = "";
                String _right = "";
 
+// FIXME: replace outer goto. just eww.
 outer:
                while (text.length() > 1) {
                        if (text.charAt(0) != '&') {
@@ -50,30 +51,30 @@ outer:
                        int pos = text.length();
                        switch (text.charAt(1)) {
                        case 'L':
-                               if (text.indexOf("&C") >= 0) {
+                               if (text.contains("&C")) {
                                        pos = Math.min(pos, text.indexOf("&C"));
                                }
-                               if (text.indexOf("&R") >= 0) {
+                               if (text.contains("&R")) {
                                        pos = Math.min(pos, text.indexOf("&R"));
                                }
                                _left = text.substring(2, pos);
                                text = text.substring(pos);
                                break;
                        case 'C':
-                               if (text.indexOf("&L") >= 0) {
+                               if (text.contains("&L")) {
                                        pos = Math.min(pos, text.indexOf("&L"));
                                }
-                               if (text.indexOf("&R") >= 0) {
+                               if (text.contains("&R")) {
                                        pos = Math.min(pos, text.indexOf("&R"));
                                }
                                _center = text.substring(2, pos);
                                text = text.substring(pos);
                                break;
                        case 'R':
-                               if (text.indexOf("&C") >= 0) {
+                               if (text.contains("&C")) {
                                        pos = Math.min(pos, text.indexOf("&C"));
                                }
-                               if (text.indexOf("&L") >= 0) {
+                               if (text.contains("&L")) {
                                        pos = Math.min(pos, text.indexOf("&L"));
                                }
                                _right = text.substring(2, pos);
@@ -288,7 +289,7 @@ outer:
                // Firstly, do the easy ones which are static
                for (MarkupTag mt : MarkupTag.values()) {
                        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());
                        }
                }
index 8576511a2bb6a077f5ed48699fb6cf7103ea614b..7e637b84f28c14b2410cf93c92cd1314c59a3525 100644 (file)
@@ -176,10 +176,7 @@ public class AreaReference {
        }
 
        // Check for the , as a sign of non-coniguous
-       if(reference.indexOf(',') == -1) {
-          return true;
-       }
-       return false;
+       return !reference.contains(",");
     }
 
     public static AreaReference getWholeRow(SpreadsheetVersion version, String start, String end) {
@@ -387,7 +384,8 @@ public class AreaReference {
 
         String partA = reference.substring(0, delimiterPos);
         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?  
             // FormulaParser has code to handle that.
             
index f530a37835f308d44bd8c26d383f721dd82b02dc..01054ca29cc5690f97492f325bdeb94b73b1111c 100644 (file)
@@ -408,7 +408,7 @@ public class CellReference {
         boolean isQuoted = reference.charAt(0) == SPECIAL_NAME_DELIMITER;
         if(!isQuoted) {
             // sheet names with spaces must be quoted
-            if (reference.indexOf(' ') == -1) {
+            if (! reference.contains(" ")) {
                 return reference.substring(0, indexOfSheetNameDelimiter);
             } else {
                 throw new IllegalArgumentException("Sheet names containing spaces must be quoted: (" + reference + ")");
index 571ae5ab73345320617291bec255dbf481effd4c..55d162c5f3563cbe9c162dbe5410342ed0241e77 100644 (file)
@@ -119,7 +119,7 @@ public abstract class POIXMLRelation {
      * @return the filename including the suffix
      */
     public String getFileName(int index) {
-        if(_defaultName.indexOf("#") == -1) {
+        if(! _defaultName.contains("#")) {
             // Generic filename in all cases
             return getDefaultFileName();
         }
index 398966a9694353a5b03674c783577359f577a487..793b0471708777530ad8b673df4d5713672eea57 100644 (file)
@@ -278,7 +278,9 @@ public final class OOXMLLite {
                 if (loc == null) continue;
                 
                 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;
         } catch (IllegalAccessException e) {
@@ -299,4 +301,4 @@ public final class OOXMLLite {
         }
     }
 
-}
\ No newline at end of file
+}
index 6e69fa91ec77469e127b02482d9ddafcde745467..5dffccec1ecdc8560ee9d95672479735adf74a3d 100644 (file)
@@ -111,7 +111,7 @@ public class XSSFComment implements Comment {
         boolean visible = false;
         if(_vmlShape != null){
             String style = _vmlShape.getStyle();
-            visible = style != null && style.indexOf("visibility:visible") != -1;
+            visible = style != null && style.contains("visibility:visible");
         }
         return visible;
     }
index f39d7d84d8e0f13207b0c2b66f9f90ef52767003..382e3ed82ea475eab8af74ddf76e83c2bccbee3e 100644 (file)
@@ -124,7 +124,7 @@ public final class RecipientChunks implements ChunkGroupWithProperties {
         if (recipientEmailChunk != null) {
             String email = recipientEmailChunk.getValue();
             int cne = email.indexOf("/CN=");
-            if (cne == -1) {
+            if (cne < 0) {
                 // Normal smtp address
                 return email;
             } else {
@@ -136,7 +136,7 @@ public final class RecipientChunks implements ChunkGroupWithProperties {
         // Might be in the name field, check there
         if (recipientNameChunk != null) {
             String name = recipientNameChunk.getValue();
-            if (name.indexOf('@') > -1) {
+            if (name.contains("@")) {
                 // Strip leading and trailing quotes if needed
                 if (name.startsWith("'") && name.endsWith("'")) {
                     return name.substring(1, name.length() - 1);
@@ -149,8 +149,9 @@ public final class RecipientChunks implements ChunkGroupWithProperties {
         // encoded as a SMTP destination in there.
         if (recipientSearchChunk != null) {
             String search = recipientSearchChunk.getAs7bitString();
-            if (search.indexOf("SMTP:") != -1) {
-                return search.substring(search.indexOf("SMTP:") + 5);
+            int idx = search.indexOf("SMTP:");
+            if (idx >= 0) {
+                return search.substring(idx + 5);
             }
         }
 
index 1d6165d62aeae65bbeeb58064c274ca2b0f7bd32..b5d0e26f0ba134ae965545a61459abba26095cfd 100644 (file)
@@ -140,7 +140,7 @@ public final class POIFSChunkParser {
             // Name in the wrong format
             return;
          }
-         if(entryName.indexOf('_') == -1) {
+         if(! entryName.contains("_")) {
             // Name in the wrong format
             return;
          }
index 755bb638042b34cba000e06a1e584f9f51cb8ec7..51b60399f88dfaacd0b60c7272fbeb22562fbb97 100644 (file)
@@ -64,9 +64,9 @@ public final class TestOutlookTextExtractor {
       
       assertContains(text, "From: Kevin Roast\n");
       assertContains(text, "To: Kevin Roast <kevin.roast@alfresco.org>\n");
-      assertEquals(-1, text.indexOf("CC:"));
-      assertEquals(-1, text.indexOf("BCC:"));
-      assertEquals(-1, text.indexOf("Attachment:"));
+      assertNotContained(text, "CC:");
+      assertNotContained(text, "BCC:");
+      assertNotContained(text, "Attachment:");
       assertContains(text, "Subject: Test the content transformer\n");
       Calendar cal = LocaleUtil.getLocaleCalendar(2007, 5, 14, 9, 42, 55);
       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, "To: travis@overwrittenstack.com\n");
-      assertEquals(-1, text.indexOf("CC:"));
-      assertEquals(-1, text.indexOf("BCC:"));
+      assertNotContained(text, "CC:");
+      assertNotContained(text, "BCC:");
       assertContains(text, "Subject: test message\n");
       assertContains(text, "Date: Fri, 6 Jul 2007 05:27:17 +0000\n");
       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");
          assertContains(text, "CC: nickb@alfresco.com; " +
                "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, "Date: Mon, 11 Jan 2010 16:2"); // Exact times differ slightly
          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, "To: 'nicolas1.23456@free.fr'");
-      assertEquals(-1, text.indexOf("CC:"));
-      assertEquals(-1, text.indexOf("BCC:"));
+      assertNotContained(text, "CC:");
+      assertNotContained(text, "BCC:");
       assertContains(text, "Subject: test");
       assertContains(text, "Date: Wed, 22 Apr");
       assertContains(text, "Attachment: test-unicode.doc\n");
index 3979a442c5802863e6b7f83a1494ed951d7d5e2c..ab3145e0673d45adc4e34ae9d6d743231732a066 100644 (file)
@@ -162,7 +162,7 @@ public final class TestProblems extends HWPFTestCase {
             String text = para.text();
 
             totalLength += text.length();
-            if (text.indexOf("{delete me}") > -1) {
+            if (text.contains("{delete me}")) {
                 para.delete();
                 deletedLength = text.length();
             }
index e6d5693846a86af1102cac7be0df5d137b4dfb8a..f8c4c65c7efed5245bb210af9912a2053c4b59f4 100644 (file)
@@ -64,7 +64,7 @@ public final class TestEscherGraphics {
     @Test
     public void testGetFont() {
         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());
         }
     }
@@ -72,7 +72,7 @@ public final class TestEscherGraphics {
     @Test
     public void testGetFontMetrics() {
         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;
         }
         FontMetrics fontMetrics = graphics.getFontMetrics(graphics.getFont());
index 087fbb2bd3ec4dc4108db0373f8801e493c0baac..9e5e0e1be44eee6dbd333e25c6406a954f6aa163 100644 (file)
@@ -17,6 +17,8 @@
 
 package org.apache.poi.poifs.filesystem;
 
+import static org.apache.poi.POITestCase.assertContains;
+
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -45,8 +47,8 @@ public class TestOfficeXMLException extends TestCase {
                        fail("expected exception was not thrown");
                } catch(OfficeXmlFileException e) {
                        // expected during successful test
-                       assertTrue(e.getMessage().indexOf("The supplied data appears to be in the Office 2007+ XML") > -1);
-                       assertTrue(e.getMessage().indexOf("You are calling the part of POI that deals with OLE2 Office Documents") > -1);
+                       assertContains(e.getMessage(), "The supplied data appears to be in the Office 2007+ XML");
+                       assertContains(e.getMessage(), "You are calling the part of POI that deals with OLE2 Office Documents");
                }
        }
     public void test2003XMLException() throws IOException
@@ -58,8 +60,8 @@ public class TestOfficeXMLException extends TestCase {
             fail("expected exception was not thrown");
         } catch(NotOLE2FileException e) {
             // expected during successful test
-            assertTrue(e.getMessage().indexOf("The supplied data appears to be a raw XML file") > -1);
-            assertTrue(e.getMessage().indexOf("Formats such as Office 2003 XML") > -1);
+            assertContains(e.getMessage(), "The supplied data appears to be a raw XML file");
+            assertContains(e.getMessage(), "Formats such as Office 2003 XML");
         }
     }
        
index 0abdf8489cb919b5eb1708b17f390b274638a538..d63061c38903a1a4f7ed2dde72a66e02ca26d844 100644 (file)
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+import static org.apache.poi.POITestCase.assertContains;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
@@ -118,7 +119,7 @@ public class TestOle10Native {
             Ole10Native.createFromEmbeddedOleObject(fs);
             fail("Should have thrown exception because OLENative lacks a length parameter");
         } catch (Ole10NativeException e) {
-            assertTrue(e.getMessage().indexOf("declared data length") > -1);
+            assertContains(e.getMessage(), "declared data length");
         }
     }