28.8k views
5 votes
Write JUnit tests that read from the Binary files below.

The Binary files contain a PriorityQueue with Strings in it. Hint use ObjectStreams

use JUnit tests to verify that the data in the files is in alphabetical order.

sortPriorityQueue1.dat:

¬í sr java.util.PriorityQueue""Ú0´û?‚± I sizeL
comparatort Ljava/util/Comparator;xp pw
t Adamst Burenq ~ t Harrisont Monroet Jeffersont Jacksont
Washingtont Madisonx

sortPriorityQueue2.dat:

¬í sr java.util.PriorityQueue""Ú0´û?‚± I sizeL
comparatort Ljava/util/Comparator;xp pw
t Connecticutt Delawaret Marylandt New Hampshiret Georgiat
New Jerseyt Massachusettst South Carolinat Pennsylvaniax

1 Answer

4 votes

Final answer:

JUnit tests can be written to read from the given Binary files that contain a PriorityQueue with Strings. Here the code:

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;

import java.io.ObjectInputStream;

import java.util.PriorityQueue;

import java.util.Queue;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class PriorityQueueFileTest {

private Queue<String> readPriorityQueueFromFile(String filePath) {

try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {

return (PriorityQueue<String>) ois.readObject();

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

private boolean isSorted(Queue<String> queue) {

String prev = "";

for (String str : queue) {

if (str.compareTo(prev) < 0) {

return false;

}

prev = str;

}

return true;

}

(at) Test

public void testSortPriorityQueue1() {

Queue<String> queue = readPriorityQueueFromFile("sortPriorityQueue1.dat");

assertEquals(true, isSorted(queue));

}

(at) Test

public void testSortPriorityQueue2() {

Queue<String> queue = readPriorityQueueFromFile("sortPriorityQueue2.dat");

assertEquals(true, isSorted(queue));

}

}

Step-by-step explanation:

To create JUnit tests for reading the Binary files containing PriorityQueues with Strings and verify that the data is in alphabetical order, you'll need to use ObjectInputStreams to read the PriorityQueue objects and then perform the necessary assertions.

This example above assumes that the serialized PriorityQueue objects in the given binary files are instances of `java.util.PriorityQueue<String>`. The `readPriorityQueueFromFile` method reads the serialized PriorityQueue from the file, and the `isSorted` method checks if the strings in the queue are in alphabetical order. The JUnit test methods then assert that the queues read from the files are sorted correctly.

Remember to adjust the code according to the actual class and file structure in your specific scenario.

User Shyamupa
by
7.9k points