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
.lib
.detached
;
19 import java
.util
.Properties
;
20 import java
.util
.UUID
;
22 import org
.apache
.commons
.logging
.Log
;
23 import org
.apache
.commons
.logging
.LogFactory
;
24 import org
.argeo
.slc
.SlcException
;
25 import org
.argeo
.slc
.core
.test
.SimpleResultPart
;
26 import org
.argeo
.slc
.core
.test
.context
.ContextUtils
;
27 import org
.argeo
.slc
.core
.test
.context
.DefaultContextTestData
;
28 import org
.argeo
.slc
.detached
.DetachedAnswer
;
29 import org
.argeo
.slc
.detached
.DetachedClient
;
30 import org
.argeo
.slc
.detached
.DetachedRequest
;
31 import org
.argeo
.slc
.detached
.DetachedStep
;
32 import org
.argeo
.slc
.detached
.ui
.UiStep
;
33 import org
.argeo
.slc
.test
.TestDefinition
;
34 import org
.argeo
.slc
.test
.TestResult
;
35 import org
.argeo
.slc
.test
.TestRun
;
36 import org
.argeo
.slc
.test
.TestStatus
;
37 import org
.springframework
.beans
.BeansException
;
38 import org
.springframework
.beans
.PropertyValue
;
39 import org
.springframework
.beans
.factory
.BeanFactory
;
40 import org
.springframework
.beans
.factory
.BeanFactoryAware
;
41 import org
.springframework
.beans
.factory
.BeanInitializationException
;
42 import org
.springframework
.beans
.factory
.BeanNameAware
;
43 import org
.springframework
.beans
.factory
.InitializingBean
;
44 import org
.springframework
.beans
.factory
.config
.BeanDefinition
;
45 import org
.springframework
.beans
.factory
.config
.BeanReference
;
46 import org
.springframework
.beans
.factory
.support
.BeanDefinitionRegistry
;
48 public class DetachedTestDefinition
implements TestDefinition
, BeanNameAware
,
49 BeanFactoryAware
, InitializingBean
{
50 private final static Log log
= LogFactory
51 .getLog(DetachedTestDefinition
.class);
53 private DetachedStep step
;
54 private DetachedClient client
;
57 private String testDefBeanName
= null;
58 private BeanDefinitionRegistry beanDefinitionRegistry
= null;
59 private String stepRef
= null;
61 public void execute(TestRun testRun
) {
62 // Look for step bean name
63 String stepBeanNameT
= null;
64 if (this.stepRef
== null && step
!= null) {
65 if (step
instanceof UiStep
) {
66 stepBeanNameT
= ((UiStep
) step
).getBeanName();
68 // Introspects bean factory in order to find step bean name
69 BeanDefinition thisBeanDef
= beanDefinitionRegistry
70 .getBeanDefinition(testDefBeanName
);
71 PropertyValue propValue
= thisBeanDef
.getPropertyValues()
72 .getPropertyValue("step");
73 Object stepBeanRef
= propValue
.getValue();
74 BeanReference ref
= (BeanReference
) stepBeanRef
;
75 stepBeanNameT
= ref
.getBeanName();
77 } else if (this.stepRef
!= null) {
78 stepBeanNameT
= this.stepRef
;
82 DetachedRequest request
= new DetachedRequest();
83 request
.setUuid(UUID
.randomUUID().toString());
84 request
.setRef(stepBeanNameT
);
86 DefaultContextTestData testData
= testRun
.getTestData();
87 if (testData
!= null) {
88 Map
<String
, Object
> values
= testData
.getValues();
89 Properties inputParameters
= new Properties();
90 inputParameters
.putAll(values
);// TODO: check conversions to string
91 request
.setProperties(inputParameters
);
95 client
.sendRequest(request
);
96 } catch (Exception e
) {
97 throw new SlcException("Could not send request for step "
101 DetachedAnswer answer
;
103 answer
= client
.receiveAnswer();
104 } catch (Exception e
) {
105 throw new SlcException("Could not receive answer #"
106 + request
.getUuid() + " for step " + stepBeanNameT
, e
);
109 if (answer
.getStatus() == DetachedAnswer
.ERROR
)
110 throw new SlcException("Error when executing step "
111 + answer
.getUuid() + ": " + answer
.getLog());
113 log
.info("Received answer for '" + request
.getRef() + "' ("
114 + answer
.getStatusAsString() + "):" + answer
.getLog());
116 if (testData
!= null) {
117 Properties outputParameters
= answer
.getProperties();
118 for (Object key
: outputParameters
.keySet())
119 testData
.getValues().put(key
.toString(),
120 outputParameters
.get(key
));
123 if (testData
!= null) {
124 ContextUtils
.compareReachedExpected(testData
,
125 testRun
.getTestResult());
127 ((TestResult
) testRun
.getTestResult())
128 .addResultPart(new SimpleResultPart(TestStatus
.PASSED
,
129 "Step " + stepBeanNameT
+ " executed successfully"));
133 public void setBeanName(String name
) {
134 this.testDefBeanName
= name
;
137 public void setBeanFactory(BeanFactory beanFactory
) throws BeansException
{
138 if (beanFactory
instanceof BeanDefinitionRegistry
)
139 beanDefinitionRegistry
= (BeanDefinitionRegistry
) beanFactory
;
141 throw new BeanInitializationException(
142 "Require BeanDefinitionRegistry");
145 public void afterPropertiesSet() throws Exception
{
148 public void setStep(DetachedStep step
) {
152 public void setClient(DetachedClient client
) {
153 this.client
= client
;
156 public void setStepRef(String stepBeanName
) {
157 this.stepRef
= stepBeanName
;