From d8afdcc3aafcb5ea7ce338d1c7990784c7156403 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Mon, 24 Nov 2008 15:29:13 +0000
Subject: [PATCH] LMS-641 MethodUtils with tests introduced

SVN: 9087
---
 .../cisd/common/utilities/MethodUtils.java    | 50 +++++++++++++++++++
 .../common/utilities/MethodUtilsTest.java     | 48 ++++++++++++++++++
 2 files changed, 98 insertions(+)
 create mode 100644 common/source/java/ch/systemsx/cisd/common/utilities/MethodUtils.java
 create mode 100644 common/sourceTest/java/ch/systemsx/cisd/common/utilities/MethodUtilsTest.java

diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/MethodUtils.java b/common/source/java/ch/systemsx/cisd/common/utilities/MethodUtils.java
new file mode 100644
index 00000000000..fa24ac8d3b2
--- /dev/null
+++ b/common/source/java/ch/systemsx/cisd/common/utilities/MethodUtils.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2008 ETH Zuerich, CISD
+ *
+ * Licensed 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 ch.systemsx.cisd.common.utilities;
+
+import java.lang.reflect.Method;
+
+/**
+ * Functions around {@link Method}.
+ *
+ * @author Franz-Josef Elmer
+ */
+public class MethodUtils
+{
+    private MethodUtils()
+    {
+    }
+
+    /**
+     * Returns the specified method as string which shows method name and parameter types
+     * in a way which more compact than {@link Method#toString()}.
+     */
+    public static String toString(Method method)
+    {
+        StringBuilder builder = new StringBuilder(method.getName()).append('(');
+        Class<?>[] parameterTypes = method.getParameterTypes();
+        for (int i = 0; i < parameterTypes.length; i++)
+        {
+            builder.append(parameterTypes[i].getSimpleName());
+            if (i < parameterTypes.length - 1)
+            {
+                builder.append(", ");
+            }
+        }
+        return builder.append(')').toString();
+    }
+}
diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/MethodUtilsTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/MethodUtilsTest.java
new file mode 100644
index 00000000000..174333567f8
--- /dev/null
+++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/MethodUtilsTest.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2008 ETH Zuerich, CISD
+ *
+ * Licensed 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 ch.systemsx.cisd.common.utilities;
+
+import org.testng.AssertJUnit;
+import org.testng.annotations.Test;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+public class MethodUtilsTest extends AssertJUnit
+{
+    @Test
+    public void testMethodWithNoParameters() throws Exception
+    {
+        assertEquals("toString()", MethodUtils.toString(String.class.getMethod("toString")));
+    }
+    
+    @Test
+    public void testMethodWithOneParameter() throws Exception
+    {
+        assertEquals("startsWith(String)", MethodUtils.toString(String.class.getMethod(
+                "startsWith", String.class)));
+    }
+    
+    @Test
+    public void testMethodWithTwoParameters() throws Exception
+    {
+        assertEquals("split(String, int)", MethodUtils.toString(String.class.getMethod(
+                "split", String.class, Integer.TYPE)));
+    }
+}
-- 
GitLab