Return-Path: Delivered-To: apmail-cayenne-commits-archive@www.apache.org Received: (qmail 82785 invoked from network); 30 Jun 2010 07:53:14 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 30 Jun 2010 07:53:14 -0000 Received: (qmail 63143 invoked by uid 500); 30 Jun 2010 07:53:14 -0000 Delivered-To: apmail-cayenne-commits-archive@cayenne.apache.org Received: (qmail 63116 invoked by uid 500); 30 Jun 2010 07:53:13 -0000 Mailing-List: contact commits-help@cayenne.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cayenne.apache.org Delivered-To: mailing list commits@cayenne.apache.org Received: (qmail 63108 invoked by uid 99); 30 Jun 2010 07:53:12 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 30 Jun 2010 07:53:12 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 30 Jun 2010 07:53:08 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 23B8723889E5; Wed, 30 Jun 2010 07:51:45 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@cayenne.apache.org From: aadamchik@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100630075145.23B8723889E5@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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("<"); 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 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 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 = ""; + assertEquals("<a>", Util.encodeXmlAttribute(unencoded)); + } + + public void testEncodeXmlAttribute3() throws Exception { + String unencoded = "a&b"; + assertEquals("a&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)); + } + }