cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r959220 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/access/jdbc/ main/java/org/apache/cayenne/util/ test/java/org/apache/cayenne/util/
Date Wed, 30 Jun 2010 07:51:45 GMT
Author: aadamchik
Date: Wed Jun 30 07:51:44 2010
New Revision: 959220

URL: http://svn.apache.org/viewvc?rev=959220&view=rev
Log:
optimizing Util.stripLineBreaks.

* 'stripLineBreaks' invocations show up on SQLTemplate execution profile, so some tweaking
should shave off a ms or two
* refactored UtilsTest

Removed:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/UtilExtTest.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/UtilTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java?rev=959220&r1=959219&r2=959220&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
Wed Jun 30 07:51:44 2010
@@ -317,7 +317,7 @@ public class SQLTemplateAction implement
 
         // note that we MUST convert line breaks to spaces. On some databases (DB2)
         // queries with breaks simply won't run; the rest are affected by CAY-726.
-        return Util.stripLineBreaks(sql, " ");
+        return Util.stripLineBreaks(sql, ' ');
     }
 
     /**

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java?rev=959220&r1=959219&r2=959220&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/Util.java
Wed Jun 30 07:51:44 2010
@@ -452,10 +452,10 @@ public class Util {
     }
 
     /**
-     * Strips "\n", "\r\n", "\r" from the argument string.
-     * 
+     * @deprecated since 3.1 in favor of {@link #stripLineBreaks(String, char)}.
      * @since 1.2
      */
+    @Deprecated
     public static String stripLineBreaks(String string, String replaceWith) {
         if (isEmptyString(string)) {
             return string;
@@ -485,20 +485,62 @@ public class Util {
     }
 
     /**
+     * Strips "\n", "\r\n", "\r" from the argument string, replacing them with a provided
+     * character.
+     * 
+     * @since 3.1
+     */
+    public static String stripLineBreaks(String string, char replaceWith) {
+
+        if (string == null) {
+            return null;
+        }
+
+        int len = string.length();
+        char[] buffer = new char[len];
+        boolean matched = false;
+
+        int j = 0;
+        for (int i = 0; i < len; i++, j++) {
+            char c = string.charAt(i);
+
+            // skip \n, \r, \r\n
+            if (c == '\n' || c == '\r') {
+                
+                matched = true;
+
+                // do lookahead
+                if (i + 1 < len && string.charAt(i + 1) == '\n') {
+                    i++;
+                }
+
+                buffer[j] = replaceWith;
+            }
+            else {
+                buffer[j] = c;
+            }
+        }
+
+        return matched ? new String(buffer, 0, j) : string;
+    }
+
+    /**
      * Encodes a string so that it can be used as an attribute value in an XML document.
      * Will do conversion of the greater/less signs, quotes and ampersands.
      */
-    public static String encodeXmlAttribute(String str) {
-        if (str == null)
+    public static String encodeXmlAttribute(String string) {
+        if (string == null) {
             return null;
+        }
 
-        int len = str.length();
-        if (len == 0)
-            return str;
+        int len = string.length();
+        if (len == 0) {
+            return string;
+        }
 
         StringBuilder encoded = new StringBuilder();
         for (int i = 0; i < len; i++) {
-            char c = str.charAt(i);
+            char c = string.charAt(i);
             if (c == '<')
                 encoded.append("&lt;");
             else if (c == '\"')

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/UtilTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/UtilTest.java?rev=959220&r1=959219&r2=959220&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/UtilTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/UtilTest.java
Wed Jun 30 07:51:44 2010
@@ -28,6 +28,8 @@ import java.util.Map;
 
 import junit.framework.TestCase;
 
+import org.apache.cayenne.CayenneException;
+
 public class UtilTest extends TestCase {
 
     private File fTmpFileInCurrentDir;
@@ -49,7 +51,7 @@ public class UtilTest extends TestCase {
     }
 
     @Override
-    protected void tearDown() throws java.lang.Exception {
+    protected void tearDown() throws Exception {
         if (!fTmpFileInCurrentDir.delete())
             throw new Exception("Error deleting temporary file: " + fTmpFileInCurrentDir);
 
@@ -76,7 +78,7 @@ public class UtilTest extends TestCase {
                 "1", "2"
         };
 
-        Map map = Util.toMap(keys, values);
+        Map<Object, Object> map = Util.toMap(keys, values);
         assertEquals(2, map.size());
         assertEquals("1", map.get("a"));
         assertEquals("2", map.get("b"));
@@ -85,7 +87,7 @@ public class UtilTest extends TestCase {
         map.put("c", "3");
 
         // check that two null maps work
-        Map emptyMap = Util.toMap(null, new Object[0]);
+        Map<Object, Object> emptyMap = Util.toMap(null, new Object[0]);
         assertTrue(emptyMap.isEmpty());
         emptyMap.put("key1", "value1");
 
@@ -103,14 +105,21 @@ public class UtilTest extends TestCase {
     }
 
     public void testStripLineBreaks() {
+
+        // no breaks
+        assertEquals("PnMusdkams34 H AnYtk M", Util.stripLineBreaks(
+                "PnMusdkams34 H AnYtk M",
+                'A'));
+
         // Windows
-        assertEquals("aAbAc", Util.stripLineBreaks("a\r\nb\r\nc", "A"));
+        assertEquals("TyusdsdsdQaAbAc", Util
+                .stripLineBreaks("TyusdsdsdQa\r\nb\r\nc", 'A'));
 
         // Mac
-        assertEquals("aBbBc", Util.stripLineBreaks("a\rb\rc", "B"));
+        assertEquals("aBbBc", Util.stripLineBreaks("a\rb\rc", 'B'));
 
         // UNIX
-        assertEquals("aCbCc", Util.stripLineBreaks("a\nb\nc", "C"));
+        assertEquals("aCbCc", Util.stripLineBreaks("a\nb\nc", 'C'));
     }
 
     public void testCopyFile() throws java.lang.Exception {
@@ -163,7 +172,7 @@ public class UtilTest extends TestCase {
 
     }
 
-    public void testDeleteFile() throws java.lang.Exception {
+    public void testDeleteFile() throws Exception {
         // delete file
         assertFalse("Temp file "
                 + fTmpFileCopy
@@ -193,11 +202,160 @@ public class UtilTest extends TestCase {
         assertFalse(tmpDir.exists());
     }
 
-    public void testCloneViaSerialization() throws java.lang.Exception {
+    public void testCloneViaSerialization() throws Exception {
         // need a special subclass of Object to make "clone" method public
         MockSerializable o1 = new MockSerializable();
         Object o2 = Util.cloneViaSerialization(o1);
         assertEquals(o1, o2);
         assertTrue(o1 != o2);
     }
+
+    public void testPackagePath1() throws Exception {
+        String expectedPath = "org/apache/cayenne/util";
+        assertEquals(expectedPath, Util.getPackagePath(UtilTest.class.getName()));
+    }
+
+    public void testPackagePath2() throws Exception {
+        // inner class
+        class TmpTest extends Object {
+        }
+
+        String expectedPath = "org/apache/cayenne/util";
+        assertEquals(expectedPath, Util.getPackagePath(TmpTest.class.getName()));
+    }
+
+    public void testPackagePath3() throws Exception {
+        assertEquals("", Util.getPackagePath("ClassWithNoPackage"));
+    }
+
+    public void testIsEmptyString1() throws Exception {
+        assertTrue(Util.isEmptyString(""));
+    }
+
+    public void testIsEmptyString2() throws Exception {
+        assertFalse(Util.isEmptyString("  "));
+    }
+
+    public void testIsEmptyString3() throws Exception {
+        assertTrue(Util.isEmptyString(null));
+    }
+
+    public void testBackslashFix() throws Exception {
+        String strBefore = "abcd\\12345\\";
+        String strAfter = "abcd/12345/";
+        assertEquals(strAfter, Util.substBackslashes(strBefore));
+    }
+
+    public void testNullSafeEquals() throws Exception {
+        // need a special subclass of Object to make "clone" method public
+        class CloneableObject implements Cloneable {
+
+            @Override
+            public Object clone() throws CloneNotSupportedException {
+                return super.clone();
+            }
+
+            @Override
+            public boolean equals(Object obj) {
+                if (obj == null)
+                    return false;
+
+                // for the purpose of this test
+                // all objects of this class considered equal
+                // (since they carry no state)
+                return obj.getClass() == this.getClass();
+            }
+        }
+
+        CloneableObject o1 = new CloneableObject();
+        Object o2 = new Object();
+        Object o3 = o1.clone();
+
+        assertTrue(o3.equals(o1));
+        assertTrue(Util.nullSafeEquals(o1, o1));
+        assertFalse(Util.nullSafeEquals(o1, o2));
+        assertTrue(Util.nullSafeEquals(o1, o3));
+        assertFalse(Util.nullSafeEquals(o1, null));
+        assertFalse(Util.nullSafeEquals(null, o1));
+        assertTrue(Util.nullSafeEquals(null, null));
+    }
+
+    public void testExtractFileExtension1() throws Exception {
+        String fullName = "n.ext";
+        assertEquals("ext", Util.extractFileExtension(fullName));
+    }
+
+    public void testExtractFileExtension2() throws Exception {
+        String fullName = "n";
+        assertNull(Util.extractFileExtension(fullName));
+    }
+
+    public void testExtractFileExtension3() throws Exception {
+        String fullName = ".ext";
+        assertNull(Util.extractFileExtension(fullName));
+    }
+
+    public void testStripFileExtension1() throws Exception {
+        String fullName = "n.ext";
+        assertEquals("n", Util.stripFileExtension(fullName));
+    }
+
+    public void testStripFileExtension2() throws Exception {
+        String fullName = "n";
+        assertEquals("n", Util.stripFileExtension(fullName));
+    }
+
+    public void testStripFileExtension3() throws Exception {
+        String fullName = ".ext";
+        assertEquals(".ext", Util.stripFileExtension(fullName));
+    }
+
+    public void testEncodeXmlAttribute1() throws Exception {
+        String unencoded = "normalstring";
+        assertEquals(unencoded, Util.encodeXmlAttribute(unencoded));
+    }
+
+    public void testEncodeXmlAttribute2() throws Exception {
+        String unencoded = "<a>";
+        assertEquals("&lt;a&gt;", Util.encodeXmlAttribute(unencoded));
+    }
+
+    public void testEncodeXmlAttribute3() throws Exception {
+        String unencoded = "a&b";
+        assertEquals("a&amp;b", Util.encodeXmlAttribute(unencoded));
+    }
+
+    public void testUnwindException1() throws Exception {
+        Throwable e = new Throwable();
+        assertSame(e, Util.unwindException(e));
+    }
+
+    public void testUnwindException2() throws Exception {
+        CayenneException e = new CayenneException();
+        assertSame(e, Util.unwindException(e));
+    }
+
+    public void testUnwindException3() throws Exception {
+        Throwable root = new Throwable();
+        CayenneException e = new CayenneException(root);
+        assertSame(root, Util.unwindException(e));
+    }
+
+    public void testPrettyTrim1() throws Exception {
+        // size is too short, must throw
+        try {
+            Util.prettyTrim("abc", 4);
+        }
+        catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+
+    public void testPrettyTrim2() throws Exception {
+        assertEquals("123", Util.prettyTrim("123", 6));
+        assertEquals("123456", Util.prettyTrim("123456", 6));
+        assertEquals("1...67", Util.prettyTrim("1234567", 6));
+        assertEquals("1...78", Util.prettyTrim("12345678", 6));
+    }
+
 }



Mime
View raw message