You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ju...@apache.org on 2007/12/21 09:28:21 UTC

svn commit: r606132 - in /incubator/tika/trunk: ./ src/main/java/org/apache/tika/mime/ src/test/java/org/apache/tika/mime/

Author: jukka
Date: Fri Dec 21 00:28:20 2007
New Revision: 606132

URL: http://svn.apache.org/viewvc?rev=606132&view=rev
Log:
TIKA-107 - Remove use of assertions for argument checking
    - Committed patch from Niall Pemberton

Added:
    incubator/tika/trunk/src/test/java/org/apache/tika/mime/PatternsTest.java
Modified:
    incubator/tika/trunk/CHANGES.txt
    incubator/tika/trunk/src/main/java/org/apache/tika/mime/MimeType.java
    incubator/tika/trunk/src/main/java/org/apache/tika/mime/MimeTypes.java
    incubator/tika/trunk/src/main/java/org/apache/tika/mime/Patterns.java
    incubator/tika/trunk/src/test/java/org/apache/tika/mime/MimeTypeTest.java
    incubator/tika/trunk/src/test/java/org/apache/tika/mime/MimeTypesTest.java

Modified: incubator/tika/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/tika/trunk/CHANGES.txt?rev=606132&r1=606131&r2=606132&view=diff
==============================================================================
--- incubator/tika/trunk/CHANGES.txt (original)
+++ incubator/tika/trunk/CHANGES.txt Fri Dec 21 00:28:20 2007
@@ -132,3 +132,6 @@
 
 60. TIKA-102 - Parser implementations loading a large amount of content
                into a single String could be problematic (Niall Pemberton)
+
+ 61. TIKA-107 - Remove use of assertions for argument checking (Niall Pemberton)
+ 
\ No newline at end of file

