Note that there are some explanatory texts on larger screens.

plurals
  1. POHow better to test multithreading program with common queue?
    text
    copied!<p>Which way should be better to use when one needs to test this program.<br> Firstly <code>askUserPathAndWord()</code> asks the user to input <code>path</code> and <code>whatFind</code>. We have two threads: </p> <ul> <li>First thread scans folder and if it finds readable files <code>put()</code> it in the queue. </li> <li>Second thread <code>take()</code>s from the queue and looks for <code>whatFind</code> into this file. If the search is successful it outputs to console the path of this file and the frequency of the word.</li> </ul> <p>This is an integration dependence with multithreading work. Which variant is better able to test this program - Junit of EasyMock? I read some tutorials about EasyMock, but I don't know in which cases it is better to use it.</p> <p><strong>Code:</strong></p> <pre><code>class FolderScan implements Runnable { private String path; private BlockingQueue&lt;File&gt; queue; private CountDownLatch latch; private File endOfWorkFile; FolderScan(String path, BlockingQueue&lt;File&gt; queue, CountDownLatch latch, File endOfWorkFile) { this.path = path; this.queue = queue; this.latch = latch; this.endOfWorkFile = endOfWorkFile; } public FolderScan() { } @Override public void run() { findFiles(path); queue.add(endOfWorkFile); latch.countDown(); } private void findFiles(String path) { try { File root = new File(path); File[] list = root.listFiles(); for (File currentFile : list) { String s = currentFile.getName().toLowerCase(); if (currentFile.isDirectory()) { findFiles(currentFile.getAbsolutePath()); } else { if (s.matches("^.*?\\.(txt|pdf|doc|docx|html|htm|xml|djvu|rar|rtf)$")) { queue.put(currentFile); } } } } catch (InterruptedException e) { e.printStackTrace(); } } } public class FileScan implements Runnable { private String whatFind; private BlockingQueue&lt;File&gt; queue; private CountDownLatch latch; private File endOfWorkFile; public FileScan(String whatFind, BlockingQueue&lt;File&gt; queue, CountDownLatch latch, File endOfWorkFile) { this.whatFind = whatFind; this.queue = queue; this.latch = latch; this.endOfWorkFile = endOfWorkFile; } public FileScan() { } @Override public void run() { while (true) { try { File file; file = queue.take(); if (file == endOfWorkFile) { break; } scan(file); } catch (InterruptedException e) { e.printStackTrace(); } } latch.countDown(); } private void scan(File file) { Scanner scanner = null; int matches = 0; try { scanner = new Scanner(file); } catch (FileNotFoundException e) { System.out.println("File Not Found."); e.printStackTrace(); } while (scanner.hasNext()) if (scanner.next().equals(whatFind)) { matches++; } if (matches &gt; 0) { String myStr = String.format( "File: %s - and the number of matches " + "is: %d", file.getAbsolutePath(), matches); System.out.println(myStr); } } public void askUserPathAndWord() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(System.in)); String path; String whatFind; BlockingQueue&lt;File&gt; queue = new LinkedBlockingQueue&lt;File&gt;(); try { System.out.println("Please, enter a Path and Word" + "(which you want to find):"); System.out.println("Please enter a Path:"); path = bufferedReader.readLine(); System.out.println("Please enter a Word:"); whatFind = bufferedReader.readLine(); if (path != null &amp;&amp; whatFind != null) { File endOfWorkFile = new File("GameOver.tmp"); CountDownLatch latch = new CountDownLatch(2); FolderScan folderScan = new FolderScan(path, queue, latch, endOfWorkFile); FileScan fileScan = new FileScan(whatFind, queue, latch, endOfWorkFile); Executor executor = Executors.newCachedThreadPool(); executor.execute(folderScan); executor.execute(fileScan); latch.await(); System.out.println("Thank you!"); } else { System.out.println("You did not enter anything"); } } catch (IOException | RuntimeException e) { System.out.println("Wrong input!"); e.printStackTrace(); } catch (InterruptedException e) { System.out.println("Interrupted."); e.printStackTrace(); } } public static void main(String[] args) { new FileScan().askUserPathAndWord(); } } </code></pre> <p><strong>Questions:</strong> </p> <ul> <li>Which kind of test will have best coverage in this case? </li> <li>How better to test contractual obligations in both variants?</li> <li>If the answer is <code>EasyMock</code>, how should we do this correctly?</li> <li>If <code>Junit</code>, how do we test <code>void</code> methods?</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload