Generation of Test Cases - java

I need to generate the test cases using white box testing..
I would like to share my idea of what we are doing and would also like you to give suggestions on it, if any, if we are going wrong.
As Netbeans and eclipse are having their own editors for writing java programs n then compile and generate test cases for the same..
But our aim is to create the application that accepts i.e reads java program written anywherei.e like notepad,command prompt i.e stored anywhere but should be .java file to be read by our application and our application should on reading the .java file should be directly able to generate the test cases for it.
What we have done is created some grammars for control statements i.e., for,if,switch,etc, have parsed the java file into tokens,and we are now in a process to create the symbol table to retrieve the symbols from user's java file consisting of symbol name,its length,value,data type.But we are stuck here..
Its getting difficult to retrieve symbols,its value and data type from java file..
Everything is coded in java,and no tool is used.Please provide some solution.

If I understood you correctly, this boils down to whether it's possible to generate test cases based on source code, and if so, how to do that. The short answer is that it's not. You can generate something, but it won't be testing anything useful. You should be designing test cases based on your requirements/specification, not your code.
Think of it this way: if your tests are based on the code, they have no frame of reference for what is correct or incorrect. As a result, the most they will be able to do is faithfully confirm that your code does whatever it is that your code does; they won't be able to compare to what the code is supposed to do.

Related

Running java codes one by one on server-side JSP

Let's explain my graduation project then my question.
My graduation project is about compiling java code online.
To compile user's code, I am using java compiler api on background bean class. After compilation is finished .class file is generated on the D:\eclipse-jee-mars-R-win32-x86_64\eclipse directory. Then the progam saves input file that is writed in text area(in JSP) to D:\eclipse-jee-mars-R-win32-x86_64\eclipse directory. After creating input file, the program runs the code by using dos command at runtime and process getInput stream. The getInput stream returns output of the code. If entered code(user's code) creates any output file, output file will be generated also.
We suggest for users that "your code's output file name should be 'output.txt'".
If the user's code creates any output file. "output.txt" file is generated on D:\eclipse-jee-mars-R-win32-x86_64\eclipse directory.Then, the program retrieves 'output.txt' contents to JSP and show the "output" of their codes to users.
Thus far, there is no problem. Problem is starting here. For one user there is no problem but think for 2 users or more. If 2 or more users compile codes at the same time problems will occur because their .class .java and "output.txt" files will be the same file because for all users we creates same name file on D:\eclipse-jee-mars-R-win32-x86_64\eclipse directory. Maybe for all users, creating files as unique name will help me but it creates another problem. Our project also analyse codes by looking runtime of the codes. This solution[unique name] brings parallel working codes and analysis results will be unfair. Because sometimes 1 code is worked, sometimes 100 code is worked on the website.
I need to compile, execute, analyse codes one by one, not parallel.
How can I solve this problem? What a solution can be used for this situation?
"Accepting random code from the internet to compile/run it server-side" - this sounds like a particularly bad idea, opening you up for all kinds of nasty attacks. DOS (Denial of Service) being only one of them.
Further, the architecture to externally execute code and share a common well-known file - as you state - makes it impossible to execute more than one of these programs at the same time
Storing this external code in some IDE's directory is another weird thing to me.
In fact, I think that this whole architecture is such a bad idea that I have a hard time suggesting a solution other than to rethink it: If you have a very good reason for accepting random code from the internet (other than researching exactly this question), please state it here.
With protest (and the suggestion to not follow this tip) I'd point to the option to just pipe the application's console output (e.g. System.out) back to the client instead of that one single wellknown file. At least now you can safely be attacked by many users coming in parallel, without mixing up the replies.
I solved my problem with using synchronized block. It is enough for my project.
In project JSP file.
synchronized(this){
//my compilation, running and analysis part is here
}
Now, one thread can process this part. It means that, for thousands users all codes are processed one by one. No parallel working codes.

How can i figure out instruction pointer address in the certain part of my java code?

