View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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 JTestCaseTestRunLog extends JTestRunLog
38  {
39  	private final WsdlTestCase testCase;
40  	private JButton addToMockServiceButton;
41  	private XFormDialog addDialog;
42  
43  	public JTestCaseTestRunLog( 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
55  				.createImageIcon( "/mockService.gif" ) ) );
56  		addToMockServiceButton.addActionListener( new AddToMockServiceAction() );
57  	}
58  
59  	public void release()
60  	{
61  		if( addDialog != null )
62  		{
63  			addDialog.release();
64  			addDialog = null;
65  		}
66  
67  		super.release();
68  	}
69  
70  	@AForm( description = "Set options for adding selected requests to a MockService", name = "Add To MockService" )
71  	private class AddToMockServiceAction implements ActionListener
72  	{
73  		private static final String CREATE_NEW_OPTION = "<Create New>";
74  
75  		@AField( name = "Target MockService", description = "The target TestSuite", type = AFieldType.ENUMERATION )
76  		public final static String MOCKSERVICE = "Target MockService";
77  
78  		@AField( name = "Open Editor", description = "Open the created MockService", type = AFieldType.BOOLEAN )
79  		public final static String OPENEDITOR = "Open Editor";
80  
81  		public void actionPerformed( ActionEvent e )
82  		{
83  			if( getLogListModel().getSize() == 0 )
84  				return;
85  
86  			if( testCase.getDiscardOkResults() )
87  			{
88  				UISupport.showInfoMessage( "Ok Results have been discarded" );
89  				return;
90  			}
91  
92  			if( addDialog == null )
93  			{
94  				addDialog = ADialogBuilder.buildDialog( this.getClass() );
95  			}
96  
97  			String[] testSuiteNames = ModelSupport.getNames( new String[] { CREATE_NEW_OPTION }, testCase.getTestSuite()
98  					.getProject().getMockServiceList() );
99  			addDialog.setOptions( MOCKSERVICE, testSuiteNames );
100 
101 			if( addDialog.show() )
102 			{
103 				String targetMockServiceName = addDialog.getValue( MOCKSERVICE );
104 
105 				WsdlMockService mockService = testCase.getTestSuite().getProject().getMockServiceByName(
106 						targetMockServiceName );
107 				if( mockService == null )
108 				{
109 					targetMockServiceName = ModelSupport.promptForUniqueName( "MockService", testCase.getTestSuite()
110 							.getProject(), "" );
111 					if( targetMockServiceName == null )
112 						return;
113 
114 					mockService = testCase.getTestSuite().getProject().addNewMockService( targetMockServiceName );
115 				}
116 
117 				int cnt = 0;
118 				MessageExchangeTestStepResult result = null;
119 				for( int c = 0; c < getLogListModel().getSize(); c++ )
120 				{
121 					Object obj = getLogListModel().getResultAt( c );
122 					if( result != obj && obj instanceof MessageExchangeTestStepResult )
123 					{
124 						result = ( MessageExchangeTestStepResult )obj;
125 
126 						for( MessageExchange me : result.getMessageExchanges() )
127 						{
128 							if( me.isDiscarded() )
129 								continue;
130 
131 							WsdlMockOperation mockOperation = mockService.getMockOperation( me.getOperation() );
132 							if( mockOperation == null )
133 								mockOperation = mockService.addNewMockOperation( ( WsdlOperation )me.getOperation() );
134 
135 							WsdlMockResponse mockResponse = mockOperation.addNewMockResponse( "Recorded Test Response "
136 									+ ( ++cnt ), false );
137 							mockResponse.setResponseContent( me.getResponseContent() );
138 
139 							Attachment[] requestAttachments = me.getResponseAttachments();
140 							if( requestAttachments != null )
141 							{
142 								for( Attachment attachment : requestAttachments )
143 								{
144 									mockResponse.addAttachment( attachment );
145 								}
146 							}
147 						}
148 					}
149 				}
150 
151 				if( cnt == 0 )
152 				{
153 					UISupport.showInfoMessage( "No response messages found" );
154 				}
155 				else
156 				{
157 					UISupport.showInfoMessage( "Added " + cnt + " MockResponses to MockService" );
158 
159 					if( addDialog.getBooleanValue( OPENEDITOR ) )
160 						UISupport.selectAndShow( mockService );
161 				}
162 
163 			}
164 		}
165 	}
166 }