package com.codahale.shore.test;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.util.Properties;
import org.hibernate.cfg.Environment;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import com.codahale.shore.AbstractConfiguration;
import com.codahale.shore.SchemaCommand;
import com.codahale.shore.integration.test.IntegrationTestConfig;
@RunWith(Enclosed.class)
public class SchemaCommandTest {
public static class A_Schema_Command {
private Properties properties;
private OutputStream output;
private SchemaCommand cmd;
private AbstractConfiguration configuration;
@Before
public void setup() throws Exception {
this.properties = new Properties();
this.output = mock(OutputStream.class);
this.configuration = new IntegrationTestConfig();
this.cmd = new SchemaCommand(configuration, properties, true, output);
}
@Test
public void itHasAConfiguration() throws Exception {
assertThat(cmd.getConfiguration(), is(sameInstance(configuration)));
}
@Test
public void itHasProperties() throws Exception {
assertThat(cmd.getProperties(), is(sameInstance(properties)));
}
@Test
public void itHasAnOutputStream() throws Exception {
assertThat(cmd.getOutputStream(), is(sameInstance(output)));
}
}
public static class Generating_A_Full_Schema {
private Properties properties;
private OutputStream output;
private SchemaCommand cmd;
private IntegrationTestConfig configuration;
@Before
public void setup() throws Exception {
this.properties = new Properties();
properties.load(new FileReader("src/test/resources/hsql-memory.properties"));
properties.setProperty(Environment.URL, "jdbc:hsqldb:mem:ShoreSchemaTest");
this.output = new ByteArrayOutputStream();
this.configuration = new IntegrationTestConfig();
this.cmd = new SchemaCommand(configuration, properties, false, output);
}
@Test
public void itGeneratesADropAndCreateSQLScript() throws Exception {
cmd.run();
assertThat(output.toString(), is(
"/* full drop-and-create script */\n" +
"drop table widgets if exists;\n" +
"create table widgets (id integer generated by default as" +
" identity (start with 1), description varchar(255)," +
" name varchar(255), primary key (id));\n"
));
}
}
public static class Generating_A_Migration_Schema {
private Properties properties;
private OutputStream output;
private SchemaCommand cmd;
private IntegrationTestConfig configuration;
@Before
public void setup() throws Exception {
this.properties = new Properties();
properties.load(new FileReader("src/test/resources/hsql-memory.properties"));
properties.setProperty(Environment.URL, "jdbc:hsqldb:mem:ShoreMigrationTest");
this.output = new ByteArrayOutputStream();
this.configuration = new IntegrationTestConfig();
this.cmd = new SchemaCommand(configuration, properties, true, output);
}
@Test
public void itGeneratesAMigrationSQLScript() throws Exception {
cmd.run();
assertThat(output.toString(), is(
"/* migration script */\n" +
"create table widgets (id integer generated by default as" +
" identity (start with 1), description varchar(255)," +
" name varchar(255), primary key (id));\n"
));
}
}
public static class Unable_To_Connect_To_The_DB {
private Properties properties;
private OutputStream output;
private SchemaCommand cmd;
private IntegrationTestConfig configuration;
@Before
public void setup() throws Exception {
this.properties = new Properties();
properties.load(new FileReader("src/test/resources/hsql-memory.properties"));
properties.setProperty(Environment.DRIVER, "org.hsqldbFAKE.jdbcDriver");
this.output = new ByteArrayOutputStream();
this.configuration = new IntegrationTestConfig();
this.cmd = new SchemaCommand(configuration, properties, true, output);
}
@Test
public void itDisplaysAnErrorMessage() throws Exception {
cmd.run();
assertThat(output.toString().startsWith("Error: unable to connect to the database.\n"), is(true));
}
}
}