Modified: incubator/tika/trunk/src/main/java/org/apache/tika/mime/MimeType.java
URL: http://svn.apache.org/viewvc/incubator/tika/trunk/src/main/java/org/apache/tika/mime/MimeType.java?rev=606132&r1=606131&r2=606132&view=diff
==============================================================================
--- incubator/tika/trunk/src/main/java/org/apache/tika/mime/MimeType.java (original)
+++ incubator/tika/trunk/src/main/java/org/apache/tika/mime/MimeType.java Fri Dec 21 00:28:20 2007
@@ -46,7 +46,9 @@
      *         <code>false</code> otherwise
      */
     public static boolean isValid(String name) {
-        assert name != null;
+        if (name == null) {
+            throw new IllegalArgumentException("Name is missing");
+        }
 
         boolean slash = false;
         for (int i = 0; i < name.length(); i++) {
@@ -117,8 +119,12 @@
      * @param name media type name
      */
     MimeType(MimeTypes registry, String name) {
-        assert registry != null;
-        assert isValid(name) && name.equals(name.toLowerCase());
+        if (registry == null) {
+            throw new IllegalArgumentException("Registry is missing");
+        }
+        if (!MimeType.isValid(name) || !name.equals(name.toLowerCase())) {
+            throw new IllegalArgumentException("Media type name is invalid");
+        }
         this.registry = registry;
         this.name = name;
     }
@@ -142,7 +148,12 @@
     }
 
     public void setSuperType(MimeType type) throws MimeTypeException {
-        assert type != null && type.registry == registry;
+        if (type == null) {
+            throw new IllegalArgumentException("MimeType is missing");
+        }
+        if (type.registry != registry) {
+            throw new IllegalArgumentException("MimeType is from a different registry");
+        }
         if (this.isDescendantOf(type)) {
             // ignore, already a descendant of the given type
         } else if (this == type) {
@@ -169,7 +180,9 @@
     }
 
     public boolean isDescendantOf(MimeType type) {
-        assert type != null;
+        if (type == null) {
+            throw new IllegalArgumentException("MimeType is missing");
+        }
         synchronized (registry) {
             for (MimeType t = superType; t != null; t = t.superType) {
                 if (t == type) {
@@ -195,7 +208,9 @@
      * @param description media type description
      */
     public void setDescription(String description) {
-        assert description != null;
+        if (description == null) {
+            throw new IllegalArgumentException("Description is missing");
+        }
         this.description = description;
     }
 
@@ -360,7 +375,9 @@
     //----------------------------------------------------------< Comparable >
 
     public int compareTo(MimeType type) {
-        assert type != null;
+        if (type == null) {
+            throw new IllegalArgumentException("MimeType is missing");
+        }
         if (type == this) {
             return 0;
         } else if (this.isDescendantOf(type)) {

Modified: incubator/tika/trunk/src/main/java/org/apache/tika/mime/MimeTypes.java
URL: http://svn.apache.org/viewvc/incubator/tika/trunk/src/main/java/org/apache/tika/mime/MimeTypes.java?rev=606132&r1=606131&r2=606132&view=diff
==============================================================================
--- incubator/tika/trunk/src/main/java/org/apache/tika/mime/MimeTypes.java (original)
+++ incubator/tika/trunk/src/main/java/org/apache/tika/mime/MimeTypes.java Fri Dec 21 00:28:20 2007
@@ -120,7 +120,9 @@
      * @return matching MIME type, or <code>null</code> if no match is found
      */
     public MimeType getMimeType(byte[] data) {
-        assert data != null;
+        if (data == null) {
+            throw new IllegalArgumentException("Data is missing");
+        }
 
         // First, check for XML descriptions (level by level)
         for (MimeType type : xmls) {
@@ -166,7 +168,9 @@
      * @throws IOException if the stream can not be read
      */
     private byte[] readMagicHeader(InputStream stream) throws IOException {
-        assert stream != null;
+        if (stream == null) {
+            throw new IllegalArgumentException("InputStream is missing");
+        }
 
         byte[] bytes = new byte[getMinLength()];
         int totalRead = 0;

Modified: incubator/tika/trunk/src/main/java/org/apache/tika/mime/Patterns.java
URL: http://svn.apache.org/viewvc/incubator/tika/trunk/src/main/java/org/apache/tika/mime/Patterns.java?rev=606132&r1=606131&r2=606132&view=diff
==============================================================================
--- incubator/tika/trunk/src/main/java/org/apache/tika/mime/Patterns.java (original)
+++ incubator/tika/trunk/src/main/java/org/apache/tika/mime/Patterns.java Fri Dec 21 00:28:20 2007
@@ -58,7 +58,9 @@
         });
 
     public void add(String pattern, MimeType type) throws MimeTypeException {
-        assert pattern != null && type != null;
+        if (pattern == null || type == null) {
+            throw new IllegalArgumentException("Pattern and/or mime type is missing");
+        }
 
         if (pattern.indexOf('*') == -1
                 && pattern.indexOf('?') == -1
@@ -128,7 +130,9 @@
      * (since this covers the majority of the patterns).
      */
     public MimeType matches(String name) {
-        assert name != null;
+        if (name == null) {
+            throw new IllegalArgumentException("Name is missing");
+        }
 
         // First, try exact match of the provided resource name
         if (names.containsKey(name)) {

Modified: incubator/tika/trunk/src/test/java/org/apache/tika/mime/MimeTypeTest.java
URL: http://svn.apache.org/viewvc/incubator/tika/trunk/src/test/java/org/apache/tika/mime/MimeTypeTest.java?rev=606132&r1=606131&r2=606132&view=diff
==============================================================================
--- incubator/tika/trunk/src/test/java/org/apache/tika/mime/MimeTypeTest.java (original)
+++ incubator/tika/trunk/src/test/java/org/apache/tika/mime/MimeTypeTest.java Fri Dec 21 00:28:20 2007
@@ -20,6 +20,50 @@
 
 public class MimeTypeTest extends TestCase {
 
+    private MimeTypes types;
+    private MimeType text;
+
+    protected void setUp() throws MimeTypeException {
+        types = new MimeTypes();
+        text = types.forName("text/plain");
+    }
+
+    /** Test MimeType constructor */
+    public void testConstrctor() {
+
+        // Missing registry
+        try {
+            new MimeType(null, "text/plain");
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
+
+        // Missing name
+        try {
+            new MimeType(types, null);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
+
+        // Invalid name (no slash)
+        try {
+            new MimeType(types, "application");
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
+
+        // Invalid name (not lower case)
+        try {
+            new MimeType(types, "TEXT/PLAIN");
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
+    }
+
     public void testIsValidName() {
         assertTrue(MimeType.isValid("application/octet-stream"));
         assertTrue(MimeType.isValid("text/plain"));
@@ -35,6 +79,32 @@
         assertFalse(MimeType.isValid("application/\u00f6ctet-stream"));
         assertFalse(MimeType.isValid("text/plain;"));
         assertFalse(MimeType.isValid("text/plain; charset=UTF-8"));
+        try {
+            MimeType.isValid(null);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
+    }
+
+    /** Test MimeType setDescription() */
+    public void testSetDescription() {
+        try {
+            text.setDescription(null);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
+    }
+
+    /** Test MimeType setSuperType() */
+    public void testSetSuperType() throws MimeTypeException {
+        try {
+            text.setSuperType(null);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
     }
 
 }

Modified: incubator/tika/trunk/src/test/java/org/apache/tika/mime/MimeTypesTest.java
URL: http://svn.apache.org/viewvc/incubator/tika/trunk/src/test/java/org/apache/tika/mime/MimeTypesTest.java?rev=606132&r1=606131&r2=606132&view=diff
==============================================================================
--- incubator/tika/trunk/src/test/java/org/apache/tika/mime/MimeTypesTest.java (original)
+++ incubator/tika/trunk/src/test/java/org/apache/tika/mime/MimeTypesTest.java Fri Dec 21 00:28:20 2007
@@ -16,6 +16,8 @@
  */
 package org.apache.tika.mime;
 
+import java.io.IOException;
+import java.io.InputStream;
 import junit.framework.TestCase;
 
 public class MimeTypesTest extends TestCase {
@@ -90,6 +92,13 @@
 
         assertTrue(html.isDescendantOf(text));
         assertFalse(text.isDescendantOf(html));
+
+        try {
+            binary.isDescendantOf(null);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
     }
 
     public void testCompareTo() {
@@ -104,6 +113,33 @@
         assertTrue(html.compareTo(binary) > 0);
         assertTrue(html.compareTo(text) > 0);
         assertTrue(html.compareTo(html) == 0);
+
+        try {
+            binary.compareTo(null);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
+    }
+
+    /** Test getMimeType(byte[]) */
+    public void testGetMimeType_byteArray() {
+        try {
+            types.getMimeType((byte[])null);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
+    }
+
+    /** Test getMimeType(InputStream) */
+    public void testGetMimeType_InputStream() throws IOException {
+        try {
+            types.getMimeType((InputStream)null);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
     }
 
 }

Added: incubator/tika/trunk/src/test/java/org/apache/tika/mime/PatternsTest.java
URL: http://svn.apache.org/viewvc/incubator/tika/trunk/src/test/java/org/apache/tika/mime/PatternsTest.java?rev=606132&view=auto
==============================================================================
--- incubator/tika/trunk/src/test/java/org/apache/tika/mime/PatternsTest.java (added)
+++ incubator/tika/trunk/src/test/java/org/apache/tika/mime/PatternsTest.java Fri Dec 21 00:28:20 2007
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.mime;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for {@link Patterns}.
+ *
+ * @version $Id$
+ */
+public class PatternsTest extends TestCase {
+
+    private Patterns patterns;
+    private MimeTypes types;
+    private MimeType text;
+
+    protected void setUp() throws MimeTypeException {
+        patterns = new Patterns();
+        types = new MimeTypes();
+        text = types.forName("text/plain");
+    }
+
+    /** Test add() */
+    public void testAdd() throws MimeTypeException {
+        try {
+            patterns.add(null, text);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
+        try {
+            patterns.add("", null);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
+        try {
+            patterns.add(null, null);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
+    }
+
+    /** Test matches() */
+    public void testMatches() {
+        try {
+            patterns.matches(null);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
+    }
+}