]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/runtime/org.argeo.slc.support.hibernate/src/test/java/org/argeo/slc/hibernate/process/SlcExecutionHibernateTest.java
Move to SLC legacy
[gpl/argeo-slc.git] / legacy / runtime / org.argeo.slc.support.hibernate / src / test / java / org / argeo / slc / hibernate / process / SlcExecutionHibernateTest.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.hibernate.process;
17
18 import java.sql.SQLException;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.argeo.slc.dao.process.SlcExecutionDao;
23 import org.argeo.slc.hibernate.unit.HibernateTestCase;
24 import org.argeo.slc.process.SlcExecution;
25 import org.argeo.slc.process.SlcExecutionStep;
26 import org.argeo.slc.unit.process.SlcExecutionTestUtils;
27 import org.hibernate.HibernateException;
28 import org.hibernate.Session;
29 import org.springframework.orm.hibernate3.HibernateCallback;
30
31 public class SlcExecutionHibernateTest extends HibernateTestCase {
32
33 public void testSave() {
34 SlcExecutionDao dao = getBean(SlcExecutionDao.class);
35
36 SlcExecution slcExec = SlcExecutionTestUtils.createSimpleSlcExecution();
37 slcExec.getSteps().add(new SlcExecutionStep("A log line"));
38 slcExec.getSteps().add(new SlcExecutionStep("Two log\nlines"));
39
40 dao.create(slcExec);
41
42 SlcExecution slcExecPersisted = dao.getSlcExecution(slcExec.getUuid());
43 assertSlcExecution(slcExec, slcExecPersisted);
44 }
45
46 public void testTailSteps() {
47 SlcExecutionDao dao = getBean(SlcExecutionDao.class);
48
49 SlcExecution slcExec = SlcExecutionTestUtils.createSimpleSlcExecution();
50 int totalStepCount = 20;
51 for (int i = 0; i < totalStepCount; i++) {
52 slcExec.getSteps().add(new SlcExecutionStep("Log " + i));
53 }
54 dao.create(slcExec);
55
56 int lastStepsCount = 7;
57 List<SlcExecutionStep> firstSteps = dao.tailSteps(slcExec.getUuid(),
58 lastStepsCount);
59 assertEquals(lastStepsCount, firstSteps.size());
60
61 SlcExecutionStep lastStep = firstSteps.get(lastStepsCount - 1);
62
63 List<SlcExecutionStep> additionalSteps = new ArrayList<SlcExecutionStep>();
64 int additionalStepsCount = 13;
65 for (int i = 0; i < additionalStepsCount; i++) {
66 additionalSteps.add(new SlcExecutionStep("Additonal log " + i));
67 }
68 dao.addSteps(slcExec.getUuid(), additionalSteps);
69
70 List<SlcExecutionStep> lastSteps = dao.tailSteps(slcExec.getUuid(),
71 lastStep.getUuid());
72 assertEquals(additionalStepsCount, lastSteps.size());
73 }
74
75 public void testModify() {
76 SlcExecutionDao dao = getBean(SlcExecutionDao.class);
77
78 // slcExecution Creation
79 SlcExecution slcExec = SlcExecutionTestUtils.createSimpleSlcExecution();
80 slcExec.getSteps().add(new SlcExecutionStep("A log line"));
81 slcExec.getSteps().add(new SlcExecutionStep("Two log\nlines"));
82
83 dao.create(slcExec);
84
85 // slcExecution retrieval and update
86 final SlcExecution slcExecRetrieved = dao.getSlcExecution(slcExec
87 .getUuid());
88
89 getHibernateTemplate().execute(new HibernateCallback() {
90
91 public Object doInHibernate(Session session)
92 throws HibernateException, SQLException {
93 session.refresh(slcExecRetrieved);
94 List<String> logLineListStep0 = slcExecRetrieved.getSteps()
95 .get(0).getLogLines();
96 for (String logLine : logLineListStep0)
97 logLine = logLine + "appended Log text";
98
99 slcExecRetrieved.getSteps().get(0)
100 .setLogLines(logLineListStep0);
101 slcExecRetrieved.getSteps().add(
102 new SlcExecutionStep("Three \n log \n lines"));
103 return null;
104 }
105 });
106
107 dao.update(slcExecRetrieved);
108
109 // updated slcExecution retrieval and comparison
110 SlcExecution slcExecUpdated = dao.getSlcExecution(slcExec.getUuid());
111
112 assertSlcExecution(slcExecRetrieved, slcExecUpdated);
113 }
114
115 public void assertSlcExecution(final SlcExecution expected,
116 final SlcExecution persisted) {
117 getHibernateTemplate().execute(new HibernateCallback() {
118
119 public Object doInHibernate(Session session)
120 throws HibernateException, SQLException {
121 session.refresh(persisted);
122 SlcExecutionTestUtils.assertSlcExecution(expected, persisted);
123 return null;
124 }
125 });
126 }
127
128 @Override
129 protected String getApplicationContextLocation() {
130 return "org/argeo/slc/hibernate/applicationContext.xml";
131 }
132
133 }