<commit_before>/*
 * #%L
 * P6Spy
 * %%
 * Copyright (C) 2013 P6Spy
 * %%
 * 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.
 * #L%
 */
package com.p6spy.engine.spy.option;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.p6spy.engine.common.P6LogQuery;
import com.p6spy.engine.logging.Category;
import com.p6spy.engine.logging.P6LogFactory;
import com.p6spy.engine.logging.P6LogLoadableOptions;
import com.p6spy.engine.logging.P6LogOptions;
import com.p6spy.engine.outage.P6OutageFactory;
import com.p6spy.engine.outage.P6OutageLoadableOptions;
import com.p6spy.engine.outage.P6OutageOptions;
import com.p6spy.engine.spy.P6Factory;
import com.p6spy.engine.spy.P6ModuleManager;
import com.p6spy.engine.spy.P6SpyFactory;
import com.p6spy.engine.spy.P6SpyLoadableOptions;
import com.p6spy.engine.spy.P6SpyOptions;
import com.p6spy.engine.spy.appender.FileLogger;
import com.p6spy.engine.spy.appender.SingleLineFormat;
import com.p6spy.engine.test.BaseTestCase;
import com.p6spy.engine.test.P6TestFramework;

public class P6TestOptionDefaults extends BaseTestCase {

  public static final File LOG_FILE = new File("spy.log");
  
  public static final Category[] DEFAULT_CATEGORIES = new Category[] { 
	  Category.INFO, Category.DEBUG, Category.RESULT, Category.RESULTSET, Category.BATCH };
  
  @SuppressWarnings("unchecked")
  private static final List<Class<? extends P6Factory>> DEFAULT_FACTORIES = Arrays.asList(
      P6SpyFactory.class, P6LogFactory.class);

  @BeforeClass
  public static void setUpAll() throws SQLException, IOException {
    // cleanup all
    LOG_FILE.delete();
    
    // make sure to reinit properly
    new P6TestFramework("blank") {
    };
  }

  @Before
  public void setUp() {
    // make sure to have no modules explicitly loaded by default
    {
      System.clearProperty(SystemProperties.P6SPY_PREFIX + P6SpyOptions.MODULELIST);
      P6SpyOptions.getActiveInstance().reload();
    }
  }
  
  @After
  public void tearDown() throws SQLException, IOException {
    System.getProperties().remove(SystemProperties.P6SPY_PREFIX + P6SpyOptions.MODULELIST);
  }
  
  @AfterClass
  public static void tearDownAll() {
    // post clean up
    LOG_FILE.delete();
  }

  @Test
  public void testDefaultOptions() {
    assertP6FactoryClassesEqual(DEFAULT_FACTORIES, P6ModuleManager.getInstance().getFactories());
    
    Assert.assertNotNull(P6SpyOptions.getActiveInstance());
    Assert.assertNotNull(P6LogOptions.getActiveInstance());
    Assert.assertNull(P6OutageOptions.getActiveInstance());
  }
  
  private void assertP6FactoryClassesEqual(List<Class<? extends P6Factory>> expected,
                                           Collection<P6Factory> factories) {
    final Set<Class<? extends P6Factory>> expectedSet = new HashSet<Class<? extends P6Factory>>(
        expected);

    for (P6Factory factory : factories) {
      expectedSet.remove(factory.getClass());
    }
    Assert.assertTrue(expectedSet.isEmpty());
  }
  
  @Test
  public void testP6SpyOptionDefaults() {
    P6SpyLoadableOptions opts = P6SpyOptions.getActiveInstance();
    Assert.assertNotNull(opts);

    Assert.assertEquals(SingleLineFormat.class.getName(), opts.getLogMessageFormat());
    Assert.assertEquals("spy.log", opts.getLogfile());
    Assert.assertTrue(opts.getAppend());
    Assert.assertNull(opts.getDateformat());
    Assert.assertEquals(FileLogger.class.getName(), opts.getAppender());
    Assert.assertEquals(P6SpyFactory.class.getName() + ","+ P6LogFactory.class.getName(), opts.getModulelist());
    Assert.assertEquals(2, opts.getModuleFactories().size());
    assertP6FactoryClassesEqual(DEFAULT_FACTORIES, opts.getModuleFactories());
    Assert.assertEquals(2, opts.getModuleNames().size());
    Assert.assertTrue(opts.getModuleNames().contains(P6SpyFactory.class.getName()));
    Assert.assertTrue(opts.getModuleNames().contains(P6LogFactory.class.getName()));
    Assert.assertEquals("", opts.getDriverlist());
    Assert.assertNull(opts.getDriverNames());
    Assert.assertFalse(opts.getStackTrace());
    Assert.assertNull(opts.getStackTraceClass());
    Assert.assertFalse(opts.getAutoflush());
    Assert.assertFalse(opts.getReloadProperties());
    Assert.assertEquals(60L, opts.getReloadPropertiesInterval());
    Assert.assertNull(opts.getJNDIContextFactory());
    Assert.assertNull(opts.getJNDIContextProviderURL());
    Assert.assertNull(opts.getJNDIContextCustom());
    Assert.assertNull(opts.getRealDataSource());
    Assert.assertNull(opts.getRealDataSourceClass());
    Assert.assertEquals("dd-MMM-yy", opts.getDatabaseDialectDateFormat());
  }

