Multiple values from same txt file - testing

I've been using this script to load values separated by new line from files to send requests with different values.
def size
File valueFile = new File("C:\\values\\myValueFile.txt")
File valueFile2 = new File("C:\\values\\myValueFile2.txt")
List lines = valueFile.readLines()
List lines2 = valueFile2.readLines()
size = lines.size.toInteger()
def myProps = testRunner.testCase.getTestStepByName("MyProperties")
for( counter in 0..size-1)
{
tempValue = lines[counter]
tempValue2 = lines2[counter]
myProps.setPropertyValue("Value", tempValue)
myProps.setPropertyValue("Value2", tempValue2)
log.info tempValue
log.info tempValue2
testRunner.runTestStepByName("updateBusinessTrip")
}
How to make it load values from same file separated by ";"?
txt file would look like that:
Value1;Value2
Value1.1;Value2.1
Value1.2;Value2.2

If I got you...:
Option 1:
tempValue = lines[counter].split(/;/)
myProps.setPropertyValue("Value", tempValues[0])
myProps.setPropertyValue("Value2", tempValue[1])
or Option 2:
(tempValue, tempValue2) = lines[counter].tokenize(';')
myProps.setPropertyValue("Value", tempValues)
myProps.setPropertyValue("Value2", tempValue2)
or another one:
File valueFile = new File("C:\\values\\myValueFile.txt")
def myProps = testRunner.testCase.getTestStepByName("MyProperties")
valueFile.splitEachLine(/;/) { items ->
myProps.setPropertyValue("Value", items[0])
myProps.setPropertyValue("Value2", items[1])
log.info tempValue
log.info tempValue2
testRunner.runTestStepByName("updateBusinessTrip")
}

Related

Read line and append something at the end

I'm new in kotlin. I'm trying to read file line by line and add something at the end to each of them.
My file before reading:
abcd;abcd;abcd;
bcda;bcda;bcda;
dacb;dacb;dacb;
My file after reading and appending:
abcd;abcd;abcd;smth1
bcda;bcda;bcda;smth2
dacb;dacb;dacb;smth3
I have code for reading file line by line but could you tell me how to add string to each of them?
val pathToFile = "abc.txt"
val scan = Scanner(File(pathToFile))
while (scan.hasNextLine()) {
val line = scan.nextLine()
var lista = ArrayList<String>()
lista = line.split(";") as ArrayList<String>
println(lista.get(0) + " and " + lista.get(1) + " and " + lista.get(2))
}
Januson has the right idea. Here is some Kotlin code to do the job:
inline fun File.mapLines(crossinline transform: (line: String) -> String) {
val tempFile = createTempFile(prefix = "transform", suffix = ".txt")
tempFile.printWriter().use { writer ->
this.forEachLine { line -> writer.println(transform(line)) }
}
check(this.delete() && tempFile.renameTo(this)) { "failed to replace file" }
}
Example usage:
val pathToFile = "abc.txt"
var index = 0
File(pathToFile).mapLines { line -> "${line}smth${++index}" }
If you are using Java 1.7+ then you can use Files.move instead of delete/renameTo:
Files.move(tempFile.toPath(), this.toPath(), StandardCopyOption.REPLACE_EXISTING)
See also Write to file after match in Kotlin.
You can't read and write to the same file unless you are using RandomAccessFile. Instead you should do the following:
Read line from your input file.
Do the modification you want (append to the end of the line, print line).
Write modified line to the output file.
After reading/writing all the data close both files.
Delete input file. Rename output file to the input file name.

How to set log filename in flume

