Tuesday, February 24, 2015

How to Read File in One Line in JDK 7 or Java 8

Reading a file in Java is not simple, it requires lots of boiler plate code, as we have seen in our earlier example of reading text files. Various things had to wrapped e.g. a FileInputStream inside a BufferedReader, loops with weird terminating conditions had to be specified and so forth. From JDK 7 onward,  you can do a lot better. It provides lots of useful classes e.g. Files and Paths to deal with file and their actual path. In this article, we will see how we can read a file in just one line. Of course, your production code won't be like that, especially if you are reading a few gigabytes into memory and want to pay attention to the character set, if you don't specify, by platform's default character encoding will be used.  In short, you will need a little more code, but for quick and dirty file reading this should do the trick. By the way, It wouldn't be a one-liner if it had exception handling. In a production application you would need to deal with the fact that the file isn't there e.g. displaying an error for the user, posting metrics, logging the error etc, but it certainly be lot less boiler code than it used to be.

Monday, February 23, 2015

Top 20 jQuery Interview Questions and Answers

Without a doubt, jQuery has given a much needed boost to JavaScript, a language so useful but equally underrated at times. Before jQuery comes into picture, we used to write lengthy JavaScript code not just for bigger but even for smaller functionality. Those code were at times both difficult to read and maintain. Having written JavaScript before using this excellent library, I realized true power of jQuery, just after using it for a month. Given it's huge popularity, jQuery interview questions are increasingly asked in any web developer interview, not just beginners but also experienced developers, including HTML and JavaScript. Since jQuery is relatively new, most interview questions are asked from core jQuery library including selectors, DOM manipulation and jQuery basics. In this article, I am sharing a list of 16 jQuery questions asked to HTML and JavaScript programmers in different interviews. Some of this questions also asked in Java Web development interview, where it's required to work on both Server side (Spring, Servlet and JSP) and Client side (HTML, CSS, JavaScript and jQuery). If you are going for an interview, where role demands multiple skills e.g. Java, jQuery, it's not expected from you to know every minor detail or comprehensive knowledge of jQuery, but if you are going for a purely client side development role, you might get more tricky and advanced jQuery questions than mentioned in this article. Nevertheless, you can use it to quickly revise some of the most frequently asked jQuery questions on interviews, but they are mostly suited for web developers with 2 to 5 years of experience, especially in Java stack.

Wednesday, February 18, 2015

How to disable JUnit Test - @Ignore annotation Example

If you are using JUnit 4, then you can use @Ignore annotation to disable a test case; what this means is that, any method which is annotated by @Ignore, will not be executed by JUnit. Why would you want to disable a test? Well, It may be because that particular test is for a functionality which is not yet implemented fully and because of that your build is failing. Like that there could be some other scenarios where you would like to temporarily disable a test or a group of tests. From JUnit 4 onward test methods are annotated with @Test, so if they are also annotated with @Ignore will not be executed as tests. Apart from disabling a single method you can also disable all methods of a class by using @Ignore annotation at class level. For example, If you annotate a class containing test methods with @Ignore and none of the containing tests will be executed. Native JUnit 4 test runners should report the number of ignored tests along with the number of tests that ran and the number of tests that failed. Also worth noting is that @Ignore annotation takes an optional default parameter if you want to record why a test is being ignored, very useful if you are not the only person who is working on that piece of code. If there is a reason to disable a test case, later that can be enabled if reason is already sorted.