Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MONDRIAN-1126] - tests demonstrate the issue isn't reproduced #620

Merged
merged 1 commit into from
Mar 3, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[MONDRIAN-1126] - tests demonstrate the issue isn't reproduced
  • Loading branch information
YuryBY committed Mar 3, 2016
commit 11200623d95a85669a3a2fe02502c80bf6a0c5ec
274 changes: 259 additions & 15 deletions testsrc/main/mondrian/olap/HierarchyBugTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
// You must accept the terms of that agreement to use this software.
//
// Copyright (C) 2003-2005 Julian Hyde
// Copyright (C) 2005-2011 Pentaho
// Copyright (C) 2005-2016 Pentaho
// All Rights Reserved.
//
// remberson, Jan 31, 2006
*/

package mondrian.olap;

import mondrian.test.FoodMartTestCase;
import mondrian.test.TestContext;

import org.olap4j.*;

import java.sql.SQLException;
import java.util.List;

public class HierarchyBugTest extends FoodMartTestCase {
public HierarchyBugTest(String name) {
Expand All @@ -23,18 +28,18 @@ public HierarchyBugTest() {
super();
}

/*
This is code that demonstrates a bug that appears when using
JPivot with the current version of Mondrian. With the previous
version of Mondrian (and JPivot), pre compilation Mondrian,
this was not a bug (or at least Mondrian did not have a null
hierarchy).
Here the Time dimension is not returned in axis == 0, rather
null is returned. This causes a NullPointer exception in JPivot
when it tries to access the (null) hierarchy's name.
If the Time hierarchy is miss named in the query string, then
the parse ought to pick it up.
*/
/**
* This is code that demonstrates a bug that appears when using
* JPivot with the current version of Mondrian. With the previous
* version of Mondrian (and JPivot), pre compilation Mondrian,
* this was not a bug (or at least Mondrian did not have a null
* hierarchy).
* Here the Time dimension is not returned in axis == 0, rather
* null is returned. This causes a NullPointer exception in JPivot
* when it tries to access the (null) hierarchy's name.
* If the Time hierarchy is miss named in the query string, then
* the parse ought to pick it up.
**/
public void testNoHierarchy() {
String queryString =
"select NON EMPTY "
Expand All @@ -48,7 +53,6 @@ public void testNoHierarchy() {

Connection conn = getConnection();
Query query = conn.parseQuery(queryString);
Result result = conn.execute(query);

String failStr = null;
int len = query.getAxes().length;
Expand All @@ -72,6 +76,246 @@ public void testNoHierarchy() {
fail(failStr);
}
}

/**
* Test cases for <a href="http://jira.pentaho.com/browse/MONDRIAN-1126">
* MONDRIAN-1126:
* member getHierarchy vs. level.getHierarchy differences in Time Dimension
* </a>
*/
public void testNamesIdentitySsasCompatibleTimeHierarchy() {
propSaver.set(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YuryBY Running this test by itself will fail. The [Time.Weekly] reference you have below is not resolvable using SSAS compatible naming (which would use [Time].[Weekly].

I'm guessing this succeeds when running the full test class because there's a sequence issue with the property value not applied. Possibly the test is loading an already cached schema, which had the default properties set from an earlier test execution.

MondrianProperties.instance().SsasCompatibleNaming, true);
String mdxTime = "SELECT\n"
+ " [Measures].[Unit Sales] ON COLUMNS,\n"
+ " [Time].[Time].[Year].Members ON ROWS\n"
+ "FROM [Sales]";
Result resultTime = executeQuery(mdxTime);
verifyMemberLevelNamesIdentityMeasureAxis(
resultTime.getAxes()[0], "[Measures]");
verifyMemberLevelNamesIdentityDimAxis(
resultTime.getAxes()[1], "[Time]");
flushContextSchemaCache();
}

public void testNamesIdentitySsasCompatibleWeeklyHierarchy() {
propSaver.set(
MondrianProperties.instance().SsasCompatibleNaming, true);
String mdxWeekly = "SELECT\n"
+ " [Measures].[Unit Sales] ON COLUMNS,\n"
+ " [Time].[Weekly].[Year].Members ON ROWS\n"
+ "FROM [Sales]";
Result resultWeekly =
getTestContext().withFreshConnection().executeQuery(mdxWeekly);
verifyMemberLevelNamesIdentityMeasureAxis(
resultWeekly.getAxes()[0], "[Measures]");
verifyMemberLevelNamesIdentityDimAxis(
resultWeekly.getAxes()[1], "[Time].[Weekly]");
flushContextSchemaCache();
}

public void testNamesIdentitySsasInCompatibleTimeHierarchy() {
// SsasCompatibleNaming defaults to false
String mdxTime = "SELECT\n"
+ " [Measures].[Unit Sales] ON COLUMNS,\n"
+ " [Time].[Year].Members ON ROWS\n"
+ "FROM [Sales]";
Result resultTime = executeQuery(mdxTime);
verifyMemberLevelNamesIdentityMeasureAxis(
resultTime.getAxes()[0], "[Measures]");
verifyMemberLevelNamesIdentityDimAxis(
resultTime.getAxes()[1], "[Time]");
flushContextSchemaCache();
}

public void testNamesIdentitySsasInCompatibleWeeklyHierarchy() {
String mdxWeekly = "SELECT\n"
+ " [Measures].[Unit Sales] ON COLUMNS,\n"
+ " [Time.Weekly].[Year].Members ON ROWS\n"
+ "FROM [Sales]";
Result resultWeekly =
getTestContext().withFreshConnection().executeQuery(mdxWeekly);
verifyMemberLevelNamesIdentityMeasureAxis(
resultWeekly.getAxes()[0], "[Measures]");
verifyMemberLevelNamesIdentityDimAxis(
resultWeekly.getAxes()[1], "[Time.Weekly]");
flushContextSchemaCache();
}

private String verifyMemberLevelNamesIdentityMeasureAxis(
Axis axis, String expected)
{
OlapElement unitSales =
axis.getPositions().get(0).get(0);
String unitSalesHierarchyName =
unitSales.getHierarchy().getUniqueName();
assertEquals(expected, unitSalesHierarchyName);
return unitSalesHierarchyName;
}

private void verifyMemberLevelNamesIdentityDimAxis(
Axis axis, String expected)
{
Member year1997 = axis.getPositions().get(0).get(0);
String year1997HierarchyName = year1997.getHierarchy().getUniqueName();
assertEquals(expected, year1997HierarchyName);
Level year = year1997.getLevel();
String yearHierarchyName = year.getHierarchy().getUniqueName();
assertEquals(year1997HierarchyName, yearHierarchyName);
}

public void testNamesIdentitySsasCompatibleOlap4j() throws SQLException {
propSaver.set(
MondrianProperties.instance().SsasCompatibleNaming, true);
verifyLevelMemberNamesIdentityOlap4jTimeHierarchy();
}

public void testNamesIdentitySsasInCompatibleOlap4j() throws SQLException {
// SsasCompatibleNaming defaults to false
verifyLevelMemberNamesIdentityOlap4jTimeHierarchy();
}

private void verifyLevelMemberNamesIdentityOlap4jTimeHierarchy()
throws SQLException
{
// essential here, in time hierarchy, is hasAll="false"
// so that we expect "[Time]"
String mdx = "SELECT\n"
+ " [Measures].[Unit Sales] ON COLUMNS,\n"
+ " [Time].[Time].[Year].Members ON ROWS\n"
+ "FROM [Sales]";
verifyLevelMemberNamesIdentityOlap4j(mdx, getTestContext(), "[Time]");
}

public void testNamesIdentitySsasCompatibleOlap4jWeekly()
throws SQLException
{
propSaver.set(
MondrianProperties.instance().SsasCompatibleNaming, true);
String mdx = "SELECT\n"
+ " [Measures].[Unit Sales] ON COLUMNS,\n"
+ " [Time].[Weekly].[Year].Members ON ROWS\n"
+ "FROM [Sales]";
verifyLevelMemberNamesIdentityOlap4j(
mdx, getTestContext(), "[Time].[Weekly]");
}

public void testNamesIdentitySsasInCompatibleOlap4jWeekly()
throws SQLException
{
// SsasCompatibleNaming defaults to false
String mdx = "SELECT\n"
+ " [Measures].[Unit Sales] ON COLUMNS,\n"
+ " [Time.Weekly].[Year].Members ON ROWS\n"
+ "FROM [Sales]";
verifyLevelMemberNamesIdentityOlap4j(
mdx, getTestContext(), "[Time.Weekly]");
}

public void testNamesIdentitySsasCompatibleOlap4jDateDim()
throws SQLException
{
propSaver.set(
MondrianProperties.instance().SsasCompatibleNaming, true);
verifyMemberLevelNamesIdentityOlap4jDateDim();
}

public void testNamesSsasInCompatibleOlap4jDateDim()
throws SQLException
{
// SsasCompatibleNaming defaults to false
verifyMemberLevelNamesIdentityOlap4jDateDim();
}

private void verifyMemberLevelNamesIdentityOlap4jDateDim()
throws SQLException
{
String mdx =
"SELECT\n"
+ " [Measures].[Unit Sales] ON COLUMNS,\n"
+ " [Date].[Date].[Year].Members ON ROWS\n"
+ "FROM [Sales]";
TestContext context = getTestContext();
String dateDim =
"<Dimension name=\"Date\" type=\"TimeDimension\" foreignKey=\"time_id\">\n"
+ " <Hierarchy hasAll=\"false\" primaryKey=\"time_id\">\n"
+ " <Table name=\"time_by_day\"/>\n"
+ " <Level name=\"Year\" column=\"the_year\" type=\"Numeric\" uniqueMembers=\"true\"\n"
+ " levelType=\"TimeYears\"/>\n"
+ " <Level name=\"Quarter\" column=\"quarter\" uniqueMembers=\"false\"\n"
+ " levelType=\"TimeQuarters\"/>\n"
+ " <Level name=\"Month\" column=\"month_of_year\" uniqueMembers=\"false\" type=\"Numeric\"\n"
+ " levelType=\"TimeMonths\"/>\n"
+ " </Hierarchy>\n"
+ " </Dimension>";
context = context.createSubstitutingCube("Sales", dateDim);
verifyLevelMemberNamesIdentityOlap4j(mdx, context, "[Date]");
}

public void testNamesIdentitySsasCompatibleOlap4jDateWeekly()
throws SQLException
{
propSaver.set(
MondrianProperties.instance().SsasCompatibleNaming, true);
String mdx = "SELECT\n"
+ " [Measures].[Unit Sales] ON COLUMNS,\n"
+ " [Date].[Weekly].[Year].Members ON ROWS\n"
+ "FROM [Sales]";
verifyMemberLevelNamesIdentityOlap4jWeekly(mdx, "[Date].[Weekly]");
}

public void testNamesIdentitySsasInCompatibleOlap4jDateDim()
throws SQLException
{
// SsasCompatibleNaming defaults to false
String mdx = "SELECT\n"
+ " [Measures].[Unit Sales] ON COLUMNS,\n"
+ " [Date.Weekly].[Year].Members ON ROWS\n"
+ "FROM [Sales]";
verifyMemberLevelNamesIdentityOlap4jWeekly(mdx, "[Date.Weekly]");
}

private void verifyMemberLevelNamesIdentityOlap4jWeekly(
String mdx, String expected) throws SQLException
{
TestContext context = getTestContext();
String dateDim =
"<Dimension name=\"Date\" type=\"TimeDimension\" foreignKey=\"time_id\">\n"
+ " <Hierarchy hasAll=\"true\" name=\"Weekly\" primaryKey=\"time_id\">\n"
+ " <Table name=\"time_by_day\"/>\n"
+ " <Level name=\"Year\" column=\"the_year\" type=\"Numeric\" uniqueMembers=\"true\"\n"
+ " levelType=\"TimeYears\"/>\n"
+ " <Level name=\"Week\" column=\"week_of_year\" type=\"Numeric\" uniqueMembers=\"false\"\n"
+ " levelType=\"TimeWeeks\"/>\n"
+ " <Level name=\"Day\" column=\"day_of_month\" uniqueMembers=\"false\" type=\"Numeric\"\n"
+ " levelType=\"TimeDays\"/>\n"
+ " </Hierarchy>\n"
+ " </Dimension>";
context = context.createSubstitutingCube("Sales", dateDim);
verifyLevelMemberNamesIdentityOlap4j(mdx, context, expected);
}

private void verifyLevelMemberNamesIdentityOlap4j(
String mdx, TestContext context, String expected) throws SQLException
{
CellSet result = context.executeOlap4jQuery(mdx);

List<org.olap4j.Position> positions =
result.getAxes().get(1).getPositions();
org.olap4j.metadata.Member year1997 =
positions.get(0).getMembers().get(0);
String year1997HierarchyName = year1997.getHierarchy().getUniqueName();
assertEquals(expected, year1997HierarchyName);

org.olap4j.metadata.Level year = year1997.getLevel();
String yearHierarchyName = year.getHierarchy().getUniqueName();
assertEquals(year1997HierarchyName, yearHierarchyName);
flushContextSchemaCache();
}

private void flushContextSchemaCache() {
getTestContext().flushSchemaCache();
}
}

// End HierarchyBugTest.java