]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.hibernate/src/test/java/org/argeo/slc/hibernate/process/SlcExecutionHibernateTest.java
Introduce H
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.hibernate / src / test / java / org / argeo / slc / hibernate / process / SlcExecutionHibernateTest.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.slc.hibernate.process;
18
19 import java.sql.SQLException;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.argeo.slc.dao.process.SlcExecutionDao;
24 import org.argeo.slc.hibernate.unit.HibernateTestCase;
25 import org.argeo.slc.process.SlcExecution;
26 import org.argeo.slc.process.SlcExecutionStep;
27 import org.argeo.slc.unit.process.SlcExecutionTestUtils;
28 import org.hibernate.HibernateException;
29 import org.hibernate.Session;
30 import org.springframework.orm.hibernate3.HibernateCallback;
31
32 public class SlcExecutionHibernateTest extends HibernateTestCase {
33
34 public void testSave() {
35 SlcExecutionDao dao = getBean(SlcExecutionDao.class);
36
37 SlcExecution slcExec = SlcExecutionTestUtils.createSimpleSlcExecution();
38 slcExec.getSteps().add(new SlcExecutionStep("A log line"));
39 slcExec.getSteps().add(new SlcExecutionStep("Two log\nlines"));
40
41 dao.create(slcExec);
42
43 SlcExecution slcExecPersisted = dao.getSlcExecution(slcExec.getUuid());
44 assertSlcExecution(slcExec, slcExecPersisted);
45 }
46
47 public void testTailSteps() {
48 SlcExecutionDao dao = getBean(SlcExecutionDao.class);
49
50 SlcExecution slcExec = SlcExecutionTestUtils.createSimpleSlcExecution();
51 int totalStepCount = 20;
52 for (int i = 0; i < totalStepCount; i++) {
53 slcExec.getSteps().add(new SlcExecutionStep("Log " + i));
54 }
55 dao.create(slcExec);
56
57 int lastStepsCount = 7;
58 List<SlcExecutionStep> firstSteps = dao.tailSteps(slcExec.getUuid(),
59 lastStepsCount);
60 assertEquals(lastStepsCount, firstSteps.size());
61
62 SlcExecutionStep lastStep = firstSteps.get(lastStepsCount - 1);
63
64 List<SlcExecutionStep> additionalSteps = new ArrayList<SlcExecutionStep>();
65 int additionalStepsCount = 13;
66 for (int i = 0; i < additionalStepsCount; i++) {
67 additionalSteps.add(new SlcExecutionStep("Additonal log " + i));
68 }
69 dao.addSteps(slcExec.getUuid(), additionalSteps);
70
71 List<SlcExecutionStep> lastSteps = dao.tailSteps(slcExec.getUuid(),
72 lastStep.getUuid());
73 assertEquals(additionalStepsCount, lastSteps.size());
74 }
75
76 public void testModify() {
77 SlcExecutionDao dao = getBean(SlcExecutionDao.class);
78
79 // slcExecution Creation
80 SlcExecution slcExec = SlcExecutionTestUtils.createSimpleSlcExecution();
81 slcExec.getSteps().add(new SlcExecutionStep("A log line"));
82 slcExec.getSteps().add(new SlcExecutionStep("Two log\nlines"));
83
84 dao.create(slcExec);
85
86 // slcExecution retrieval and update
87 final SlcExecution slcExecRetrieved = dao.getSlcExecution(slcExec
88 .getUuid());
89
90 getHibernateTemplate().execute(new HibernateCallback() {
91
92 public Object doInHibernate(Session session)
93 throws HibernateException, SQLException {
94 session.refresh(slcExecRetrieved);
95 List<String> logLineListStep0 = slcExecRetrieved.getSteps()
96 .get(0).getLogLines();
97 for (String logLine : logLineListStep0)
98 logLine = logLine + "appended Log text";
99
100 slcExecRetrieved.getSteps().get(0)
101 .setLogLines(logLineListStep0);
102 slcExecRetrieved.getSteps().add(
103 new SlcExecutionStep("Three \n log \n lines"));
104 return null;
105 }
106 });
107
108 dao.update(slcExecRetrieved);
109
110 // updated slcExecution retrieval and comparison
111 SlcExecution slcExecUpdated = dao.getSlcExecution(slcExec.getUuid());
112
113 assertSlcExecution(slcExecRetrieved, slcExecUpdated);
114 }
115
116 public void assertSlcExecution(final SlcExecution expected,
117 final SlcExecution persisted) {
118 getHibernateTemplate().execute(new HibernateCallback() {
119
120 public Object doInHibernate(Session session)
121 throws HibernateException, SQLException {
122 session.refresh(persisted);
123 SlcExecutionTestUtils.assertSlcExecution(expected, persisted);
124 return null;
125 }
126 });
127 }
128
129 @Override
130 protected String getApplicationContextLocation() {
131 return "org/argeo/slc/hibernate/applicationContext.xml";
132 }
133
134 }