/**
 * Copyright (C) 2015 Sandor Nemeth (sandor.nemeth.1986@gmail.com)
 *
 * Licensed 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 com.sandornemeth.metrics.spring.autoconfigure;

import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.context.annotation.Configuration;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class CsvReporterConfigurationTest extends AbstractMetricsAutoconfigurationTestSupport {

  @Rule
  public TemporaryFolder temporaryFolder = new TemporaryFolder();

  @Test
  public void shouldNotLoadACsvReporterIfNoneConfigured() {
    this.load(CsvReporterTestConfiguration.class);
  }

  @Test
  public void csvReporterShouldBeActivatedOnConfiguration() throws InterruptedException {
    String[] contents = temporaryFolder.getRoot().list();
    assertThat(contents.length, is(0));
    this.load(CsvReporterTestConfiguration.class, "spring.metrics.reporters.csv.enabled=true",
              "spring.metrics.reporters.csv.formatFor=US",
              "spring.metrics.reporters.csv.reportFolder=" + temporaryFolder.getRoot()
                  .getAbsolutePath());

    // sleep a little bit and verify if report files are generated.
    Thread.sleep(2000);
    String[] loggedContents = temporaryFolder.getRoot().list((f, name) -> name.endsWith(".csv"));
    assertThat(loggedContents.length, Matchers.greaterThan(0));
  }

  @Configuration
  protected static class CsvReporterTestConfiguration {

  }

}

@@ -133,16 +133,19 @@
 cense%22);
+%0A *
  you may
@@ -169,19 +169,16 @@
 e except
-%0A *
  in comp
@@ -201,16 +201,19 @@
 License.
+%0A *
  You may
@@ -250,16 +250,24 @@
 at%0A *%0A *
+        
  http://
@@ -375,16 +375,19 @@
 software
+%0A *
  distrib
@@ -408,19 +408,16 @@
  License
-%0A *
  is dist
@@ -444,16 +444,19 @@
 %22 BASIS,
+%0A *
  WITHOUT
@@ -508,19 +508,16 @@
  express
-%0A *
  or impl
@@ -520,16 +520,19 @@
 implied.
+%0A *
  See the
@@ -591,16 +591,19 @@
 ions and
+%0A *
  limitat
@@ -612,19 +612,16 @@
 ns under
-%0A *
  the Lic
@@ -1194,58 +1194,1143 @@
 d() 
-%7B%0A    this.load(CsvReporterTestConfiguration.class
+throws InterruptedException %7B%0A    String%5B%5D contents = temporaryFolder.getRoot().list();%0A    assertThat(contents.length, is(0));%0A    this.load(CsvReporterTestConfiguration.class);%0A%0A    // sleep a little bit and verify if report files are generated.%0A    Thread.sleep(2000);%0A    String%5B%5D loggedContents = temporaryFolder.getRoot().list((f, name) -%3E name.endsWith(%22.csv%22));%0A    assertThat(loggedContents.length, is(0));%0A  %7D%0A%0A  @Test public void shouldNotLoadCsvReporterWhenExplicitlyNotEnabled() throws InterruptedException %7B%0A    String%5B%5D contents = temporaryFolder.getRoot().list();%0A    assertThat(contents.length, is(0));%0A    this.load(CsvReporterTestConfiguration.class, %22spring.metrics.reporters.csv.enabled=false%22,%0A              %22spring.metrics.reporters.csv.formatFor=US%22,%0A              %22spring.metrics.reporters.csv.reportFolder=%22 + temporaryFolder.getRoot()%0A                  .getAbsolutePath());%0A%0A    // sleep a little bit and verify if report files are generated.%0A    Thread.sleep(2000);%0A    String%5B%5D loggedContents = temporaryFolder.getRoot().list((f, name) -%3E name.endsWith(%22.csv%22));%0A    assertThat(loggedContents.length, is(0)
 );%0A 

new tests for the csv reporter