EMMA Coverage Report (generated Fri Jun 19 09:16:10 CEST 2009)
[all classes][org.ktc.rbutils.rb.check]

COVERAGE SUMMARY FOR SOURCE FILE [FileChecker.java]

nameclass, %method, %block, %line, %
FileChecker.java100% (1/1)100% (11/11)99%  (288/292)97%  (64/66)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class FileChecker100% (1/1)100% (11/11)99%  (288/292)97%  (64/66)
performCheck (): void 100% (1/1)90%  (37/41)86%  (12/14)
FileChecker (File, File): void 100% (1/1)100% (49/49)100% (11/11)
acceptKey (String): boolean 100% (1/1)100% (2/2)100% (1/1)
checkIfValuesInSync (String): void 100% (1/1)100% (66/66)100% (12/12)
checkIsRbMissing (): boolean 100% (1/1)100% (27/27)100% (8/8)
checkLocalesInSync (): boolean 100% (1/1)100% (41/41)100% (7/7)
checkUnreachedRbKeys (): void 100% (1/1)100% (28/28)100% (6/6)
getFormattedPropsValue (String): String 100% (1/1)100% (5/5)100% (1/1)
getFormattedRbKey (String): String 100% (1/1)100% (2/2)100% (1/1)
getFormattedRbValue (String): String 100% (1/1)100% (7/7)100% (1/1)
loadBundle (): ResourceBundle 100% (1/1)100% (24/24)100% (4/4)

