What do you think about testing this program, keep in mind what unit testing is. From Wikipedia, "unit testing is a method by which individual units of source code are tested ... to determine if they are suitable for use."
Remember also that you are jumping a little to the deep end because testing a multi-threaded program is difficult. As much as possible, you should eliminate the complexity of the interaction of threads with testing functionality.
Looking at your program, you have a pretty good encapsulation and separation of problems, so that you are well on your way. My strategy here would be to *Scan both *Scan objects regardless of streaming. To do this, correct what their roles are. I would say the following:
FolderScan scans the directory structure, finds files of a certain type and puts the files that pass the filter to the queue. When it runs out of directory structures, it puts a specific file in the queue, counts the latch, and exits.FileScan consumes a file queue, performs an operation on them, and outputs the output to the console. When it hits a specific file, it counts the latch and shuts down.
Since you already have more or less working code, since you modify tests for it (as opposed to writing them when writing code, which is preferable), you want to change the source as little as possible in order to get tested. After that, you may want to reorganize the code, and your tests will give you confidence in this.
FolderScan Test
As a first step, create a new JUnit test for FolderScan . You can write several tests, but from a high level, each of them should fill the directory structure with some files. I would check each of these cases at least:
- One folder with the file that passes the filter.
- One folder with a file that does NOT skip the filter.
- A subfolder with a file in each.
The more whey the test, the better. Each test simply creates a new instance of FolderScan , gives it the arguments that you specified, pointing to this folder. Call run () and make sure:
- The queue contains the
File objects that you expect. CountDownLatch decreased.- The last item in the queue is the βtriggerβ
File .
FileScan test
At a high level, the test for this should now be clear: create text File objects, fill the queue with them and a marker, and pass them to new FileScan objects. Again, the more granular, the better, at least:
- File with corresponding line
- File without corresponding line
- Multiple files
There is a problem with this class, although this is a classic Singleton testing problem. The result of this object is actually transmitted through the System.out channel, which you will need to connect to check the result. As a first pass, I would recommend reorganizing the constructor for transfer to PrintStream . In the production process, you will go to System.out , in the test you will pass what you can check: a new PrintStream(new ByteArrayOutputStream()) , which you can check the contents or, even better, the layout.
In the end, each test should check:
- The queue is down.
CountDownLatch reduced- The specified
PrintStream has the expected content in it.
You should have a lot of confidence that both FileScan and FolderScan work as advertised.
* Testing askUserPathAndWord *
I see no easy way to test this function / class as written. This violates the Single Responsibility Principle and just does too many things. I would highlight the following responsibilities for new methods or classes:
- Give the user a word and path
- Based on the word, create a
FileScan with the correct parameters (a Factory) - Set the path, create a
FolderScan with the correct parameters (a Factory) - For two starts and latches, create an
ExecutorService , leave them in the queue and wait on the latches
Then you can check them out yourself.
* Next steps *
One of the nice things about these tests is that, as soon as you have them, you can freely reorganize your documents and be sure that you have not violated. For example, you can look at Callable instead of Runnable , which allows you to access Future links, remove the output parameter from FolderScan and completely remove CountDownLatch .
Happy testing!