I am using Apache flume for log collection. This is my config file
httpagent.sources = http-source
httpagent.sinks = local-file-sink
httpagent.channels = ch3
#Define source properties
httpagent.sources.http-source.type = org.apache.flume.source.http.HTTPSource
httpagent.sources.http-source.channels = ch3
httpagent.sources.http-source.port = 8082
# Local File Sink
httpagent.sinks.local-file-sink.type = file_roll
httpagent.sinks.local-file-sink.channel = ch3
httpagent.sinks.local-file-sink.sink.directory = /home/avinash/log_dir
httpagent.sinks.local-file-sink.sink.rollInterval = 21600
# Channels
httpagent.channels.ch3.type = memory
httpagent.channels.ch3.capacity = 1000
My application is working fine.My problem is that in the log_dir the files are using some random number (I guess its timestamp) timestamp as by default.
How to give a proper filename suffix for logfiles ?
Having a look on the documentation it seems there is no parameter for configuring the name of the files that are going to be created. I've gone to the sources looking for some hidden parameter, but there is no one :)
Going into the details of the implementation, it seems the name of the file is managed by the PathManager class:
private PathManager pathController;
...
#Override
public Status process() throws EventDeliveryException {
...
if (outputStream == null) {
File currentFile = pathController.getCurrentFile();
logger.debug("Opening output stream for file {}", currentFile);
try {
outputStream = new BufferedOutputStream(new FileOutputStream(currentFile));
...
}
Which, as you already noticed, is based on the current timestamp (showing the constructor and the next file getter):
public PathManager() {
seriesTimestamp = System.currentTimeMillis();
fileIndex = new AtomicInteger();
}
public File nextFile() {
currentFile = new File(baseDirectory, seriesTimestamp + "-" + fileIndex.incrementAndGet());
return currentFile;
}
So, I think the only possibility you have is to extend the File Roll sink and override the process() method in order to use a custom path controller.
For sources you have execute commands to tail and pre-pend or append details, based on shell scripting. Below is a sample:
# Describe/configure the source for tailing file
httpagent.sources.source.type = exec
httpagent.sources.source.shell = /bin/bash -c
httpagent.sources.source.command = tail -F /path/logs/*_details.log
httpagent.sources.source.restart = true
httpagent.sources.source.restartThrottle = 1000
httpagent.sources.source.logStdErr = true

Uploading file to Server using JSP

i was searching for a way to upload a file to server and found the following code ..
File file ;
int maxFileSize = 5000 * 1024;
int maxMemSize = 5000 * 1024;
ServletContext context = pageContext.getServletContext();
String filePath = context.getInitParameter("file-upload");
// Verify the content type
String contentType = request.getContentType();
if ((contentType.indexOf("multipart/form-data") >= 0)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(maxMemSize);
// Location to save data that is larger than maxMemSize.
factory.setRepository(new File("/user2/tst/test"));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( maxFileSize );
try{
// Parse the request to get file items.
List fileItems = upload.parseRequest(request);
// Process the uploaded file items
Iterator i = fileItems.iterator();
out.println("<html>");
out.println("<head>");
out.println("<title>JSP File upload</title>");
out.println("</head>");
out.println("<body>");
while ( i.hasNext () )
{
FileItem fi = (FileItem)i.next();
if ( !fi.isFormField () )
{
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
String fileName = fi.getName();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// Write the file
if( fileName.lastIndexOf("\\") >= 0 ){
file = new File( filePath +
fileName.substring( fileName.lastIndexOf("\\"))) ;
}else{
file = new File( filePath +
fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}
fi.write( file ) ;
out.println("Uploaded Filename: " + filePath +
fileName + "<br>");
}
}
out.println("</body>");
out.println("</html>");
}catch(Exception ex) {
System.out.println(ex);
}
}else{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>No file uploaded</p>");
out.println("</body>");
out.println("</html>");
}
this code is working perfectly fine .. the only problem i am facing is , the file that is updated is getting stored in apache/bin folder ..
for this it was specified to add the following code to web.xml that is available in ROOT/WEB-INF
<context-param>
<description>Location to store uploaded file</description>
<param-name>file-upload</param-name>
<param-value>
/user2/tst
</param-value>
</context-param>
even after this the file is getting stored in bin folder ..
i needed some help in this regard .. thanks in advance ..

how to take log file backup automatically

How to take automatically backup of a log file(.txt) when it's size reached a threshold level, say 5MB. The backup file name should be like (log_file_name)_(system_date) and original log file should be cleaned(0 KB).
Please help. Thanks in advance.
Check your log file size using lenght().Then check if its bigger then 5mb call extendLogFile() func.
This is c# code u can easly convert to java
Size check:
if (size > 400 * 100 * 100)
{
extendLogFile(Path);
}
Copy old log file in archive directory and create new log file:
private static void extendLogFile(string lPath)
{
string name = lPath.Substring(0, lPath.LastIndexOf("."));
string UniquName = GenerateUniqueNameUsingDate(); // create a unique name for old log files like '12-04-2013-12-43-00'
string ArchivePath = System.IO.Path.GetDirectoryName(lPath) + "\\Archive";
if (!string.IsNullOrEmpty(ArchivePath) && !System.IO.Directory.Exists(ArchivePath))
{
System.IO.Directory.CreateDirectory(ArchivePath);
}
string newName = ArcivePath + "\\" + UniquName;
if (!File.Exists(newName))
{
File.Copy(lPath, newName + ".txt");
using (FileStream stream = new FileStream(lPath, FileMode.Create))
using (TextWriter writer = new StreamWriter(stream))
{
writer.WriteLine("");
}
}
}

Webmatrix file upload error

I am trying to create an upload page where I upload a .swf file and then I add the filename to my database. Previously I have been able to achieve this, however, now it seems to give me the error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" My code is as follows:
#{
Page.Title = "Add Game";
//Variables
var GameName = "";
var Tags = "";
var Gamefile = "";
//Required fields
Validation.RequireField("Name", "Please give the game a name.");
Validation.RequireField("file", "Please upload a file.");
//Maximum name length
Validation.Add("Name",
Validator.StringLength(
maxLength: 100,
errorMessage: "Name must be less than 100 characters")
);
//SWF file validation
Validation.Add("file",
Validator.Regex(#"^.*\.(swf|SWF)$", "Invalid filetype, you must upload a .swf flash file")
);
if (IsPost && Validation.IsValid()) {
var db = Database.Open("Surgestuff");
var gCat = "";
var fileData = Request.Files[0];
var fileName = Guid.NewGuid().ToString() + ".swf";
var fileSavePath = Server.MapPath("~/upload/" + fileName);
var AddBy = WebSecurity.CurrentUserName;
gCat=Request["formCat"];
Gamefile = fileName;
fileData.SaveAs(fileSavePath);
var SQLINSERT = "INSERT INTO Games (Name, file_path, Category, AddBy) " + "VALUES (#0, #1, #2, #3)";
db.Execute(SQLINSERT, GameName, Gamefile, gCat, AddBy);
Response.Redirect("~/Games");
}
}
And for some reason, even when i have a file submitted, the
var fileData = Request.Files[0]; gives me that error
The Web Pages 2 Validation helpers do not work with an input type="file". They only work with elements that are included in the Request.Form collection. A file upload appears in the Request.Files collection.
There are a couple of approaches you can take to validate the file upload. You can use ModelState:
if(IsPost && Request.Files[0].ContentLength == 0){
ModelState.AddError("file", "You must choose a file");
}
if (IsPost && Validation.IsValid() && ModelState.IsValid) {
// etc
Or you can add a hidden field, and when the form is submitted, populate its value with that of the file upload via JavaScript. Then you can use the new Validation helpers as you are currently trying to do, but on the hidden field instead.