Coverage Report - org.mule.example.loanbroker.AbstractLoanBrokerApp
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractLoanBrokerApp
0%
0/118
0%
0/23
0
 
 1  
 /*
 2  
  * $Id: AbstractLoanBrokerApp.java 20321 2010-11-24 15:21:24Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.example.loanbroker;
 12  
 
 13  
 import org.mule.api.MuleException;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.config.ConfigurationBuilder;
 16  
 import org.mule.config.spring.SpringXmlConfigurationBuilder;
 17  
 import org.mule.example.loanbroker.messages.Customer;
 18  
 import org.mule.example.loanbroker.messages.CustomerQuoteRequest;
 19  
 import org.mule.module.client.MuleClient;
 20  
 import org.mule.module.client.RemoteDispatcher;
 21  
 
 22  
 import java.io.IOException;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 
 28  
 /**
 29  
  * Runs the LoanBroker example application.
 30  
  */
 31  
 public abstract class AbstractLoanBrokerApp
 32  
 {
 33  0
     private List<Customer> customers = new ArrayList<Customer>();
 34  0
     private RemoteDispatcher remoteClient = null;
 35  
     private String config;
 36  
 
 37  
     public AbstractLoanBrokerApp() throws Exception
 38  0
     {
 39  0
         this.config = null;
 40  0
         init();
 41  0
     }
 42  
 
 43  
     public AbstractLoanBrokerApp(String config) throws Exception
 44  0
     {
 45  0
         this.config = config;
 46  0
         init();
 47  0
     }
 48  
 
 49  
     protected void init() throws Exception
 50  
     {
 51  0
         MuleClient muleClient = new MuleClient(true);
 52  0
         remoteClient = muleClient.getRemoteDispatcher("tcp://localhost:5555");
 53  0
         customers.add(new Customer("Jenson Button", 123));
 54  0
         customers.add(new Customer("Michael Schumacker", 456));
 55  0
         customers.add(new Customer("Juan Pablo Montoya", 789));
 56  0
         customers.add(new Customer("David Colthard", 101));
 57  0
         customers.add(new Customer("Rubens Barrichello", 112));
 58  0
         customers.add(new Customer("Mark Webber", 131));
 59  0
         customers.add(new Customer("Takuma Sato", 415));
 60  0
         customers.add(new Customer("Kimi Raikkonen", 161));
 61  0
         customers.add(new Customer("Ralf Schumacher", 718));
 62  0
         customers.add(new Customer("Jarno Trulli", 192));
 63  0
     }
 64  
 
 65  
     protected ConfigurationBuilder getConfigBuilder() throws MuleException
 66  
     {
 67  0
         return new SpringXmlConfigurationBuilder(config);
 68  
     }
 69  
 
 70  
     protected void dispose() throws Exception
 71  
     {
 72  0
         remoteClient.dispose();
 73  0
     }
 74  
 
 75  
     protected void run(boolean synchronous) throws Exception
 76  
     {
 77  0
         int response = 0;
 78  0
         while (response != 'q')
 79  
         {
 80  0
             System.out.println("\n" + LocaleMessage.menu());
 81  
 
 82  0
             response = readCharacter();
 83  
 
 84  0
             switch (response)
 85  
             {
 86  
                 case '1' :
 87  
                 {
 88  0
                     CustomerQuoteRequest request = getRequestFromUser();
 89  0
                     request(request, synchronous);
 90  0
                     break;
 91  
                 }
 92  
 
 93  
                 case '2' :
 94  
                 {
 95  0
                     sendRandomRequests(100, synchronous);
 96  0
                     break;
 97  
                 }
 98  
 
 99  
                 case '3' :
 100  
                 {
 101  0
                     System.out.println(LocaleMessage.menuOptionNumberOfRequests());
 102  0
                     int number = readInt();
 103  0
                     if (number < 1)
 104  
                     {
 105  0
                         System.out.println(LocaleMessage.menuErrorNumberOfRequests());
 106  
                     }
 107  
                     else
 108  
                     {
 109  0
                         sendRandomRequests(number, synchronous);
 110  
                     }
 111  0
                     break;
 112  
                 }
 113  
 
 114  
                 case 'q' :
 115  
                 {
 116  0
                     System.out.println(LocaleMessage.exiting());
 117  0
                     dispose();
 118  0
                     System.exit(0);
 119  
                 }
 120  
 
 121  
                 default :
 122  
                 {
 123  0
                     System.out.println(LocaleMessage.menuError());
 124  
                 }
 125  
             }
 126  
         }
 127  0
     }
 128  
 
 129  
     public CustomerQuoteRequest createRequest()
 130  
     {
 131  0
         int index = new Double(Math.random() * 10).intValue();
 132  0
         Customer c = customers.get(index);
 133  
 
 134  0
         return new CustomerQuoteRequest(c, getRandomAmount(), getRandomDuration());
 135  
     }
 136  
 
 137  
     protected static CustomerQuoteRequest getRequestFromUser() throws IOException
 138  
     {
 139  0
         byte[] buf = new byte[128];
 140  0
         System.out.print(LocaleMessage.enterName());
 141  0
         System.in.read(buf);
 142  0
         String name = new String(buf).trim();
 143  0
         System.out.print(LocaleMessage.enterLoanAmount());
 144  0
         buf = new byte[16];
 145  0
         System.in.read(buf);
 146  0
         String amount = new String(buf).trim();
 147  0
         System.out.print(LocaleMessage.enterLoanDuration());
 148  0
         buf = new byte[16];
 149  0
         System.in.read(buf);
 150  0
         String duration = new String(buf).trim();
 151  
 
 152  0
         int d = 0;
 153  
         try
 154  
         {
 155  0
             d = Integer.parseInt(duration);
 156  
         }
 157  0
         catch (NumberFormatException e)
 158  
         {
 159  0
             System.out.println(LocaleMessage.loanDurationError(duration));
 160  0
             d = getRandomDuration();
 161  0
         }
 162  
 
 163  0
         double a = 0;
 164  
         try
 165  
         {
 166  0
             a = Double.valueOf(amount).doubleValue();
 167  
         }
 168  0
         catch (NumberFormatException e)
 169  
         {
 170  0
             System.out.println(LocaleMessage.loanAmountError(amount));
 171  0
             a = getRandomAmount();
 172  0
         }
 173  
 
 174  0
         Customer c = new Customer(name, getRandomSsn());
 175  0
         CustomerQuoteRequest request = new CustomerQuoteRequest(c, a, d);
 176  0
         return request;
 177  
     }
 178  
 
 179  
     public void request(CustomerQuoteRequest request, boolean sync) throws Exception
 180  
     {
 181  0
         if (!sync)
 182  
         {
 183  0
             remoteClient.dispatchRemote("CustomerRequests", request, null);
 184  0
             System.out.println(LocaleMessage.sentAsync());
 185  
             // let the request catch up
 186  0
             Thread.sleep(3000);
 187  
             
 188  
         }
 189  
         else
 190  
         {
 191  0
             MuleMessage result = remoteClient.sendRemote("CustomerRequests", request, null);
 192  0
             if (result == null)
 193  
             {
 194  0
                 System.out.println(LocaleMessage.requestError());
 195  
             }
 196  
             else
 197  
             {
 198  0
                 System.out.println(LocaleMessage.requestResponse(result.getPayload()));
 199  
             }
 200  
         }
 201  0
     }
 202  
 
 203  
     public void requestDispatch(int number, String endpoint) throws Exception
 204  
     {
 205  0
         for (int i = 0; i < number; i++)
 206  
         {
 207  0
             remoteClient.dispatchRemote(endpoint, createRequest(), null);
 208  
         }
 209  0
     }
 210  
 
 211  
     public List<Object> requestSend(int number, String endpoint) throws Exception
 212  
     {
 213  0
         return requestSend(number, endpoint, null);
 214  
     }
 215  
 
 216  
     public List<Object> requestSend(int number, String endpoint, Map properties) throws Exception
 217  
     {
 218  0
         List<Object> results = new ArrayList<Object>(number);
 219  0
         for (int i = 0; i < number; i++)
 220  
         {
 221  0
             MuleMessage result = remoteClient.sendRemote(endpoint, createRequest(), properties);
 222  
             
 223  0
             if (result != null)
 224  
             {
 225  0
                 results.add(result.getPayload());
 226  
             }
 227  
         }
 228  0
         return results;
 229  
     }
 230  
 
 231  
     protected void sendRandomRequests(int number, boolean synchronous) throws Exception
 232  
     {
 233  0
         if (synchronous)
 234  
         {
 235  0
             List<Object> list = requestSend(number, "CustomerRequests");
 236  0
             int i = 1;
 237  0
             System.out.println("sendRandomRequests");
 238  0
             for (Iterator<Object> iterator = list.iterator(); iterator.hasNext(); i++)
 239  
             {
 240  0
                 System.out.println("sendRandomRequests results :" +
 241  
                     LocaleMessage.request(i, iterator.next().toString()));
 242  
             }
 243  0
         }
 244  
         else
 245  
         {
 246  0
             this.requestDispatch(number, "CustomerRequests");
 247  0
             System.out.println(LocaleMessage.sentAsync());
 248  
         }
 249  0
     }
 250  
 
 251  
     protected static int readCharacter() throws IOException
 252  
     {
 253  0
         byte[] buf = new byte[16];
 254  0
         System.in.read(buf);
 255  0
         return buf[0];
 256  
     }
 257  
 
 258  
     protected static String readString() throws IOException
 259  
     {
 260  0
         byte[] buf = new byte[80];
 261  0
         System.in.read(buf);
 262  0
         return new String(buf).trim();
 263  
     }
 264  
 
 265  
     protected static int readInt() throws IOException
 266  
     {
 267  
         try
 268  
         {
 269  0
             return Integer.parseInt(readString());
 270  
         }
 271  0
         catch (NumberFormatException nfex)
 272  
         {
 273  0
             return 0;
 274  
         }
 275  
     }
 276  
 
 277  
     protected static double getRandomAmount()
 278  
     {
 279  0
         return Math.round(Math.random() * 18000);
 280  
     }
 281  
 
 282  
     protected static int getRandomDuration()
 283  
     {
 284  0
         return new Double(Math.random() * 60).intValue();
 285  
     }
 286  
 
 287  
     protected static int getRandomSsn()
 288  
     {
 289  0
         return new Double(Math.random() * 6000).intValue();
 290  
     }
 291  
 }