.classpath |
1
modules/enterprise/binding/src/main/java/org/rhq/bindings/output/TabularWriter.java |
94 +++++++---
modules/enterprise/binding/src/test/java/org/rhq/bindings/TabularWriterTest.java |
58 +++++-
3 files changed, 126 insertions(+), 27 deletions(-)
New commits:
commit f80179210e18065e5d32850726f9b79826dd669b
Author: John Mazzitelli <mazz(a)redhat.com>
Date: Tue Nov 29 17:18:02 2011 -0500
[BZ 756086] allow CLI to support printing of arrays (primitive and object). This only
supports single-dimension arrays but none of our APIs return multi-dim arrays AFAIK
diff --git
a/modules/enterprise/binding/src/main/java/org/rhq/bindings/output/TabularWriter.java
b/modules/enterprise/binding/src/main/java/org/rhq/bindings/output/TabularWriter.java
index 382ce8c..6ead45c 100644
--- a/modules/enterprise/binding/src/main/java/org/rhq/bindings/output/TabularWriter.java
+++ b/modules/enterprise/binding/src/main/java/org/rhq/bindings/output/TabularWriter.java
@@ -79,6 +79,8 @@ public class TabularWriter {
static {
SIMPLE_TYPES.add(Byte.class);
SIMPLE_TYPES.add(Byte.TYPE);
+ SIMPLE_TYPES.add(Character.class);
+ SIMPLE_TYPES.add(Character.TYPE);
SIMPLE_TYPES.add(Short.class);
SIMPLE_TYPES.add(Short.TYPE);
SIMPLE_TYPES.add(Integer.class);
@@ -130,7 +132,48 @@ public class TabularWriter {
}
if (object != null && object.getClass().isArray()) {
- print((Object[]) object);
+ if (!object.getClass().getComponentType().isPrimitive()) {
+ print((Object[]) object);
+ } else {
+ Class<?> oClass = object.getClass();
+ // note: we assume single-dimension arrays!
+ out.println("Array of " +
(oClass.getComponentType().getName()));
+ if (oClass == byte[].class) {
+ for (byte i : (byte[]) object) {
+ this.out.println(i);
+ }
+ } else if (oClass == short[].class) {
+ for (short i : (short[]) object) {
+ this.out.println(i);
+ }
+ } else if (oClass == int[].class) {
+ for (int i : (int[]) object) {
+ this.out.println(i);
+ }
+ } else if (oClass == long[].class) {
+ for (long i : (long[]) object) {
+ this.out.println(i);
+ }
+ } else if (oClass == char[].class) {
+ for (char i : (char[]) object) {
+ this.out.println(i);
+ }
+ } else if (oClass == float[].class) {
+ for (float i : (float[]) object) {
+ this.out.println(i);
+ }
+ } else if (oClass == double[].class) {
+ for (double i : (double[]) object) {
+ this.out.println(i);
+ }
+ } else if (oClass == boolean[].class) {
+ for (boolean i : (boolean[]) object) {
+ this.out.println(i);
+ }
+ } else {
+ this.out.println("*Printing of this data type is not
supported*");
+ }
+ }
return;
}
@@ -285,7 +328,7 @@ public class TabularWriter {
for (Object object : list) {
data[i++][0] = (String) object;
}
-
+ this.print(data);
} else {
if (consistentMaps(list)) {
@@ -318,30 +361,33 @@ public class TabularWriter {
}
}
- headers = new String[pdList.size()];
- data = new String[list.size()][pdList.size()];
+ if (pdList.isEmpty()) {
+ printStrings(list);
+ } else {
+ headers = new String[pdList.size()];
+ data = new String[list.size()][pdList.size()];
- for (PropertyDescriptor pd : pdList) {
- headers[i++] = pd.getName();
- }
- i = 0;
- for (Object row : list) {
- int j = 0;
for (PropertyDescriptor pd : pdList) {
-
- Object val = "?";
- val = invoke(row, pd.getReadMethod());
- if (val == null) {
- data[i][j++] = "";
- } else {
- data[i][j++] = shortVersion(val);
+ headers[i++] = pd.getName();
+ }
+ i = 0;
+ for (Object row : list) {
+ int j = 0;
+ for (PropertyDescriptor pd : pdList) {
+
+ Object val = "?";
+ val = invoke(row, pd.getReadMethod());
+ if (val == null) {
+ data[i][j++] = "";
+ } else {
+ data[i][j++] = shortVersion(val);
+ }
}
+ i++;
}
- i++;
- }
-
- this.print(data);
+ this.print(data);
+ }
}
}
} catch (Exception e) {
@@ -530,13 +576,11 @@ public class TabularWriter {
for (Integer col : columns) {
actualColumnWidths[col] = maxColumnWidth;
}
- }
- else if (columns.size() == 0) {
+ } else if (columns.size() == 0) {
// If the columns list is empty then that means that there is enough
available
// space for each column so we are done.
return;
- }
- else if (extraSpace > 0) {
+ } else if (extraSpace > 0) {
// Since we have extra space, we will go ahead and recalculate the widths
for
// those columns still needing space
int newMaxColumnWidth = (maxColumnWidth + extraSpace) / columns.size();
diff --git
a/modules/enterprise/binding/src/test/java/org/rhq/bindings/TabularWriterTest.java
b/modules/enterprise/binding/src/test/java/org/rhq/bindings/TabularWriterTest.java
index a80d2c2..a69f7bf 100644
--- a/modules/enterprise/binding/src/test/java/org/rhq/bindings/TabularWriterTest.java
+++ b/modules/enterprise/binding/src/test/java/org/rhq/bindings/TabularWriterTest.java
@@ -23,6 +23,8 @@
package org.rhq.bindings;
+import static org.testng.Assert.assertEquals;
+
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
@@ -42,8 +44,6 @@ import org.rhq.core.domain.measurement.AvailabilityType;
import org.rhq.core.domain.resource.Resource;
import org.rhq.core.domain.shared.ResourceBuilder;
-import static org.testng.Assert.*;
-
public class TabularWriterTest {
StringWriter stringWriter;
@@ -57,6 +57,60 @@ public class TabularWriterTest {
}
@Test
+ public void testPrintEntityArray() {
+ User[] users = new User[] { new User(1, "rhqadmin",
"rhqadmin"), new User(222, "name", "pw") };
+
+ writer.print(users);
+
+ assertLineEquals(1, "id password username", "Cannot print entity
array header");
+ // skip line 2 - its the separator "--------------"
+ assertLineEquals(3, "1 rhqadmin rhqadmin", "Cannot print first
entity array item");
+ assertLineEquals(4, "222 pw name ", "Cannot print second
entity array item");
+ }
+
+ /**
+ * This tests printing String arrays via the print(Object) API
+ */
+ @Test
+ public void testPrintStringArray() {
+ String[] objArray = new String[] { new String("first string"), new
String("second string") };
+ Object obj = objArray;
+ writer.print(obj);
+
+ // first two lines are the header and "----" separator
+ assertLineEquals(3, "first string ", "Cannot print first string
array item");
+ assertLineEquals(4, "second string", "Cannot print first string
array item");
+ }
+
+ /**
+ * This tests printing "primitive object" arrays via the print(Object) API
+ */
+ @Test
+ public void testPrintObjectArray() {
+ Integer[] objArray = new Integer[] { new Integer("123"), new
Integer("789") };
+ Object obj = objArray;
+ writer.print(obj);
+
+ String expected = "Array of java.lang.Integer\n123\n789\n";
+ String actual = stringWriter.toString();
+ assertEquals(actual, expected, "Could not print an object Integer
array");
+ }
+
+ /**
+ * This tests printing primitive arrays via the print(Object) API
+ */
+ @Test
+ public void testPrintPrimaryArray() {
+ byte[] primitiveArray = "abc".getBytes();
+ Object obj = primitiveArray;
+ writer.print(obj);
+
+ String expected = "Array of byte\n" + (int) 'a' + '\n'
+ (int) 'b' + '\n' + (int) 'c' + '\n';
+ String actual = stringWriter.toString();
+ assertEquals(actual, expected, "Could not print a primitive byte
array");
+ }
+
+ @Test
public void aByteShouldPrintUnchanged() {
byte value = 1;
commit 4a30174e4cb99e7290b7f8d7b21be19572ed57e6
Author: John Mazzitelli <mazz(a)redhat.com>
Date: Tue Nov 29 17:15:48 2011 -0500
add bindings test module to eclipse classpath
diff --git a/.classpath b/.classpath
index 29ff1d4..24fb54d 100644
--- a/.classpath
+++ b/.classpath
@@ -167,6 +167,7 @@
<classpathentry kind="src"
path="modules/enterprise/server/jar/src/main/filtered-sources/java"/>
<classpathentry kind="src"
path="modules/enterprise/server/jar/target/generated-sources/antlr3"/>
<classpathentry kind="src"
path="modules/enterprise/binding/src/main/java"/>
+ <classpathentry kind="src"
path="modules/enterprise/binding/src/test/java"/>
<classpathentry kind="src"
path="modules/enterprise/gui/coregui/src/main/java"/>
<classpathentry kind="src"
path="modules/enterprise/gui/coregui/target/generated-sources/gwt"/>
<classpathentry kind="src"
path="modules/enterprise/gui/portal-war/src/main/java"/>
Show replies by date