http://git-wip-us.apache.org/repos/asf/lens/blob/7c7c86da/lens-cube/src/test/java/org/apache/lens/cube/metadata/TestCubeMetastoreClient.java
----------------------------------------------------------------------
diff --git 
a/lens-cube/src/test/java/org/apache/lens/cube/metadata/TestCubeMetastoreClient.java
 
b/lens-cube/src/test/java/org/apache/lens/cube/metadata/TestCubeMetastoreClient.java
index e5dbde7..c6ce6ad 100644
--- 
a/lens-cube/src/test/java/org/apache/lens/cube/metadata/TestCubeMetastoreClient.java
+++ 
b/lens-cube/src/test/java/org/apache/lens/cube/metadata/TestCubeMetastoreClient.java
@@ -19,9 +19,9 @@
 
 package org.apache.lens.cube.metadata;
 
+import static org.apache.lens.cube.metadata.DateFactory.*;
 import static org.apache.lens.cube.metadata.MetastoreUtil.*;
 import static org.apache.lens.cube.metadata.UpdatePeriod.*;
-import static org.apache.lens.cube.parse.CubeTestSetup.DateOffsetProvider;
 import static org.apache.lens.server.api.util.LensUtil.getHashMap;
 
 import static org.testng.Assert.*;
@@ -35,7 +35,6 @@ import 
org.apache.lens.cube.metadata.timeline.EndsAndHolesPartitionTimeline;
 import org.apache.lens.cube.metadata.timeline.PartitionTimeline;
 import org.apache.lens.cube.metadata.timeline.StoreAllPartitionTimeline;
 import org.apache.lens.cube.metadata.timeline.TestPartitionTimelines;
-import org.apache.lens.cube.parse.TimeRange;
 import org.apache.lens.server.api.error.LensException;
 import org.apache.lens.server.api.util.LensUtil;
 
