Coverage Report - org.apache.shiro.samples.aspectj.bank.Account
 
Classes in this File Line Coverage Branch Coverage Complexity
Account
86%
39/45
75%
6/8
1.538
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.shiro.samples.aspectj.bank;
 20  
 
 21  
 
 22  
 import org.apache.commons.lang.builder.ToStringBuilder;
 23  
 import org.apache.commons.lang.builder.ToStringStyle;
 24  
 
 25  
 import java.sql.Timestamp;
 26  
 import java.util.ArrayList;
 27  
 import java.util.Date;
 28  
 import java.util.List;
 29  
 
 30  
 public class Account {
 31  
 
 32  
     private static long _SEQUENCE;
 33  
 
 34  
     private long _id;
 35  
 
 36  
     private String _ownerName;
 37  
 
 38  
     private volatile boolean _isActive;
 39  
 
 40  
     private double _balance;
 41  
 
 42  
     private final List<AccountTransaction> _transactions;
 43  
 
 44  
     private String _createdBy;
 45  
 
 46  
     private Date _creationDate;
 47  
 
 48  24
     public Account(String anOwnerName) {
 49  24
         _id = ++_SEQUENCE;
 50  24
         _ownerName = anOwnerName;
 51  24
         _isActive = true;
 52  24
         _balance = 0.0d;
 53  24
         _transactions = new ArrayList<AccountTransaction>();
 54  24
         _createdBy = "unknown";
 55  24
         _creationDate = new Date();
 56  24
     }
 57  
 
 58  
     /**
 59  
      * Returns the id attribute.
 60  
      *
 61  
      * @return The id value.
 62  
      */
 63  
     public long getId() {
 64  88
         return _id;
 65  
     }
 66  
 
 67  
     /**
 68  
      * Returns the ownerName attribute.
 69  
      *
 70  
      * @return The ownerName value.
 71  
      */
 72  
     public String getOwnerName() {
 73  66
         return _ownerName;
 74  
     }
 75  
 
 76  
     /**
 77  
      * Returns the isActive attribute.
 78  
      *
 79  
      * @return The isActive value.
 80  
      */
 81  
     public boolean isActive() {
 82  74
         return _isActive;
 83  
     }
 84  
 
 85  
     /**
 86  
      * Changes the value of the attributes isActive.
 87  
      *
 88  
      * @param aIsActive The new value of the isActive attribute.
 89  
      */
 90  
     public void setActive(boolean aIsActive) {
 91  6
         _isActive = aIsActive;
 92  6
     }
 93  
 
 94  
     /**
 95  
      * Changes the value of the attributes ownerName.
 96  
      *
 97  
      * @param aOwnerName The new value of the ownerName attribute.
 98  
      */
 99  
     public void setOwnerName(String aOwnerName) {
 100  0
         _ownerName = aOwnerName;
 101  0
     }
 102  
 
 103  
     /**
 104  
      * Returns the balance attribute.
 105  
      *
 106  
      * @return The balance value.
 107  
      */
 108  
     public double getBalance() {
 109  156
         return _balance;
 110  
     }
 111  
 
 112  
     /**
 113  
      * Returns the transactions attribute.
 114  
      *
 115  
      * @return The transactions value.
 116  
      */
 117  
     public List<AccountTransaction> getTransactions() {
 118  188
         return _transactions;
 119  
     }
 120  
 
 121  
     protected void applyTransaction(AccountTransaction aTransaction) throws NotEnoughFundsException, InactiveAccountException {
 122  38
         if (!_isActive) {
 123  0
             throw new InactiveAccountException("Unable to apply " + aTransaction.getType() + " of amount " + aTransaction.getAmount() + " to account " + _id);
 124  
         }
 125  
 
 126  72
         synchronized (_transactions) {
 127  38
             if (AccountTransaction.TransactionType.DEPOSIT == aTransaction.getType()) {
 128  18
                 _transactions.add(aTransaction);
 129  18
                 _balance += aTransaction.getAmount();
 130  
 
 131  18
             } else if (AccountTransaction.TransactionType.WITHDRAWAL == aTransaction.getType()) {
 132  20
                 if (_balance < aTransaction.getAmount()) {
 133  4
                     throw new NotEnoughFundsException("Unable to withdraw " + aTransaction.getAmount() + "$ from account " + _id + " - current balance is " + _balance);
 134  
                 }
 135  16
                 _transactions.add(aTransaction);
 136  16
                 _balance -= aTransaction.getAmount();
 137  
 
 138  16
             } else {
 139  0
                 throw new IllegalArgumentException("The transaction passed in has an invalid type: " + aTransaction.getType());
 140  
             }
 141  
         }
 142  34
     }
 143  
 
 144  
     /**
 145  
      * Changes the value of the attributes createdBy.
 146  
      *
 147  
      * @param aCreatedBy The new value of the createdBy attribute.
 148  
      */
 149  
     protected void setCreatedBy(String aCreatedBy) {
 150  24
         _createdBy = aCreatedBy;
 151  24
     }
 152  
 
 153  
     /**
 154  
      * Returns the createdBy attribute.
 155  
      *
 156  
      * @return The createdBy value.
 157  
      */
 158  
     public String getCreatedBy() {
 159  0
         return _createdBy;
 160  
     }
 161  
 
 162  
     /**
 163  
      * Returns the creationDate attribute.
 164  
      *
 165  
      * @return The creationDate value.
 166  
      */
 167  
     public Date getCreationDate() {
 168  0
         return _creationDate;
 169  
     }
 170  
 
 171  
     /* (non-Javadoc)
 172  
     * @see java.lang.Object#toString()
 173  
     */
 174  
 
 175  
     public String toString() {
 176  768
         return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).
 177  384
                 append("id", _id).
 178  384
                 append("ownerName", _ownerName).
 179  384
                 append("isActive", _isActive).
 180  384
                 append("balance", _balance).
 181  384
                 append("tx.count", _transactions.size()).
 182  384
                 append("createdBy", _createdBy).
 183  384
                 append("creationDate", new Timestamp(_creationDate.getTime())).
 184  384
                 toString();
 185  
     }
 186  
 }