Coverage Report - com.puppycrawl.tools.checkstyle.checks.UniquePropertiesCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
UniquePropertiesCheck
100%
26/26
100%
8/8
2.4
UniquePropertiesCheck$1
N/A
N/A
2.4
UniquePropertiesCheck$UniqueProperties
100%
8/8
75%
3/4
2.4
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  Oliver Burn
 4  
 //
 5  
 // This library is free software; you can redistribute it and/or
 6  
 // modify it under the terms of the GNU Lesser General Public
 7  
 // License as published by the Free Software Foundation; either
 8  
 // version 2.1 of the License, or (at your option) any later version.
 9  
 //
 10  
 // This library is distributed in the hope that it will be useful,
 11  
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // Lesser General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU Lesser General Public
 16  
 // License along with this library; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 ////////////////////////////////////////////////////////////////////////////////
 19  
 package com.puppycrawl.tools.checkstyle.checks;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.FileInputStream;
 23  
 import java.io.IOException;
 24  
 import java.util.List;
 25  
 import java.util.Properties;
 26  
 import java.util.regex.Matcher;
 27  
 import java.util.regex.Pattern;
 28  
 
 29  
 import com.google.common.collect.HashMultiset;
 30  
 import com.google.common.collect.Multiset;
 31  
 import com.google.common.collect.Multiset.Entry;
 32  
 import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
 33  
 
 34  
 /**
 35  
  * Checks the uniqueness of property keys (left from equal sign) in the
 36  
  * properties file.
 37  
  *
 38  
  * @author Pavel Baranchikov
 39  
  */
 40  
 public class UniquePropertiesCheck extends AbstractFileSetCheck
 41  
 {
 42  
 
 43  
     /**
 44  
      * Localization key for check violation.
 45  
      */
 46  
     public static final String MSG_KEY = "properties.duplicateproperty";
 47  
     /**
 48  
      * Localization key for IO exception occurred on file open.
 49  
      */
 50  
     public static final String IO_EXCEPTION_KEY = "unable.open.cause";
 51  
 
 52  
     /**
 53  
      * Construct the check with default values.
 54  
      */
 55  
     public UniquePropertiesCheck()
 56  3
     {
 57  3
         super.setFileExtensions(new String[]
 58  
         {"properties"});
 59  3
     }
 60  
 
 61  
     @Override
 62  
     protected void processFiltered(File aFile, List<String> aLines)
 63  
     {
 64  2
         final UniqueProperties properties = new UniqueProperties();
 65  
 
 66  
         try {
 67  
             // As file is already read, there should not be any exceptions.
 68  2
             properties.load(new FileInputStream(aFile));
 69  
         }
 70  1
         catch (IOException e) {
 71  1
             log(0, IO_EXCEPTION_KEY, aFile.getPath(),
 72  
                     e.getLocalizedMessage());
 73  1
         }
 74  
 
 75  2
         for (Entry<String> duplication : properties
 76  
                 .getDuplicatedStrings().entrySet())
 77  
         {
 78  6
             final String keyName = duplication.getElement();
 79  6
             final int lineNumber = getLineNumber(aLines, keyName);
 80  
             // Number of occurrences is number of duplications + 1
 81  6
             log(lineNumber, MSG_KEY, keyName, duplication.getCount() + 1);
 82  6
         }
 83  2
     }
 84  
 
 85  
     /**
 86  
      * Method returns line number the key is detected in the checked properties
 87  
      * files first.
 88  
      *
 89  
      * @param aLines
 90  
      *            properties file lines list
 91  
      * @param aKeyNane
 92  
      *            key name to look for
 93  
      * @return line number of first occurrence. If no key found in properties
 94  
      *         file, 0 is returned
 95  
      */
 96  
     protected int getLineNumber(List<String> aLines, String aKeyNane)
 97  
     {
 98  7
         final String keyPatternString =
 99  
                 "^" + aKeyNane.replace(" ", "\\\\ ") + "[\\s:=].*$";
 100  7
         final Pattern keyPattern = Pattern.compile(keyPatternString);
 101  7
         int lineNumber = 1;
 102  7
         final Matcher matcher = keyPattern.matcher("");
 103  7
         for (String line : aLines) {
 104  106
             matcher.reset(line);
 105  106
             if (matcher.matches()) {
 106  6
                 break;
 107  
             }
 108  100
             ++lineNumber;
 109  
         }
 110  7
         if (lineNumber > aLines.size()) {
 111  1
             lineNumber = 0;
 112  
         }
 113  7
         return lineNumber;
 114  
     }
 115  
 
 116  
     /**
 117  
      * Properties subclass to store duplicated property keys in a separate map.
 118  
      *
 119  
      * @author Pavel Baranchikov
 120  
      */
 121  4
     private static class UniqueProperties extends Properties
 122  
     {
 123  
         /**
 124  
          * Default serial version id.
 125  
          */
 126  
         private static final long serialVersionUID = 1L;
 127  
         /**
 128  
          * Multiset, holding duplicated keys. Keys are added here only if they
 129  
          * already exist in Properties' inner map.
 130  
          */
 131  2
         private final Multiset<String> mDuplicatedStrings = HashMultiset
 132  
                 .create();
 133  
 
 134  
         @Override
 135  
         public synchronized Object put(Object aKey, Object aValue)
 136  
         {
 137  37
             final Object oldValue = super.put(aKey, aValue);
 138  37
             if (oldValue != null && aKey instanceof String) {
 139  8
                 final String keyString = (String) aKey;
 140  8
                 mDuplicatedStrings.add(keyString);
 141  
             }
 142  37
             return oldValue;
 143  
         }
 144  
 
 145  
         public Multiset<String> getDuplicatedStrings()
 146  
         {
 147  2
             return mDuplicatedStrings;
 148  
         }
 149  
     }
 150  
 }