import org.splat.dal.bo.som.Scenario;
import org.splat.dal.bo.som.Study;
import org.splat.dal.bo.som.Document.Properties;
+import org.splat.dal.bo.som.UsedByRelation;
+import org.splat.dal.bo.som.UsesRelation;
import org.splat.dal.dao.kernel.UserDAO;
import org.splat.dal.dao.som.Database;
import org.splat.dal.dao.som.ScenarioDAO;
}
/**
- * Test of generating a study document index.<BR>
- * <B>Description :</B> <BR>
- * <i>Create a study and try to generate the next document index.</i><BR>
- * <B>Action : </B><BR>
- * <i>1. call DAO's read method for an existing id.</i><BR>
- * <B>Test data : </B><BR>
- * <i>no input parameters</i><BR>
- *
- * <B>Outcome results:</B><BR>
- * <i>
- * <ul>
- * <li>The new index must be equal to the incremented old one and saved into the database<BR>
- * </li>
- * </ul>
- * </i>
- *
- * @throws InvalidPropertyException
- * if an invalid property is used when creating objects
- * @throws MultiplyDefinedException
- * when trying to create an object with already existing id
- * @throws MissedPropertyException
- * if a mandatory property is not defined for an object to be created
- * @throws SQLException
- * if test study creation is failed
- * @throws IOException
- * if test study creation is failed
- * @throws ParseException
- * @throws InterruptedException
- * @throws NotApplicableException
- *
- */
+ * Test of generating a study document index.<BR>
+ * <B>Description :</B> <BR>
+ * <i>Create a study and try to generate the next document index.</i><BR>
+ * <B>Action : </B><BR>
+ * <i>1. call DAO's read method for an existing id.</i><BR>
+ * <B>Test data : </B><BR>
+ * <i>no input parameters</i><BR>
+ *
+ * <B>Outcome results:</B><BR>
+ * <i>
+ * <ul>
+ * <li>The new index must be equal to the incremented old one and saved into the database<BR>
+ * </li>
+ * </ul>
+ * </i>
+ *
+ * @throws InvalidPropertyException
+ * if an invalid property is used when creating objects
+ * @throws MultiplyDefinedException
+ * when trying to create an object with already existing id
+ * @throws MissedPropertyException
+ * if a mandatory property is not defined for an object to be created
+ * @throws SQLException
+ * if test study creation is failed
+ * @throws IOException
+ * if test study creation is failed
+ * @throws ParseException
+ * @throws InterruptedException
+ * @throws NotApplicableException
+ *
+ */
@Test
- public void testCreateDoc() throws InvalidPropertyException,
- MissedPropertyException, MultiplyDefinedException, IOException,
- SQLException, NotApplicableException, InterruptedException,
- ParseException {
- LOG.debug(">>>>> BEGIN testCreateDoc()");
- startNestedTransaction();
-
- HibernateTemplate ht = getHibernateTemplate();
- Study aStudy = createStudy();
- // Call DAO's create method for a good transient study.
- Long id = aStudy.getIndex();
- Assert.assertNotNull(id,
- "Create method returns null instead of a new id.");
- Assert.assertTrue(id > 0, "The new id is not a positive number.");
-
- // Call DAO's get method for an existing id.
- _studyDAO.flush();
- getHibernateTemplate().evict(aStudy);
- getHibernateTemplate().clear();
- Study aStudyFound = _studyDAO.get(id);
- ht.evict(aStudyFound);
-
- long userId = aStudyFound.getAuthor().getIndex();
- org.splat.som.Step[] studySteps = _projectElementService
- .getSteps(aStudyFound);
- org.splat.som.Step[] scSteps = _projectElementService
- .getSteps(aStudyFound.getScenarii()[0]);
-
- List<org.splat.som.Step> steps = new ArrayList<org.splat.som.Step>();
- steps.addAll(Arrays.asList(studySteps));
- steps.addAll(Arrays.asList(scSteps));
- steps.addAll(Arrays.asList(studySteps));
- DecimalFormat tostring = new DecimalFormat(Document.suformat);
- for (org.splat.som.Step step : steps) {
- LOG.debug("Create a document for the step " + step.getNumber());
-
- List<DocumentType> dtypes = _documentTypeService.selectTypesOf(step
- .getStep());
- if (dtypes.size() > 0) {
- DocumentType dtype = dtypes.get(0);
- int oldInd = ht.get(Study.class, aStudyFound.getIndex())
- .getLastLocalIndex();
-
- // Create a file in the download directory
- String fname = "filename" + step.getNumber() + ".py";
- String filePath = getDownloadPath(aStudyFound.getAuthor())
- + fname;
- LOG.debug(step.getStep().getKey() + ": "
- + step.getOwner().getClass().getSimpleName()
- + ": Create test file: " + filePath);
- FileWriter fw = new FileWriter(filePath);
- fw
- .write("Simulation of filename.py file for creating a new document at "
- + new Date());
- fw.close();
-
- Publication addoc = _publicationService.createDoc(aStudyFound
- .getIndex(), step, dtype.getIndex(), userId, fname,
- "tstdoc", ProgressState.inWORK, "", "", new Date(),
- null);
-
- _studyDAO.flush();
- ht.flush();
-
- int ind = ht.get(Study.class, aStudyFound.getIndex())
- .getLastLocalIndex();
-
- LOG.debug("Previous index: " + oldInd + ", new index: " + ind);
- Assert.assertEquals(ht.get(Study.class, aStudyFound.getIndex())
- .getLastLocalIndex(), oldInd + 1,
- "Incremented index must be saved in the database.");
- aStudy = (Study) ht.find(
- "from Study where rid = " + aStudyFound.getIndex())
- .get(0);
- Assert.assertEquals(aStudy.getLastLocalIndex(), oldInd + 1,
- "Incremented index must be saved in the database.");
- Assert.assertEquals(addoc.getSourceFile().getName(), tostring
- .format(aStudy.getLastLocalIndex())
- + "_" + fname, "The generated file name is incorrect.");
- }
- }
-
- rollbackNestedTransaction();
- LOG.debug(">>>>> END testCreateDoc()");
- }
+ public void testCreateDoc() throws InvalidPropertyException,
+ MissedPropertyException, MultiplyDefinedException, IOException,
+ SQLException, NotApplicableException, InterruptedException,
+ ParseException {
+ LOG.debug(">>>>> BEGIN testCreateDoc()");
+ startNestedTransaction();
+
+ HibernateTemplate ht = getHibernateTemplate();
+ Study aStudy = createStudy();
+ // Call DAO's create method for a good transient study.
+ Long id = aStudy.getIndex();
+ Assert.assertNotNull(id,
+ "Create method returns null instead of a new id.");
+ Assert.assertTrue(id > 0, "The new id is not a positive number.");
+
+ // Call DAO's get method for an existing id.
+ _studyDAO.flush();
+ getHibernateTemplate().evict(aStudy);
+ getHibernateTemplate().clear();
+ Study aStudyFound = _studyDAO.get(id);
+ ht.evict(aStudyFound);
+
+ long userId = aStudyFound.getAuthor().getIndex();
+ org.splat.som.Step[] studySteps = _projectElementService
+ .getSteps(aStudyFound);
+ org.splat.som.Step[] scSteps = _projectElementService
+ .getSteps(aStudyFound.getScenarii()[0]);
+
+ List<org.splat.som.Step> steps = new ArrayList<org.splat.som.Step>();
+ steps.addAll(Arrays.asList(studySteps));
+ steps.addAll(Arrays.asList(scSteps));
+ steps.addAll(Arrays.asList(studySteps));
+ DecimalFormat tostring = new DecimalFormat(Document.suformat);
+ for (org.splat.som.Step step : steps) {
+ LOG.debug("Create a document for the step " + step.getNumber());
+
+ List<DocumentType> dtypes = _documentTypeService.selectTypesOf(step
+ .getStep());
+ if (dtypes.size() > 0) {
+ DocumentType dtype = dtypes.get(0);
+ int oldInd = ht.get(Study.class, aStudyFound.getIndex())
+ .getLastLocalIndex();
+
+ // Create a file in the download directory
+ String fname = "filename" + step.getNumber() + ".py";
+ String filePath = getDownloadPath(aStudyFound.getAuthor())
+ + fname;
+ LOG.debug(step.getStep().getKey() + ": "
+ + step.getOwner().getClass().getSimpleName()
+ + ": Create test file: " + filePath);
+ FileWriter fw = new FileWriter(filePath);
+ fw
+ .write("Simulation of filename.py file for creating a new document at "
+ + new Date());
+ fw.close();
+
+ Publication addoc = _publicationService.createDoc(aStudyFound
+ .getIndex(), step, dtype.getIndex(), userId, fname,
+ "tstdoc", ProgressState.inWORK, "", "", new Date(),
+ null);
+
+ _studyDAO.flush();
+ ht.flush();
+
+ int ind = ht.get(Study.class, aStudyFound.getIndex())
+ .getLastLocalIndex();
+
+ LOG.debug("Previous index: " + oldInd + ", new index: " + ind);
+ Assert.assertEquals(ht.get(Study.class, aStudyFound.getIndex())
+ .getLastLocalIndex(), oldInd + 1,
+ "Incremented index must be saved in the database.");
+ aStudy = (Study) ht.find(
+ "from Study where rid = " + aStudyFound.getIndex())
+ .get(0);
+ Assert.assertEquals(aStudy.getLastLocalIndex(), oldInd + 1,
+ "Incremented index must be saved in the database.");
+ Assert.assertEquals(addoc.getSourceFile().getName(), tostring
+ .format(aStudy.getLastLocalIndex())
+ + "_" + fname, "The generated file name is incorrect.");
+ }
+ }
+
+ rollbackNestedTransaction();
+ LOG.debug(">>>>> END testCreateDoc()");
+ }
/**
* Test of file attaching to a study document.<BR>
LOG.debug(">>>>> END testReplace()");
}
+ /**
+ * Test testUpdateRelations method for updating relations at versioning
+ * document.<BR>
+ * <B>Description :</B> <BR>
+ * <i>Create a study and a scenario with documents and try to update
+ * relations for every document in Study or Scenario.<BR>
+ * </i><BR>
+ * <B>Action : </B><BR>
+ * <i>1. call the method for the every document in the Study or
+ * Scenario.</i><BR>
+ * <B>Test data : </B><BR>
+ * <i>no input parameters</i><BR>
+ *
+ * <B>Outcome results:</B><BR>
+ * <i>
+ * <ul>
+ * <li>1: Update relations for every document: <BR>
+ * Delete old UsedBy relation from old version and create new UsedBy
+ * relation to new version document. Create new Uses relation from new
+ * version document</li>
+ * </ul>
+ * </i>
+ *
+ * @throws BusinessException
+ * if test data creation is failed
+ * @throws IOException
+ * if application configuration loading or test data creation is
+ * failed
+ * @throws SQLException
+ * if application configuration loading or test data creation is
+ * failed
+ */
+ @Test
+ public final void testUpdateRelations() throws
+ BusinessException, IOException, SQLException {
+ LOG.debug(">>>>> BEGIN testUpdateRelations()");
+ startNestedTransaction();
+
+ HibernateTemplate ht = getHibernateTemplate();
+
+ Database.getInstance().reset();
+ _projectSettings.getAllSteps().clear(); // Clear config to be able to
+ // load it again
+ _projectSettings.configure("classpath:test/som.xml");
+
+ User goodUser = TestEntitiesGenerator.getTestUser("GoodUser");
+ _userDAO.create(goodUser);
+
+ // Create private study
+ Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
+ aStudy.setTitle("0.This is private study");
+ Long studyId = _studyDAO.create(aStudy);
+
+ // Add a scenario to the study
+ Scenario scen = TestEntitiesGenerator.getTestScenario(aStudy);
+ Long scenId = _scenarioDAO.create(scen);
+ ht.flush();
+
+ org.splat.som.Step[] studySteps = _projectElementService
+ .getSteps(aStudy);
+ org.splat.som.Step[] scSteps = _projectElementService.getSteps(scen);
+
+ List<org.splat.som.Step> steps = new ArrayList<org.splat.som.Step>();
+ steps.addAll(Arrays.asList(studySteps));
+ steps.addAll(Arrays.asList(scSteps));
+ Publication[] currentArray = new Publication[steps.size() * 2 - 2];
+ Publication[] nextArray = new Publication[steps.size() * 2 - 2];
+ Map<Integer, String[]> docusesMap = new HashMap<Integer, String[]>();
+ Map<Integer, long[]> docusedMap = new HashMap<Integer, long[]>();
+ int pubNumber = 0;
+ for (org.splat.som.Step step : steps) {
+ List<DocumentType> dtypes = _documentTypeService.selectTypesOf(step
+ .getStep());
+ if (dtypes.size() > 0) {
+ DocumentType dtype = dtypes.get(0);
+ if (step.getOwner() instanceof Study) {
+ currentArray[pubNumber] = addDoc(aStudy, step, "current"
+ + pubNumber, dtype);
+ currentArray[pubNumber + 1] = addDoc(aStudy, step,
+ "current" + (pubNumber + 1), dtype);
+
+ } else {
+ currentArray[pubNumber] = addDoc(scen, step, "current"
+ + pubNumber, dtype);
+ currentArray[pubNumber + 1] = addDoc(scen, step, "current"
+ + (pubNumber + 1), dtype);
+ }
+
+ pubNumber += 2;
+ }
+ }
+
+ ht.flush();
+ ht.update(aStudy);
+ ht.flush();
+
+ for (Integer numPub = currentArray.length - 1; numPub >= 0; numPub--) {
+ long[] docusedby = new long[1];
+ if (numPub != 0) {
+ currentArray[numPub].addDependency(currentArray[numPub - 1]);
+ }
+ if (numPub != currentArray.length - 1) {
+ docusedby[0] = currentArray[numPub + 1].getIndex();
+ docusedMap.put(new Integer(numPub), docusedby);
+ }
+ }
+
+ for (int numPub = 0; numPub < currentArray.length; numPub++) {
+ String[] docuses = new String[1];
+ nextArray[numPub] = addDoc(aStudy, currentArray[numPub].getStep(),
+ "next" + numPub, currentArray[numPub].value().getType());
+ ht.flush();
+ if (numPub != 0) {
+ docuses[0] = String.valueOf(nextArray[numPub - 1].value()
+ .getIndex());
+ docusesMap.put(numPub, docuses);
+ }
+ _publicationService.updateRelations(currentArray[numPub].getStep(),
+ currentArray[numPub], nextArray[numPub],
+ docusesMap.get(numPub), docusedMap.get(numPub));
+ }
+
+ //Action
+ for (int numPub = 0; numPub < currentArray.length; numPub++) {
+ if (numPub != currentArray.length - 1) {
+
+ Assert.assertEquals(
+ currentArray[numPub].getRelations(UsedByRelation.class),
+ new ArrayList<Publication>());
+ Assert.assertTrue(nextArray[numPub].getRelations(
+ UsedByRelation.class)
+ .contains(currentArray[numPub + 1]));
+ Assert.assertTrue(nextArray[numPub].getRelations(
+ UsedByRelation.class).contains(nextArray[numPub + 1]));
+ }
+
+ if (numPub != 0) {
+ Assert.assertEquals(
+ currentArray[numPub].getRelations(UsesRelation.class)
+ .get(0), nextArray[numPub - 1]);
+ Assert.assertEquals(
+ nextArray[numPub].getRelations(UsesRelation.class).get(
+ 0), nextArray[numPub - 1]);
+ }
+ }
+ rollbackNestedTransaction();
+ LOG.debug(">>>>> END testUpdateRelations()");
+ }
+
+
/**
* Find a document publication in the project element.
*
in.close();
return res.toString();
}
-}
+}
\ No newline at end of file