JUnit test case to check if file was created - unit-testing

I have created a simple program that wil create the dat file through buffered writer and will write some data into that file , now please advise what junit test cases I can have through which I can check whether the file is created or not in c: drive , I am using junit 3 , please advise.

File file = new File("c:/pathOfTheCreatedFile");
assertTrue(file.exists());
// TODO: read the file. Check that it contains what it's supposed to contain

Related

How Do I Read a File in Crystal Lang?

I am familiar with Ruby and am trying to write a program in Crystal.
I have a file called special_file.txt that I want to read in my Crystal program, how do I do that?
Crystal is inspired by Ruby syntax and so you can often read and perform File operations in a similar manner. For example, Crystal has a File classclass which is an instance of the IO class containing a read method.
To read a file's contents on your filesystem you can instantiate a File object and invoke the gets_to_end method coming from the IO super class:
file = File.new("path/to/file")
content = file.gets_to_end
file.close
The gets_to_end method reads an entire IO objects data to a String variable.
You can also use a block invocation to achieve a similar result:
# Implicit close with `open`
content = File.open("path/to/file") do |file|
file.gets_to_end
end
Finally, the most idiomatic way to read the contents of an entire file would be the one line:
# Shortcut:
content = File.read("path/to/file")

How to i inject the original file name (input file) into the defined stream when using siddhi-io-csv extension in siddhi when using WSO2SP

In WSO2SP there is a way to use a CSV file to generate events. e.g. the ReceiveEventsFromFile sample. In this sample i can drop a file into a folder and the extension will read the file and produce events.
When i define the input stream i would like to know if there is a way i can get the original file (input file) name as a parameter e.g INPUT_FILE_NAME
Is there functionality to achieve something similar to the below sample.
dir.uri='file:/wso2sp-4.3.0/samples/artifacts/ReceiveEventsFromFile/files/new',
action.after.process='move',
tailing='false',
move.after.process='file:/wso2sp-4.3.0/samples/artifacts/ReceiveEventsFromFile/files/consumed',
#map(type='csv', #attributes(ALERT_ID = "1", file = 'file.name')))
#sink(type='log')
define stream BatchFileStream (ALERT_ID string, file string);```
thanks in advance
With the current version of the siddhi CSV mapper the File metedata does not get passed from siddhi-io-file.
What i had to do is write a custom siddhi-io-file implementation that would pass the metedata to my custom mapper.

How to perform unit test on the append function of Azure Data Lake written in .Net Framework?

I have created Azure webjobs that contains methods for file creation and Appending data to that file on Datalake Store. I am done with all its development part publishing webjobs etc. Now i am going to write unit tests to test whether the data i am sending is successfully appended to file or not All I need to know is how to perform such kind of unit test any idea?
what I currently thought of doing it is by cleaning all the data from my datalake file and then sending a test data to it. so on the basis of one of the column data of the whole data i sent, i will check whether it got appended or not. Is there any way that can give a quick status of whether my test data is written or not?
Note: Actually i want to know how to delete a particular row of a csv file on data lake but i dont want to use usql to search for the required row. (I am not directly sending data to Datalake it is written via Azure service bus queue which then triggers webjobs to append data to a file on datalake.)
Aside from looking at the file, I can see few other choices. If only your unit test is writing to the file, then you can send appends of variable lengths and then see whether the size of the file is updated appropriately as a result of successful appends. You can always read the file and see whether you data made it as well.
I solved my problem in the way that i Got the length of my file on Datalake store using:
var fileoffset = _adlsFileSystemClient.FileSystem.GetFileStatus(_dlAccountName, "/MyFile.csv").FileStatus.Length;
after getting length i sent my test data to the datalake and after that i again got the length of a file using same code. so the first length i.e before sending test data it was my offset and the length got after sending test data was my destination length i.e from offset to the destination length i read my datalake file using:
Stream Stream1 = _adlsFileSystemClient.FileSystem.Open(_dlAccountName, "/MyFile.csv", totalfileLength, fileoffset);
After getting my data in a stream I tried searching for the test data i sent using following code:
Note:I had a a column of guids in file on the basis of which i search my sent guid in a filestream. make sure to convert your search data to byte and then pass it to the function ReadOneSrch(..).
static bool ReadOneSrch(Stream fileStream, byte[] mydata)
{
int b;
long i = 0;
while ((b = fileStream.ReadByte()) != -1)
{
if (b == mydata[i++])
{
if (i == mydata.Length)
return true;
}
else
i = b == mydata[0] ? 1 : 0;
}
return false;
}

C++ ifstream tries to open file while being written

I am polling a directory constantly for files and every time I see a file that meets some certain criteria for reading, I open the file and parse it.
string newLine;
ifstream fileReader;
fileReader.open(filename.c_str());
while(!fileReader.eof())
{
getline(fileReader, newLine);
// do some stuff with the line...
}
filereader.close();
The above code is in a loop that runs every 1 second checking a directory for new files. My issue is that as I am transferring files into the folder for processing, my loop finds the file and passes the name of the file to ifstream who then opens it and tries to parse an incomplete file. How do I make ifstream wait until the file is done being written before it tries to parse the file?
EDIT:
Wanted to better word the issue here since a replier seems to have misunderstood my issue. I have 2 directories
mydirectory/
mydirectoryParsed/
THe way my code works is that my program checks for files in mydirectory/ and when it finds them, parses them and uses the information in the files. No writing to the files are done. Once I am done parsing the file, the file is moved to mydirectoryparsed/
The issue is that when I transfer files over the network into mydirectory/ the ifstream sees these files midtransfer and starts reading them before they finish writing to the directory. How do I make ifstream wait until the file is completely written before parsing it?
Don't transfer the files directly into the directory that your program is watching; instead, transfer them into a different directory on the same drive, and then when the transfer is done, move them into the watched directory. That way, the complete file appears in the watched directory in a single atomic operation.
Alternatively, you could use a naming convention in the watched directory — append a suffix like ".partial" to files that are being transferred, and then rename the file to remove the suffix when the transfer is done. Have your program ignore files whose names end with the suffix.
You're not supposed to open the file every time you write in it. Open it once!
Some pseudo-code for this would be :
1- Open file
2- Get the data you want to write, treat that data
3- Call the write to file function
4- Loop until you have nothing left to write
5- Close de file

How to write some command on a batch file through C++ program?

I need some help regarding my C++ program. A batch file named abc.bat is located somewhere in my hardisk. I know that in C++ I can use this line of code to execute that abc.bat file:
system ("file path here\\abc.bat");
I want to send some commands to that batch file so that after executing that abc.bat file my C++ program should write commands to its console and execute them. How can I do that?
You can do this by opening a pipe. In brief:
FILE *bat = popen("path\\abc.bat", "w");
fprintf(bat, "first command\n");
fprintf(bat, "second command\n");
pclose(bat);
The text you write to bat will end up on the standard input of the batch file.