How to cover catch blocks? - java

I would like to have a full code coverage of my method writeList(), but I don't know how to cover catch blocks.
My method is :
public class ListOfNumbers {
//...another code...
public void writeList() {
try (FileOutputStream inputStream = new FileOutputStream(this.pathOut);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(inputStream);
PrintWriter out = new PrintWriter(outputStreamWriter)) {
for (int i = 0; i < this.numbers.size(); i++) {
out.println("Value at: " + i + " = " + this.numbers.get(i));
}
} catch (final ArrayIndexOutOfBoundsException e) {
ListOfNumbers.LOGGER.error("Caught ArrayIndexOutOfBoundsException: " + e.getMessage(),
e);
} catch (final FileNotFoundException e) {
ListOfNumbers.LOGGER.error("Caught FileNotFoundException: " + e.getMessage(), e);
} catch (final IOException e) {
ListOfNumbers.LOGGER.error("Caught IOException: " + e.getMessage(), e);
}
}
}

To do it, If I were you, I would rewrite my code to make it more testable by moving the code that write the numbers into a different method, in order to mock this new method to make it throw whatever you want using Mockito.
So your code could be something like that:
public void writeList() {
try (FileOutputStream inputStream = new FileOutputStream(this.pathOut);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(inputStream);
PrintWriter out = new PrintWriter(outputStreamWriter)) {
// Delegate the way to write the numbers to this new method
writeNumbers(out);
} catch (final ArrayIndexOutOfBoundsException e) {
ListOfNumbers.LOGGER.error(
"Caught ArrayIndexOutOfBoundsException: " + e.getMessage(), e
);
} catch (final FileNotFoundException e) {
ListOfNumbers.LOGGER.error("Caught FileNotFoundException: " + e.getMessage(), e);
} catch (final IOException e) {
ListOfNumbers.LOGGER.error("Caught IOException: " + e.getMessage(), e);
}
}
/**
* The new method that is protected here but could also be package protected
* but cannot be private to be able to override it.
*/
protected void writeNumbers(PrintWriter out) {
for (int i = 0; i < this.numbers.size(); i++) {
out.println("Value at: " + i + " = " + this.numbers.get(i));
}
}
Then your unit test could be:
#Test
public void causeAIOException() {
ListOfNumbers lon = // Create your instance here
// Create a Spy to be able to mock the method writeNumbers
ListOfNumbers listOfNumbers = Mockito.spy(lon);
// This will make any call to writeNumbers throw a IOException
Mockito.doThrow(IOException.class).when(listOfNumbers)
.writeNumbers(Matchers.any(PrintWriter.class));
// Call the method on the spy
listOfNumbers.writeList();
}
NB: In case of FileNotFoundException, you could simply provide an existing folder as pathOut as it is one of the cases for which new FileOutputStream will throw a FileNotFoundException and it is easy to reproduce.

class ListOfNumbers {
public void writeList() throws ArrayIndexOutOfBoundsException,FileNotFoundException,IOException {
try (FileOutputStream inputStream = new FileOutputStream(this.pathOut);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(inputStream);
PrintWriter out = new PrintWriter(outputStreamWriter)) {
for (int i = 0; i < this.numbers.size(); i++) {
out.println("Value at: " + i + " = " + this.numbers.get(i));
}
}
}
public class Main{
public static void main(String [] args){
try{
ListOfNumbers list=new ListOfNumbers();
try{
list.writeList();
}
} catch (ArrayIndexOutOfBoundsException e) {
ListOfNumbers.LOGGER.error("Caught ArrayIndexOutOfBoundsException: " + e.getMessage(),
e);
} catch (FileNotFoundException e) {
ListOfNumbers.LOGGER.error("Caught FileNotFoundException: " + e.getMessage(), e);
} catch (IOException e) {
ListOfNumbers.LOGGER.error("Caught IOException: " + e.getMessage(), e);
}
}
}
}

Related

Writing into a text file without overwriting existing content

