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  package com.eviware.soapui.model.support;
14  
15  import java.io.FileWriter;
16  import java.io.IOException;
17  import java.io.PrintWriter;
18  import java.util.Arrays;
19  
20  import com.eviware.soapui.impl.wsdl.MutableTestPropertyHolder;
21  import com.eviware.soapui.model.TestPropertyHolder;
22  import com.eviware.soapui.model.testsuite.TestProperty;
23  
24  public class TestPropertyUtils
25  {
26  	private static boolean ascending;
27  
28  	public static int saveTo( TestPropertyHolder propertyHolder, String fileName ) throws IOException
29  	{
30  		PrintWriter writer = new PrintWriter( new FileWriter( fileName ) );
31  
32  		for( TestProperty prop : propertyHolder.getPropertyList() )
33  		{
34  			writer.print( prop.getName() );
35  			writer.print( '=' );
36  			String value = prop.getValue();
37  			if( value == null )
38  				value = "";
39  
40  			String[] lines = value.split( "\n" );
41  			for( int c = 0; c < lines.length; c++ )
42  			{
43  				if( c > 0 )
44  					writer.println( "//" );
45  				writer.print( lines[c] );
46  			}
47  
48  			writer.println();
49  		}
50  
51  		writer.close();
52  		return propertyHolder.getPropertyCount();
53  	}
54  
55  	public synchronized static void sortProperties( MutableTestPropertyHolder holder )
56  	{
57  		ascending = false;
58  		
59  		String[] names = holder.getPropertyNames();
60  		
61  		quicksort( holder, 0, holder.getPropertyCount() - 1 );
62  		if( Arrays.equals( names, holder.getPropertyNames() ))
63  		{
64  			ascending = true;
65  			quicksort( holder, 0, holder.getPropertyCount() - 1 );
66  		}
67  	}
68  
69  	private static void quicksort( MutableTestPropertyHolder array, int lo, int hi )
70  	{
71  		if( hi > lo )
72  		{
73  			int partitionPivotIndex = ( int )( Math.random() * ( hi - lo ) + lo );
74  			int newPivotIndex = partition( array, lo, hi, partitionPivotIndex );
75  			quicksort( array, lo, newPivotIndex - 1 );
76  			quicksort( array, newPivotIndex + 1, hi );
77  		}
78  		// return (T[]) array;
79  	}
80  
81  	private static int partition( MutableTestPropertyHolder array, int lo, int hi, int pivotIndex )
82  	{
83  		TestProperty pivotValue = array.getPropertyAt( pivotIndex );
84  
85  		swap( array, pivotIndex, hi ); // send to the back
86  
87  		int index = lo;
88  
89  		for( int i = lo; i < hi; i++ )
90  		{
91  			if( ascending )
92  			{
93  				if( ( array.getPropertyAt( i ).getName().toUpperCase().compareTo( pivotValue.getName().toUpperCase() ) >= 0 ) )
94  				{
95  					swap( array, i, index );
96  					index++ ;
97  				}
98  			}
99  			else
100 			{
101 				if( ( array.getPropertyAt( i ).getName().toUpperCase().compareTo( pivotValue.getName().toUpperCase() ) <= 0 ) )
102 				{
103 					swap( array, i, index );
104 					index++ ;
105 				}
106 			}
107 		}
108 
109 		swap( array, hi, index );
110 
111 		return index;
112 	}
113 
114 	private static void swap( MutableTestPropertyHolder array, int i, int j )
115 	{
116 		String prop1 = array.getPropertyAt( i ).getName();
117 		String prop2 = array.getPropertyAt( j ).getName();
118 
119 		array.moveProperty( prop1, j );
120 		array.moveProperty( prop2, i );
121 
122 		//      	
123 		// T temp = array[i];
124 		// array[i] = array[j];
125 		// array[j] = temp;
126 	}
127 }