EMMA Coverage Report (generated Thu May 22 12:08:10 CDT 2014)
[all classes][org.springframework.batch.core.repository.dao]

COVERAGE SUMMARY FOR SOURCE FILE [XStreamExecutionContextStringSerializer.java]

nameclass, %method, %block, %line, %
XStreamExecutionContextStringSerializer.java100% (1/1)71%  (5/7)80%  (69/86)78%  (18/23)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class XStreamExecutionContextStringSerializer100% (1/1)71%  (5/7)80%  (69/86)78%  (18/23)
setHierarchicalStreamDriver (HierarchicalStreamDriver): void 0%   (0/1)0%   (0/4)0%   (0/2)
setReflectionProvider (ReflectionProvider): void 0%   (0/1)0%   (0/4)0%   (0/2)
init (): void 100% (1/1)69%  (20/29)83%  (5/6)
XStreamExecutionContextStringSerializer (): void 100% (1/1)100% (6/6)100% (2/2)
afterPropertiesSet (): void 100% (1/1)100% (3/3)100% (2/2)
deserialize (InputStream): Object 100% (1/1)100% (28/28)100% (5/5)
serialize (Object, OutputStream): void 100% (1/1)100% (12/12)100% (4/4)

1/*
2 * Copyright 2006-2013 the original author or authors.
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 
17package org.springframework.batch.core.repository.dao;
18 
19import java.io.BufferedReader;
20import java.io.IOException;
21import java.io.InputStream;
22import java.io.InputStreamReader;
23import java.io.OutputStream;
24 
25import org.springframework.batch.core.repository.ExecutionContextSerializer;
26import org.springframework.beans.factory.InitializingBean;
27import org.springframework.core.serializer.Deserializer;
28import org.springframework.core.serializer.Serializer;
29import org.springframework.util.Assert;
30 
31import com.thoughtworks.xstream.XStream;
32import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
33import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
34import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
35 
36/**
37 * Implementation that uses XStream and Jettison to provide serialization.
38 *
39 * @author Thomas Risberg
40 * @author Michael Minella
41 * @since 2.0
42 * @see ExecutionContextSerializer
43 */
44public class XStreamExecutionContextStringSerializer implements ExecutionContextSerializer, InitializingBean {
45 
46        private ReflectionProvider reflectionProvider = null;
47 
48        private HierarchicalStreamDriver hierarchicalStreamDriver;
49 
50        private XStream xstream;
51 
52        public void setReflectionProvider(ReflectionProvider reflectionProvider) {
53                this.reflectionProvider = reflectionProvider;
54        }
55 
56        public void setHierarchicalStreamDriver(HierarchicalStreamDriver hierarchicalStreamDriver) {
57                this.hierarchicalStreamDriver = hierarchicalStreamDriver;
58        }
59 
60        @Override
61        public void afterPropertiesSet() throws Exception {
62                init();
63        }
64 
65        public synchronized void init() throws Exception {
66                if (hierarchicalStreamDriver == null) {
67                        this.hierarchicalStreamDriver = new JettisonMappedXmlDriver();
68                }
69                if (reflectionProvider == null) {
70                        xstream =  new XStream(hierarchicalStreamDriver);
71                }
72                else {
73                        xstream = new XStream(reflectionProvider, hierarchicalStreamDriver);
74                }
75        }
76 
77        /**
78         * Serializes the passed execution context to the supplied OutputStream.
79         *
80         * @param context
81         * @param out
82         * @see Serializer#serialize(Object, OutputStream)
83         */
84        @Override
85        public void serialize(Object context, OutputStream out) throws IOException {
86                Assert.notNull(context);
87                Assert.notNull(out);
88 
89                out.write(xstream.toXML(context).getBytes());
90        }
91 
92        /**
93         * Deserializes the supplied input stream into a new execution context.
94         *
95         * @param in
96         * @return a reconstructed execution context
97         * @see Deserializer#deserialize(InputStream)
98         */
99        @Override
100        public Object deserialize(InputStream in) throws IOException {
101                BufferedReader br = new BufferedReader(new InputStreamReader(in));
102 
103                StringBuilder sb = new StringBuilder();
104 
105                String line;
106                while ((line = br.readLine()) != null) {
107                        sb.append(line);
108                }
109 
110                return xstream.fromXML(sb.toString());
111        }
112}

[all classes][org.springframework.batch.core.repository.dao]
EMMA 2.0.5312 (C) Vladimir Roubtsov