@@ -100,11 +99,7 @@ public class TestCubeMetastoreClient {
   private static Set<ExprColumn> cubeExpressions = new HashSet<>();
   private static Set<JoinChain> joinChains = new HashSet<>();
   private static Set<ExprColumn> dimExpressions = new HashSet<>();
-  private static DateOffsetProvider dateOffsetProvider = new 
DateOffsetProvider(HOURLY);
 
-  public static Date getDateWithOffset(int i) {
-    return dateOffsetProvider.get(i);
-  }
 
   /**
    * Get the date partition as field schema
@@ -127,7 +122,7 @@ public class TestCubeMetastoreClient {
   private static HashMap<String, Date> getTimePartitionByOffsets(Object... 
args) {
     for (int i = 1; i < args.length; i += 2) {
       if (args[i] instanceof Integer) {
-        args[i] = getDateWithOffset((Integer) args[i]);
+        args[i] = getDateWithOffset(HOURLY, (Integer) args[i]);
       }
     }
     return getHashMap(args);
@@ -169,13 +164,13 @@ public class TestCubeMetastoreClient {
       new FieldSchema("msr4", "bigint", "fourth measure"), "Measure4", null, 
"COUNT", null));
     cubeMeasures.add(new ColumnMeasure(
       new FieldSchema("msrstarttime", "int", "measure with start time"),
-      "Measure With Starttime", null, null, null, getDateWithOffset(0), null, 
null, 0.0, 999999.0));
+      "Measure With Starttime", null, null, null, NOW, null, null, 0.0, 
999999.0));
     cubeMeasures.add(new ColumnMeasure(
       new FieldSchema("msrendtime", "float", "measure with end time"),
-      "Measure With Endtime", null, "SUM", "RS", getDateWithOffset(0), 
getDateWithOffset(0), null));
+      "Measure With Endtime", null, "SUM", "RS", NOW, NOW, null));
     cubeMeasures.add(new ColumnMeasure(
       new FieldSchema("msrcost", "double", "measure with cost"), "Measure With 
cost",
-      null, "MAX", null, getDateWithOffset(0), getDateWithOffset(0), 100.0));
+      null, "MAX", null, NOW, NOW, 100.0));
     cubeMeasures.add(new ColumnMeasure(
       new FieldSchema("msrcost2", "bigint", "measure with cost"),
       "Measure With cost2", null, "MAX", null, null, null, 100.0, 0.0, 
999999999999999999999999999.0));
@@ -234,12 +229,12 @@ public class TestCubeMetastoreClient {
 
     List<CubeDimAttribute> locationHierarchyWithStartTime = new ArrayList<>();
     locationHierarchyWithStartTime.add(new ReferencedDimAtrribute(new 
FieldSchema("zipcode2", "int", "zip"),
-      "Zip refer2", new TableReference("zipdim", "zipcode"), 
getDateWithOffset(0), getDateWithOffset(0),
+      "Zip refer2", new TableReference("zipdim", "zipcode"), NOW, NOW,
       100.0, true, 1000L));
     locationHierarchyWithStartTime.add(new ReferencedDimAtrribute(new 
FieldSchema("cityid2", "int", "city"),
-      "City refer2", new TableReference("citydim", "id"), 
getDateWithOffset(0), null, null));
+      "City refer2", new TableReference("citydim", "id"), NOW, null, null));
     locationHierarchyWithStartTime.add(new ReferencedDimAtrribute(new 
FieldSchema("stateid2", "int", "state"),
-      "state refer2", new TableReference("statedim", "id"), 
getDateWithOffset(0), null, 100.0));
+      "state refer2", new TableReference("statedim", "id"), NOW, null, 100.0));
     locationHierarchyWithStartTime.add(new ReferencedDimAtrribute(new 
FieldSchema("countryid2", "int", "country"),
       "Country refer2", new TableReference("countrydim", "id"), null, null, 
null));
     locationHierarchyWithStartTime.add(new BaseDimAttribute(new 
FieldSchema("regionname2", "string", "region"),
@@ -248,9 +243,10 @@ public class TestCubeMetastoreClient {
     cubeDimensions
       .add(new HierarchicalDimAttribute("location2", "localtion hierarchy2", 
locationHierarchyWithStartTime));
     cubeDimensions.add(new BaseDimAttribute(new FieldSchema("dim1startTime", 
"string", "basedim"),
-      "Dim With starttime", getDateWithOffset(0), null, 100.0));
+      "Dim With starttime", NOW, null, 100.0));
     cubeDimensions.add(new ReferencedDimAtrribute(new FieldSchema("dim2start", 
"string", "ref dim"),
-      "Dim2 with starttime", new TableReference("testdim2", "id"), 
getDateWithOffset(0), getDateWithOffset(0), 100.0));
+      "Dim2 with starttime", new TableReference("testdim2", "id"),
+      NOW, NOW, 100.0));
 
     List<TableReference> multiRefs = new ArrayList<>();
     multiRefs.add(new TableReference("testdim2", "id"));
@@ -260,12 +256,12 @@ public class TestCubeMetastoreClient {
     cubeDimensions.add(new ReferencedDimAtrribute(new FieldSchema("dim3", 
"string", "multi ref dim"), "Dim3 refer",
       multiRefs));
     cubeDimensions.add(new ReferencedDimAtrribute(new FieldSchema("dim3start", 
"string", "multi ref dim"),
-      "Dim3 with starttime", multiRefs, getDateWithOffset(0), null, 100.0));
+      "Dim3 with starttime", multiRefs, NOW, null, 100.0));
 
     cubeDimensions.add(new BaseDimAttribute(new FieldSchema("region", 
"string", "region dim"), "region", null, null,
       null, null, regions));
     cubeDimensions.add(new BaseDimAttribute(new FieldSchema("regionstart", 
"string", "region dim"),
-      "Region with starttime", getDateWithOffset(0), null, 100.0, null, 
regions));
+      "Region with starttime", NOW, null, 100.0, null, regions));
     JoinChain zipCity = new JoinChain("cityFromZip", "Zip City", "zip city 
desc");
     List<TableReference> chain = new ArrayList<>();
     chain.add(new TableReference(cubeName, "zipcode"));
@@ -1317,13 +1313,12 @@ public class TestCubeMetastoreClient {
     StoreAllPartitionTimeline storeAllPartitionTimeline, UpdatePeriod 
updatePeriod,
     int firstOffset, int latestOffset, int... holeOffsets) throws 
LensException {
     Date[] holeDates = new Date[holeOffsets.length];
-    for (int i = 0; i < holeOffsets.length; i++) {
-      holeDates[i] = getDateWithOffset(holeOffsets[i]);
+    for(int i = 0; i < holeOffsets.length; i++) {
+      holeDates[i] = getDateWithOffset(HOURLY, holeOffsets[i]);
     }
     assertTimeline(endsAndHolesPartitionTimeline, storeAllPartitionTimeline, 
updatePeriod,
-      getDateWithOffset(firstOffset), getDateWithOffset(latestOffset), 
holeDates);
+      getDateWithOffset(HOURLY, firstOffset), getDateWithOffset(HOURLY, 
latestOffset), holeDates);
   }
-
   private void assertTimeline(EndsAndHolesPartitionTimeline 
endsAndHolesPartitionTimeline,
     StoreAllPartitionTimeline storeAllPartitionTimeline, UpdatePeriod 
updatePeriod,
     Date first, Date latest, Date... holes) throws LensException {
@@ -1397,10 +1392,10 @@ public class TestCubeMetastoreClient {
     return values;
   }
 
-  private TimePartition[] toPartitionArray(UpdatePeriod updatePeriod, Date... 
dates) throws LensException {
-    TimePartition[] values = new TimePartition[dates.length];
-    for (int i = 0; i < dates.length; i++) {
-      values[i] = TimePartition.of(updatePeriod, dates[i]);
+  private TimePartition[] toPartitionArray(UpdatePeriod updatePeriod, int... 
offsets) throws LensException {
+    TimePartition[] values = new TimePartition[offsets.length];
+    for (int i = 0; i < offsets.length; i++) {
+      values[i] = TimePartition.of(updatePeriod, 
getDateWithOffset(updatePeriod, offsets[i]));
     }
     return values;
   }
@@ -1709,8 +1704,7 @@ public class TestCubeMetastoreClient {
     List<Partition> parts = client.getPartitionsByFilter(storageTableName, 
"dt='latest'");
     assertEquals(1, parts.size());
     assertEquals(TextInputFormat.class.getCanonicalName(), 
parts.get(0).getInputFormatClass().getCanonicalName());
-    
assertEquals(parts.get(0).getParameters().get(getLatestPartTimestampKey("dt")),
-      HOURLY.format(getDateWithOffset(0)));
+    
assertEquals(parts.get(0).getParameters().get(getLatestPartTimestampKey("dt")), 
getDateStringWithOffset(HOURLY, 0));
 
     client.dropPartition(cubeDim.getName(), c1, timeParts, null, HOURLY);
     assertFalse(client.dimPartitionExists(cubeDim.getName(), c1, timeParts));
@@ -1769,7 +1763,7 @@ public class TestCubeMetastoreClient {
     String storageTableName = getFactOrDimtableStorageTableName(dimName, c1);
     assertFalse(client.dimTableLatestPartitionExists(storageTableName));
 
-    Map<String, Date> timePartsNow = getHashMap(getDatePartitionKey(), 
getDateWithOffset(0));
+    Map<String, Date> timePartsNow = getHashMap(getDatePartitionKey(), NOW);
     StoragePartitionDesc sPartSpec0 = new 
StoragePartitionDesc(cubeDim.getName(), timePartsNow, null, HOURLY);
 
     client.addPartition(sPartSpec0, c1);
@@ -1782,7 +1776,7 @@ public class TestCubeMetastoreClient {
     Partition latestPart = parts.get(0);
     assertEquals(latestPart.getInputFormatClass(), TextInputFormat.class);
     assertFalse(latestPart.getCols().contains(newcol));
-    
assertEquals(latestPart.getParameters().get(getLatestPartTimestampKey("dt")), 
HOURLY.format(getDateWithOffset(0)));
+    
assertEquals(latestPart.getParameters().get(getLatestPartTimestampKey("dt")), 
getDateStringWithOffset(HOURLY, 0));
 
     // Partition with different schema
     cubeDim.alterColumn(newcol);
@@ -1805,7 +1799,7 @@ public class TestCubeMetastoreClient {
     latestPart = parts.get(0);
     assertEquals(latestPart.getInputFormatClass(), 
SequenceFileInputFormat.class);
     assertTrue(latestPart.getCols().contains(newcol));
-    
assertEquals(latestPart.getParameters().get(getLatestPartTimestampKey("dt")), 
HOURLY.format(getDateWithOffset(1)));
+    
assertEquals(latestPart.getParameters().get(getLatestPartTimestampKey("dt")), 
getDateStringWithOffset(HOURLY, 1));
 
     // add one more partition
     Map<String, Date> timeParts2 = 
getTimePartitionByOffsets(getDatePartitionKey(), 2);
@@ -1824,7 +1818,7 @@ public class TestCubeMetastoreClient {
     latestPart = parts.get(0);
     assertEquals(latestPart.getInputFormatClass(), TextInputFormat.class);
     assertTrue(latestPart.getCols().contains(newcol));
-    
assertEquals(latestPart.getParameters().get(getLatestPartTimestampKey("dt")), 
HOURLY.format(getDateWithOffset(2)));
+    
assertEquals(latestPart.getParameters().get(getLatestPartTimestampKey("dt")), 
getDateStringWithOffset(HOURLY, 2));
 
     // drop the last added partition
     client.dropPartition(cubeDim.getName(), c1, timeParts2, null, HOURLY);
@@ -1835,8 +1829,7 @@ public class TestCubeMetastoreClient {
     assertEquals(parts.size(), 1);
     latestPart = parts.get(0);
     assertEquals(latestPart.getInputFormatClass(), 
SequenceFileInputFormat.class);
-    
assertEquals(latestPart.getParameters().get(getLatestPartTimestampKey("dt")),
-      HOURLY.format(getDateWithOffset(1)));
+    
assertEquals(latestPart.getParameters().get(getLatestPartTimestampKey("dt")), 
getDateStringWithOffset(HOURLY, 1));
     assertEquals(client.getAllParts(storageTableName).size(), 3);
 
     // drop the first partition, leaving the middle.
@@ -1848,8 +1841,7 @@ public class TestCubeMetastoreClient {
     assertEquals(parts.size(), 1);
     latestPart = parts.get(0);
     assertEquals(latestPart.getInputFormatClass(), 
SequenceFileInputFormat.class);
-    
assertEquals(latestPart.getParameters().get(getLatestPartTimestampKey("dt")),
-      HOURLY.format(getDateWithOffset(1)));
+    
assertEquals(latestPart.getParameters().get(getLatestPartTimestampKey("dt")), 
getDateStringWithOffset(HOURLY, 1));
     assertEquals(client.getAllParts(storageTableName).size(), 2);
 
     client.dropPartition(cubeDim.getName(), c1, timeParts1, null, HOURLY);
@@ -1892,52 +1884,52 @@ public class TestCubeMetastoreClient {
     Map<String, Date> timeParts = new HashMap<>();
     Map<String, String> nonTimeParts = new HashMap<>();
 
-    timeParts.put(getDatePartitionKey(), getDateWithOffset(0));
+    timeParts.put(getDatePartitionKey(), NOW);
     nonTimeParts.put("region", "asia");
     StoragePartitionDesc sPartSpec = new StoragePartitionDesc(dimName, 
timeParts, nonTimeParts, HOURLY);
     client.addPartition(sPartSpec, c3);
-    expectedLatestValues.put("asia", getDateWithOffset(0));
+    expectedLatestValues.put("asia", NOW);
     assertLatestForRegions(storageTableName, expectedLatestValues);
 
-    timeParts.put(getDatePartitionKey(), getDateWithOffset(-1));
+    timeParts.put(getDatePartitionKey(), getDateWithOffset(HOURLY, -1));
     nonTimeParts.put("region", "africa");
     sPartSpec = new StoragePartitionDesc(dimName, timeParts, nonTimeParts, 
HOURLY);
     client.addPartition(sPartSpec, c3);
-    expectedLatestValues.put("asia", getDateWithOffset(0));
-    expectedLatestValues.put("africa", getDateWithOffset(-1));
+    expectedLatestValues.put("asia", NOW);
+    expectedLatestValues.put("africa", getDateWithOffset(HOURLY, -1));
     assertLatestForRegions(storageTableName, expectedLatestValues);
 
-    timeParts.put(getDatePartitionKey(), getDateWithOffset(1));
+    timeParts.put(getDatePartitionKey(), getDateWithOffset(HOURLY, 1));
     nonTimeParts.put("region", "africa");
     sPartSpec = new StoragePartitionDesc(dimName, timeParts, nonTimeParts, 
HOURLY);
     client.addPartition(sPartSpec, c3);
-    expectedLatestValues.put("asia", getDateWithOffset(0));
-    expectedLatestValues.put("africa", getDateWithOffset(1));
+    expectedLatestValues.put("asia", NOW);
+    expectedLatestValues.put("africa", getDateWithOffset(HOURLY, 1));
     assertLatestForRegions(storageTableName, expectedLatestValues);
 
-    timeParts.put(getDatePartitionKey(), getDateWithOffset(3));
+    timeParts.put(getDatePartitionKey(), getDateWithOffset(HOURLY, 3));
     nonTimeParts.put("region", "asia");
     sPartSpec = new StoragePartitionDesc(dimName, timeParts, nonTimeParts, 
HOURLY);
     client.addPartition(sPartSpec, c3);
-    expectedLatestValues.put("asia", getDateWithOffset(3));
-    expectedLatestValues.put("africa", getDateWithOffset(1));
+    expectedLatestValues.put("asia", getDateWithOffset(HOURLY, 3));
+    expectedLatestValues.put("africa", getDateWithOffset(HOURLY, 1));
     assertLatestForRegions(storageTableName, expectedLatestValues);
 
     client.dropPartition(dimName, c3, timeParts, nonTimeParts, HOURLY);
-    expectedLatestValues.put("asia", getDateWithOffset(0));
-    expectedLatestValues.put("africa", getDateWithOffset(1));
+    expectedLatestValues.put("asia", NOW);
+    expectedLatestValues.put("africa", getDateWithOffset(HOURLY, 1));
     assertLatestForRegions(storageTableName, expectedLatestValues);
 
-    timeParts.put(getDatePartitionKey(), getDateWithOffset(0));
+    timeParts.put(getDatePartitionKey(), NOW);
     client.dropPartition(dimName, c3, timeParts, nonTimeParts, HOURLY);
     expectedLatestValues.remove("asia");
     assertLatestForRegions(storageTableName, expectedLatestValues);
 
     nonTimeParts.put("region", "africa");
-    timeParts.put(getDatePartitionKey(), getDateWithOffset(-1));
+    timeParts.put(getDatePartitionKey(), getDateWithOffset(HOURLY, -1));
     assertLatestForRegions(storageTableName, expectedLatestValues);
 
-    timeParts.put(getDatePartitionKey(), getDateWithOffset(3));
+    timeParts.put(getDatePartitionKey(), getDateWithOffset(HOURLY, 3));
     nonTimeParts.remove("africa");
     assertLatestForRegions(storageTableName, expectedLatestValues);
   }
@@ -2016,8 +2008,7 @@ public class TestCubeMetastoreClient {
     String c1TableName = getFactOrDimtableStorageTableName(cubeDim.getName(), 
c1);
     assertEquals(client.getAllParts(c1TableName).size(), 8);
 
-    assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null),
-      toPartitionArray(HOURLY, getDateWithOffset(0), getDateWithOffset(0), 
getDateWithOffset(1)));
+    assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null), 
toPartitionArray(HOURLY, 0, 0, 1));
 
     Map<String, Date> timeParts4 = 
getTimePartitionByOffsets(getDatePartitionKey(), 0, itPart.getName(), 1,
       etPart.getName(), -1);
@@ -2030,8 +2021,7 @@ public class TestCubeMetastoreClient {
     client.addPartitions(Arrays.asList(partSpec4, partSpec5), c1);
 
     assertEquals(client.getAllParts(c1TableName).size(), 10);
-    assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null),
-      toPartitionArray(HOURLY, getDateWithOffset(1), getDateWithOffset(1), 
getDateWithOffset(1)));
+    assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null), 
toPartitionArray(HOURLY, 1, 1, 1));
     Map<String, Date> timeParts6 = 
getTimePartitionByOffsets(getDatePartitionKey(), -2, itPart.getName(), -1,
       etPart.getName(), -2);
     final StoragePartitionDesc partSpec6 = new 
StoragePartitionDesc(cubeDim.getName(), timeParts6, null, HOURLY);
@@ -2046,28 +2036,23 @@ public class TestCubeMetastoreClient {
 
     client.addPartition(partSpec7, c1);
     assertEquals(client.getAllParts(c1TableName).size(), 12);
-    assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null),
-      toPartitionArray(HOURLY, getDateWithOffset(1), getDateWithOffset(1), 
getDateWithOffset(1)));
+    assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null), 
toPartitionArray(HOURLY, 1, 1, 1));
 
     client.dropPartition(cubeDim.getName(), c1, timeParts5, null, HOURLY);
     assertEquals(client.getAllParts(c1TableName).size(), 11);
-    assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null),
-      toPartitionArray(HOURLY, getDateWithOffset(0), getDateWithOffset(1), 
getDateWithOffset(1)));
+    assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null), 
toPartitionArray(HOURLY, 0, 1, 1));
 
     client.dropPartition(cubeDim.getName(), c1, timeParts7, null, HOURLY);
     assertEquals(client.getAllParts(c1TableName).size(), 10);
-    assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null),
-      toPartitionArray(HOURLY, getDateWithOffset(0), getDateWithOffset(1), 
getDateWithOffset(1)));
+    assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null), 
toPartitionArray(HOURLY, 0, 1, 1));
 
     client.dropPartition(cubeDim.getName(), c1, timeParts2, nonTimeSpec, 
HOURLY);
     assertEquals(client.getAllParts(c1TableName).size(), 9);
-    assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null),
-      toPartitionArray(HOURLY, getDateWithOffset(0), getDateWithOffset(1), 
getDateWithOffset(0)));
+    assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null), 
toPartitionArray(HOURLY, 0, 1, 0));
 
     client.dropPartition(cubeDim.getName(), c1, timeParts4, null, HOURLY);
     assertEquals(client.getAllParts(c1TableName).size(), 8);
-    assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null),
-      toPartitionArray(HOURLY, getDateWithOffset(0), getDateWithOffset(0), 
getDateWithOffset(0)));
+    assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null), 
toPartitionArray(HOURLY, 0, 0, 0));
 
     client.dropPartition(cubeDim.getName(), c1, timeParts3, nonTimeSpec, 
HOURLY);
     assertEquals(client.getAllParts(c1TableName).size(), 5);

http://git-wip-us.apache.org/repos/asf/lens/blob/7c7c86da/lens-cube/src/test/java/org/apache/lens/cube/metadata/TestDateUtil.java
----------------------------------------------------------------------
diff --git 
a/lens-cube/src/test/java/org/apache/lens/cube/metadata/TestDateUtil.java 
b/lens-cube/src/test/java/org/apache/lens/cube/metadata/TestDateUtil.java
new file mode 100644
index 0000000..7e239f3
--- /dev/null
+++ b/lens-cube/src/test/java/org/apache/lens/cube/metadata/TestDateUtil.java
@@ -0,0 +1,297 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.lens.cube.metadata;
+
+import static java.util.Calendar.DAY_OF_MONTH;
+import static java.util.Calendar.MONTH;
+
+import static org.apache.lens.cube.metadata.DateUtil.*;
+import static org.apache.lens.cube.metadata.UpdatePeriod.*;
+
+import static org.apache.commons.lang.time.DateUtils.addMilliseconds;
+
+import static org.testng.Assert.assertEquals;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Set;
+
+import org.apache.lens.server.api.error.LensException;
+
+import org.apache.commons.lang.time.DateUtils;
+
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+import com.beust.jcommander.internal.Sets;
+import com.google.common.collect.Lists;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * Unit tests for cube DateUtil class TestDateUtil.
+ */
+@Slf4j
+public class TestDateUtil {
+
+  public static final String[] TEST_PAIRS = {
+    "2013-Jan-01", "2013-Jan-31", "2013-Jan-01", "2013-May-31",
+    "2013-Jan-01", "2013-Dec-31", "2013-Feb-01", "2013-Apr-25",
+    "2012-Feb-01", "2013-Feb-01", "2011-Feb-01", "2013-Feb-01",
+    "2013-Jan-02", "2013-Feb-02", "2013-Jan-02", "2013-Mar-02",
+  };
+
+  public static final SimpleDateFormat DATE_FMT = new 
SimpleDateFormat("yyyy-MMM-dd");
+
+  private Date[] pairs;
+
+  @BeforeTest
+  public void setUp() {
+    pairs = new Date[TEST_PAIRS.length];
+    for (int i = 0; i < TEST_PAIRS.length; i++) {
+      try {
+        pairs[i] = DATE_FMT.parse(TEST_PAIRS[i]);
+      } catch (ParseException e) {
+        log.error("Parsing exception while setup.", e);
+      }
+    }
+  }
+
+
+  @Test
+  public void testMonthsBetween() throws Exception {
+    int i = 0;
+    assertEquals(getMonthlyCoveringInfo(pairs[i], DateUtils.round(pairs[i + 
1], MONTH)),
+      new CoveringInfo(1, true),
+      "2013-Jan-01 to 2013-Jan-31");
+
+    i += 2;
+    assertEquals(getMonthlyCoveringInfo(pairs[i], DateUtils.round(pairs[i + 
1], MONTH)),
+      new CoveringInfo(5, true),
+      "2013-Jan-01 to 2013-May-31");
+
+    i += 2;
+    assertEquals(getMonthlyCoveringInfo(pairs[i], DateUtils.round(pairs[i + 
1], MONTH)),
+      new CoveringInfo(12, true),
+      "2013-Jan-01 to 2013-Dec-31");
+
+    i += 2;
+    assertEquals(getMonthlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(2, false),
+      "2013-Feb-01 to 2013-Apr-25");
+
+    i += 2;
+    assertEquals(getMonthlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(12, true),
+      "2012-Feb-01 to 2013-Feb-01");
+
+    i += 2;
+    assertEquals(getMonthlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(24, true),
+      "2011-Feb-01 to 2013-Feb-01");
+
+    i += 2;
+    assertEquals(getMonthlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(0, false),
+      "2013-Jan-02 to 2013-Feb-02");
+
+    i += 2;
+    assertEquals(getMonthlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(1, false),
+      "2013-Jan-02 to 2013-Mar-02");
+  }
+
+  @Test
+  public void testQuartersBetween() throws Exception {
+    int i = 0;
+    assertEquals(getQuarterlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(0, false),
+      "2013-Jan-01 to 2013-Jan-31");
+
+    i += 2;
+    assertEquals(getQuarterlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(1, false),
+      "2013-Jan-01 to 2013-May-31");
+
+    i += 2;
+    assertEquals(getQuarterlyCoveringInfo(pairs[i], DateUtils.round(pairs[i + 
1], MONTH)),
+      new CoveringInfo(4, true),
+      "2013-Jan-01 to 2013-Dec-31");
+
+    i += 2;
+    assertEquals(getQuarterlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(0, false),
+      "2013-Feb-01 to 2013-Apr-25");
+
+    i += 2;
+    assertEquals(getQuarterlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(3, false),
+      "2012-Feb-01 to 2013-Feb-01");
+
+    i += 2;
+    assertEquals(getQuarterlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(7, false),
+      "2011-Feb-01 to 2013-Feb-01");
+  }
+
+  @Test
+  public void testYearsBetween() throws Exception {
+    int i = 0;
+    assertEquals(getYearlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(0, false),
+      "" + pairs[i] + "->" + pairs[i + 1]);
+
+    i += 2;
+    assertEquals(getYearlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(0, false),
+      "" + pairs[i] + "->" + pairs[i + 1]);
+
+    i += 2;
+    assertEquals(getYearlyCoveringInfo(pairs[i], DateUtils.round(pairs[i + 1], 
MONTH)),
+      new CoveringInfo(1, true), ""
+        + pairs[i] + "->" + pairs[i + 1]);
+
+    i += 2;
+    assertEquals(getYearlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(0, false),
+      "" + pairs[i] + "->" + pairs[i + 1]);
+
+    i += 2;
+    assertEquals(getYearlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(0, false),
+      "" + pairs[i] + "->" + pairs[i + 1]);
+
+    i += 2;
+    assertEquals(getYearlyCoveringInfo(pairs[i], pairs[i + 1]), new 
CoveringInfo(1, false),
+      "" + pairs[i] + "->" + pairs[i + 1]);
+  }
+
+  @Test
+  public void testWeeksBetween() throws Exception {
+    CoveringInfo weeks;
+
+    weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2013-May-26"), 
DATE_FMT.parse("2013-Jun-2"));
+    assertEquals(weeks, new CoveringInfo(1, true), "2013-May-26 to 
2013-Jun-2");
+
+    weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2013-May-27"), 
DATE_FMT.parse("2013-Jun-3"));
+    assertEquals(weeks, new CoveringInfo(0, false), "2013-May-26 to 
2013-Jun-2");
+
+    weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2013-May-27"), 
DATE_FMT.parse("2013-Jun-9"));
+    assertEquals(weeks, new CoveringInfo(1, false), "2013-May-26 to 
2013-Jun-2");
+
+    weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2013-May-27"), 
DATE_FMT.parse("2013-Jun-1"));
+    assertEquals(weeks, new CoveringInfo(0, false), "2013-May-27 to 
2013-Jun-1");
+
+    weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2013-May-25"), 
DATE_FMT.parse("2013-Jun-2"));
+    assertEquals(weeks, new CoveringInfo(1, false), "2013-May-25 to 
2013-Jun-1");
+
+    weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2013-May-26"), 
DATE_FMT.parse("2013-Jun-9"));
+    assertEquals(weeks, new CoveringInfo(2, true), "2013-May-26 to 
2013-Jun-8");
+
+    weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2013-May-26"), 
DATE_FMT.parse("2013-Jun-10"));
+    assertEquals(weeks, new CoveringInfo(2, false), "2013-May-26 to 
2013-Jun-10");
+
+    weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2015-Dec-27"), 
DATE_FMT.parse("2016-Jan-03"));
+    assertEquals(weeks, new CoveringInfo(1, true), "2015-Dec-27 to 
2016-Jan-03");
+  }
+
+  @Test
+  public void testNowWithGranularity() throws Exception {
+    String dateFmt = "yyyy/MM/dd-HH.mm.ss.SSS";
+    // Tuesday Sept 23, 2014, 12.02.05.500 pm
+    String testDateStr = "2014/09/23-12.02.05.500";
+    final SimpleDateFormat sdf = new SimpleDateFormat(dateFmt);
+    final Date testDate = sdf.parse(testDateStr);
+
+    System.out.print("@@ testDateStr=" + testDateStr + " parsed date=" + 
testDate);
+
+    // Tests without a diff, just resolve now with different granularity
+    assertEquals(testDateStr, sdf.format(resolveDate("now", testDate)));
+    assertEquals("2014/01/01-00.00.00.000", sdf.format(resolveDate("now.year", 
testDate)));
+    assertEquals("2014/09/01-00.00.00.000", 
sdf.format(resolveDate("now.month", testDate)));
+    // Start of week resolves to Sunday
+    assertEquals("2014/09/21-00.00.00.000", sdf.format(resolveDate("now.week", 
testDate)));
+    assertEquals("2014/09/23-00.00.00.000", sdf.format(resolveDate("now.day", 
testDate)));
+    assertEquals("2014/09/23-12.00.00.000", sdf.format(resolveDate("now.hour", 
testDate)));
+    assertEquals("2014/09/23-12.02.00.000", 
sdf.format(resolveDate("now.minute", testDate)));
+    assertEquals("2014/09/23-12.02.05.000", 
sdf.format(resolveDate("now.second", testDate)));
+
+    // Tests with a diff
+    assertEquals("2014/09/22-00.00.00.000", sdf.format(resolveDate("now.day 
-1day", testDate)));
+    assertEquals("2014/09/23-10.00.00.000", sdf.format(resolveDate("now.hour 
-2hour", testDate)));
+    assertEquals("2014/09/24-12.00.00.000", sdf.format(resolveDate("now.hour 
+24hour", testDate)));
+    assertEquals("2015/01/01-00.00.00.000", sdf.format(resolveDate("now.year 
+1year", testDate)));
+    assertEquals("2014/02/01-00.00.00.000", sdf.format(resolveDate("now.year 
+1month", testDate)));
+  }
+
+  @Test
+  public void testFloorDate() throws ParseException {
+    Date date = ABSDATE_PARSER.get().parse("2015-01-01-00:00:00,000");
+    Date curDate = date;
+    for (int i = 0; i < 284; i++) {
+      assertEquals(getFloorDate(curDate, YEARLY), date);
+      curDate = addMilliseconds(curDate, 111111111);
+    }
+    assertEquals(getFloorDate(curDate, YEARLY), DateUtils.addYears(date, 1));
+    assertEquals(getFloorDate(date, WEEKLY), 
ABSDATE_PARSER.get().parse("2014-12-28-00:00:00,000"));
+  }
+
+  @Test
+  public void testCeilDate() throws ParseException {
+    Date date = ABSDATE_PARSER.get().parse("2015-12-26-06:30:15,040");
+    assertEquals(getCeilDate(date, YEARLY), 
ABSDATE_PARSER.get().parse("2016-01-01-00:00:00,000"));
+    assertEquals(getCeilDate(date, MONTHLY), 
ABSDATE_PARSER.get().parse("2016-01-01-00:00:00,000"));
+    assertEquals(getCeilDate(date, DAILY), 
ABSDATE_PARSER.get().parse("2015-12-27-00:00:00,000"));
+    assertEquals(getCeilDate(date, HOURLY), 
ABSDATE_PARSER.get().parse("2015-12-26-07:00:00,000"));
+    assertEquals(getCeilDate(date, MINUTELY), 
ABSDATE_PARSER.get().parse("2015-12-26-06:31:00,000"));
+    assertEquals(getCeilDate(date, SECONDLY), 
ABSDATE_PARSER.get().parse("2015-12-26-06:30:16,000"));
+    assertEquals(getCeilDate(date, WEEKLY), 
ABSDATE_PARSER.get().parse("2015-12-27-00:00:00,000"));
+  }
+
+  @Test
+  public void testTimeDiff() throws LensException {
+    ArrayList<String> minusFourDays =
+      Lists.newArrayList("-4 days", "-4days", "-4day", "-4 day", "- 4days", "- 
4 day");
+    ArrayList<String> plusFourDays =
+      Lists.newArrayList("+4 days", "4 days", "+4days", "4day", "4 day", "+ 
4days", "+ 4 day", "+4 day");
+    Set<TimeDiff> diffs = Sets.newHashSet();
+    for (String diffStr : minusFourDays) {
+      diffs.add(TimeDiff.parseFrom(diffStr));
+    }
+    assertEquals(diffs.size(), 1);
+    TimeDiff minusFourDaysDiff = diffs.iterator().next();
+    assertEquals(minusFourDaysDiff.quantity, -4);
+    assertEquals(minusFourDaysDiff.updatePeriod, DAILY);
+
+    diffs.clear();
+    for (String diffStr : plusFourDays) {
+      diffs.add(TimeDiff.parseFrom(diffStr));
+    }
+    assertEquals(diffs.size(), 1);
+    TimeDiff plusFourDaysDiff = diffs.iterator().next();
+    assertEquals(plusFourDaysDiff.quantity, 4);
+    assertEquals(plusFourDaysDiff.updatePeriod, DAILY);
+    Date now = new Date();
+    
assertEquals(minusFourDaysDiff.offsetFrom(plusFourDaysDiff.offsetFrom(now)), 
now);
+    
assertEquals(plusFourDaysDiff.offsetFrom(minusFourDaysDiff.offsetFrom(now)), 
now);
+    assertEquals(minusFourDaysDiff.negativeOffsetFrom(now), 
plusFourDaysDiff.offsetFrom(now));
+    assertEquals(minusFourDaysDiff.offsetFrom(now), 
plusFourDaysDiff.negativeOffsetFrom(now));
+  }
+
+  @Test
+  public void testRelativeToAbsolute() throws LensException {
+    Date now = new Date();
+    Date nowDay = DateUtils.truncate(now, DAY_OF_MONTH);
+    Date nowDayMinus2Days = DateUtils.add(nowDay, DAY_OF_MONTH, -2);
+    assertEquals(relativeToAbsolute("now", now), 
DateUtil.ABSDATE_PARSER.get().format(now));
+    assertEquals(relativeToAbsolute("now.day", now), 
DateUtil.ABSDATE_PARSER.get().format(nowDay));
+    assertEquals(relativeToAbsolute("now.day - 2 days", now), 
DateUtil.ABSDATE_PARSER.get().format(nowDayMinus2Days));
+    assertEquals(relativeToAbsolute("now.day - 2 day", now), 
DateUtil.ABSDATE_PARSER.get().format(nowDayMinus2Days));
+    assertEquals(relativeToAbsolute("now.day - 2day", now), 
DateUtil.ABSDATE_PARSER.get().format(nowDayMinus2Days));
+    assertEquals(relativeToAbsolute("now.day -2 day", now), 
DateUtil.ABSDATE_PARSER.get().format(nowDayMinus2Days));
+    assertEquals(relativeToAbsolute("now.day -2 days", now), 
DateUtil.ABSDATE_PARSER.get().format(nowDayMinus2Days));
+  }
+}

http://git-wip-us.apache.org/repos/asf/lens/blob/7c7c86da/lens-cube/src/test/java/org/apache/lens/cube/parse/CubeTestSetup.java
----------------------------------------------------------------------
diff --git 
a/lens-cube/src/test/java/org/apache/lens/cube/parse/CubeTestSetup.java 
b/lens-cube/src/test/java/org/apache/lens/cube/parse/CubeTestSetup.java
index 1357035..2a50d74 100644
--- a/lens-cube/src/test/java/org/apache/lens/cube/parse/CubeTestSetup.java
+++ b/lens-cube/src/test/java/org/apache/lens/cube/parse/CubeTestSetup.java
@@ -19,14 +19,14 @@
 
 package org.apache.lens.cube.parse;
 
-import static java.util.Calendar.*;
+import static java.util.Calendar.DAY_OF_MONTH;
+import static java.util.Calendar.HOUR_OF_DAY;
 
+import static org.apache.lens.cube.metadata.DateFactory.*;
 import static org.apache.lens.cube.metadata.UpdatePeriod.*;
 
 import static org.testng.Assert.*;
 
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
 import java.util.*;
 
 import org.apache.lens.cube.metadata.*;
@@ -39,7 +39,6 @@ import org.apache.lens.server.api.LensConfConstants;
 import org.apache.lens.server.api.error.LensException;
 
 import org.apache.commons.lang.StringUtils;
-import org.apache.commons.lang.time.DateUtils;
 import org.apache.hadoop.hive.conf.HiveConf;
 import org.apache.hadoop.hive.metastore.api.Database;
 import org.apache.hadoop.hive.metastore.api.FieldSchema;
@@ -88,10 +87,6 @@ import lombok.extern.slf4j.Slf4j;
 @Slf4j
 public class CubeTestSetup {
 
-  public static final String HOUR_FMT = "yyyy-MM-dd-HH";
-  public static final SimpleDateFormat HOUR_PARSER = new 
SimpleDateFormat(HOUR_FMT);
-  public static final String MONTH_FMT = "yyyy-MM";
-  public static final SimpleDateFormat MONTH_PARSER = new 
SimpleDateFormat(MONTH_FMT);
   private Set<CubeMeasure> cubeMeasures;
   private Set<CubeDimAttribute> cubeDimensions;
   public static final String TEST_CUBE_NAME = "testCube";
@@ -101,30 +96,6 @@ public class CubeTestSetup {
   public static final String DERIVED_CUBE_NAME2 = "der2";
   public static final String DERIVED_CUBE_NAME3 = "der3";
 
-  // Time Instances as Date Type
-  public static final Date NOW;
-  public static final Date LAST_HOUR;
-  public static final Date TWODAYS_BACK;
-  public static final Date ONE_DAY_BACK;
-  public static final Date TWO_MONTHS_BACK;
-  public static final Date BEFORE_4_DAYS_START;
-  public static final Date BEFORE_4_DAYS_END;
-  public static final Date THIS_YEAR_START;
-  public static final Date THIS_YEAR_END;
-  public static final Date LAST_YEAR_START;
-  public static final Date LAST_YEAR_END;
-
-  // Time Ranges
-  public static final String LAST_HOUR_TIME_RANGE;
-  public static final String TWO_DAYS_RANGE;
-  public static final String TWO_DAYS_RANGE_TTD;
-  public static final String THIS_YEAR_RANGE;
-  public static final String LAST_YEAR_RANGE;
-  public static final String TWO_MONTHS_RANGE_UPTO_MONTH;
-  public static final String TWO_MONTHS_RANGE_UPTO_HOURS;
-  public static final String TWO_DAYS_RANGE_BEFORE_4_DAYS;
-
-  private static boolean zerothHour;
   private static String c0 = "C0";
   private static String c1 = "C1";
   private static String c2 = "C2";
@@ -134,99 +105,13 @@ public class CubeTestSetup {
   private static Map<String, String> factValidityProperties = 
Maps.newHashMap();
   @Getter
   private static Map<String, List<UpdatePeriod>> storageToUpdatePeriodMap = 
new LinkedHashMap<>();
-  public static class DateOffsetProvider extends HashMap<Integer, Date> {
-    private final UpdatePeriod updatePeriod;
-    Calendar calendar = Calendar.getInstance();
-
-    public DateOffsetProvider(UpdatePeriod updatePeriod) {
-      this.updatePeriod = updatePeriod;
-    }
-    {
-      put(0, calendar.getTime());
-    }
-
-    @Override
-    public Date get(Object key) {
-      if (!containsKey(key) && key instanceof Integer) {
-        calendar.setTime(super.get(0));
-        calendar.add(updatePeriod.calendarField(), (Integer) key);
-        put((Integer) key, calendar.getTime());
-      }
-      return super.get(key);
-    }
-  }
-
   static {
-    Calendar cal = Calendar.getInstance();
-    // Keep in sync
-    NOW = cal.getTime();
-    log.debug("Test now:{}", NOW);
-
-    // Figure out if current hour is 0th hour
-    zerothHour = (cal.get(HOUR_OF_DAY) == 0);
-
-    // Figure out last hour
-    cal.add(HOUR_OF_DAY, -1);
-    LAST_HOUR = cal.getTime();
-    log.debug("LastHour:{}", LAST_HOUR);
-
-    cal.setTime(NOW);
-    cal.add(DAY_OF_MONTH, -1);
-    ONE_DAY_BACK = cal.getTime();
-    cal.add(DAY_OF_MONTH, -1);
-    TWODAYS_BACK = cal.getTime();
-    System.out.println("Test TWODAYS_BACK:" + TWODAYS_BACK);
-
-    // two months back
-    cal.setTime(NOW);
-    cal.add(MONTH, -2);
-    TWO_MONTHS_BACK = cal.getTime();
-    System.out.println("Test TWO_MONTHS_BACK:" + TWO_MONTHS_BACK);
-
-    // Before 4days
-    cal.setTime(NOW);
-    cal.add(DAY_OF_MONTH, -4);
-    BEFORE_4_DAYS_END = cal.getTime();
-    cal.add(DAY_OF_MONTH, -2);
-    BEFORE_4_DAYS_START = cal.getTime();
-
-
-    THIS_YEAR_START = DateUtils.truncate(NOW, YEARLY.calendarField());
-    THIS_YEAR_END = DateUtils.addYears(THIS_YEAR_START, 1);
-    LAST_YEAR_START = DateUtils.addYears(THIS_YEAR_START, -1);
-    LAST_YEAR_END = THIS_YEAR_START;
-    TWO_DAYS_RANGE_BEFORE_4_DAYS =
-      "time_range_in(d_time, '" + 
CubeTestSetup.getDateUptoHours(BEFORE_4_DAYS_START) + "','"
-        + CubeTestSetup.getDateUptoHours(BEFORE_4_DAYS_END) + "')";
-
-
-    TWO_DAYS_RANGE = "time_range_in(d_time, '" + 
getDateUptoHours(TWODAYS_BACK) + "','" + getDateUptoHours(NOW) + "')";
-    TWO_DAYS_RANGE_TTD = "time_range_in(test_time_dim, '" + 
getDateUptoHours(TWODAYS_BACK) + "','"
-      + getDateUptoHours(NOW) + "')";
-    THIS_YEAR_RANGE =
-      "time_range_in(d_time, '" + getDateUptoHours(THIS_YEAR_START) + "','" + 
getDateUptoHours(THIS_YEAR_END) + "')";
-    LAST_YEAR_RANGE =
-      "time_range_in(d_time, '" + getDateUptoHours(LAST_YEAR_START) + "','" + 
getDateUptoHours(LAST_YEAR_END) + "')";
-    TWO_MONTHS_RANGE_UPTO_MONTH =
-      "time_range_in(d_time, '" + getDateUptoMonth(TWO_MONTHS_BACK) + "','" + 
getDateUptoMonth(NOW) + "')";
-    TWO_MONTHS_RANGE_UPTO_HOURS =
-      "time_range_in(d_time, '" + getDateUptoHours(TWO_MONTHS_BACK) + "','" + 
getDateUptoHours(NOW) + "')";
-
-    // calculate LAST_HOUR_TIME_RANGE
-    LAST_HOUR_TIME_RANGE = getTimeRangeString(getDateUptoHours(LAST_HOUR), 
getDateUptoHours(NOW));
     factValidityProperties.put(MetastoreConstants.FACT_RELATIVE_START_TIME, 
"now.year - 90 days");
   }
 
-  public static boolean isZerothHour() {
-    return zerothHour;
-  }
 
   public static String getDateUptoHours(Date dt) {
-    return HOUR_PARSER.format(dt);
-  }
-
-  public static String getDateUptoMonth(Date dt) {
-    return MONTH_PARSER.format(dt);
+    return HOURLY.format(dt);
   }
 
   interface StoragePartitionProvider {
@@ -265,23 +150,16 @@ public class CubeTestSetup {
     StringBuilder expected = new StringBuilder();
     for (Map.Entry<String, String> entry : 
storageTableToWhereClause.entrySet()) {
       String storageTable = entry.getKey();
-      expected.append(selExpr);
-      expected.append(storageTable);
-      expected.append(" ");
-      expected.append(cubeName);
-      expected.append(" WHERE ");
-      expected.append("(");
+      expected.append(selExpr).append(storageTable).append(" 
").append(cubeName).append(" WHERE ").append("(");
       if (notLatestConditions != null) {
         for (String cond : notLatestConditions) {
           expected.append(cond).append(" AND ");
         }
       }
       if (whereExpr != null) {
-        expected.append(whereExpr);
-        expected.append(" AND ");
+        expected.append(whereExpr).append(" AND ");
       }
-      expected.append(entry.getValue());
-      expected.append(")");
+      expected.append(entry.getValue()).append(")");
       if (postWhereExpr != null) {
         expected.append(" ").append(postWhereExpr);
       }
@@ -328,27 +206,20 @@ public class CubeTestSetup {
     assertEquals(1, numTabs);
     for (Map.Entry<String, String> entry : 
storageTableToWhereClause.entrySet()) {
       String storageTable = entry.getKey();
-      expected.append(selExpr);
-      expected.append(storageTable);
-      expected.append(" ");
-      expected.append(cubeName);
-      expected.append(joinExpr);
-      expected.append(" WHERE ");
-      expected.append("(");
+      expected.append(selExpr).append(storageTable).append(" 
").append(cubeName).append(joinExpr)
+        .append(" WHERE ").append("(");
       if (notLatestConditions != null) {
         for (String cond : notLatestConditions) {
           expected.append(cond).append(" AND ");
         }
       }
       if (whereExpr != null) {
-        expected.append(whereExpr);
-        expected.append(" AND ");
+        expected.append(whereExpr).append(" AND ");
       }
       expected.append(entry.getValue());
       if (joinWhereConds != null) {
         for (String joinEntry : joinWhereConds) {
-          expected.append(" AND ");
-          expected.append(joinEntry);
+          expected.append(" AND ").append(joinEntry);
         }
       }
       expected.append(")");
@@ -379,7 +250,7 @@ public class CubeTestSetup {
 
   public static Map<String, String> 
getWhereForDailyAndHourly2daysWithTimeDim(String cubeName, String 
timedDimension,
     Date from, Date to, String... storageTables) {
-    Map<String, String> storageTableToWhereClause = new LinkedHashMap<String, 
String>();
+    Map<String, String> storageTableToWhereClause = new LinkedHashMap<>();
     if (storageToUpdatePeriodMap.isEmpty()) {
       String whereClause = getWhereForDailyAndHourly2daysWithTimeDim(cubeName, 
timedDimension, from, to);
       storageTableToWhereClause.put(getStorageTableString(storageTables), 
whereClause);
@@ -398,7 +269,7 @@ public class CubeTestSetup {
   private static String getStorageTableString(String... storageTables) {
     String dbName = getDbName();
     if (!StringUtils.isBlank(dbName)) {
-      List<String> tbls = new ArrayList<String>();
+      List<String> tbls = new ArrayList<>();
       for (String tbl : storageTables) {
         tbls.add(dbName + tbl);
       }
@@ -409,10 +280,10 @@ public class CubeTestSetup {
 
   public static String getWhereForDailyAndHourly2daysWithTimeDim(String 
cubeName, String timedDimension, Date from,
     Date to) {
-    List<String> hourlyparts = new ArrayList<String>();
-    List<String> dailyparts = new ArrayList<String>();
+    Set<String> hourlyparts = new HashSet<>();
+    Set<String> dailyparts = new HashSet<>();
     Date dayStart;
-    if (!CubeTestSetup.isZerothHour()) {
+    if (!isZerothHour()) {
       addParts(hourlyparts, HOURLY, from, DateUtil.getCeilDate(from, DAILY));
       addParts(hourlyparts, HOURLY, DateUtil.getFloorDate(to, DAILY),
         DateUtil.getFloorDate(to, HOURLY));
@@ -421,7 +292,7 @@ public class CubeTestSetup {
       dayStart = from;
     }
     addParts(dailyparts, DAILY, dayStart, DateUtil.getFloorDate(to, DAILY));
-    List<String> parts = new ArrayList<String>();
+    List<String> parts = new ArrayList<>();
     parts.addAll(hourlyparts);
     parts.addAll(dailyparts);
     Collections.sort(parts);
@@ -434,7 +305,7 @@ public class CubeTestSetup {
     List<String> hourlyparts = new ArrayList<String>();
     List<String> dailyparts = new ArrayList<String>();
     Date dayStart;
-    if (!CubeTestSetup.isZerothHour()) {
+    if (!isZerothHour()) {
       addParts(hourlyparts, HOURLY, from, DateUtil.getCeilDate(from, DAILY));
       addParts(hourlyparts, HOURLY, DateUtil.getFloorDate(to, DAILY),
         DateUtil.getFloorDate(to, HOURLY));
@@ -458,7 +329,7 @@ public class CubeTestSetup {
     List<String> monthlyparts = new ArrayList<String>();
     Date dayStart = TWO_MONTHS_BACK;
     Date monthStart = TWO_MONTHS_BACK;
-    if (!CubeTestSetup.isZerothHour()) {
+    if (!isZerothHour()) {
       addParts(hourlyparts, HOURLY, TWO_MONTHS_BACK,
         DateUtil.getCeilDate(TWO_MONTHS_BACK, DAILY));
       addParts(hourlyparts, HOURLY, DateUtil.getFloorDate(NOW, DAILY),
@@ -510,7 +381,7 @@ public class CubeTestSetup {
 
     Date dayStart = TWO_MONTHS_BACK;
     Date monthStart = TWO_MONTHS_BACK;
-    if (!CubeTestSetup.isZerothHour()) {
+    if (!isZerothHour()) {
       addParts(hourlyparts, HOURLY, TWO_MONTHS_BACK,
         DateUtil.getCeilDate(TWO_MONTHS_BACK, DAILY));
       addParts(hourlyparts, HOURLY, DateUtil.getFloorDate(NOW, DAILY),
@@ -571,13 +442,12 @@ public class CubeTestSetup {
     return storageTableToWhereClause;
   }
 
-  public static void addParts(List<String> partitions, UpdatePeriod 
updatePeriod, Date from, Date to) {
-    DateFormat fmt = updatePeriod.format();
+  public static void addParts(Collection<String> partitions, UpdatePeriod 
updatePeriod, Date from, Date to) {
     Calendar cal = Calendar.getInstance();
     cal.setTime(from);
     Date dt = cal.getTime();
     while (dt.before(to)) {
-      String part = fmt.format(dt);
+      String part = updatePeriod.format(dt);
       cal.add(updatePeriod.calendarField(), 1);
       partitions.add(part);
       dt = cal.getTime();
@@ -1418,9 +1288,9 @@ public class CubeTestSetup {
     }
 
     // Add all hourly partitions for TWO_DAYS_RANGE_BEFORE_4_DAYS
-    cal.setTime(BEFORE_4_DAYS_START);
+    cal.setTime(BEFORE_6_DAYS);
     temp = cal.getTime();
-    while (!(temp.after(BEFORE_4_DAYS_END))) {
+    while (!(temp.after(BEFORE_4_DAYS))) {
       Map<String, Date> timeParts = new HashMap<String, Date>();
       timeParts.put("ttd", temp);
       timeParts.put("ttd2", temp);
@@ -1533,9 +1403,9 @@ public class CubeTestSetup {
     }
 
     // Add all hourly partitions for TWO_DAYS_RANGE_BEFORE_4_DAYS
-    cal.setTime(BEFORE_4_DAYS_START);
+    cal.setTime(BEFORE_6_DAYS);
     temp = cal.getTime();
-    while (!(temp.after(BEFORE_4_DAYS_END))) {
+    while (!(temp.after(BEFORE_4_DAYS))) {
       Map<String, Date> timeParts = new HashMap<String, Date>();
       timeParts.put(TestCubeMetastoreClient.getDatePartitionKey(), temp);
       StoragePartitionDesc sPartSpec = new 
StoragePartitionDesc(fact.getName(), timeParts, null, HOURLY);
@@ -1585,9 +1455,9 @@ public class CubeTestSetup {
     assertTimeline(client, fact.getName(), c4, HOURLY, "ttd2", ttd2StoreAll);
 
     // Add all hourly partitions for TWO_DAYS_RANGE_BEFORE_4_DAYS
-    cal.setTime(BEFORE_4_DAYS_START);
+    cal.setTime(BEFORE_6_DAYS);
     temp = cal.getTime();
-    while (!(temp.after(BEFORE_4_DAYS_END))) {
+    while (!(temp.after(BEFORE_4_DAYS))) {
       Map<String, Date> timeParts = new HashMap<String, Date>();
       timeParts.put("ttd", temp);
       timeParts.put("ttd2", temp);
@@ -2692,9 +2562,4 @@ public class CubeTestSetup {
     System.out.println("--query- " + query);
     HQLParser.printAST(HQLParser.parseHQL(query, new HiveConf()));
   }
-
-
-  private static String getTimeRangeString(final String startDate, final 
String endDate) {
-    return "time_range_in(d_time, '" + startDate + "','" + endDate + "')";
-  }
 }

http://git-wip-us.apache.org/repos/asf/lens/blob/7c7c86da/lens-cube/src/test/java/org/apache/lens/cube/parse/FieldsCannotBeQueriedTogetherTest.java
----------------------------------------------------------------------
diff --git 
a/lens-cube/src/test/java/org/apache/lens/cube/parse/FieldsCannotBeQueriedTogetherTest.java
 
b/lens-cube/src/test/java/org/apache/lens/cube/parse/FieldsCannotBeQueriedTogetherTest.java
index 0fea9f1..ff7c15f 100644
--- 
a/lens-cube/src/test/java/org/apache/lens/cube/parse/FieldsCannotBeQueriedTogetherTest.java
+++ 
b/lens-cube/src/test/java/org/apache/lens/cube/parse/FieldsCannotBeQueriedTogetherTest.java
@@ -18,7 +18,7 @@
  */
 package org.apache.lens.cube.parse;
 
-import static org.apache.lens.cube.parse.CubeTestSetup.*;
+import static org.apache.lens.cube.metadata.DateFactory.*;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.fail;
@@ -301,8 +301,7 @@ public class FieldsCannotBeQueriedTogetherTest extends 
TestQueryRewrite {
     Configuration queryConf = new Configuration(conf);
     queryConf.setBoolean(CubeQueryConfUtil.REPLACE_TIMEDIM_WITH_PART_COL, 
true);
 
-    testFieldsCannotBeQueriedTogetherError("select msr4 from basecube where " 
+ "time_range_in(d_time, '"
-        + getDateUptoHours(TWODAYS_BACK) + "','" + 
getDateUptoHours(CubeTestSetup.NOW) + "')",
+    testFieldsCannotBeQueriedTogetherError("select msr4 from basecube where " 
+ TWO_DAYS_RANGE,
         Arrays.asList("d_time", "msr4"), queryConf);
   }
 
@@ -321,8 +320,7 @@ public class FieldsCannotBeQueriedTogetherTest extends 
TestQueryRewrite {
     Configuration queryConf = new Configuration(conf);
     queryConf.setBoolean(CubeQueryConfUtil.REPLACE_TIMEDIM_WITH_PART_COL, 
false);
 
-    testFieldsCannotBeQueriedTogetherError("select msr4 from basecube where " 
+ "time_range_in(d_time, '"
-        + getDateUptoHours(TWODAYS_BACK) + "','" + 
getDateUptoHours(CubeTestSetup.NOW) + "')",
+    testFieldsCannotBeQueriedTogetherError("select msr4 from basecube where " 
+ TWO_DAYS_RANGE,
         Arrays.asList("d_time", "msr4"), queryConf);
   }
 

http://git-wip-us.apache.org/repos/asf/lens/blob/7c7c86da/lens-cube/src/test/java/org/apache/lens/cube/parse/TestAggregateResolver.java
----------------------------------------------------------------------
diff --git 
a/lens-cube/src/test/java/org/apache/lens/cube/parse/TestAggregateResolver.java 
b/lens-cube/src/test/java/org/apache/lens/cube/parse/TestAggregateResolver.java
index 753ca33..a48d753 100644
--- 
a/lens-cube/src/test/java/org/apache/lens/cube/parse/TestAggregateResolver.java
+++ 
b/lens-cube/src/test/java/org/apache/lens/cube/parse/TestAggregateResolver.java
@@ -19,6 +19,7 @@
 
 package org.apache.lens.cube.parse;
 
+import static org.apache.lens.cube.metadata.DateFactory.*;
 import static org.apache.lens.cube.parse.CubeTestSetup.*;
 import static org.apache.lens.cube.parse.TestCubeRewriter.compareQueries;
 

http://git-wip-us.apache.org/repos/asf/lens/blob/7c7c86da/lens-cube/src/test/java/org/apache/lens/cube/parse/TestBaseCubeQueries.java
----------------------------------------------------------------------
diff --git 
a/lens-cube/src/test/java/org/apache/lens/cube/parse/TestBaseCubeQueries.java 
b/lens-cube/src/test/java/org/apache/lens/cube/parse/TestBaseCubeQueries.java
index ee84a4c..97c6d08 100644
--- 
a/lens-cube/src/test/java/org/apache/lens/cube/parse/TestBaseCubeQueries.java
+++ 
b/lens-cube/src/test/java/org/apache/lens/cube/parse/TestBaseCubeQueries.java
@@ -19,9 +19,10 @@
 
 package org.apache.lens.cube.parse;
 
+import static org.apache.lens.cube.metadata.DateFactory.*;
+import static org.apache.lens.cube.metadata.DateUtil.*;
 import static 
org.apache.lens.cube.parse.CandidateTablePruneCause.CandidateTablePruneCode.MISSING_PARTITIONS;
 import static org.apache.lens.cube.parse.CubeTestSetup.*;
-import static org.apache.lens.cube.parse.DateUtil.*;
 import static org.apache.lens.cube.parse.TestCubeRewriter.compareQueries;
 
 import static org.apache.hadoop.hive.ql.parse.HiveParser.KW_AND;
@@ -33,6 +34,7 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.apache.lens.cube.error.LensCubeErrorCode;
+import org.apache.lens.cube.metadata.TimeRange;
 import org.apache.lens.cube.metadata.UpdatePeriod;
 import 
org.apache.lens.cube.parse.CandidateTablePruneCause.CandidateTablePruneCode;
 import org.apache.lens.server.api.error.LensException;
@@ -47,7 +49,6 @@ import org.testng.annotations.BeforeTest;
 import org.testng.annotations.Test;
 
 import com.google.common.base.Splitter;
-
 import lombok.Getter;
 
 public class TestBaseCubeQueries extends TestQueryRewrite {

http://git-wip-us.apache.org/repos/asf/lens/blob/7c7c86da/lens-cube/src/test/java/org/apache/lens/cube/parse/TestBetweenTimeRangeWriter.java
----------------------------------------------------------------------
diff --git 
a/lens-cube/src/test/java/org/apache/lens/cube/parse/TestBetweenTimeRangeWriter.java
 
b/lens-cube/src/test/java/org/apache/lens/cube/parse/TestBetweenTimeRangeWriter.java
index 9a2493c..eeba861 100644
--- 
a/lens-cube/src/test/java/org/apache/lens/cube/parse/TestBetweenTimeRangeWriter.java
+++ 
b/lens-cube/src/test/java/org/apache/lens/cube/parse/TestBetweenTimeRangeWriter.java
@@ -19,12 +19,12 @@
 
 package org.apache.lens.cube.parse;
 
+import static org.apache.lens.cube.metadata.DateFactory.NOW;
+import static org.apache.lens.cube.metadata.DateFactory.TWODAYS_BACK;
+import static org.apache.lens.cube.metadata.UpdatePeriod.DAILY;
+
 import java.text.DateFormat;
-import java.util.ArrayList;
 import java.util.Date;
-import java.util.List;
-
-import org.apache.lens.cube.metadata.UpdatePeriod;
 
 import org.testng.Assert;
 
@@ -50,9 +50,9 @@ public class TestBetweenTimeRangeWriter extends 
TestTimeRangeWriter {
     String expected = null;
     if (format == null) {
       expected =
-        getBetweenClause("test", "dt", CubeTestSetup.TWODAYS_BACK, 
CubeTestSetup.NOW, UpdatePeriod.DAILY.format());
+        getBetweenClause("test", "dt", TWODAYS_BACK, NOW, DAILY.format());
     } else {
-      expected = getBetweenClause("test", "dt", CubeTestSetup.TWODAYS_BACK, 
CubeTestSetup.NOW, format);
+      expected = getBetweenClause("test", "dt", TWODAYS_BACK, NOW, format);
     }
     Assert.assertEquals(expected, whereClause);
   }
@@ -62,17 +62,4 @@ public class TestBetweenTimeRangeWriter extends 
TestTimeRangeWriter {
     String last = format.format(end);
     return " (" + alias + "." + colName + " BETWEEN '" + first + "' AND '" + 
last + "') ";
   }
-
-  @Override
-  public void validateSingle(String whereClause, DateFormat format) {
-    List<String> parts = new ArrayList<String>();
-    if (format == null) {
-      parts.add(UpdatePeriod.DAILY.format(CubeTestSetup.ONE_DAY_BACK));
-    } else {
-      parts.add(format.format(CubeTestSetup.ONE_DAY_BACK));
-    }
-
-    System.out.println("Expected :" + StorageUtil.getWherePartClause("dt", 
"test", parts));
-    Assert.assertEquals(whereClause, StorageUtil.getWherePartClause("dt", 
"test", parts));
-  }
 }

Reply via email to