/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// (C) Copyright 2003-2006 Julian Hyde
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
// remberson, Jan 31, 2006
*/

package mondrian.olap;

import mondrian.test.FoodMartTestCase;
import mondrian.olap.*;

public class HierarchyBugTest extends FoodMartTestCase {
    public HierarchyBugTest(String name) {
        super(name);
    }
    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.
     */
    public void testNoHierarchy() {
        String queryString = 
            "select NON EMPTY " +
            "Crossjoin(Hierarchize(Union({[Time].LastSibling}, " +
            "[Time].LastSibling.Children)), " +
            "{[Measures].[Unit Sales],      " +
            "[Measures].[Store Cost]}) ON columns, " +
            "NON EMPTY Hierarchize(Union({[Store].[All Stores]}, " +
            "[Store].[All Stores].Children)) ON rows " +
            "from [Sales]";

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

        String failStr = null;
        int len = query.axes.length;
        System.out.println("HierarchyBugTest.testNoHierarchy: len=" +len);
        for (int i = 0; i < len; i++) {
            Hierarchy[] hs = query.getMdxHierarchiesOnAxis(i);
            if (hs == null) {
                System.out.println("HierarchyBugTest.testNoHierarchy: got null i=" +i);
            } else {
                for (int j = 0; j < hs.length; j++) {
                    Hierarchy h = hs[j];
                    System.out.print("HierarchyBugTest.testNoHierarchy: j=" +j);
                    // This should NEVER be null, but it is.
                    if (h == null) {
                        System.out.println(": got null");
                        failStr = "Got a null Hierarchy, " + 
                            "Should be Time Hierarchy";
                    } else {
                        System.out.println(": h=" +h.getName());
                    }
                }
            }
        }       
        if (failStr != null) {
            fail (failStr);
        }
    }
}

@@ -1915,83 +1915,8 @@
 th;%0A
-        System.out.println(%22HierarchyBugTest.testNoHierarchy: len=%22 +len);%0A
     
@@ -1947,24 +1947,24 @@
 len; i++) %7B%0A
+
             
@@ -2048,96 +2048,8 @@
 ) %7B%0A
-                System.out.println(%22HierarchyBugTest.testNoHierarchy: got null i=%22 +i);%0A
     
@@ -2164,89 +2164,8 @@
 j%5D;%0A
-                    System.out.print(%22HierarchyBugTest.testNoHierarchy: j=%22 +j);%0A
     
@@ -2262,66 +2262,8 @@
 ) %7B%0A
-                        System.out.println(%22: got null%22);%0A
     
@@ -2320,16 +2320,16 @@
 y, %22 + %0A
+
         
@@ -2380,102 +2380,8 @@
 y%22;%0A
-                    %7D else %7B%0A                        System.out.println(%22: h=%22 +h.getName());%0A
     

MONDRIAN Remove print statements that showed what the error was.