Coverage Report - org.argouml.uml.cognitive.critics.CrUnconventionalAttrName
 
Classes in this File Line Coverage Branch Coverage Complexity
CrUnconventionalAttrName
12%
9/71
0%
0/42
4
 
 1  
 /* $Id: CrUnconventionalAttrName.java 17849 2010-01-12 19:50:34Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    maurelio1234
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2007 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.uml.cognitive.critics;
 40  
 
 41  
 import java.util.HashSet;
 42  
 import java.util.Set;
 43  
 
 44  
 import javax.swing.Icon;
 45  
 
 46  
 import org.argouml.cognitive.Critic;
 47  
 import org.argouml.cognitive.Designer;
 48  
 import org.argouml.cognitive.ListSet;
 49  
 import org.argouml.cognitive.ToDoItem;
 50  
 import org.argouml.cognitive.critics.Wizard;
 51  
 import org.argouml.model.Model;
 52  
 import org.argouml.uml.cognitive.UMLDecision;
 53  
 import org.argouml.uml.cognitive.UMLToDoItem;
 54  
 
 55  
 /**
 56  
  * Critic to detect whether an attribute name obeys to certain rules.<p>
 57  
  *
 58  
  * Checks for:
 59  
  * <ul>
 60  
  * <li> all lower case or
 61  
  * <li> all upper case
 62  
  * </ul>
 63  
  * where trailing underscores are removed, and
 64  
  * constants are not nagged at.
 65  
  */
 66  
 public class CrUnconventionalAttrName extends AbstractCrUnconventionalName {
 67  
 
 68  
     /**
 69  
      * The constructor.
 70  
      */
 71  900
     public CrUnconventionalAttrName() {
 72  900
         setupHeadAndDesc();
 73  900
         addSupportedDecision(UMLDecision.NAMING);
 74  900
         setKnowledgeTypes(Critic.KT_SYNTAX);
 75  900
         addTrigger("feature_name");
 76  900
     }
 77  
 
 78  
 
 79  
     /*
 80  
      * @see org.argouml.uml.cognitive.critics.CrUML#predicate2(
 81  
      *      java.lang.Object, org.argouml.cognitive.Designer)
 82  
      */
 83  
     public boolean predicate2(Object dm, Designer dsgr) {
 84  0
         if (!Model.getFacade().isAAttribute(dm)) {
 85  0
             return NO_PROBLEM;
 86  
         }
 87  
 
 88  0
         Object attr = /*(MAttribute)*/ dm;
 89  0
         String nameStr = Model.getFacade().getName(attr);
 90  0
         if (nameStr == null || nameStr.equals("")) {
 91  0
             return NO_PROBLEM;
 92  
         }
 93  
 
 94  0
         int pos = 0;
 95  0
         int length = nameStr.length();
 96  
 
 97  0
         for (; pos < length && nameStr.charAt(pos) == '_'; pos++) {
 98  
         }
 99  
 
 100  
         // If the name is only underscores
 101  0
         if (pos >= length) {
 102  0
             return PROBLEM_FOUND;
 103  
         }
 104  
 
 105  
         // check for all uppercase and/or mixed with underscores
 106  0
         char initalChar = nameStr.charAt(pos);
 107  0
         boolean allCapitals = true;
 108  0
         for (; pos < length; pos++) {
 109  0
             if (!Character.isUpperCase(nameStr.charAt(pos))
 110  
                 && nameStr.charAt(pos) != '_') {
 111  0
                 allCapitals = false;
 112  0
                 break;
 113  
             }
 114  
         }
 115  0
         if (allCapitals) {
 116  0
             return NO_PROBLEM;
 117  
         }
 118  
 
 119  
         // check whether constant, constants are often weird and thus not a
 120  
         // problem
 121  0
         if (Model.getFacade().isReadOnly(attr)) {
 122  0
             return NO_PROBLEM;
 123  
         }
 124  
 
 125  0
         if (!Character.isLowerCase(initalChar)) {
 126  0
             return PROBLEM_FOUND;
 127  
         }
 128  
 
 129  0
         return NO_PROBLEM;
 130  
     }
 131  
 
 132  
     /*
 133  
      * @see org.argouml.cognitive.critics.Critic#toDoItem( java.lang.Object,
 134  
      *      org.argouml.cognitive.Designer)
 135  
      */
 136  
     public ToDoItem toDoItem(Object dm, Designer dsgr) {
 137  0
         Object f = dm;
 138  0
         ListSet offs = computeOffenders(f);
 139  0
         return new UMLToDoItem(this, offs, dsgr);
 140  
     }
 141  
 
 142  
     /**
 143  
      * @param dm the feature
 144  
      * @return the set of offenders
 145  
      */
 146  
     protected ListSet computeOffenders(Object dm) {
 147  0
         ListSet offs = new ListSet(dm);
 148  0
         offs.add(Model.getFacade().getOwner(dm));
 149  0
         return offs;
 150  
     }
 151  
 
 152  
     /*
 153  
      * @see org.argouml.uml.cognitive.critics.AbstractCrUnconventionalName#computeSuggestion(java.lang.String)
 154  
      */
 155  
     public String computeSuggestion(String name) {
 156  
         String sug;
 157  
         int nu;
 158  
 
 159  0
         if (name == null) {
 160  0
             return "attr";
 161  
         }
 162  
 
 163  0
         for (nu = 0; nu < name.length(); nu++) {
 164  0
             if (name.charAt(nu) != '_') {
 165  0
                 break;
 166  
             }
 167  
         }
 168  
 
 169  0
         if (nu > 0) {
 170  0
             sug = name.substring(0, nu);
 171  
         } else {
 172  0
             sug = "";
 173  
         }
 174  
 
 175  0
         if (nu < name.length()) {
 176  0
             sug += Character.toLowerCase(name.charAt(nu));
 177  
         }
 178  
 
 179  0
         if (nu + 1 < name.length()) {
 180  0
             sug += name.substring(nu + 1);
 181  
         }
 182  
 
 183  0
         return sug;
 184  
     }
 185  
     
 186  
     /*
 187  
      * @see org.argouml.cognitive.Poster#getClarifier()
 188  
      */
 189  
     public Icon getClarifier() {
 190  0
         return ClAttributeCompartment.getTheInstance();
 191  
     }
 192  
 
 193  
     /*
 194  
      * @see org.argouml.cognitive.Poster#stillValid(
 195  
      *      org.argouml.cognitive.ToDoItem, org.argouml.cognitive.Designer)
 196  
      */
 197  
     public boolean stillValid(ToDoItem i, Designer dsgr) {
 198  0
         if (!isActive()) {
 199  0
             return false;
 200  
         }
 201  0
         ListSet offs = i.getOffenders();
 202  0
         Object f = offs.get(0);
 203  0
         if (!predicate(f, dsgr)) {
 204  0
             return false;
 205  
         }
 206  0
         ListSet newOffs = computeOffenders(f);
 207  0
         boolean res = offs.equals(newOffs);
 208  0
         return res;
 209  
     }
 210  
 
 211  
 
 212  
     /*
 213  
      * @see org.argouml.cognitive.critics.Critic#initWizard(
 214  
      *         org.argouml.cognitive.ui.Wizard)
 215  
      */
 216  
     public void initWizard(Wizard w) {
 217  0
         if (w instanceof WizMEName) {
 218  0
             ToDoItem item = (ToDoItem) w.getToDoItem();
 219  0
             Object me = item.getOffenders().get(0);
 220  0
             String sug = computeSuggestion(Model.getFacade().getName(me));
 221  0
             String ins = super.getInstructions();
 222  0
             ((WizMEName) w).setInstructions(ins);
 223  0
             ((WizMEName) w).setSuggestion(sug);
 224  
         }
 225  0
     }
 226  
 
 227  
     /*
 228  
      * @see org.argouml.cognitive.critics.Critic#getWizardClass(org.argouml.cognitive.ToDoItem)
 229  
      */
 230  
     @Override
 231  
     public Class getWizardClass(ToDoItem item) {
 232  0
         return WizMEName.class;
 233  
     }
 234  
 
 235  
     /*
 236  
      * @see org.argouml.uml.cognitive.critics.CrUML#getCriticizedDesignMaterials()
 237  
      */
 238  
     public Set<Object> getCriticizedDesignMaterials() {
 239  900
         Set<Object> ret = new HashSet<Object>();
 240  900
         ret.add(Model.getMetaTypes().getAttribute());
 241  900
         return ret;
 242  
     }
 243  
     
 244  
     /**
 245  
      * The UID.
 246  
      */
 247  
     private static final long serialVersionUID = 4741909365018862474L;
 248  
 
 249  
 }