Index: src/main/java/com/xpn/xwiki/stats/impl/XWikiStats.java =================================================================== --- src/main/java/com/xpn/xwiki/stats/impl/XWikiStats.java (revision 5418) +++ src/main/java/com/xpn/xwiki/stats/impl/XWikiStats.java (working copy) @@ -53,13 +53,18 @@ setIntValue("period", period); } - public int getPeriodAsInt(Date date, int type) { + public static int getPeriodAsInt(Date date, int type) { Calendar cal = Calendar.getInstance(); - cal.setTime(date); - if (type == PERIOD_MONTH) - return cal.get(Calendar.YEAR) * 100 + (cal.get(Calendar.MONTH)+1); - else - return cal.get(Calendar.YEAR) * 10000 + (cal.get(Calendar.MONTH)+1) * 100 + (cal.get(Calendar.DAY_OF_MONTH)+1); + if (date != null){ + cal.setTime(date); + } + if (type == PERIOD_MONTH){ + // The first month of the year is JANUARY which is 0 + return cal.get(Calendar.YEAR) * 100 + (cal.get(Calendar.MONTH)+1); + } else { + // The first day of the month has value 1 + return cal.get(Calendar.YEAR) * 10000 + (cal.get(Calendar.MONTH)+1) * 100 + cal.get(Calendar.DAY_OF_MONTH); + } } public int getPageViews() { Index: src/test/java/com/xpn/xwiki/stats/impl/XWikiStatsTest.java =================================================================== --- src/test/java/com/xpn/xwiki/stats/impl/XWikiStatsTest.java (revision 0) +++ src/test/java/com/xpn/xwiki/stats/impl/XWikiStatsTest.java (revision 0) @@ -0,0 +1,51 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package com.xpn.xwiki.stats.impl; + +import java.text.SimpleDateFormat; +import java.util.Calendar; + +import junit.framework.TestCase; + +/** + * Unit tests for the {@link XWikiStats} class. + * + * @version $Id: $ + */ +public class XWikiStatsTest extends TestCase +{ + /** + * Test for the {@link XWikiStats#getPeriodAsInt(java.util.Date, int)} + */ + public void testGetPeriodAsInt() + { + Calendar cal = Calendar.getInstance(); + + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); + String a = sdf.format(cal.getTime()); + String b = XWikiStats.getPeriodAsInt(cal.getTime(), XWikiStats.PERIOD_MONTH) + ""; + assertTrue("Wrong month period format", a.equals(b)); + + sdf = new SimpleDateFormat("yyyyMMdd"); + a = sdf.format(cal.getTime()); + b = XWikiStats.getPeriodAsInt(cal.getTime(), XWikiStats.PERIOD_DAY) + ""; + assertTrue("Wrong day period format", a.equals(b)); + } +}