1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
package com.zz.service;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dbunit.database.DatabaseDataSourceConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ReplacementDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.util.StringUtils;
import com.zz.sub.commons.exception.subDomainException;
import com.zz.prj.entity.Flux;
import com.zz.prj.util.DataSetLocation;
import com.excilys.ebi.spring.dbunit.test.DataSetTestExecutionListener;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:config/ioc/application-context-spring-test.xml" })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, CleanInsertTestExecutionListener3A.class, TransactionalTestExecutionListener.class, })
@TransactionConfiguration(transactionManager = "myTxManager", defaultRollback = true)
public class FluxDSImplTest36466 {
private static final Log log = LogFactory.getLog(FluxDSImplTest36466.class);
private static final String user = "test";
private static final Date date = new Date(0);
private static final boolean ON_QMANAGER = false;
private static final boolean WITH_BINDINGS = false;
private static final boolean SEND_QDN = false;
private static final boolean FORCEONQMANAGER = false;
@PersistenceContext
private EntityManager entityManager;
@Inject
private PlatformTransactionManager myTxManager;
@Autowired
private IFluxDS fluxDS;
// Chargement gros flux
@Test
public void testSauvegardeGrosFlux1() throws subDomainException {
// on modifie le flux dans une transaction et on le sauvegarde
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = null;
try {
status = myTxManager.getTransaction(def);
// entityManager.createNativeQuery("SET @@foreign_key_checks = 0;").executeUpdate();
// entityManager.flush();
Flux flux = entityManager.find(Flux.class, 36466L);
Flux xx = fluxDS.createOrUpdate(flux, ON_QMANAGER, WITH_BINDINGS, SEND_QDN, FORCEONQMANAGER, date, user);
} finally {
if (status != null && !status.isCompleted()) {
myTxManager.rollback(status);
}
entityManager.clear();
}
}
}
class CleanInsertTestExecutionListener3A implements TestExecutionListener {
private static final Logger LOG = LoggerFactory.getLogger(CleanInsertTestExecutionListener3A.class);
private static final String fn = "/dataset/creationFluxUSTest/PRD36466.xml";
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
// location of the data set
String dataSetResourcePath = null;
// first, the annotation on the test class
DataSetLocation dsLocation = testContext.getTestInstance().getClass().getAnnotation(DataSetLocation.class);
if (dsLocation != null) {
// found the annotation
dataSetResourcePath = dsLocation.value();
LOG.info("annotated test, using data set: {} ", dataSetResourcePath);
} else {
// no annotation, lets try with the name of the test
String tempDsRes = testContext.getTestInstance().getClass().getName();
// tempDsRes = StringUtils.replace(tempDsRes, " . ", " / ");
tempDsRes = fn;
if (getClass().getResourceAsStream(tempDsRes) != null) {
LOG.info(" detected default dataset: {} ", tempDsRes);
dataSetResourcePath = tempDsRes;
} else {
LOG.info(" no default dataset ");
}
}
if (dataSetResourcePath != null) {
Resource dataSetResource = testContext.getApplicationContext().getResource(dataSetResourcePath);
FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
builder.setColumnSensing(true);
IDataSet dataSet = builder.build(dataSetResource.getInputStream());
ReplacementDataSet replaceDataSet = new ReplacementDataSet(dataSet);
replaceDataSet.addReplacementObject("[NULL]", null);
replaceDataSet.addReplacementObject("[null]", null);
replaceDataSet.addReplacementObject("[Xnull]", null);
Calendar cal = Calendar.getInstance(Locale.getDefault());
replaceDataSet.addReplacementObject("[NOW]", cal.getTime());
IDatabaseConnection dbConn = new DatabaseDataSourceConnection(testContext.getApplicationContext().getBean(DataSource.class));
// BAD TEST
// dbConn.getConnection().prepareStatement("set @@session.foreign_key_checks = 0").execute();
// dbConn.getConnection().prepareStatement("set referential_integrity FALSE").execute();
// dbConn.getConnection().prepareStatement("set foreign_key_checks=0;").execute();
// dbConn.getConnection().prepareStatement("set foreign_key_checks=0").execute();
// dbConn.getConnection().createStatement().execute("SET FOREIGN_KEY_CHECKS=0;");
DatabaseOperation.CLEAN_INSERT.execute(dbConn, replaceDataSet);
} else {
LOG.info(" {} does not have any data set, no data injection ", testContext.getClass().getName());
}
}
@Override
public void beforeTestClass(TestContext testContext) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void afterTestClass(TestContext testContext) throws Exception {
// TODO Auto-generated method stub
}
// (
) the remaining methods of the interface are not used
} |
Partager