View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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  package com.eviware.soapui.impl.wsdl.panels.testcase;
14  
15  import java.awt.event.ActionEvent;
16  import java.awt.event.ActionListener;
17  
18  import javax.swing.JButton;
19  
20  import com.eviware.soapui.impl.wsdl.WsdlOperation;
21  import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
22  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
23  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
24  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
25  import com.eviware.soapui.model.iface.Attachment;
26  import com.eviware.soapui.model.iface.MessageExchange;
27  import com.eviware.soapui.model.support.ModelSupport;
28  import com.eviware.soapui.model.testsuite.MessageExchangeTestStepResult;
29  import com.eviware.soapui.support.UISupport;
30  import com.eviware.soapui.support.components.JXToolBar;
31  import com.eviware.x.form.XFormDialog;
32  import com.eviware.x.form.support.ADialogBuilder;
33  import com.eviware.x.form.support.AField;
34  import com.eviware.x.form.support.AForm;
35  import com.eviware.x.form.support.AField.AFieldType;
36  
37  public class TestCaseTestRunLog extends TestRunLog
38  {
39  	private final WsdlTestCase testCase;
40  	private JButton addToMockServiceButton;
41  	private XFormDialog addDialog;
42  
43  	public TestCaseTestRunLog( WsdlTestCase testCase )
44  	{
45  		super( testCase.getSettings() );
46  		this.testCase = testCase;
47  	}
48  	
49  	@Override
50  	protected void addToolbarButtons( JXToolBar toolbar )
51  	{
52  		super.addToolbarButtons( toolbar );
53  		
54  		toolbar.addFixed( addToMockServiceButton = UISupport.createToolbarButton(UISupport.createImageIcon( "/mockService.gif" ) ) );
55  		addToMockServiceButton.addActionListener( new AddToMockServiceAction() );
56  	}
57  	
58  	public void release()
59  	{
60  		if( addDialog != null )
61  		{
62  			addDialog.release();
63  			addDialog = null;
64  		}
65  		
66  		super.release();
67  	}
68  
69  	@AForm( description = "Set options for adding selected requests to a MockService", name = "Add To MockService" )
70  	private class AddToMockServiceAction implements ActionListener
71  	{
72  		private static final String CREATE_NEW_OPTION = "<Create New>";
73  
74  		@AField( name = "Target MockService", description = "The target TestSuite", type = AFieldType.ENUMERATION )
75  		public final static String MOCKSERVICE = "Target MockService";
76  		
77  		@AField( name = "Open Editor", description = "Open the created MockService", type = AFieldType.BOOLEAN )
78  		public final static String OPENEDITOR = "Open Editor";
79  
80  		public void actionPerformed( ActionEvent e )
81  		{
82  			if( getLogListModel().getSize() == 0 )
83  				return;
84  			
85  			if( testCase.getDiscardOkResults() )
86  			{
87  				UISupport.showInfoMessage( "Ok Results have been discarded" );
88  				return;
89  			}
90  
91  			if( addDialog == null )
92  			{
93  				addDialog = ADialogBuilder.buildDialog( this.getClass() );
94  			}
95  
96  			String[] testSuiteNames = ModelSupport.getNames( new String[] { CREATE_NEW_OPTION },  testCase.getTestSuite().getProject()
97  						.getMockServiceList() );
98  			addDialog.setOptions( MOCKSERVICE, testSuiteNames );
99  
100 			if( addDialog.show() )
101 			{
102 				String targetMockServiceName = addDialog.getValue( MOCKSERVICE );
103 
104 				WsdlMockService mockService = testCase.getTestSuite().getProject().getMockServiceByName( targetMockServiceName );
105 				if( mockService == null )
106 				{
107 					targetMockServiceName = ModelSupport.promptForUniqueName( "MockService",  testCase.getTestSuite().getProject(), "" );
108 					if( targetMockServiceName == null )
109 						return;
110 
111 					mockService =  testCase.getTestSuite().getProject().addNewMockService( targetMockServiceName );
112 				}
113 
114 				int cnt = 0;
115 				MessageExchangeTestStepResult result = null;
116 				for( int c = 0; c < getLogListModel().getSize(); c++  )
117 				{
118 					Object obj = getLogListModel().getResultAt( c );
119 					if( result != obj && obj instanceof MessageExchangeTestStepResult )
120 					{
121 						result = ( MessageExchangeTestStepResult ) obj;
122 						
123 						for( MessageExchange me : result.getMessageExchanges() )
124 						{
125 							if( me.isDiscarded() )
126 								continue;
127 							
128 							WsdlMockOperation mockOperation = mockService.getMockOperation( me.getOperation() );
129 							if( mockOperation == null )
130 								mockOperation = mockService.addNewMockOperation( ( WsdlOperation ) me.getOperation() );
131 
132 							WsdlMockResponse mockResponse = mockOperation.addNewMockResponse( "Recorded Test Response " + ( ++cnt ), false );
133 							mockResponse.setResponseContent( me.getResponseContent() );
134 
135 							Attachment[] requestAttachments = me.getResponseAttachments();
136 							if( requestAttachments != null )
137 							{
138 								for( Attachment attachment : requestAttachments )
139 								{
140 									mockResponse.addAttachment( attachment );
141 								}
142 							}
143 						}
144 					}
145 				}
146 				
147 				if( cnt == 0 )
148 				{
149 					UISupport.showInfoMessage( "No response messages found" );
150 				}
151 				else
152 				{
153 					UISupport.showInfoMessage( "Added " + cnt + " MockResponses to MockService" );
154 					
155 					if( addDialog.getBooleanValue( OPENEDITOR ))
156 						UISupport.selectAndShow( mockService );
157 				}
158 				
159 			}
160 		}
161 	}
162 }