You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by se...@apache.org on 2004/06/23 01:42:44 UTC

cvs commit: jakarta-jmeter/src/functions/org/apache/jmeter/functions StringFromFile.java

sebb        2004/06/22 16:42:44

  Modified:    src/functions/org/apache/jmeter/functions Tag: rel-2_0
                        StringFromFile.java
  Log:
  Fix end of sequence handling; allow loop count to work
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.14.2.2  +56 -17    jakarta-jmeter/src/functions/org/apache/jmeter/functions/StringFromFile.java
  
  Index: StringFromFile.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/StringFromFile.java,v
  retrieving revision 1.14.2.1
  retrieving revision 1.14.2.2
  diff -u -r1.14.2.1 -r1.14.2.2
  --- StringFromFile.java	12 Jun 2004 20:26:58 -0000	1.14.2.1
  +++ StringFromFile.java	22 Jun 2004 23:42:44 -0000	1.14.2.2
  @@ -33,6 +33,7 @@
   import org.apache.jmeter.threads.JMeterVariables;
   import org.apache.jmeter.util.JMeterUtils;
   import org.apache.jorphan.logging.LoggingManager;
  +import org.apache.jorphan.util.JMeterStopThreadException;
   import org.apache.log.Logger;
   
   /**
  @@ -131,7 +132,7 @@
       }
   
       private int myStart = 0;
  -    private int myCurrent = 0;
  +    private int myCurrent = -1;
   	private int myEnd = 0;
   	
       private void openFile()
  @@ -139,33 +140,64 @@
   		String tn = Thread.currentThread().getName();
           fileName = ((CompoundVariable) values[0]).execute();
   
  +        String start = "";
   		if (values.length >= PARAM_START)
   		{
  -			String tmp = ((CompoundVariable) values[PARAM_START-1]).execute();
  -			myStart = Integer.valueOf(tmp).intValue();
  +			start = ((CompoundVariable) values[PARAM_START-1]).execute();
  +			try
  +			{
  +				myStart = Integer.valueOf(start).intValue();
  +			}
  +			catch (NumberFormatException e)
  +			{
  +				myStart=1;// so "" will give 1
  +			}
   			// Have we use myCurrent yet?
  -			if (myCurrent == 0) myCurrent=myStart;
  +			if (myCurrent == -1) myCurrent=myStart;
   		}
   
   		if (values.length >= PARAM_END)
   		{
   			String tmp = ((CompoundVariable) values[PARAM_END-1]).execute();
  -			myEnd = Integer.valueOf(tmp).intValue();
  +			try
  +			{
  +				myEnd = Integer.valueOf(tmp).intValue();
  +			}
  +			catch (NumberFormatException e)
  +			{
  +				myEnd=0;
  +			}
  +			
   		}
   
   		if (values.length >= PARAM_START)
   		{
  -			log.info("Start ="+myStart+" Current = "+myCurrent+" End ="+myEnd);//$NON-NLS-1$
  +			log.info(tn+" Start = "+myStart+" Current = "+myCurrent+" End = "+myEnd);//$NON-NLS-1$
   			if (values.length >= PARAM_END){
   				if (myCurrent > myEnd){
  -					log.info("No more files to process, "+myCurrent+" > "+myEnd);//$NON-NLS-1$
  +					log.info(tn+" No more files to process, "+myCurrent+" > "+myEnd);//$NON-NLS-1$
   					myBread=null;
   					return;
   				}
   			}
  -			log.info("Using format "+fileName);
  -			DecimalFormat myFormatter = new DecimalFormat(fileName);
  -			fileName = myFormatter.format(myCurrent);
  +			/*
  +			 * DecimalFormat adds the number to the end of the format if there are
  +			 * no formatting characters, so we need a way to prevent this from messing
  +			 * up the file name.
  +			 * 
  +			 */
  +			if (start.length()>0) // Only try to format if there is a number
  +			{
  +				log.info("Using format "+fileName);
  +				try {
  +					DecimalFormat myFormatter = new DecimalFormat(fileName);
  +					fileName = myFormatter.format(myCurrent);
  +				}
  +				catch (NumberFormatException e)
  +				{
  +					log.warn("Bad file name format ",e);
  +				}
  +			}
   			myCurrent++;// for next time
           }
   
  @@ -218,26 +250,32 @@
                   String line = myBread.readLine();
                   if (line == null && reopenFile)
                   { // EOF, re-open file
  -                    log.info("Reached EOF on " + fileName);//$NON-NLS-1$
  +    				String tn = Thread.currentThread().getName();
  +                    log.info(tn+" Reached EOF on " + fileName);//$NON-NLS-1$
                       closeFile();
                       openFile();
                       if (myBread != null) {
   						line = myBread.readLine();
                       } else {
                       	line = ERR_IND;
  +                    	if (values.length >= PARAM_END){// Are we processing a file sequence?
  +                    		log.info(tn + " Detected end of sequence.");
  +               				throw new JMeterStopThreadException("End of sequence");
  +                    	}
                       }
                   }
                   myValue = line;
               }
  -            catch (Exception e)
  +            catch (IOException e)
               {
   				String tn = Thread.currentThread().getName();
                   log.error(tn + " error reading file " + e.toString());//$NON-NLS-1$
               }
           } else { // File was not opened successfully
           	if (values.length >= PARAM_END){// Are we processing a file sequence?
  -        		log.info("Detected end of sequence.");
  -        		throw new RuntimeException("Stop Thread");//TODO there has to be a better way...
  +				String tn = Thread.currentThread().getName();
  +        		log.info(tn + " Detected end of sequence.");
  +   				throw new JMeterStopThreadException("End of sequence");
           	}
           }
   
  @@ -246,7 +284,8 @@
           }
           
           if (log.isDebugEnabled()){
  -            log.debug(this +"::StringFromFile.execute() name:" //$NON-NLS-1$ 
  +			String tn = Thread.currentThread().getName();
  +            log.debug(tn + " name:" //$NON-NLS-1$ 
                    + myName + " value:" + myValue);//$NON-NLS-1$
           }
           
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org