EMMA Coverage Report (generated Fri Jan 30 13:20:29 EST 2009)
[all classes][org.springframework.batch.support]

COVERAGE SUMMARY FOR SOURCE FILE [PropertiesConverter.java]

nameclass, %method, %block, %line, %
PropertiesConverter.java100% (1/1)80%  (4/5)75%  (65/87)74%  (17/23)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class PropertiesConverter100% (1/1)80%  (4/5)75%  (65/87)74%  (17/23)
PropertiesConverter (): void 0%   (0/1)0%   (0/3)0%   (0/2)
stringToProperties (String): Properties 100% (1/1)70%  (31/44)82%  (9/11)
propertiesToString (Properties): String 100% (1/1)77%  (20/26)75%  (6/8)
<static initializer> 100% (1/1)100% (5/5)100% (1/1)
contains (String, String): boolean 100% (1/1)100% (9/9)100% (1/1)

1/*
2 * Copyright 2006-2007 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.support;
18 
19import java.io.IOException;
20import java.io.StringReader;
21import java.io.StringWriter;
22import java.util.Properties;
23 
24import org.springframework.util.DefaultPropertiesPersister;
25import org.springframework.util.PropertiesPersister;
26import org.springframework.util.StringUtils;
27 
28/**
29 * Utility to convert a Properties object to a String and back. Ideally this utility should have been used to convert to
30 * string in order to convert that string back to a Properties Object. Attempting to convert a string obtained by
31 * calling Properties.toString() will return an invalid Properties object. The format of Properties is that used by
32 * {@link PropertiesPersister} from the Spring Core, so a String in the correct format for a Spring property editor is
33 * fine (key=value pairs separated by new lines).
34 * 
35 * @author Lucas Ward
36 * @author Dave Syer
37 * 
38 * @see PropertiesPersister
39 */
40public final class PropertiesConverter {
41 
42        private static final PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
43 
44        // prevents the class from being instantiated
45        private PropertiesConverter() {
46        };
47 
48        /**
49         * Parse a String to a Properties object. If string is null, an empty Properties object will be returned. The input
50         * String is a set of name=value pairs, delimited by either newline or comma (for brevity). If the input String
51         * contains a newline it is assumed that the separator is newline, otherwise comma.
52         * 
53         * @param stringToParse String to parse.
54         * @return Properties parsed from each string.
55         * @see PropertiesPersister
56         */
57        public static Properties stringToProperties(String stringToParse) {
58 
59                if (stringToParse == null) {
60                        return new Properties();
61                }
62 
63                if (!contains(stringToParse, "\n")) {
64                        return StringUtils.splitArrayElementsIntoProperties(StringUtils
65                                .commaDelimitedListToStringArray(stringToParse), "=");
66                }
67 
68                StringReader stringReader = new StringReader(stringToParse);
69 
70                Properties properties = new Properties();
71 
72                try {
73                        propertiesPersister.load(properties, stringReader);
74                        // Exception is only thrown by StringReader after it is closed,
75                        // so never in this case.
76                } catch (IOException ex) {
77                        throw new IllegalStateException("Error while trying to parse String to java.util.Properties,"
78                                + " given String: " + properties);
79                }
80 
81                return properties;
82        }
83 
84        /**
85         * Convert Properties object to String. This is only necessary for compatibility with converting the String back to
86         * a properties object. If an empty properties object is passed in, a blank string is returned, otherwise it's
87         * string representation is returned.
88         * 
89         * @param propertiesToParse
90         * @return String representation of properties object
91         */
92        public static String propertiesToString(Properties propertiesToParse) {
93 
94                // If properties is empty, return a blank string.
95                if (propertiesToParse == null || propertiesToParse.size() == 0) {
96                        return "";
97                }
98 
99                StringWriter stringWriter = new StringWriter();
100 
101                try {
102                        propertiesPersister.store(propertiesToParse, stringWriter, null);
103                } catch (IOException ex) {
104                        // Exception is never thrown by StringWriter
105                        throw new IllegalStateException("Error while trying to convert properties to string");
106                }
107 
108                return stringWriter.toString();
109        }
110 
111        private static boolean contains(String str, String searchStr) {
112                return str.indexOf(searchStr) != -1;
113        }
114}

[all classes][org.springframework.batch.support]
EMMA 2.0.5312 (C) Vladimir Roubtsov