Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 264   Methods: 19
NCLOC: 149   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ResourceBundleConfiguration.java 20% 33.7% 47.4% 33.6%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * names of its contributors may be used to endorse or promote products
 15    * derived from this software without specific prior written permission.
 16    *
 17    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.drjava.config;
 38   
 39    import edu.rice.cs.util.swing.Utilities;
 40   
 41    import java.util.ResourceBundle;
 42    import java.util.MissingResourceException;
 43    import java.io.*;
 44    import java.util.Enumeration;
 45   
 46    /** A configuration in a resource bundle.
 47    * @version $Id: ResourceBundleConfiguration.java 5246 2010-05-07 19:10:44Z mgricken $
 48    */
 49    public class ResourceBundleConfiguration extends FileConfiguration {
 50    /** Name of the resource bundle. */
 51    protected final String _resourceBundleName;
 52   
 53    /** Resource bundle containing the configuration. */
 54    protected final ResourceBundle _bundle;
 55   
 56    /** Shadowed configuration used if the resource bundle does not define an option. */
 57    protected final FileConfiguration _shadowed;
 58   
 59    /** Initializes this Configuration object with the given OptionMap.
 60    * @param resourceBundleName name of the resource bundle
 61    * @param shadowed configuration that should be used if the resource bundle does not define an option.
 62    */
 63  824 public ResourceBundleConfiguration(String resourceBundleName, FileConfiguration shadowed) {
 64  824 super(shadowed.getFile());
 65  824 _resourceBundleName = resourceBundleName;
 66  824 _bundle = ResourceBundle.getBundle(resourceBundleName);
 67  824 _shadowed = shadowed;
 68  824 map = new OptionMap() {
 69  0 public <T> T getOption(OptionParser<T> o) {
 70  0 if (o==null) return _shadowed.getOptionMap().getOption(o);
 71  0 try {
 72  0 String str = _bundle.getString(o.getName());
 73  0 return o.parse(str); // defined in resource bundle
 74    }
 75    catch(MissingResourceException mre) {
 76    // not defined, delegate to shadowed configuration
 77  0 return _shadowed.getOptionMap().getOption(o);
 78    }
 79    }
 80   
 81  0 public <T> T setOption(Option<T> o, T val) {
 82  0 if (o==null) return _shadowed.getOptionMap().setOption(o, val);
 83  0 try {
 84  0 String str = _bundle.getString(o.getName());
 85  0 return null; // defined in resource bundle, can't be set
 86    }
 87    catch(MissingResourceException mre) {
 88    // not defined, delegate to shadowed configuration
 89  0 return _shadowed.getOptionMap().setOption(o, val);
 90    }
 91    }
 92   
 93  304 public <T> String getString(OptionParser<T> o) {
 94  0 if (o==null) return _shadowed.getOptionMap().getString(o);
 95  304 try {
 96  304 String str = _bundle.getString(o.getName());
 97  0 return str; // defined in resource bundle
 98    }
 99    catch(MissingResourceException mre) {
 100    // not defined, delegate to shadowed configuration
 101  304 return _shadowed.getOptionMap().getString(o);
 102    }
 103    }
 104   
 105  0 public <T> void setString(OptionParser<T> o, String s) {
 106  0 if (o==null) _shadowed.getOptionMap().setString(o, s);
 107  0 try {
 108  0 String str = _bundle.getString(o.getName());
 109  0 return; // defined in resource bundle, can't be set
 110    }
 111    catch(MissingResourceException mre) {
 112    // not defined, delegate to shadowed configuration
 113  0 _shadowed.getOptionMap().setString(o, s);
 114    }
 115    }
 116   
 117  0 public <T> T removeOption(OptionParser<T> o) {
 118  0 if (o==null) return _shadowed.getOptionMap().removeOption(o);
 119  0 try {
 120  0 String str = _bundle.getString(o.getName());
 121  0 return null; // defined in resource bundle, can't be removed
 122    }
 123    catch(MissingResourceException mre) {
 124    // not defined, delegate to shadowed configuration
 125  0 return _shadowed.getOptionMap().removeOption(o);
 126    }
 127    }
 128   
 129  5414 public Iterable<OptionParser<?>> keys() {
 130    // TODO merge with keys from this configuration
 131  5414 Iterable<OptionParser<?>> shadowedKeys = _shadowed.getOptionMap().keys();
 132    // HashMap<String,OptionParser<?>> combined = HashMap<String,OptionParser<?>>();
 133    // Enumeration<String> keyEn = _bundle.getKeys();
 134    // while(keyEn.hasMoreElements()) {
 135    // String key = keyEn.nextElement();
 136    // combined
 137    // }
 138  5414 return shadowedKeys;
 139    }
 140    };
 141    }
 142   
 143    /** Sets the given option to the given value and notifies all listeners of that option of the change.
 144    * @param op Option to set
 145    * @param value New value for the option
 146    */
 147  877 public <T> T setSetting(final Option<T> op, final T value) {
 148  0 if (op==null) return _shadowed.setSetting(op, value);
 149  877 try {
 150  877 String str = _bundle.getString(op.getName());
 151  0 return null; // defined in resource bundle, can't be set
 152    }
 153    catch(MissingResourceException mre) {
 154    // not defined, delegate to shadowed configuration
 155  877 return _shadowed.setSetting(op, value);
 156    }
 157    }
 158   
 159    /** Gets the current value of the given Option. */
 160  64433 public <T> T getSetting(Option<T> op) {
 161  0 if (op==null) return _shadowed.getSetting(op);
 162  64433 try {
 163  64433 String str = _bundle.getString(op.getName());
 164  0 return op.parse(str); // defined in resource bundle
 165    }
 166    catch(MissingResourceException mre) {
 167    // not defined, delegate to shadowed configuration
 168  64433 return _shadowed.getSetting(op);
 169    }
 170    }
 171   
 172    /** Return true if the option is editable. If it was defined in the resource bundle, it is not editable. */
 173  104494 public <T> boolean isEditable(Option<T> op) {
 174  0 if (op==null) return _shadowed.isEditable(op);
 175  104494 try {
 176  104494 String str = _bundle.getString(op.getName());
 177  0 return false; // defined, not editable
 178    }
 179    catch(MissingResourceException mre) {
 180    // not defined, delegate to shadowed configuration
 181  104494 return _shadowed.isEditable(op);
 182    }
 183    }
 184   
 185    /** Resets to the default values, overwriting any existing values. */
 186  232 public void resetToDefaults() {
 187    // just delegate, our values don't change
 188  232 _shadowed.resetToDefaults();
 189    }
 190   
 191    /** Returns a string representation of the contents of the OptionMap. */
 192  0 public String toString() {
 193  0 StringBuilder sb = new StringBuilder();
 194  0 sb.append("In resource bundle ");
 195  0 sb.append(_resourceBundleName);
 196  0 sb.append(":\n");
 197  0 boolean empty = true;
 198  0 Enumeration<String> keyEn = _bundle.getKeys();
 199  0 while(keyEn.hasMoreElements()) {
 200  0 String key = keyEn.nextElement();
 201  0 sb.append(key);
 202  0 sb.append(" = ");
 203  0 sb.append(_bundle.getString(key));
 204  0 sb.append("\n");
 205  0 empty = false;
 206    }
 207  0 if (empty) sb.append("\tnothing\n");
 208  0 sb.append("\nIn shadowed configuration:\n");
 209  0 sb.append(_shadowed);
 210  0 return sb.toString();
 211    }
 212   
 213    /** Calls SavableConfiguration.loadConfiguration, which loads all values from the file, based on the defaults in
 214    * OptionConstants.
 215    */
 216  0 public void loadConfiguration() throws IOException {
 217  0 _shadowed.loadConfiguration(); // just delegate, our values won't change
 218    }
 219   
 220    /** Saves the current settings to the stored properties file. */
 221  0 public void saveConfiguration() throws IOException {
 222  0 _shadowed.saveConfiguration();
 223    }
 224   
 225    /** Saves the current settings to the stored properties file.
 226    * @param header Description of the properties list
 227    */
 228  0 public void saveConfiguration(final String header) throws IOException {
 229  0 _shadowed.saveConfiguration(header);
 230    }
 231   
 232    /** Creates an OptionMapLoader with the values loaded from the InputStream
 233    * (and defaults where values weren't specified) and loads them into
 234    * this Configuration's OptionMap.
 235    * @param is InputStream containing properties-style keys and values
 236    */
 237  0 public void loadConfiguration(InputStream is) throws IOException {
 238  0 _shadowed.loadConfiguration(is); // just delegate, our values won't change
 239    }
 240   
 241    /** Used to save the values from this Configuration into the given OutputStream
 242    * as a Properties file. The elements weren't ordered, so now the properties
 243    * are written in the same way as the about dialog.
 244    * Values equal to their defaults are not written to disk.
 245    */
 246  0 public void saveConfiguration(OutputStream os, String header) throws IOException {
 247  0 _shadowed.saveConfiguration(os,header);
 248    }
 249   
 250    /** Adds an OptionListener to the given Option, to be notified each time the option changes.
 251    * @param op Option to listen for changes on
 252    * @param l OptionListener wishing to listen
 253    */
 254  98954 public <T> void addOptionListener(Option<T> op, OptionListener<T> l) {
 255  98954 op.addListener(this,l);
 256  98954 op.addListener(_shadowed,l);
 257    }
 258   
 259    /** Removes an OptionListener from an Option to which it was listening. */
 260  1728 public <T> void removeOptionListener(Option<T> op, OptionListener<T> l) {
 261  1728 op.removeListener(this,l);
 262  1728 op.removeListener(_shadowed,l);
 263    }
 264    }