Thursday, 24 February 2011

Maven Quick Reference Card

I use normally the Content Assist in M2Eclipse Plugin to enter the maven commands, but if you use your own xml editor, a new Maven Quick Reference Card directly from Apache makes the life much easier. it contains the most basic items.

hope you enjoy using it.

Wednesday, 16 February 2011

Convert JUnit3 Tests modules to JUnit4

I needed to convert many test files to JUnit4, after a while, I considered that is a really painful task to do it manually. So I wrote a little program to automate the process.
I tested this application on a huge number of files wrote by various Persons and having various formats, therefore I hope that I works on every possible JUnit module.
Please send me a short comment if you found a case, that I didn't included in it.

you can call the application by entering this command on Command Line:

java -jar JUnit4Converter.jar -f <your JUnit3 Java file>

this will printout the converted file to Console, if you wanted to do an Inplace Conversion, add "-i" to commands.

java -jar JUnit4Converter.jar -i -f <your JUnit3 Java file>

You can access the application on Github

Monday, 14 February 2011

Read multiple files from Resource JAR or Disk

following to my further Post about loading one file from a JAR file or Disk, here is a method which loads directly multiple files. it will decide base of given URL, if it must load those files from Disk or a Resource JAR file.
In my sample, I wanted to be more detailed and load just XML files.

     * Loads multiple xml files from Disk or Resource JAR file.
     @param clazz current class
     @param path package path of resources
     @return the resource listing
     @throws URISyntaxException the URI syntax exception
     @throws IOException Signals that an I/O exception has occurred.
    List<InputStream> loadMultipleResources(final Class<?> clazz, final String paththrows URISyntaxException, IOException {
        final List<InputStream> listOfFiles = new ArrayList<InputStream>();

        // it is a normal directory, so just find and list files with XML extension
        URL directoryUrl = clazz.getClassLoader().getResource(path);
        if (directoryUrl != null && directoryUrl.getProtocol().equals("file")) {
            final File[] files = new File(directoryUrl.toURI()).listFiles(
                    new FilenameFilter() {
                        public boolean accept(final File dir, final String name) {
                            return name.endsWith(".xml");

            for (final File f : files)
                listOfFiles.add(new FileInputStream(f));

            return listOfFiles;

        if (directoryUrl == null) {
            final String packageName = clazz.getPackage().getName();
            directoryUrl = clazz.getResource(packageName);

        // the given path is a JAR file, so extract all XML Files from classpath and return their content as stream
        if (directoryUrl.getProtocol().equals("jar")) {
            final String jarPath = directoryUrl.getPath().substring(5, directoryUrl.getPath().indexOf("!"));
            final JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
            final Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                final String name = entries.nextElement().getName();
                if (name.startsWith(path&& name.endsWith(".xml")) { // filter according to the path
                    String entry = name.substring(path.length());
                    final int checkSubdir = entry.indexOf("/");
                    if (checkSubdir >= 0) {
                        entry = entry.substring(0, checkSubdir);
                    final InputStream inputStream = clazz.getClassLoader().getResourceAsStream(name);

            return listOfFiles;

        throw new UnsupportedOperationException("Cannot list files for URL " + directoryUrl);