/*
 * Copyright (C) 2016 Selerity, Inc. (support@seleritycorp.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.seleritycorp.common.base.test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;

import org.junit.Before;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.seleritycorp.common.base.config.Config;
import com.seleritycorp.common.base.config.EnvironmentConfig;
import com.seleritycorp.common.base.inject.InjectorFactory;

public class InjectingTestCase extends FileTestCase {
  Path basePath;
  SettableStaticClock clock;
  SettableUuidGenerator uuidGenerator;

  @Before
  public void setUpInjector() throws IOException {
    InjectorFactory.forceInjector(Guice.createInjector());

    basePath = createTempDirectory();
    clock = new SettableStaticClock();
    uuidGenerator = new SettableUuidGenerator();
    InjectorFactory.register(new TestModule(basePath, clock, uuidGenerator));
  }

  public Path getBasePath() {
    return basePath;
  }

  public SettableStaticClock getClock() {
    return clock;
  }

  public SettableUuidGenerator getUuidGenerator() {
    return uuidGenerator;
  }

  @Override
  protected void writeFile(Path path, String contents) throws IOException {
    super.writeFile(path, contents);
    FileTime fileTime = FileTime.fromMillis(clock.getMillisEpoch());
    Files.setLastModifiedTime(path, fileTime);
  }
  

  /**
   * Gets a Config object that wraps the OS' environment variables.
   * 
   * @return the config object that wraps the OS' environment variables
   */
  protected Config getEnvironment() {
    Injector injector = InjectorFactory.getInjector();
    Key<Config> key = Key.get(Config.class, EnvironmentConfig.class);
    return injector.getInstance(key);
  }
}

@@ -812,16 +812,71 @@
 .Before;
+%0Aimport org.junit.internal.AssumptionViolatedException;
 %0A%0Aimport
@@ -2425,11 +2425,1835 @@
 ey);%0A  %7D
+%0A%0A  /**%0A   * Checks whether or not the test is running in an CI environment.%0A   * %0A   * %3Cp%3EYou can use this predicate in an %7B@link Assume%7D block to allow skipping tests in%0A   * local/dev environments, but enforce them on CI servers. %0A   * %0A   * @return false, if the test is running in an CI environment. Otherwise, true.%0A   */%0A  protected boolean isRunningOutsideCi() %7B%0A    Config environment = getEnvironment();%0A    return environment.get(%22JENKINS_URL%22, %22%22).isEmpty();%0A  %7D%0A  %0A  /**%0A   * Checks whether or not the test is running in an CI environment.%0A   * %0A   * %3Cp%3EYou can use this predicate in an %7B@link Assume%7D block to allow skipping tests in%0A   * local/dev environments, but enforce them on CI servers. %0A   * %0A   * @return true, if the test is running in an CI environment. Otherwise, false.%0A   */%0A  protected boolean isRunningInsideCi() %7B%0A    return !isRunningOutsideCi();%0A  %7D%0A  %0A  /**%0A   * Fail the test inside CI environments, skip the test otherwise.%0A   * %0A   * %3Cp%3EThis method helps to to abort tests that have unmet external dependencies%0A   * (e.g.: Databases, network connection, ...). As not all developers can be bothered to setup%0A   * all dependencies, the test is skipped for developers. But in CI environments, where we expect%0A   * that all external dependencies are met, the test will fail hard.%0A   * %0A   * @param reason The reason for the skip/failure.%0A   * @throws Throwable always. Thereby the test is skipped/fails.%0A   */%0A  protected void failInCiSkipOtherwise(String reason) throws Throwable %7B%0A    final Throwable t;%0A    if (isRunningInsideCi()) %7B%0A      if (reason != null) %7B%0A        t = new AssertionError(reason);%0A      %7D else %7B %0A        t = new AssertionError();%0A      %7D%0A    %7D else %7B%0A      t = new AssumptionViolatedException((reason != null) ? reason : %22%3Cno reason given%3E%22);%0A    %7D%0A    throw t;%0A  %7D
 %0A%7D%0A

Add helpers for tests to check/skip/fail if running inside/outside CI