I'm just trying to get a memory trace generated by Java code(JVM) using pin tool(pinatrace). But every result of the microbenchmarks I try to figure out the traces shows almost same graphs, and even utilizing processbuilder inside Java code gives no remarkable results at least in comparing among different microbenchmarks. I realized that I should extract the substantive part(excluding useless part for analysis, like initializing, accessing to JVM in the middle of main code....) of the code to figure out what is going wrong.
Because the instruction pointer is sent to pinatrace at the moment it detects memory access, I thought getting the instruction pointer of substantive part and set the parameter with those information would work. But Finding the way(or the tool) for this job is driving me crazy, since Java is not a familiar field to me....
I've already tried IDA pro, but it only says The input file format is wrong, when I input .class file. and setting .java file as input shows me just binary code which seems like the text file is converted to binary things and nothing. Tried to install IDAjava addon but also does not work....... Is there any easy method to get the IP instead? what I just want to know is IP from java code..
Thank you!

Using eclipse, can i make just a "scratch pad" page for java?

Sometimes i just want to write out some quick java to see if my algorithm works.
Maybe I am preparing for an interview! Maybe I just want to see how my code is running.
At these times, I typically make a new java file, and save it in a project i've marked called "scratchyCat". Don't judge me.
However, 99% of the code there is useless, I don't need to save it or ever even see it again. Does eclipse have some shortcut to create a java page that i can just write code on, compile and run, and then delete?
Alternatively, is there some website (like jsfiddle) that would let me do the same for java?

C++ or Java Library for automated code editing?

I am writing/planning to write a program that takes in a java file (or multiple java files), and edits and adds functions/classes/variables and then outputs a new java file (or multiple files).
Is there a C++ or Java library that
Can recognize and output names of classes/functions within a text file
Can recognize and output the names of the input arguments for said classes/functions
Can allow me to insert code at specific lines or within specific functions
Can search for a given variable name/value
Maintains original file formatting
I would prefer not having to manually code something to do the above, so any help would be appreciated.
Thank you in advance!
EDIT: I currently use Eclipse, and am unsure of how to proceed. So to further explain my question:
In eclipse, if I write a program that opens another .java file, How would I go about 'asking' eclipse to output, say, all the class names of the .java file I just opened?
Also I will explain the 'purpose' of this project to further clarify. I want to write a program that can take in any java file, and turn it into a class that can implemented remotely via RMI. To do this I will need to add an execute() function, have the file implement Task and Serializable and add a few variables, etc... Based on my knowledge, doing this in Eclipse would require manual editing of the program, but I would like to completely automate this process.
Thank you, again.
Much of what you need can be found in a modern IDE; and some very good IDEs are open source (eclipse and Intellij IDEA Community Edition for Java). You might look there to see if there are modules that suite your needs.
Looks like you are talking of a tool like eclipse. You might not be looking for a full fledged IDE, but the requirements that you have mentioned are fulfilled by any basic IDE.
If you wish to make one of your own, you can do that using eclipse rich client platform.
All that you would need from Java is the reflection API.

Method of extracting data from a website?

I want to grab data from a website (for example, the names, identification number, and list of resources someone is using) and post it to another website.
What I was thinking of doing was using cURL to grab the information from an existing REST api on one website. Then, what I wanted to do is write a program or an api to post that information onto another website.
Upon using a cURL, how/where can I store that information so that I can use it via another program? Would it be easier to write one single program that extracts the information from the first website and posts it to the other? If so would it be possible to do so using Java/give an idea on how to do so? I'm not asking for code, just a method to do this. I'm using the Eclipse for Java Web EE developer's IDE.
I'd write it as 2-3 programs. One that extracts the data, one that formats the data (if necessary), one that posts the data.
My gut tells me the easiest way to do this is a pure bash script. But if you want to use Java for this you can.
I would save the output in a file for the post-er to read from. This has the benefit of letting you write/test the poster without the 2 other programs working. That said, I recommend you write the get-er program first. That way you know what data you're really dealing with.
Now, if you happen to write both the formatter and the post-er in java, I would write this as one program instead of "piping" files between them. The formatter will read in the file, turn it into a data structure/class, and the post-er will read this data structure/class.
This is only superficially different from my previous paragraph. The point is each "part" is independent from each other. This allows you to test a part without running the whole thing. That's the important thing.
As for how/where to store the information from the get-er, just redirect it to a file. Here's a tutorial on how.
Truth be told, I can't tell if you're using the linux cURL program or a java implementation like this one. My answer would be very different depending on this.

Categories

Resources