/*******************************************************************************
 * Copyright (c) 2001-2005 Sasa Markovic and Ciaran Treanor.
 * Copyright (c) 2011-2013 The OpenNMS Group, Inc.
 *
 * This library 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 library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *******************************************************************************/
package org.jrobin.core;

import java.util.Random;

import static org.junit.Assert.*;

import org.jrobin.core.RrdBackendFactory;
import org.jrobin.core.RrdDb;
import org.jrobin.core.RrdDef;
import org.jrobin.core.RrdNioBackendFactory;
import org.jrobin.core.Sample;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class JRobinTest {
    RrdDb m_db = null;

    Random m_random = new Random();

    @BeforeClass
    public static void beforeClass() throws Exception {
        if (!RrdBackendFactory.isInstanceCreated()) {
            RrdBackendFactory.setDefaultFactory("NIO");
        }
    }

    @Before
    public void setUp() throws Exception {
        RrdDef def = new RrdDef("target/test.jrb");
        def.setStep(300);
        def.addDatasource("test", "GAUGE", 300L, Double.NaN, Double.NaN);
        def.addArchive("RRA:AVERAGE:0.5:1:2016");
        m_db = new RrdDb(def);
    }

    @After
    public void tearDown() throws Exception {
        RrdNioBackendFactory factory = (RrdNioBackendFactory) RrdBackendFactory.getDefaultFactory();
        final SyncManager syncManager = factory.getSyncManager();
        assertNotNull(syncManager);
        assertNotNull(syncManager.getTimer());
        m_db.close();
        assertNull(syncManager.getTimer());
    }

    @Test
    public void testRrdUpdate() throws Exception {
        Sample sample = null;
        long startTime = System.currentTimeMillis() - 1000;
        // for (int i = 0; i < 1000000; i++) {
        sample = m_db.createSample();
        sample.setAndUpdate(startTime + ":" + m_random.nextInt());
        // sample.setAndUpdate(startTime++ + ":" + m_random.nextInt());
        // }
    }
}

@@ -1049,240 +1049,116 @@
 ort 
-java.util.Random;%0A%0Aimport static org.junit.Assert.*;%0A%0Aimport org.jrobin.core.RrdBackendFactory;%0Aimport org.jrobin.core.RrdDb;%0Aimport org.jrobin.core.RrdDef;%0Aimport org.jrobin.core.RrdNioBackendFactory;%0Aimport org.jrobin.core.Sample;
+static org.junit.Assert.assertNotNull;%0Aimport static org.junit.Assert.assertNull;%0A%0Aimport java.util.Random;%0A
 %0Aimp
@@ -1283,16 +1283,41 @@
 nTest %7B%0A
+    RrdDef m_def = null;%0A
     RrdD
@@ -1549,11 +1549,12 @@
 ry(%22
-NIO
+FILE
 %22);%0A
@@ -1633,23 +1633,18 @@
         
-RrdDef 
+m_
 def = ne
@@ -1676,24 +1676,26 @@
 %22);%0A        
+m_
 def.setStep(
@@ -1704,24 +1704,26 @@
 0);%0A        
+m_
 def.addDatas
@@ -1784,16 +1784,18 @@
         
+m_
 def.addA
@@ -1832,39 +1832,8 @@
 %22);%0A
-        m_db = new RrdDb(def);%0A
     
@@ -1904,313 +1904,37 @@
     
-RrdNioBackendFactory factory = (RrdNioBackendFactory) RrdBackendFactory.getDefaultFactory();%0A        final SyncManager syncManager = factory.getSyncManager();%0A        assertNotNull(syncManager);%0A        assertNotNull(syncManager.getTimer());%0A        m_db.close();%0A        assertNull(syncManager.getTimer()
+if (m_db != null) m_db.close(
 );%0A 
@@ -1992,32 +1992,65 @@
 ows Exception %7B%0A
+        m_db = new RrdDb(m_def);%0A
         Sample s
@@ -2366,10 +2366,664 @@
 %7D%0A    %7D%0A
+    %0A    @Test%0A    public void testRrdNioBackend() throws Exception %7B%0A        final RrdNioBackendFactory factory = (RrdNioBackendFactory)RrdBackendFactory.getFactory(%22NIO%22);%0A        m_db = new RrdDb(m_def, factory);%0A        Sample sample = null;%0A        long startTime = System.currentTimeMillis() - 1000;%0A        sample = m_db.createSample();%0A        sample.setAndUpdate(startTime + %22:%22 + m_random.nextInt());%0A%0A        final SyncManager syncManager = factory.getSyncManager();%0A        assertNotNull(syncManager);%0A        assertNotNull(syncManager.getTimer());%0A        m_db.close();%0A        m_db = null;%0A        assertNull(syncManager.getTimer());%0A    %7D%0A
 %7D%0A

fix up test to test NIO issue