From 31702dd106c4d38d22c2cdd1b7cb6cf66e2055c9 Mon Sep 17 00:00:00 2001 From: Julian Hyde Date: Sat, 17 Oct 2009 19:53:39 +0000 Subject: [PATCH] Rename Olap4jUtil.uniqueNameToStringArray, and add unit test. git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@288 c6a108a4-781c-0410-a6c6-c2d559e19af0 --- .../driver/xmla/XmlaOlap4jConnection.java | 4 +-- src/org/olap4j/impl/Olap4jUtil.java | 15 ++++++-- testsrc/org/olap4j/impl/Olap4jUtilTest.java | 36 +++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/org/olap4j/driver/xmla/XmlaOlap4jConnection.java b/src/org/olap4j/driver/xmla/XmlaOlap4jConnection.java index 1fdab79..11c0c77 100644 --- a/src/org/olap4j/driver/xmla/XmlaOlap4jConnection.java +++ b/src/org/olap4j/driver/xmla/XmlaOlap4jConnection.java @@ -1672,7 +1672,7 @@ XmlaOlap4jHierarchy getHierarchy(Element row) { final String dimensionUniqueName = stringElement(row, "DIMENSION_UNIQUE_NAME"); String dimensionName = - Olap4jUtil.uniqueNameToStringArray(dimensionUniqueName)[0]; + Olap4jUtil.parseUniqueName(dimensionUniqueName).get(0); XmlaOlap4jDimension dimension = getCube(row).dimensions.get(dimensionName); dimension.getHierarchies().size(); @@ -1723,7 +1723,7 @@ public XmlaOlap4jLevel getLevel(Element row) { final String dimensionUniqueName = stringElement(row, "DIMENSION_UNIQUE_NAME"); String dimensionName = - Olap4jUtil.uniqueNameToStringArray(dimensionUniqueName)[0]; + Olap4jUtil.parseUniqueName(dimensionUniqueName).get(0); XmlaOlap4jDimension dimension = getCube(row).dimensions.get(dimensionName); for (Hierarchy hierarchyInit : dimension.getHierarchies()) { diff --git a/src/org/olap4j/impl/Olap4jUtil.java b/src/org/olap4j/impl/Olap4jUtil.java index d64b8d4..b8abb8d 100644 --- a/src/org/olap4j/impl/Olap4jUtil.java +++ b/src/org/olap4j/impl/Olap4jUtil.java @@ -351,7 +351,18 @@ public static RuntimeException needToImplement(Object o) { throw new UnsupportedOperationException("need to implement " + o); } - public static String[] uniqueNameToStringArray(String uniqueName) { + /** + * Parses a unique name. + * + *

For example, {@code uniqueNameToStringArray("[foo].[bar]")} returns + * {@code ["foo", "bar"]}. + * + * @see org.olap4j.mdx.IdentifierNode#parseIdentifier(String) + * + * @param uniqueName Unique name + * @return Parsed unique name + */ + public static List parseUniqueName(String uniqueName) { List trail = new ArrayList(); Pattern regex = Pattern.compile("([^\\[\\]\\.]*)"); Matcher matcher = regex.matcher(uniqueName); @@ -361,7 +372,7 @@ public static String[] uniqueNameToStringArray(String uniqueName) { trail.add(match); } } - return trail.toArray(new String[trail.size()]); + return trail; } /** diff --git a/testsrc/org/olap4j/impl/Olap4jUtilTest.java b/testsrc/org/olap4j/impl/Olap4jUtilTest.java index 0094b14..bc26e8b 100644 --- a/testsrc/org/olap4j/impl/Olap4jUtilTest.java +++ b/testsrc/org/olap4j/impl/Olap4jUtilTest.java @@ -235,6 +235,42 @@ public void testUnmodifiableArrayList() { assertEquals(arrayList, list3); assertEquals(arrayList.hashCode(), list3.hashCode()); } + + /** + * Unit test for {@link Olap4jUtil#parseUniqueName(String)}. + */ + public void testUniqueNameToStringArray() { + List a; + + a = Olap4jUtil.parseUniqueName("foo.bar"); + assertEquals(2, a.size()); + assertEquals("foo", a.get(0)); + assertEquals("bar", a.get(1)); + + // with spaces + a = Olap4jUtil.parseUniqueName("[foo bar].[baz]"); + assertEquals(2, a.size()); + assertEquals("foo bar", a.get(0)); + assertEquals("baz", a.get(1)); + + // with dots + a = Olap4jUtil.parseUniqueName("[foo.bar].[baz]"); + assertEquals(3, a.size()); + assertEquals("foo", a.get(0)); + assertEquals("bar", a.get(1)); + assertEquals("baz", a.get(2)); + + // Unique names can have '&'s in them. I'm not sure that this is the + // behavior we want, but this test at least documents the current + // behavior. + a = Olap4jUtil.parseUniqueName("[customers].&[baz]&[2]"); + assertEquals(5, a.size()); + assertEquals("customers", a.get(0)); + assertEquals("&", a.get(1)); + assertEquals("baz", a.get(2)); + assertEquals("&", a.get(3)); + assertEquals("2", a.get(4)); + } } // End Olap4jUtilTest.java