I have this current implementation but I couldn't append the new data that were being passed on in processName, activity and numberOfRecords. It just keeps creating a separate .txt file for each transaction.
#Override
public void saveSummaryLogs(String processName, String activity,
int numberOfRecords) throws Exception {
log.info("Test if this method is being executed.");
log.info(INSERT_LOGS_TO_FILE);
log.info("Creating a folder directory is being executed!");
try {
File f = new File(summaryLogsDbDir);
f.mkdirs();
} catch (Exception e) {
throw new Exception("Permission denied to create parent folder.",
e);
}
writeToFile(processName, activity, numberOfRecords);
log.info("writeToFile has been called and executed!");
}
public void writeToFile(String processName, String activity,
int numberOfRecords) throws Exception {
log.info("writeToFile is being executed!");
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyy_HHmmss");
try (BufferedWriter out = new BufferedWriter(new FileWriter(
summaryLogsDbDir + LOGS_FILENAME + dateFormat.format(date)
+ ".txt"))) {
out.append(dateFormat.format(date)+ ": " + processName + "\n");
out.append(dateFormat.format(date)+ ": " + activity + "\n");
out.append(dateFormat.format(date)+ ": " + numberOfRecords + "\n");
} catch (Exception e) {
throw new Exception("Failed a summary-logs.txt file.", e);
}
log.info("writeToFile has been executed!");
}
Any help of advise is greatly appreciated. Thanks!

This method throws two different exceptions. How could man catch the two exceptions and prints exception catched for both exception types to console?

public class CatchingExceptions {
private int erroneousMethod(int p) {
if (p == 0) {
throw new IllegalArgumentException();
}
int x = 0x01;
return p / (x >> Math.abs(p)); // this line will throw!
}
The task is to implement the following method to catch and print the two exceptions.
public void catchExceptions(int passthrough) {
erroneousMethod(passthrough); // will throw!
try{
????
} catch (IllegalArgumentException e){
System.out.println("???? ");
}
}
Call the method inside the try block:
public void catchExceptions(int passthrough) {
try{
erroneousMethod(passthrough);
} catch (RuntimeException e) { // catches all unchecked exceptions
String message = e.getMessage() == null ? "" : (": " + e.getMessage());
System.out.println(e.getClass().getSimpleName() + ": " + message);
}
}

Java Try Catch block

I initially started programming in college and learnt vb.net. Now I have decided to make the move to Java and have some queries. In vb, the try catch statement is laid out as follows
try
Catch ex as exception
finally
End catch
but from the java website (https://docs.oracle.com/javase/tutorial/essential/exceptions/putItTogether.html)
i found that in java you use two catches like so:
try {
} catch (ExceptionType name) {
} catch (ExceptionType name) {
}
i was hoping someone could explain why you need two catches in java and what do the respective catches do/catch.
Thanks.
In Java, you can use multiple catch blocks.
It doesn't necessarily means you have to.
It depends on the code your have in the try block, and how many checked Exceptions it may potentially throw (or even unchecked Exceptions if you really want to catch that, typically you don't and you don't have to).
One bad practice is to use a single handler for general Exception (or worse, Throwable, which would also catch RuntimeExceptions and Errors):
try {
// stuff that throws multiple exceptions
}
// bad
catch (Exception e) {
// TODO
}
The good practice is to catch all potentially thrown checked Exceptions.
If some of them are related in terms of inheritance, always catch the child classes first (i.e. the more specific Exceptions), lest your code won't compile:
try {
// stuff that throws FileNotFoundException AND IOException
}
// good: FileNotFoundException is a child class of IOException - both checked
catch (FileNotFoundException fnfe) {
// TODO
}
catch (IOException ioe) {
// TODO
}
Also take a look at Java 7's multi-catch blocks, where unrelated Exceptions can be caught all at once with a | separator between each Exception type:
try (optionally with resources) {
// stuff that throws FileNotFoundException and MyOwnCheckedException
}
// below exceptions are unrelated
catch (FileNotFoundException | MyOwnCheckedException e) {
// TODO
}
Note
In this example you linked to, the first code snippet below Putting it all together may arguably be considered as sub-optimal: it does catch the potentially thrown Exceptions, but one of them is an IndexOutOfBoundsException, which is a RuntimeException (unchecked) and should not be handled in theory.
Instead, the SIZE variable (or likely constant) should be replaced by a reference to the size of the List being iterated, i.e. list.size(), in order to prevent IndexOutOfBoundsException from being thrown.
I guess in this case it's just to provide an example though.
The code that is on the page that is in link i have modified it with single exception. Problem here is that in this case you will not able to know that exception is where whether due to
IndexOutOfBoundsException or IOException
just you know that a exception occurs
import java.io.*;
import java.util.List;
import java.util.ArrayList;
public class ListOfNumbers {
public static void main(String... s) {
ListOfNumbers lon = new ListOfNumbers();
lon.writeList();
}
private List<Integer> list;
private static final int SIZE = 10;
public ListOfNumbers() {
list = new ArrayList<Integer>(SIZE);
for (int i = 0; i < SIZE; i++) {
list.add(new Integer(i));
}
}
public void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering" + " try statement");
out = new PrintWriter(new FileWriter("e://OutFile.txt"));
for (int i = 0; i < SIZE; i++) {
out.println("Value at: " + i + " = " + list.get(i));
}
} catch (Exception e) {
System.err.println("Caught Exception: " + e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
} else {
System.out.println("PrintWriter not open");
}
}
}
}
Let us understand the concept it is better to know why the code fails due to which particular type of exception whether
IndexOutOfBoundsException or IOException
Now The Code with handling of different Exception
import java.io.*;
import java.util.List;
import java.util.ArrayList;
public class ListOfNumbers {
public static void main(String... s) {
ListOfNumbers lon = new ListOfNumbers();
lon.writeList();
}
private List<Integer> list;
private static final int SIZE = 10;
public ListOfNumbers() {
list = new ArrayList<Integer>(SIZE);
for (int i = 0; i < SIZE; i++) {
list.add(new Integer(i));
}
}
public void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering" + " try statement");
out = new PrintWriter(new FileWriter("e://OutFile.txt"));
for (int i = 0; i < SIZE; i++) {
out.println("Value at: " + i + " = " + list.get(i));
}
} catch (IndexOutOfBoundsException e) {
System.err.println("Caught IndexOutOfBoundsException: " +
e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
} else {
System.out.println("PrintWriter not open");
}
}
}
}
Here we could come to know that whether it fails due to creation of file at location
e://OutFile.txt
drive Not on my system
error printed as
Caught Exception: e:\OutFile.txt (The system cannot find the path
specified) Entering try statement PrintWriter not open
Next Case
Now when i comment the line
list.add(new Integer(i));
import java.io.*;
import java.util.List;
import java.util.ArrayList;
public class ListOfNumbers {
public static void main(String... s) {
ListOfNumbers lon = new ListOfNumbers();
lon.writeList();
}
private List<Integer> list;
private static final int SIZE = 10;
public ListOfNumbers() {
list = new ArrayList<Integer>(SIZE);
for (int i = 0; i < SIZE; i++) {
// list.add(new Integer(i));
}
}
public void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering" + " try statement");
out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++) {
out.println("Value at: " + i + " = " + list.get(i));
}
} catch (IndexOutOfBoundsException e) {
System.err.println("Caught IndexOutOfBoundsException: " +
e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
} else {
System.out.println("PrintWriter not open");
}
}
}
}
then it clearly says that it fails for index out of bound exception
Entering try statement Caught IndexOutOfBoundsException: Index: 0,
Size: 0 Closing PrintWriter
So for the purpose of debugging the application properly and efficiently it is good.
I have created condition for the other type of exception
NoClassDefFoundError
java.lang.NoClassDefFoundError: ListOfNumbers
Caused by: java.lang.ClassNotFoundException: stackprac.ListOfNumbers
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Exception in thread "main" Process exited with exit code 1.