1/*
2 * Copyright  2005-2006 The RbUtils Project
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 */
17 
18// $Id: FileChecker.java,v 1.5 2006/09/17 23:56:00 ktcguru Exp $
19 
20package org.ktc.rbutils.rb.check;
21 
22import java.io.File;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.Collections;
26import java.util.Enumeration;
27import java.util.Iterator;
28import java.util.List;
29import java.util.Locale;
30import java.util.MissingResourceException;
31import java.util.ResourceBundle;
32 
33import org.ktc.rbutils.api.audit.AbstractFileProcessor;
34import org.ktc.rbutils.api.file.FileTools;
35import org.ktc.rbutils.api.file.ValidateFile;
36import org.ktc.rbutils.api.i18n.PropertiesRB;
37 
38/**
39 * Check a properties file against a <code>ResourceBundle</code>.
40 * @since RbUtils 0.1.0
41 * @version $Revision: 1.5 $
42 * @author redfish
43 * @author ktcguru
44 */
45public class FileChecker extends AbstractFileProcessor {
46    // TODO Javadoc: improve Javadoc
47 
48    /** The properties source file. */
49    protected PropertiesRB propertiesRB;
50 
51    /** The RB class file to be inspected. */
52    protected ResourceBundle rbFile;
53 
54    /**
55     * List of keys in the resourcebundle. This list will be used to check if keys are not reached
56     * during the check.
57     * @since RbUtils 0.9.3.3
58     */
59    protected final List rbKeys = new ArrayList();
60 
61    /**
62     * Instanciates a new <code>FileChecker</code>.
63     * <p>
64     * @param root the root directory of the properties file.
65     * @param rbSrcFile the properties file used for the check.
66     * @throws org.apache.commons.lang.NullArgumentException if a parameter is <code>null</code>.
67     * @throws java.io.FileNotFoundException if a parameter is a <code>File</code> that does not
68     *             exist in the filesystem.
69     * @throws org.ktc.rbutils.api.file.NotDirectoryException if <code>root</code> is not a
70     *             directory in the filesystem.
71     * @throws org.ktc.rbutils.api.file.NotFileException if <code>rbSrcFile</code> is not a file
72     *             in the filesystem.
73     * @throws IOException if problems occured on Properties file load.
74     */
75    public FileChecker(final File root, final File rbSrcFile) throws IOException {
76        // Validate arguments
77        ValidateFile.isDirectory(root);
78        ValidateFile.isFile(rbSrcFile);
79 
80        // Load RB file into PropertiesRB
81        propertiesRB = new PropertiesRB();
82        propertiesRB.load(rbSrcFile);
83 
84        // Fiels inherited from AbstractFileAuditer
85        className = FileTools.inferPackageClassName(root, rbSrcFile, true);
86        fileLocale = propertiesRB.getLocale();
87        fileName = FileTools.getStrippedFileName(root, rbSrcFile, true);
88 
89        // Set the resource class for messages
90        classResource = messages.class.getName();
91    }
92 
93    /**
94     * Performs the check of properties file.
95     * <p>
96     * First, we look if a ResourceBundle is found in the classpath. If so, we check all keys of the
97     * properties file to check synchronization with the ResourceBundle. <br>
98     * Errors are reported via the registered loggers.
99     */
100    public void performCheck() {
101        // Check starts
102        fireProcessFileStarted();
103 
104        // Check if compiled file exists
105        // Do nothing if the compiled file is missing
106        // Check locale synchronization between the rb and the properties
107        // Check rb compiled file versus properties file if locales are in sync
108        if (!checkIsRbMissing() && checkLocalesInSync()) {
109            // Check keys and values
110            final List sortedKeys = new ArrayList(propertiesRB.keySet());
111            Collections.sort(sortedKeys);
112 
113            for (final Iterator iter = sortedKeys.iterator(); iter.hasNext();) {
114                final String key = (String) iter.next();
115                try {
116                    checkIfValuesInSync(key);
117                }
118                // We don't want to stop the audit if an error occurs on key inspection.
119                // Others have to be inspected too.
120                catch (final Exception exception) {
121                    fireException(exception);
122                }
123            }
124 
125            // Check unreached keys in the resourcebundle
126            checkUnreachedRbKeys();
127        }
128 
129        // Check finishes
130        fireProcessFileEnded();
131    }
132 
133    /**
134     * Loads the <code>ResourceBundle</code> relying on the properties file and stores it in the
135     * <code>rbfile</code> field. <br>
136     * Also initialized the <code>rbKeys</code> field.
137     * <p>
138     * This must be the first step before launching checks.
139     * @return the loaded bundle.
140     * @throws MissingResourceException if the bundle cannot be loaded.
141     * @since RbUtils 0.9.3.3
142     */
143    protected ResourceBundle loadBundle() {
144        rbFile = ResourceBundle.getBundle(className, fileLocale);
145 
146        // TODO Code - use a libraries to transform an enumeration into a list
147        // java.util.Collections provides the list(Enumeration) method but it has been added in Java
148        // 1.4. Look for methods in the commons-collection package.
149        for (final Enumeration rbKeysEnum = rbFile.getKeys(); rbKeysEnum.hasMoreElements();) {
150            rbKeys.add(rbKeysEnum.nextElement());
151        }
152 
153        return rbFile;
154    }
155 
156    /**
157     * Checks if the <code>ResourceBundle</code> relying on the properties file exists.
158     * <p>
159     * First, tries to load the bundle using {@link #loadBundle()}. If the load fails, the method
160     * fire errors to all listeners and returns <code>true</code>. Returns <code>false</code>
161     * otherwise.
162     * @return <code>true</code> is the <code>ResourceBundle</code> cannot be loaded;
163     *         <code>false</code> otherwise.
164     * @since RbUtils 0.9.3.3
165     */
166    protected boolean checkIsRbMissing() {
167        boolean isRBcompiledMissing = false;
168        try {
169            loadBundle();
170        }
171        catch (final MissingResourceException ex) {
172            final Object[] args = {className};
173            fireError(getMessage(messages.FILE_RB_MISSING, args),
174                      ErrorType.getMessage(errors.ERRORTYPE_NO_CLASS));
175            isRBcompiledMissing = true;
176        }
177        return isRBcompiledMissing;
178    }
179 
180    /**
181     * Checks if the <code>Locale</code> of the properties and the rb are in sync.
182     * <p>
183     * First, check the locales; if they are not in sync, fire errors to all listeners and returns
184     * <code>false</code>.<br>
185     * In this implementation, tests if the RB and properties <code>Locale</code> are the same.
186     * @return <code>true</code> is the RB and the properties file have the same
187     *         <code>Locale</code>; <code>false</code> otherwise.
188     * @since RbUtils 0.9.3.3
189     */
190    protected boolean checkLocalesInSync() {
191        boolean inSync = true;
192 
193        final Locale rbLocale = rbFile.getLocale();
194        // TODO why is fileLocale checked ?
195        if (rbLocale == null || fileLocale == null || !rbLocale.equals(fileLocale)) {
196            inSync = false;
197            final Object[] args = {fileLocale, className};
198            fireError(getMessage(messages.FILE_RB_LOCALE_MISSING, args),
199                      ErrorType.getMessage(errors.ERRORTYPE_NO_LOCALE));
200        }
201 
202        return inSync;
203    }
204 
205    /**
206     * Tests whether or not the specified key should be checked.
207     * @param key the key to be tested.
208     * @return always <code>true</code> in this implementation.
209     * @since RbUtils 0.9.3.3
210     */
211    protected boolean acceptKey(final String key) {
212        return true;
213    }
214 
215    /**
216     * Formats a key into another one whichwill be used to get the corresponding value in the rb
217     * file.
218     * @param key the key to be formatted.
219     * @return the intial key in this implementation.
220     * @since RbUtils 0.9.3.3
221     */
222    protected String getFormattedRbKey(final String key) {
223        return key;
224    }
225 
226    /**
227     * Returns the formatted value of this key in the properties.
228     * @param key key used to get the formatted value.
229     * @return in this implementation, the value of this key in the properties.
230     * @since RbUtils 0.9.3.3
231     */
232    protected String getFormattedPropsValue(final String key) {
233        return propertiesRB.getProperty(key);
234    }
235 
236    /**
237     * Returns the formatted value of the formatted value of this key in the rb.
238     * @param key key used to get the formatted value.
239     * @return in this implementation, the value of this key in the rb.
240     * @since RbUtils 0.9.3.3
241     */
242    protected String getFormattedRbValue(final String key) {
243        return rbFile.getString(getFormattedRbKey(key));
244    }
245 
246    /**
247     * Checks if value in rb and properties are in sync.
248     * <ul>
249     * <li>launches the check if the key is accepted</li>
250     * <li>gets the formatted properties value</li>
251     * <li>gets the formatted rb value and fires error if the value does not exist in the rb</li>
252     * <li>checks if the formatted properties and rb values are equals</li>
253     * </ul>
254     * <p>
255     * This method should not be overriden. Overrides the utility key an value methods instead.
256     * @param key the key to be checked.
257     * @since RbUtils 0.9.3.3
258     */
259    protected void checkIfValuesInSync(final String key) {
260        if (acceptKey(key)) {
261            try {
262                final String formattedPropsValue = getFormattedPropsValue(key);
263                final String formattedRbValue = getFormattedRbValue(key);
264 
265                // formattedPropsValue is not null if key exists the properties file
266                if (formattedPropsValue == null || !formattedPropsValue.equals(formattedRbValue)) {
267                    final Object[] args = {key, formattedPropsValue, formattedRbValue};
268                    fireError(getMessage(messages.FILE_KEY_UNSYNCHRONIZED, args),
269                              ErrorType.getMessage(errors.ERRORTYPE_NO_SYNCH));
270                }
271 
272                // Key has been reached in the rb, so remove it from the list
273                rbKeys.remove(getFormattedRbKey(key));
274 
275            }
276            // Key is missing in the RB
277            catch (final MissingResourceException missingresourceexception) {
278                final Object[] args = {key};
279                fireError(getMessage(messages.FILE_KEY_MISSING, args),
280                          ErrorType.getMessage(errors.ERRORTYPE_NO_KEY));
281            }
282        }
283    }
284 
285    /**
286     * Checks that all keys in the resourcebundle have been reached during the check. <br>
287     * Fire an error to logger for each unreached key.
288     * @since RbUtils 0.9.3.3
289     */
290    protected void checkUnreachedRbKeys() {
291        for (final Iterator iterUnreachedKeys = rbKeys.iterator(); iterUnreachedKeys.hasNext();) {
292            final String unreachedKey = (String) iterUnreachedKeys.next();
293            final Object[] args = {unreachedKey};
294            fireError(getMessage(messages.FILE_RB_KEY_UNREACHED, args),
295                      ErrorType.getMessage(errors.ERRORTYPE_UNREACHED_KEY));
296        }
297    }
298 
299}

[all classes][org.ktc.rbutils.rb.check]
EMMA 2.0.5312 (C) Vladimir Roubtsov