/*
* Copyright 2013 Objectos, Fábrica de Software LTDA.
*
* 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 br.com.objectos.way.dbunit;
import static com.google.common.collect.Sets.newHashSet;
import java.util.Set;
import org.dbunit.IDatabaseTester;
import org.dbunit.JdbcDatabaseTester;
import br.com.objectos.way.base.sql.Credential;
import com.google.common.base.Throwables;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.Singleton;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class WayDBUnitModuleBuilderMysql extends AbstractModule implements WayDBUnitModuleBuilder {
private final Credential credential;
private final Set<String> catalogs = newHashSet();
public WayDBUnitModuleBuilderMysql(Credential credential) {
this.credential = credential;
}
@Override
public WayDBUnitModuleBuilder addCatalog(String catalog) {
catalogs.add(catalog);
return this;
}
@Override
public Module buildModule() {
return this;
}
@Override
protected void configure() {
install(new WayDBUnitModuleCore());
MysqlConfig config = new MysqlConfig(catalogs);
bind(VendorConfig.class).toInstance(config);
}
@Provides
@Singleton
IDatabaseTester getTester() {
try {
String driverClass = credential.getDriverClass();
String url = credential.getUrl();
String username = credential.getUser();
String password = credential.getPassword();
return new JdbcDatabaseTester(driverClass, url, username, password);
} catch (ClassNotFoundException e) {
throw Throwables.propagate(e);
}
}
}