How does TCP connection works on Android?

I am using smack for building a chat app in Android. I am using a sticky_service to hold the connection. I have a confusion that if my app goes to sleep what happens to TCP connection. I have read few answers on the page - How to make the Android device hold a TCP connection to Internet without wake lock?
It wakes up for a brief period of time - For smack I can think of it as the processmessage listener (http://www.programcreek.com/java-api-examples/index.php?api=org.jivesoftware.smack.MessageListener) is called. I am inserting data in db for that. Is there any guarantee that work will be complete or if the execution is left in between will it be started from there.
Hello dear you can use this code snippet :
protected void connect() {
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": connecting");
features.encryptionEnabled = false;
lastConnect = SystemClock.elapsedRealtime();
lastPingSent = SystemClock.elapsedRealtime();
this.attempt++;
try {
shouldAuthenticate = shouldBind = !account.isOptionSet(Account.OPTION_REGISTER);
tagReader = new XmlReader(wakeLock);
tagWriter = new TagWriter();
packetCallbacks.clear();
this.changeStatus(Account.State.CONNECTING);
final Bundle result = DNSHelper.getSRVRecord(account.getServer());
final ArrayList<Parcelable> values = result.getParcelableArrayList("values");
if ("timeout".equals(result.getString("error"))) {
throw new IOException("timeout in dns");
} else if (values != null) {
int i = 0;
boolean socketError = true;
while (socketError && values.size() > i) {
final Bundle namePort = (Bundle) values.get(i);
try {
String srvRecordServer;
try {
srvRecordServer = IDN.toASCII(namePort.getString("name"));
} catch (final IllegalArgumentException e) {
// TODO: Handle me?`
srvRecordServer = "";
}
final int srvRecordPort = namePort.getInt("port");
final String srvIpServer = namePort.getString("ip");
final InetSocketAddress addr;
if (srvIpServer != null) {
addr = new InetSocketAddress(srvIpServer, srvRecordPort);
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
+ ": using values from dns " + srvRecordServer
+ "[" + srvIpServer + "]:" + srvRecordPort);
} else {
addr = new InetSocketAddress(srvRecordServer, srvRecordPort);
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
+ ": using values from dns "
+ srvRecordServer + ":" + srvRecordPort);
}
socket = new Socket();
socket.connect(addr, 20000);
socketError = false;
} catch (final UnknownHostException e) {
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
i++;
} catch (final IOException e) {
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
i++;
}
}
if (socketError) {
throw new UnknownHostException();
}
} else if (result.containsKey("error")
&& "nosrv".equals(result.getString("error", null))) {
//todo:change here to server.
socket = new Socket(server_ip, server port);
} else {
throw new IOException("timeout in dns");
}
final OutputStream out = socket.getOutputStream();
tagWriter.setOutputStream(out);
final InputStream in = socket.getInputStream();
tagReader.setInputStream(in);
tagWriter.beginDocument();
sendStartStream();
Tag nextTag;
while ((nextTag = tagReader.readTag()) != null) {
if (nextTag.isStart("stream")) {
processStream(nextTag);
break;
} else {
throw new IOException("unknown tag on connect");
}
}
if (socket.isConnected()) {
socket.close();
}
} catch (final UnknownHostException | ConnectException e) {
this.changeStatus(Account.State.SERVER_NOT_FOUND);
} catch (final IOException | XmlPullParserException | NoSuchAlgorithmException e) {
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
this.changeStatus(Account.State.OFFLINE);
} finally {
if (wakeLock.isHeld()) {
try {
wakeLock.release();
} catch (final RuntimeException ignored) {
}
}
}
}
enjoy your code:)

