2 * Copyright (C) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.hibernate
.process
;
18 import java
.sql
.SQLException
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
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
;
31 public class SlcExecutionHibernateTest
extends HibernateTestCase
{
33 public void testSave() {
34 SlcExecutionDao dao
= getBean(SlcExecutionDao
.class);
36 SlcExecution slcExec
= SlcExecutionTestUtils
.createSimpleSlcExecution();
37 slcExec
.getSteps().add(new SlcExecutionStep("A log line"));
38 slcExec
.getSteps().add(new SlcExecutionStep("Two log\nlines"));
42 SlcExecution slcExecPersisted
= dao
.getSlcExecution(slcExec
.getUuid());
43 assertSlcExecution(slcExec
, slcExecPersisted
);
46 public void testTailSteps() {
47 SlcExecutionDao dao
= getBean(SlcExecutionDao
.class);
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
));
56 int lastStepsCount
= 7;
57 List
<SlcExecutionStep
> firstSteps
= dao
.tailSteps(slcExec
.getUuid(),
59 assertEquals(lastStepsCount
, firstSteps
.size());
61 SlcExecutionStep lastStep
= firstSteps
.get(lastStepsCount
- 1);
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
));
68 dao
.addSteps(slcExec
.getUuid(), additionalSteps
);
70 List
<SlcExecutionStep
> lastSteps
= dao
.tailSteps(slcExec
.getUuid(),
72 assertEquals(additionalStepsCount
, lastSteps
.size());
75 public void testModify() {
76 SlcExecutionDao dao
= getBean(SlcExecutionDao
.class);
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"));
85 // slcExecution retrieval and update
86 final SlcExecution slcExecRetrieved
= dao
.getSlcExecution(slcExec
89 getHibernateTemplate().execute(new HibernateCallback() {
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";
99 slcExecRetrieved
.getSteps().get(0)
100 .setLogLines(logLineListStep0
);
101 slcExecRetrieved
.getSteps().add(
102 new SlcExecutionStep("Three \n log \n lines"));
107 dao
.update(slcExecRetrieved
);
109 // updated slcExecution retrieval and comparison
110 SlcExecution slcExecUpdated
= dao
.getSlcExecution(slcExec
.getUuid());
112 assertSlcExecution(slcExecRetrieved
, slcExecUpdated
);
115 public void assertSlcExecution(final SlcExecution expected
,
116 final SlcExecution persisted
) {
117 getHibernateTemplate().execute(new HibernateCallback() {
119 public Object
doInHibernate(Session session
)
120 throws HibernateException
, SQLException
{
121 session
.refresh(persisted
);
122 SlcExecutionTestUtils
.assertSlcExecution(expected
, persisted
);
129 protected String
getApplicationContextLocation() {
130 return "org/argeo/slc/hibernate/applicationContext.xml";