package com.adamlewis.guice.persist.jooq;

import java.util.Set;

import com.adamlewis.guice.persist.jooq.modules.ConfigurationModule;
import com.adamlewis.guice.persist.jooq.modules.DataSourceModule;
import com.adamlewis.guice.persist.jooq.modules.SettingsModule;
import com.google.common.collect.Sets;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import org.jooq.Configuration;
import org.jooq.conf.BackslashEscaping;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class JooqPersistServiceTest {

  private Injector injector;

  @Before
  public void setup() {
    injector = null;
  }

  @Test
  public void canCreateWithoutConfiguration() {
    JooqPersistService jooqPersistService = givenJooqPersistServiceWithModule();
    jooqPersistService.begin();

    assertEquals(DataSourceModule.DEFAULT_DIALECT, jooqPersistService.get().configuration().dialect());
  }

  @Test
  public void canProvideAConfiguration() {
    JooqPersistService jooqPersistService = givenJooqPersistServiceWithModule(new ConfigurationModule());
    jooqPersistService.begin();

    assertEquals(injector.getInstance(Configuration.class), jooqPersistService.get().configuration());
  }

  @Test
  public void canProvideSettings() {
    JooqPersistService jooqPersistService = givenJooqPersistServiceWithModule(new SettingsModule());
    jooqPersistService.begin();

    // We can't assert on Settings.equals() because jooq clones the Settings instance and Settings does not override equals().
    assertEquals(SettingsModule.ESCAPING, jooqPersistService.get().settings().getBackslashEscaping());
  }

  @Test
  public void canProvideSettingsAndConfigurationButSettingsIsIgnored() {
    JooqPersistService jooqPersistService = givenJooqPersistServiceWithModule(new ConfigurationModule(), new SettingsModule());
    jooqPersistService.begin();

    assertEquals(injector.getInstance(Configuration.class), jooqPersistService.get().configuration());
    assertEquals(BackslashEscaping.DEFAULT, jooqPersistService.get().settings().getBackslashEscaping());
  }

  @Test(expected = IllegalStateException.class)
  public void throwsIfUnitOfWorkIsNotStarted() {
    JooqPersistService jooqPersistService = givenJooqPersistServiceWithModule();

    jooqPersistService.get();
  }

  private JooqPersistService givenJooqPersistServiceWithModule(Module... modules) {
    Set<Module> moduleList = Sets.newHashSet(modules);
    moduleList.add(new JooqPersistModule());
    moduleList.add(new DataSourceModule());
    injector = Guice.createInjector(moduleList);
    return injector.getInstance(JooqPersistService.class);
  }
}
@@ -53,19 +53,17 @@
 va.util.
-Set
+*
 ;%0A%0Aimpor
@@ -260,47 +260,8 @@
 le;%0A
-import com.google.common.collect.Sets;%0A
 impo
@@ -2417,23 +2417,41 @@
 t = 
-Sets.
 new
+ 
 HashSet
+%3CModule%3E(Arrays.asList
 (mod
@@ -2455,16 +2455,17 @@
 modules)
+)
 ;%0A    mo

remove Guava usage from tests