/**
 * Copyright (c) 2002-2011 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.server.rrd;

import org.junit.Test;
import org.rrd4j.DsType;
import org.rrd4j.core.Sample;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

public class RrdSamplerTest
{
    @Test
    public void canSampleADatabase()
    {
        Sampleable testSamplable = new TestSamplable( "myTest", 15 );

        Sample sample = mock( Sample.class );

        RrdSampler sampler = new RrdSamplerImpl( sample, testSamplable );
        sampler.updateSample();

        verify( sample ).setValue( "myTest", 15 );
    }

    @Test
    public void shouldIgnoreUnableToSampleExceptions()
    {
        Sampleable failingSampleable = new FailingSamplable( "myTest" );

        Sample sample = mock( Sample.class );

        RrdSampler sampler = new RrdSamplerImpl( sample, failingSampleable );

        sampler.updateSample();

        verify( sample, never() ).setValue( "myTest", 15 );
    }

    private class TestSamplable implements Sampleable
    {
        private String name;
        private double value;

        private TestSamplable( String name, double value )
        {
            this.name = name;
            this.value = value;
        }

        public String getName()
        {
            return name;
        }

        public double getValue()
        {
            return value;
        }

        public DsType getType()
        {
            return DsType.GAUGE;
        }
    }

    private class FailingSamplable implements Sampleable
    {
        private String name;

        private FailingSamplable( String name )
        {
            this.name = name;
        }

        public String getName()
        {
            return name;
        }

        public double getValue()
        {
            throw new UnableToSampleException();
        }

        public DsType getType()
        {
            return DsType.GAUGE;
        }
    }
}

@@ -891,22 +891,80 @@
 4j.core.
-Sample
+RrdDb;%0Aimport org.rrd4j.core.Sample;%0A%0Aimport java.io.IOException
 ;%0A%0Aimpor
@@ -1039,16 +1039,56 @@
 .never;%0A
+import static org.mockito.Mockito.stub;%0A
 import s
@@ -1198,16 +1198,35 @@
 tabase()
+ throws IOException
 %0A    %7B%0A 
@@ -1307,42 +1307,147 @@
     
-Sample sample = mock( Sample.class
+RrdDb rrd = mock( RrdDb.class );%0A        final Sample sample = mock( Sample.class );%0A        stub( rrd.createSample( 0 ) ).toReturn( sample
  );%0A
@@ -1496,22 +1496,19 @@
 erImpl( 
-sample
+rrd
 , testSa
@@ -1673,16 +1673,35 @@
 ptions()
+ throws IOException
 %0A    %7B%0A 
@@ -1785,42 +1785,147 @@
     
-Sample sample = mock( Sample.class
+RrdDb rrd = mock( RrdDb.class );%0A        final Sample sample = mock( Sample.class );%0A        stub( rrd.createSample( 0 ) ).toReturn( sample
  );%0A
@@ -1974,22 +1974,19 @@
 erImpl( 
-sample
+rrd
 , failin

fix text-compiler failure