<commit_before>/*-
 * #%L
 * Bobcat
 * %%
 * Copyright (C) 2018 Cognifide Ltd.
 * %%
 * 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.cognifide.qa.bb.junit5.allure;

import static com.cognifide.qa.bb.junit5.JUnit5Constants.NAMESPACE;
import static com.cognifide.qa.bb.junit5.allure.AllureConstants.ALLURE_CREATE_ENVIROMENT;
import static com.cognifide.qa.bb.junit5.allure.AllureConstants.ALLURE_REPORT;

import com.cognifide.qa.bb.junit5.guice.InjectorUtils;
import com.google.inject.Injector;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.LoggerFactory;

/**
 * Creates enviroment file for report
 */
public class EnviromentInfoExtension implements AfterAllCallback {

  private static final String ENVIROMENT_FILE = "environment.properties";

  private static final org.slf4j.Logger LOG = LoggerFactory
      .getLogger(EnviromentInfoExtension.class);

  @Override
  public void afterAll(ExtensionContext context) throws Exception {
    Injector injector = InjectorUtils.retrieveInjectorFromStore(context, NAMESPACE);
    if (injector != null && injector.getInstance(Properties.class)
        .getProperty(ALLURE_REPORT, "false").equals("true") && injector
        .getInstance(Properties.class).getProperty(ALLURE_CREATE_ENVIROMENT, "false")
        .equals("true")) {
      prepareEnviromentFile(injector.getInstance(Properties.class));
    }
  }

  private void prepareEnviromentFile(Properties properties) {
    StringBuilder enviromentProperties = new StringBuilder();
    properties.stringPropertyNames().stream().forEach(propertyName -> {
      enviromentProperties.append(propertyName);
      enviromentProperties.append("=");
      enviromentProperties.append(properties.getProperty(propertyName));
      enviromentProperties.append("\n");
    });

    System.getProperty("allure.results.directory");
    File enviromentFile = new File(
        System.getProperty("allure.results.directory"), ENVIROMENT_FILE);
    try {
      FileUtils.writeStringToFile(enviromentFile, enviromentProperties.toString());
    } catch (IOException e) {<commit_msg>#265 fix for properties<commit_after>/*-
 * #%L
 * Bobcat
 * %%
 * Copyright (C) 2018 Cognifide Ltd.
 * %%
 * 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.cognifide.qa.bb.junit5.allure;

import static com.cognifide.qa.bb.junit5.JUnit5Constants.NAMESPACE;
import static com.cognifide.qa.bb.junit5.allure.AllureConstants.ALLURE_CREATE_ENVIROMENT;
import static com.cognifide.qa.bb.junit5.allure.AllureConstants.ALLURE_REPORT;

import com.cognifide.qa.bb.junit5.guice.InjectorUtils;
import com.google.inject.Injector;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.LoggerFactory;

/**
 * Creates enviroment file for report
 */
public class EnviromentInfoExtension implements AfterAllCallback {

  private static final String ENVIROMENT_FILE = "environment.properties";

  private static final org.slf4j.Logger LOG = LoggerFactory
      .getLogger(EnviromentInfoExtension.class);

  @Override
  public void afterAll(ExtensionContext context) throws Exception {
    Injector injector = InjectorUtils.retrieveInjectorFromStore(context, NAMESPACE);
    if (injector != null && injector.getInstance(Properties.class)
        .getProperty(ALLURE_REPORT, "false").equals("true") && injector
        .getInstance(Properties.class).getProperty(ALLURE_CREATE_ENVIROMENT, "false")
        .equals("true")) {
      prepareEnviromentFile(injector.getInstance(Properties.class));
    }
  }

  private void prepareEnviromentFile(Properties properties) {

    File enviromentFile = new File(
        System.getProperty("allure.results.directory"), ENVIROMENT_FILE);
    try (FileOutputStream propertiesOutputStream = new FileOutputStream(enviromentFile)) {
      properties.store(propertiesOutputStream, null);
    } catch (IOException e) {
{'fraction_non_alphanumeric': 0.09967538667175864, 'fraction_numerical': 0.006683215581439756, 'mean_word_length': 4.626208378088077, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '380436', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1528, 'n_words': 371}