Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed @Expected annotation #3

Merged
merged 2 commits into from
Nov 24, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 18 additions & 18 deletions api/src/main/java/org/jboss/arquillian/persistence/Expected.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* 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,
* 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.
Expand All @@ -27,34 +27,34 @@


/**
* Verifies state of underlying data store using data sets
* Verifies state of underlying data store using data sets
* defined by this annotation. Verification is invoked after test's
* execution (including transaction if enabled).
*
*
* <br />
* If files are not specified explicitly, following strategy is applied:
* <ul>
* <li>Assumption that files are stored in <code>datasets</code> folder.</li>
* <li>
* If files are not specified explicitly, following strategy is applied:
* <ul>
* <li>Assumption that files are stored in <code>datasets</code> folder.</li>
* <li>
* If {@link Data} annotation is defined on method level, file name has following format:
* <i>expected-[fully qualified class name]#[test method name].[default format]</i>.
* </li>
* <li>
* If {@link Data} annotation is defined on class level, file name has following format:
* <i>expected-[fully qualified class name]#.[default format]</i></li>.
* </ul>
* <br /><br />
* If not specified in <code>arquillian.xml</code>, then expected format is XML.
*
* <i>expected-[fully qualified class name]#[test method name].[default format]</i>.
* </li>
* <li>
* If {@link Data} annotation is defined on class level, file name has following format:
* <i>expected-[fully qualified class name]#.[default format]</i></li>.
* </ul>
* <br /><br />
* If not specified in <code>arquillian.xml</code>, then expected format is XML.
*
* Presence of this annotation in the test class will enable Arquillian Persistence Extension.
*
*
* @author <a href="mailto:bartosz.majsak@gmail.com">Bartosz Majsak</a>
*
*/
@Target({TYPE, METHOD})
@Retention(RUNTIME)
@Inherited
public @interface Expected
public @interface Expected
{

String[] value() default "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* 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,
* 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.
Expand All @@ -37,6 +37,7 @@
import org.jboss.arquillian.persistence.metadata.DataSetProvider;
import org.jboss.arquillian.persistence.metadata.MetadataExtractor;
import org.jboss.arquillian.persistence.metadata.MetadataProvider;
import org.jboss.arquillian.persistence.test.AssertionErrorCollector;
import org.jboss.arquillian.test.spi.annotation.ClassScoped;
import org.jboss.arquillian.test.spi.annotation.TestScoped;
import org.jboss.arquillian.test.spi.event.suite.After;
Expand All @@ -48,50 +49,57 @@ public class PersistenceTestHandler

@Inject
private Instance<PersistenceConfiguration> configuration;

@Inject @ClassScoped
private InstanceProducer<MetadataExtractor> metadataExtractor;

@Inject @TestScoped
private InstanceProducer<javax.sql.DataSource> dataSourceProducer;


@Inject @TestScoped
private InstanceProducer<AssertionErrorCollector> assertionErrorCollectorProducer;

@Inject
private Event<PrepareData> prepareDataEvent;

@Inject
private Event<CompareData> compareDataEvent;

@Inject
private Event<CleanUpData> cleanUpDataEvent;

@Inject
private Event<TransactionStarted> transactionStartedEvent;

@Inject
private Event<TransactionFinished> transactionFinishedEvent;

@Inject
private Instance<Context> contextInst;

public void beforeSuite(@Observes BeforeClass beforeClass)
{
metadataExtractor.set(new MetadataExtractor(beforeClass.getTestClass()));
}

public void beforeTest(@Observes Before beforeTestEvent)
{
MetadataProvider metadataProvider = new MetadataProvider(beforeTestEvent.getTestMethod(), metadataExtractor.get(), configuration.get());
if (!metadataProvider.isPersistenceFeatureEnabled())
{
return;
}
assertionErrorCollectorProducer.set(new AssertionErrorCollector());

String dataSourceName = metadataProvider.getDataSourceName();
dataSourceProducer.set(loadDataSource(dataSourceName));

DataSetProvider dataSetProvider = new DataSetProvider(metadataExtractor.get(), configuration.get());
prepareDataEvent.fire(new PrepareData(beforeTestEvent, dataSetProvider.getDataSetDescriptors(beforeTestEvent.getTestMethod())));


if (metadataProvider.isDataSeedOperationRequested())
{
DataSetProvider dataSetProvider = new DataSetProvider(metadataExtractor.get(), configuration.get());
prepareDataEvent.fire(new PrepareData(beforeTestEvent, dataSetProvider.getDataSetDescriptors(beforeTestEvent.getTestMethod())));
}

if (metadataProvider.isTransactional())
{
transactionStartedEvent.fire(new TransactionStarted(beforeTestEvent));
Expand All @@ -105,23 +113,24 @@ public void afterTest(@Observes After afterTestEvent)
{
return;
}

if (metadataProvider.isTransactional())
{
transactionFinishedEvent.fire(new TransactionFinished(afterTestEvent));
}

if (metadataProvider.isDataVerificationExpected())
if (metadataProvider.isDataVerificationRequested())
{
DataSetProvider dataSetProvider = new DataSetProvider(metadataExtractor.get(), configuration.get());
compareDataEvent.fire(new CompareData(afterTestEvent, dataSetProvider.getExpectedDataSetDescriptors(afterTestEvent.getTestMethod())));
}

cleanUpDataEvent.fire(new CleanUpData(afterTestEvent));
assertionErrorCollectorProducer.get().report();
}

// Private methods

private DataSource loadDataSource(String dataSourceName)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* 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,
* 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.
Expand All @@ -22,8 +22,13 @@

import org.dbunit.Assertion;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.dataset.Column;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.SortedTable;
import org.dbunit.dataset.SortedTable.AbstractRowComparator;
import org.dbunit.dataset.datatype.DataType;
import org.dbunit.dataset.datatype.TypeCastException;
import org.dbunit.operation.DatabaseOperation;
import org.dbunit.operation.TransactionOperation;
import org.jboss.arquillian.core.api.Instance;
Expand All @@ -36,19 +41,23 @@
import org.jboss.arquillian.persistence.event.CleanUpData;
import org.jboss.arquillian.persistence.event.CompareData;
import org.jboss.arquillian.persistence.event.PrepareData;
import org.jboss.arquillian.persistence.test.AssertionErrorCollector;

public class DBUnitDatasetHandler implements DataHandler
{

@Inject
private Instance<DatabaseConnection> databaseConnection;

@Inject
private Instance<DataSetRegister> dataSetRegister;

@Inject
private Instance<PersistenceConfiguration> configuration;

private Instance<PersistenceConfiguration> configuration;

@Inject
private Instance<AssertionErrorCollector> assertionErrorCollector;

@Override
public void prepare(@Observes PrepareData prepareDataEvent)
{
Expand All @@ -73,18 +82,26 @@ public void compare(@Observes CompareData compareDataEvent)
String[] tableNames = expectedDataSet.getTableNames();
for (String tableName : tableNames)
{
ITable currentTableState = currentDataSet.getTable(tableName);
ITable expectedTableState = expectedDataSet.getTable(tableName);
String[] columnsToIgnore = DataSetUtils.columnsNotSpecifiedInExpectedDataSet(expectedTableState,currentTableState);
Assertion.assertEqualsIgnoreCols(expectedTableState, currentTableState, columnsToIgnore);
SortedTable expectedTableState = new SortedTable(expectedDataSet.getTable(tableName));
SortedTable currentTableState = new SortedTable(currentDataSet.getTable(tableName), expectedTableState.getTableMetaData());
String[] columnsToIgnore = DataSetUtils.columnsNotSpecifiedInExpectedDataSet(expectedTableState, currentTableState);
try
{
Assertion.assertEqualsIgnoreCols(expectedTableState, currentTableState, columnsToIgnore);
}
catch (AssertionError error)
{
assertionErrorCollector.get().collect(error);
}

}
}
catch (Exception e)
{
throw new DBUnitDataSetHandlingException(e);
throw new DBUnitDataSetHandlingException(e);
}
}

@Override
public void cleanup(@Observes CleanUpData cleanupDataEvent)
{
Expand Down Expand Up @@ -114,8 +131,8 @@ private void applyInitStatement()
catch (Exception e)
{
throw new DBUnitDataSetHandlingException(e);
}
finally
}
finally
{
if (initStatement != null)
{
Expand Down Expand Up @@ -144,5 +161,20 @@ private void cleanDatabase() throws Exception
IDataSet dataSet = connection.createDataSet();
new TransactionOperation(DatabaseOperation.DELETE_ALL).execute(connection, dataSet);
}



private static class RowComparator extends AbstractRowComparator {

public RowComparator(ITable table, Column[] sortColumns) {
super(table, sortColumns);
}

protected int compare(Column column, Object value1, Object value2)
throws TypeCastException {
DataType dataType = column.getDataType();
int result = dataType.compare(value1, value2);
return result;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* 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,
* 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.
Expand Down Expand Up @@ -42,7 +42,7 @@
import org.jboss.arquillian.test.spi.annotation.TestScoped;

/**
*
*
* @author Bartosz Majsak
*
*/
Expand All @@ -57,11 +57,11 @@ public class DBUnitPersistenceTestLifecycleHandler

@Inject @TestScoped
private InstanceProducer<DataSetRegister> dataSetRegisterProducer;

// ------------------------------------------------------------------------------------------------
// Intercepting data handling events
// ------------------------------------------------------------------------------------------------

public void initializeDataSeeding(@Observes(precedence = 1000) EventContext<PrepareData> context)
{
PrepareData prepareDataEvent = context.getEvent();
Expand All @@ -72,11 +72,12 @@ public void initializeDataSeeding(@Observes(precedence = 1000) EventContext<Prep

public void initializeDataVerification(@Observes(precedence = 1000) EventContext<CompareData> context)
{
CompareData compareDataEvent = context.getEvent();
CompareData compareDataEvent = context.getEvent();
createDatabaseConnection();
createExpectedDataSets(compareDataEvent.getDataSetDescriptors());
context.proceed();
}

public void closeConnection(@Observes(precedence = 1000) EventContext<CleanUpData> context)
{
try
Expand All @@ -89,11 +90,16 @@ public void closeConnection(@Observes(precedence = 1000) EventContext<CleanUpDat
throw new DBUnitConnectionException("Unable to close connection", e);
}
}

// ------------------------------------------------------------------------------------------------

private void createDatabaseConnection()
{
{
if (databaseConnectionProducer.get() != null)
{
return;
}

try
{
DataSource dataSource = databaseSourceInstance.get();
Expand All @@ -119,7 +125,7 @@ private void createInitialDataSets(List<DataSetDescriptor> dataSetDescriptors)
}
dataSetRegisterProducer.set(dataSetRegister);
}

private IDataSet createInitialDataSet(DataSetDescriptor dataSetDescriptor)
{
final String file = dataSetDescriptor.getFileName();
Expand Down Expand Up @@ -154,5 +160,5 @@ private DataSetRegister getOrCreateDataSetRegister()
}
return dataSetRegister;
}

}