Java read file with scanner

I have this code that have some methods for creating a file, adding data to the file and then read the file with scanner.
My problem is that I want it to run my three methods at once but it stops
at the method number two and does not read the file with readFile() method
createFile();
addResponses(file);
readFile(file);
I can not run these three together. It does not read the file. But if I take
the other methods away like this
//createFile();
//addResponses(file);
readFile(file);
Then the read file method works.
I hope you did understand my problem. Is there something wrong with my code?
import java.io.*;
import java.util.Formatter;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Main {
static Formatter f;
static String sträng = " ";
static BufferedWriter output;
static File file;
static int nummer = 1;
static int counter = 0;
static private StringBuffer strBuff;
static InputStream is;
static FileWriter fw;
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws Exception {
createFile();
addResponses(file);
readFile(file);
}
public static int addResponse() {
if (nummer == 6) {
try {
output.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
System.exit(0);
}
sträng = JOptionPane.showInputDialog("Numbers 1-5 to number " + nummer");
try {
return Integer.parseInt(sträng);
} catch (NumberFormatException f) {
return 6;
}
}
public static File createFile() {
try {
file = new File("numbers.txt");
f = new Formatter(file);
f.close();
} catch (SecurityException se) {
System.err.println("You dont have write access to this file");
System.exit(1);
} catch (Exception ex) {
System.err.println("Error opening or creating file");
System.exit(1);
}
return file;
}
public static void readFile(File x) {
try {
x = new File("numbers.txt");
Scanner in = new Scanner(x);
while (in.hasNextLine()) {
System.out.println(in.nextLine());
}
in.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public static void addResponses(File f) throws IOException {
try {
fw = new FileWriter(f, true);
output = new BufferedWriter(fw);
int x = addResponse();
if (nummer == 1) {
output.write(String.format("%s%10s\n", "Rating", " Frequency"));
}
while (x != -1) {
if (x > 0 && x < 6) {
output.write(String.format("%s%10s\n", nummer, sträng));
nummer++;
} else {
JOptionPane.showMessageDialog(null, "Input only numbers between 1-5");
}
x = addResponse();
}
output.close();
} catch (IOException io) {
JOptionPane.showMessageDialog(null, "Wrong");
}
}
}
after playing around with the code, I found out that in your addResponse() method , you have added System.exit(0); so baiscally program was terminating. I've change it to return -1 and it seems to be working.
by the way, this is a very bad coding practice, each method should do stuff seperately regarless of other method. in your case everything is so integerated that is very hard to root the problem. I recommend you looking at some coding convention.
this is how addResponse() method should be working:
public static File createFile() {
try {
file = new File("numbers.txt");
f = new Formatter(file);
f.close();
} catch (SecurityException se) {
System.err.println("You dont have write access to this file");
System.exit(1);
} catch (Exception ex) {
System.err.println("Error opening or creating file");
System.exit(1);
}
return file;
}

Categories

Resources