package proof.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.OutputStream;
import java.io.PrintStream;

import org.junit.Test;

import proof.exception.InvalidConfigurationException;
import proof.exception.UnsupportedSolverException;
import proof.solver.SolverFactory;

/**
 * Tests for {@link Config}.
 *
 * @author Tilo Wiedera
 */
public class ConfigTest {
  private final static String FILE = "src/test/resources/log/invalid/empty.json";
  private final PrintStream out = new PrintStream(new OutputStream() {
    @Override
    public void write(int b) {}
  });

  @Test
  public void testSimple() throws InvalidConfigurationException {
    String[] args = {"-f", FILE};
    Config config = new Config(args, out);

    assertFalse(config.verbose);
    assertEquals(FILE, config.file.toString());
  }

  @Test
  public void testVerbose() throws InvalidConfigurationException {
    String[] args = {"-f", "src/test/resources/log/invalid/empty.json", "-v"};
    Config config = new Config(args, out);

    assertTrue(config.verbose);
  }

  @Test
  public void testSolvers() throws InvalidConfigurationException {
    SolverFactory factory = new SolverFactory();

    try {
      factory.getSolver("gurobi");
      String[] args = {"-f", FILE, "-s", "gurobi"};
      new Config(args, out);
    } catch (UnsupportedSolverException expected) {
    }

    try {
      factory.getSolver("cplex");
      String[] args = {"-f", FILE, "-s", "cplex"};
      new Config(args, out);
    } catch (UnsupportedSolverException expected) {
    }

    try {
      factory.getSolver("scip");
      String[] args = {"-f", FILE, "-s", "scip"};
      new Config(args, out);
    } catch (UnsupportedSolverException expected) {
    }
  }

  @Test(expected = InvalidConfigurationException.class)
  public void testInvalidFile() throws InvalidConfigurationException {
    String[] args = {"-f", "invalid-file-path"};
    new Config(args, out);
  }

  @Test(expected = InvalidConfigurationException.class)
  public void testMissingFile() throws InvalidConfigurationException {
    String[] args = {"-f", "-v"};
    new Config(args, out);
  }

  @Test(expected = InvalidConfigurationException.class)
  public void testEmpty() throws InvalidConfigurationException {
    new Config(new String[0], out);
  }

  @Test(expected = InvalidConfigurationException.class)
  public void testInvalidFlag() throws InvalidConfigurationException {
    String[] args = {"-f", FILE, "some-invalid-flag"};
    new Config(args, out);
  }
}

@@ -146,16 +146,74 @@
 rtTrue;%0A
+import static org.junit.Assert.fail;%0A%0Aimport java.io.File;
 %0Aimport 
@@ -926,20 +926,41 @@
 tEquals(
-FILE
+new File(FILE).toString()
 , config
@@ -2223,32 +2223,227 @@
 tionException %7B%0A
+    String%5B%5D args = %7B%22-f%22%7D;%0A    new Config(args, out);%0A  %7D%0A%0A  @Test(expected = InvalidConfigurationException.class)%0A  public void testMissingFile_badFlag() throws InvalidConfigurationException %7B%0A
     String%5B%5D arg
@@ -2866,11 +2866,794 @@
 ut);%0A  %7D
+%0A%0A  @Test(expected = InvalidConfigurationException.class)%0A  public void testDuplicateArgument() throws InvalidConfigurationException %7B%0A    String%5B%5D args = %7B%22-f%22, FILE, %22-v%22, %22-v%22%7D;%0A    new Config(args, out);%0A  %7D%0A%0A  @Test(expected = InvalidConfigurationException.class)%0A  public void testMissingSolver() throws InvalidConfigurationException %7B%0A    String%5B%5D args = %7B%22-f%22, FILE, %22-s%22%7D;%0A    new Config(args, out);%0A  %7D%0A%0A  @Test%0A  public void testSingleton() throws InvalidConfigurationException %7B%0A    try %7B%0A      assertTrue(Config.get() instanceof Config);%0A      String%5B%5D args = %7B%22-f%22, FILE%7D;%0A      Config.Create(args, out);%0A      fail(%22Configuration must be unique.%22);%0A    %7D catch (InvalidConfigurationException %7C RuntimeException expected) %7B%0A      // this is supposed to happen%0A    %7D%0A  %7D
 %0A%7D%0A

Extend Config Test