View Javadoc

1   /*
2    * soapUI, copyright (C) 2004-2010 eviware.com
3    *
4    * soapUI is free software; you can redistribute it and/or modify it under the
5    * terms of version 2.1 of the GNU Lesser General Public License as published by
6    * the Free Software Foundation.
7    *
8    * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9    * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   * See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  /*
14   * soapUI, copyright (C) 2004-2010 eviware.com
15   *
16   * soapUI is free software; you can redistribute it and/or modify it under the
17   * terms of version 2.1 of the GNU Lesser General Public License as published by
18   * the Free Software Foundation.
19   *
20   * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
21   * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22   * See the GNU Lesser General Public License for more details at gnu.org.
23   */
24  
25  package com.eviware.soapui.impl.wsdl.mock.dispatch;
26  
27  import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
28  import com.eviware.soapui.impl.wsdl.mock.WsdlMockRequest;
29  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
30  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResult;
31  
32  public class RandomMockOperationDispatcher extends AbstractMockOperationDispatcher
33  {
34  	public RandomMockOperationDispatcher( WsdlMockOperation mockOperation )
35  	{
36  		super( mockOperation );
37  	}
38  
39  	public WsdlMockResponse selectMockResponse( WsdlMockRequest request, WsdlMockResult result )
40  	{
41  		synchronized( result.getMockOperation() )
42  		{
43  			synchronized( this )
44  			{
45  				int currentDispatchIndex = ( int )( ( Math.random() * getMockOperation().getMockResponseCount() ) + 0.5F );
46  
47  				if( currentDispatchIndex >= getMockOperation().getMockResponseCount() )
48  					currentDispatchIndex = 0;
49  
50  				return getMockOperation().getMockResponseAt( currentDispatchIndex );
51  			}
52  		}
53  	}
54  
55  	public static class Factory implements MockOperationDispatchFactory
56  	{
57  		public MockOperationDispatcher build( WsdlMockOperation mockOperation )
58  		{
59  			return new RandomMockOperationDispatcher( mockOperation );
60  		}
61  	}
62  }