[hibernate-commits] Hibernate SVN: r16093 - in core/branches/Branch_3_3/core/src: test/java/org/hibernate and 1 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Thu Mar 5 23:48:32 EST 2009
Author: steve.ebersole at jboss.com
Date: 2009-03-05 23:48:32 -0500 (Thu, 05 Mar 2009)
New Revision: 16093
Added:
core/branches/Branch_3_3/core/src/test/java/org/hibernate/util/
core/branches/Branch_3_3/core/src/test/java/org/hibernate/util/StringHelperTest.java
Modified:
core/branches/Branch_3_3/core/src/main/java/org/hibernate/util/StringHelper.java
Log:
HHH-3800 - Allow chopping of class names in various logging scenarios
Modified: core/branches/Branch_3_3/core/src/main/java/org/hibernate/util/StringHelper.java
===================================================================
--- core/branches/Branch_3_3/core/src/main/java/org/hibernate/util/StringHelper.java 2009-03-06 03:01:35 UTC (rev 16092)
+++ core/branches/Branch_3_3/core/src/main/java/org/hibernate/util/StringHelper.java 2009-03-06 04:48:32 UTC (rev 16093)
@@ -99,13 +99,16 @@
public static String[] replace(String templates[], String placeholder, String replacement) {
String[] result = new String[templates.length];
for ( int i =0; i<templates.length; i++ ) {
- result[i] = replace( templates[i], placeholder, replacement );;
+ result[i] = replace( templates[i], placeholder, replacement );
}
return result;
}
public static String replace(String template, String placeholder, String replacement, boolean wholeWords) {
- int loc = template == null ? -1 : template.indexOf( placeholder );
+ if ( template == null ) {
+ return template; // returnign null!
+ }
+ int loc = template.indexOf( placeholder );
if ( loc < 0 ) {
return template;
}
@@ -125,7 +128,10 @@
public static String replaceOnce(String template, String placeholder, String replacement) {
- int loc = template == null ? -1 : template.indexOf( placeholder );
+ if ( template == null ) {
+ return template; // returnign null!
+ }
+ int loc = template.indexOf( placeholder );
if ( loc < 0 ) {
return template;
}
@@ -171,6 +177,9 @@
* @return The collapsed name.
*/
public static String collapse(String name) {
+ if ( name == null ) {
+ return null;
+ }
int breakPoint = name.lastIndexOf( '.' );
if ( breakPoint < 0 ) {
return name;
@@ -208,12 +217,29 @@
* @return The name itself, or the partially unqualified form if it begins with the qualifier base.
*/
public static String partiallyUnqualify(String name, String qualifierBase) {
- if ( ! name.startsWith( qualifierBase ) ) {
+ if ( name == null || ! name.startsWith( qualifierBase ) ) {
return name;
}
return name.substring( qualifierBase.length() + 1 ); // +1 to include the following '.'
}
+ /**
+ * Cross between {@link #collapse} and {@link #partiallyUnqualify}. Functions much like {@link #collapse}
+ * except that only the qualifierBase is collapsed. For example, with a base of 'org.hibernate' the name
+ * 'org.hibernate.util.StringHelper' would become 'o.h.util.StringHelper'.
+ *
+ * @param name The (potentially) qualified name.
+ * @param qualifierBase The qualifier base.
+ *
+ * @return The name itself if it does not begin with the qualifierBase, or the properly collapsed form otherwise.
+ */
+ public static String collapseQualifierBase(String name, String qualifierBase) {
+ if ( name == null || ! name.startsWith( qualifierBase ) ) {
+ return collapse( name );
+ }
+ return collapseQualifier( qualifierBase, true ) + name.substring( qualifierBase.length() );
+ }
+
public static String[] suffix(String[] columns, String suffix) {
if ( suffix == null ) return columns;
String[] qualified = new String[columns.length];
@@ -391,10 +417,13 @@
}
/**
- * Generate a nice alias for the given class name or collection role
- * name and unique integer. Subclasses of Loader do <em>not</em> have
- * to use aliases of this form.
- * @return an alias of the form <tt>foo1_</tt>
+ * Generate a nice alias for the given class name or collection role name and unique integer. Subclasses of
+ * Loader do <em>not</em> have to use aliases of this form.
+ *
+ * @param description The base name (usually an entity-name or collection-role)
+ * @param unique A uniquing value
+ *
+ * @return an alias of the form <samp>foo1_</samp>
*/
public static String generateAlias(String description, int unique) {
return generateAliasRoot(description) +
Added: core/branches/Branch_3_3/core/src/test/java/org/hibernate/util/StringHelperTest.java
===================================================================
--- core/branches/Branch_3_3/core/src/test/java/org/hibernate/util/StringHelperTest.java (rev 0)
+++ core/branches/Branch_3_3/core/src/test/java/org/hibernate/util/StringHelperTest.java 2009-03-06 04:48:32 UTC (rev 16093)
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.util;
+
+import junit.framework.TestCase;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class StringHelperTest extends TestCase {
+ private static final String BASE_PACKAGE = "org.hibernate";
+ private static final String STRING_HELPER_FQN = "org.hibernate.util.StringHelper";
+ private static final String STRING_HELPER_NAME = StringHelper.unqualify( STRING_HELPER_FQN );
+
+ public void testNameCollapsing() {
+ assertNull( StringHelper.collapse( null ) );
+ assertEquals( STRING_HELPER_NAME, StringHelper.collapse( STRING_HELPER_NAME ) );
+ assertEquals( "o.h.u.StringHelper", StringHelper.collapse( STRING_HELPER_FQN ) );
+ }
+
+ public void testPartialNameUnqualification() {
+ assertNull( StringHelper.partiallyUnqualify( null, BASE_PACKAGE ) );
+ assertEquals( STRING_HELPER_NAME, StringHelper.partiallyUnqualify( STRING_HELPER_NAME, BASE_PACKAGE ) );
+ assertEquals( "util.StringHelper", StringHelper.partiallyUnqualify( STRING_HELPER_FQN, BASE_PACKAGE ) );
+ }
+
+ public void testBasePackageCollapsing() {
+ assertNull( StringHelper.collapseQualifierBase( null, BASE_PACKAGE ) );
+ assertEquals( STRING_HELPER_NAME, StringHelper.collapseQualifierBase( STRING_HELPER_NAME, BASE_PACKAGE ) );
+ assertEquals( "o.h.util.StringHelper", StringHelper.collapseQualifierBase( STRING_HELPER_FQN, BASE_PACKAGE ) );
+ }
+}
More information about the hibernate-commits
mailing list