2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.hibernate
.process
;
19 import java
.sql
.SQLException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
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
;
32 public class SlcExecutionHibernateTest
extends HibernateTestCase
{
34 public void testSave() {
35 SlcExecutionDao dao
= getBean(SlcExecutionDao
.class);
37 SlcExecution slcExec
= SlcExecutionTestUtils
.createSimpleSlcExecution();
38 slcExec
.getSteps().add(new SlcExecutionStep("A log line"));
39 slcExec
.getSteps().add(new SlcExecutionStep("Two log\nlines"));
43 SlcExecution slcExecPersisted
= dao
.getSlcExecution(slcExec
.getUuid());
44 assertSlcExecution(slcExec
, slcExecPersisted
);
47 public void testTailSteps() {
48 SlcExecutionDao dao
= getBean(SlcExecutionDao
.class);
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
));
57 int lastStepsCount
= 7;
58 List
<SlcExecutionStep
> firstSteps
= dao
.tailSteps(slcExec
.getUuid(),
60 assertEquals(lastStepsCount
, firstSteps
.size());
62 SlcExecutionStep lastStep
= firstSteps
.get(lastStepsCount
- 1);
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
));
69 dao
.addSteps(slcExec
.getUuid(), additionalSteps
);
71 List
<SlcExecutionStep
> lastSteps
= dao
.tailSteps(slcExec
.getUuid(),
73 assertEquals(additionalStepsCount
, lastSteps
.size());
76 public void testModify() {
77 SlcExecutionDao dao
= getBean(SlcExecutionDao
.class);
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"));
86 // slcExecution retrieval and update
87 final SlcExecution slcExecRetrieved
= dao
.getSlcExecution(slcExec
90 getHibernateTemplate().execute(new HibernateCallback() {
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";
100 slcExecRetrieved
.getSteps().get(0)
101 .setLogLines(logLineListStep0
);
102 slcExecRetrieved
.getSteps().add(
103 new SlcExecutionStep("Three \n log \n lines"));
108 dao
.update(slcExecRetrieved
);
110 // updated slcExecution retrieval and comparison
111 SlcExecution slcExecUpdated
= dao
.getSlcExecution(slcExec
.getUuid());
113 assertSlcExecution(slcExecRetrieved
, slcExecUpdated
);
116 public void assertSlcExecution(final SlcExecution expected
,
117 final SlcExecution persisted
) {
118 getHibernateTemplate().execute(new HibernateCallback() {
120 public Object
doInHibernate(Session session
)
121 throws HibernateException
, SQLException
{
122 session
.refresh(persisted
);
123 SlcExecutionTestUtils
.assertSlcExecution(expected
, persisted
);
130 protected String
getApplicationContextLocation() {
131 return "org/argeo/slc/hibernate/applicationContext.xml";