  @Test<commit_msg>added tests for defaults of jmx and jmxPrefix options<commit_after>/*
 * #%L
 * P6Spy
 * %%
 * Copyright (C) 2013 P6Spy
 * %%
 * 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.
 * #L%
 */
package com.p6spy.engine.spy.option;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.p6spy.engine.common.P6LogQuery;
import com.p6spy.engine.logging.Category;
import com.p6spy.engine.logging.P6LogFactory;
import com.p6spy.engine.logging.P6LogLoadableOptions;
import com.p6spy.engine.logging.P6LogOptions;
import com.p6spy.engine.outage.P6OutageFactory;
import com.p6spy.engine.outage.P6OutageLoadableOptions;
import com.p6spy.engine.outage.P6OutageOptions;
import com.p6spy.engine.spy.P6Factory;
import com.p6spy.engine.spy.P6ModuleManager;
import com.p6spy.engine.spy.P6SpyFactory;
import com.p6spy.engine.spy.P6SpyLoadableOptions;
import com.p6spy.engine.spy.P6SpyOptions;
import com.p6spy.engine.spy.appender.FileLogger;
import com.p6spy.engine.spy.appender.SingleLineFormat;
import com.p6spy.engine.test.BaseTestCase;
import com.p6spy.engine.test.P6TestFramework;

public class P6TestOptionDefaults extends BaseTestCase {

  public static final File LOG_FILE = new File("spy.log");
  
  public static final Category[] DEFAULT_CATEGORIES = new Category[] { 
	  Category.INFO, Category.DEBUG, Category.RESULT, Category.RESULTSET, Category.BATCH };
  
  @SuppressWarnings("unchecked")
  private static final List<Class<? extends P6Factory>> DEFAULT_FACTORIES = Arrays.asList(
      P6SpyFactory.class, P6LogFactory.class);

  @BeforeClass
  public static void setUpAll() throws SQLException, IOException {
    // cleanup all
    LOG_FILE.delete();
    
    // make sure to reinit properly
    new P6TestFramework("blank") {
    };
  }

  @Before
  public void setUp() {
    // make sure to have no modules explicitly loaded by default
    {
      System.clearProperty(SystemProperties.P6SPY_PREFIX + P6SpyOptions.MODULELIST);
      P6SpyOptions.getActiveInstance().reload();
    }
  }
  
  @After
  public void tearDown() throws SQLException, IOException {
    System.getProperties().remove(SystemProperties.P6SPY_PREFIX + P6SpyOptions.MODULELIST);
  }
  
  @AfterClass
  public static void tearDownAll() {
    // post clean up
    LOG_FILE.delete();
  }

  @Test
  public void testDefaultOptions() {
    assertP6FactoryClassesEqual(DEFAULT_FACTORIES, P6ModuleManager.getInstance().getFactories());
    
    Assert.assertNotNull(P6SpyOptions.getActiveInstance());
    Assert.assertNotNull(P6LogOptions.getActiveInstance());
    Assert.assertNull(P6OutageOptions.getActiveInstance());
  }
  
  private void assertP6FactoryClassesEqual(List<Class<? extends P6Factory>> expected,
                                           Collection<P6Factory> factories) {
    final Set<Class<? extends P6Factory>> expectedSet = new HashSet<Class<? extends P6Factory>>(
        expected);

    for (P6Factory factory : factories) {
      expectedSet.remove(factory.getClass());
    }
    Assert.assertTrue(expectedSet.isEmpty());
  }
  
  @Test
  public void testP6SpyOptionDefaults() {
    P6SpyLoadableOptions opts = P6SpyOptions.getActiveInstance();
    Assert.assertNotNull(opts);

    Assert.assertEquals(SingleLineFormat.class.getName(), opts.getLogMessageFormat());
    Assert.assertEquals("spy.log", opts.getLogfile());
    Assert.assertTrue(opts.getAppend());
    Assert.assertNull(opts.getDateformat());
    Assert.assertEquals(FileLogger.class.getName(), opts.getAppender());
    Assert.assertEquals(P6SpyFactory.class.getName() + ","+ P6LogFactory.class.getName(), opts.getModulelist());
    Assert.assertEquals(2, opts.getModuleFactories().size());
    assertP6FactoryClassesEqual(DEFAULT_FACTORIES, opts.getModuleFactories());
    Assert.assertEquals(2, opts.getModuleNames().size());
    Assert.assertTrue(opts.getModuleNames().contains(P6SpyFactory.class.getName()));
    Assert.assertTrue(opts.getModuleNames().contains(P6LogFactory.class.getName()));
    Assert.assertEquals("", opts.getDriverlist());
    Assert.assertNull(opts.getDriverNames());
    Assert.assertFalse(opts.getStackTrace());
    Assert.assertNull(opts.getStackTraceClass());
    Assert.assertFalse(opts.getAutoflush());
    Assert.assertFalse(opts.getReloadProperties());
    Assert.assertEquals(60L, opts.getReloadPropertiesInterval());
    Assert.assertNull(opts.getJNDIContextFactory());
    Assert.assertNull(opts.getJNDIContextProviderURL());
    Assert.assertNull(opts.getJNDIContextCustom());
    Assert.assertNull(opts.getRealDataSource());
    Assert.assertNull(opts.getRealDataSourceClass());
    Assert.assertEquals("dd-MMM-yy", opts.getDatabaseDialectDateFormat());
    Assert.assertTrue(opts.getJmx());
    Assert.assertNull(opts.getJmxPrefix());
  }

  @Test
{'fraction_non_alphanumeric': 0.11566221580656802, 'fraction_numerical': 0.01335258029592205, 'mean_word_length': 4.624048706240487, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '375658', 'n_tokens_mistral': 3410, 'n_tokens_neox': 3378, 'n_words': 643}