Posts Tagged ‘java’

Files and directories in Java: the NIO.2 Approach

In the previous post I told you about the old fashioned way to handle files in java.
In this article we will focus on the new API.
The File class is no longer used. We will use the “Path” class instead.
Files is a utility class to create either files or directories, that takes a path in the constructor:

	Path filePath=Paths.get("/home/laura/demo.txt");
	Files.createFile(filePath);
	
	Path dirPath=Paths.get("/home/laura/newdir");
	Files.createDirectory(dirPath);
	
	Path nestedDirPath=Paths.get("/home/laura/newdir3/newdir4");
	Files.createDirectories(nestedDirPath);

The “createDirectories” method created the whole file system structure (the nested directories).

With NIO.2 copying or deleting a file or directory have become a matter of one line of code, thanks to the very straightforward methods “copy”, “delete”, etc.

Many things can be achieved with the utility classes Files and Paths.

Java IO: Handling characters files efficiently

Let´s take a look a the classic IO file handling in java.
First let´s start with the “File” class.
The File class is not used to read or write data. It´s used to create or delete files and directories, for searching and working with paths.
So it´s not used for the files or directories content.

The File class constructor does not create anything on the hard drive. As you can see in the following snippet, you need to use the createNewFile method for it:

		File file = new File("/home/laura/demo.txt");
		System.out.println("Does the file exists?"+file.exists());
		boolean created = file.createNewFile();
		System.out.println("File created?"+created);
		System.out.println("Does the file exists?"+file.exists());

The createNewFile method returns “false” if the file already exists.

If the goal is handling text files, you don´t need to use any Stream class. All you need is a writer one. The basic option is to use the naked “FileReader” and “FileWriter” classes:

		FileWriter fileWriter= new FileWriter(file);
		fileWriter.write("hello world. this is a text file.");
		fileWriter.flush();
		fileWriter.close();
		
		FileReader fileReader= new FileReader(file);
		char[] in = new char[100];		
		int size = fileReader.read(in);
	
		System.out.println("File size:"+size);
		
		for(char c: in){
			System.out.print(c);
		}
	
		fileReader.close();

The FileReader method “read” delivers the amount of character read.

Notice that after writing the text into the file, you need to call the method “flush”. It´s necessary to call it to make sure that the whole data flows into the file before closing the writer.

So far so good. But this simple way shown above is not the most elegant solution out there, because we have been using an array (that has a fixed size)!

A much better approach is wrapping it up using the classes BufferedReader and BufferedWriter:

		FileWriter fileWriter= new FileWriter(file);
		BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
		
		bufferedWriter.write("hello world. this is a text file.");
		bufferedWriter.newLine();
		bufferedWriter.write("You are using the buffered reader now!");			
		bufferedWriter.close();
		
		FileReader fileReader= new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		
		String data;			
		while((data=bufferedReader.readLine())!=null){
			System.out.println(data);		
		}
		
		bufferedReader.close();

Notice that buffered reader also has a method called “readLine”, that you wouldn´t get with a simple File reader.
Once you close a writer it cannot be reopened again. You get an exception if you use it after closing it!

Debugging and Testing in Java : enabling assertions

For testing and debugging purposes you can enable the assertions evaluation with the VM parameter “-ea” ( or “-enableassertions” if you prefer the whole thing).

The assertions remain in the code and are just ignored at runtime if you don´t enable them. You can think of them as an aid in case of need.

The following method throws an exception if the parameter given is not a String containing “Laura”:


         private void testFirstName(String firstName){
                 assert(firstName.equals("Laura"));

               System.out.println("The given first name is Laura");
         }

A string message can be displayed in the stack trace, by adding a second expression to the assertion, like :

	private void testFirstName(String firstName){
		assert(firstName.equals("Laura")) : "the name "+ firstName+ " is not Laura!";
		
		System.out.println("The given first name is Laura");
	}

The output in this second case would be something like:

Exception in thread "main" java.lang.AssertionError: the name Anna is not Laura!
at com.demo.AssertionsDemo.testFirstName(AssertionsDemo.java:15)
at com.demo.AssertionsDemo.main(AssertionsDemo.java:8)

Since Java 1.4 “assert” has become a keyword.

Assertions can be disabled with the VM option “-da” or “-disableassertions”. Although the assertions are disabled by default, the manual disabling might make sense if you don´t want to enable the assertions for all the classes.

For example if you want to run a java jar file (for example “demo.jar”), but disabling the assertions for one of its classes (ex. “com.demo.Demo.java”) you need to use the following parameters:

> java -jar -ea -da:com.demo.Demo

To disable them for the whole “com.demo” package (including subpackages):

> java -jar -ea -da:com.demo...

So entering VM parameters like “-ea:” or “-ea:” allow you to select which assertions to evaluate.

Java: the Comparable and Comparator interfaces

The Comparable interface allows you to sort by a specific object variable that you choose for your needs.
Take a look at the following object:

public class Book implements Comparable<Book> {
	
	private String title;
	private String author;
	private double price;
	
	public Book(String title, String author, double price) {
		super();
		this.title = title;
		this.author = author;
		this.price = price;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}


	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}


	@Override
	public int compareTo(Book that) {
		return Double.valueOf(this.price).compareTo(Double.valueOf(that.price));
	}

	public String toString() {
		return " \n " + title + " \t " + author + " \t " + price;
		}
		
}

Notice the way the compareTo method is overriden. It´s a double primitive variable, but you need the wrapper class to compare it. The following wouldn´t work:

	@Override
	public int compareTo(Book that) {
		return this.price.compareTo(that.price);
		return 0;
	}

Implementing the Comparable interface affects the sorting criteria. For example:

import java.util.Arrays;

public class ComparableTest{
	
	public static void main(String[] args) {
		
		Book[] books= {
		new Book("The capital","Marx,Karl",10.5),
		new Book("Interpretation of dreams","Freud, S.",12.5),
		new Book("Killed by Death","Kilmister, Lemmy",8)
		};
		
		System.out.println("Before sorting: \n"+Arrays.toString(books));

	Arrays.sort(books);
	System.out.println("\n After sorting: \n"+Arrays.toString(books));
	
	}
	
}

The output is:

Before sorting:
[
The capital Marx,Karl 10.5,
Interpretation of dreams Freud, S. 12.5,
Killed by Death Kilmister, Lemmy 8.0]


After sorting:
[
Killed by Death Kilmister, Lemmy 8.0,
The capital Marx,Karl 10.5,
Interpretation of dreams Freud, S. 12.5]

 

If you need to do a more complex comparison, you can make your own comparator by extending the Comparator interface (not “Comparable”).
For example you can create your own Comparator:

public class BookComparator implements Comparator<Book> {

	@Override
	public int compare(Book b1, Book b2) {
		
		int comparePrice = Double.valueOf(b1.getPrice()).compareTo(Double.valueOf(b2.getPrice()));
		if (comparePrice!=0) 
			{return comparePrice;
			}
		else return b1.getAuthor().compareTo(b2.getAuthor());
	}

}

It tries to order by price first, but ifthe books have the same price, they will be sorted by the author name too.

If you runt the following main method:

	public static void main(String[] args) {

		ArrayList<Book> booksList = new ArrayList<Book>();
		booksList.add(new Book("The capital", "Marx,Karl", 10.5));
		booksList.add(new Book("Peace and War", "Tostoj. L.", 12.5));
		booksList.add(new Book("Interpretation of dreams", "Freud, S.", 12.5));
		booksList.add(new Book("Killed by Death", "Kilmister, Lemmy", 8));
		
		System.out.println("before sorting:");
		for (Book book : booksList) {
			 System.out.println( book.toString());

		}

		Collections.sort(booksList, new BookComparator());
		System.out.println(""
				+ "\n after sorting:");
		
		for (Book book : booksList) {
			 System.out.println(book.toString());

		}

Then you see the following output:

before sorting:
The capital Marx,Karl 10.5
Peace and War Tostoj. L. 12.5
Interpretation of dreams Freud, S. 12.5
Killed by Death Kilmister, Lemmy 8.0

after sorting:
Killed by Death Kilmister, Lemmy 8.0
The capital Marx,Karl 10.5
Interpretation of dreams Freud, S. 12.5
Peace and War Tostoj. L. 12.5

Notice the order of the last two books.

 

Getting started with Java Threads – Part 2 : join() and sleep()

The thread class provides some methods to define the threads behavior.
Two important methods to consider are:
join(), to make the other instanced threads to wait for it to day. You can provide a timeout as parameter.
sleep();

The sleep() method throws a checked exception called “InterruptedException”,each time the thread receives an interrupt request. You have to handle in the code by wrapping the method in a try-catch block.

The following code will make you understand the join method.
We define a MyThread thread:

public class MyThread implements Runnable {

	public void run() {
		Thread thread= Thread.currentThread();
		System.out.println("Implementing the runnable interface"
				+ " - Thread name: " + thread.getName()
				+ " - priority: " + thread.getPriority()
				+ " - group: " + thread.getThreadGroup().getName());

	}

}

Then we can observe the behaviour running the following main:

		public static void main(String[] args) {

		// extending Thread
		Thread myThread = new Thread(new MyThread());
		Thread myJoiningThread = new Thread(new MyThread());

		myThread.setName("myThread");
		myJoiningThread.setName("myJoiningThread");

		System.out.println("Threads will be started...");

		myJoiningThread.start();

		try {
			// waits for this thread to die
			myJoiningThread.join();
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		myThread.start();

	}

Without the try-catch block in which the join method is invoked the threads would be started asynchronously (the order is not predictable) and in the console you might get either:
Threads will be started...
Implementing the runnable interface - Thread name: myThread - priority: 5 - group: main
Implementing the runnable interface - Thread name: myJoiningThread - priority: 5 - group: main

or:

Threads will be started...
Implementing the runnable interface - Thread name: myJoiningThread - priority: 5 - group: main
Implementing the runnable interface - Thread name: myThread - priority: 5 - group: main

But if you include the try-catch block, the myJoiningThread will always be invoked first.

To understand the sleep method you can create a MySleepingThread:

public class MySleepingThread implements Runnable {

	public void run() {
		Thread thread= Thread.currentThread();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Implementing the runnable interface"
				+ " - Thread name: " + thread.getName()
				+ " - priority: " + thread.getPriority()
				+ " - group: " + thread.getThreadGroup().getName());

	}

}

Then you can run the following main method:

public static void main(String[] args) {

		// extending Thread
		Thread myThread = new Thread(new MyThread());
		Thread mySleepingThread = new Thread(new MySleepingThread());
		myThread.setName("myThread");
		mySleepingThread.setName("mySleepingThread");

		System.out.println("Threads will be started...");

		mySleepingThread.start();
		myThread.start();

	}

You can see in the console that the sleeping thread will be started after 3 seconds.

The sleep method is static. By invoking “Thread.sleep(3000)” you are delaying the creation of the current thread.

If you use the method in the main method, the sleeping thread will be “main”.

Getting started with Java Threads – Part 1

I am learning for the Java OCP 7 and summarizing some relevant infos about Multithreading.
For now I am referring to chapter 13 in the book by G.Ganesh.

You can create threads in 2 ways: extending the Thread class or implementing the Runnable interface
Extending the Thread class is more convenient if you don´t need to extend another class that is not “Thread” (since in java you can only extend one class), because you can use the class methods directly (like “getName()“).
If you implement the Runnable interface (preferred way because of inheritance) you will have to use the static method “currentThread()” of the Thread class to do your operations on the thread (like setting or getting the name).

The two fundamental methods to know are:
run(): like a main method, necessary to enable the execution of a thread.
start(): that creates the thread.

The thread will be terminated when the run method execution is complete. It´s invoked implicitly by the JVM (DON´T invoke it explicitly!).
The main method starts a Thread called “main“. If you create and start your own thread it will have default names like “Thread-0“, “Thread-1“.

If you extend the thread class you need to override the run() method (otherwise it does nothing). If you implement the Runnable interface you will have to implement the run() method (mandatory)

The following code will help you to understand more about these first concepts.

public class MyThread1 extends Thread {

	public void run() {
		System.out.println("Extending the Thread class - Thread name: "
				+ getName() + " - priority: " + getPriority() + " - group: "
				+ getThreadGroup().getName());

	}

}
public class MyThread2 implements Runnable {

	public void run() {
		Thread thread= Thread.currentThread();
		System.out.println("Implementing the runnable interface"
				+ " - Thread name: " + thread.getName()
				+ " - priority: " + thread.getPriority()
				+ " - group: " + thread.getThreadGroup().getName());

	}

}
public class ThreadsDemo {

	public static void main(String[] args) {

		// extending Thread
		Thread myThread1 = new MyThread1();
		// implementing Runnable
		Thread myThread2 = new Thread(new MyThread2());

		System.out.println("Threads defined. Default properties: ");
		//name, priority, group returned by the toString method 
		System.out.println(myThread1);
		System.out.println(myThread2);
		System.out.println("Changing default names...");
		myThread1.setName("Thread_1");
		myThread2.setName("Thread_2");
		System.out.println("Threads will be started...");
		myThread1.start();
		myThread2.start();

	}

}

The output of the main method should be something like:

Threads defined. Default properties:
Thread[Thread-0,5,main]
Thread[Thread-1,5,main]
Threads will be started...
Extending the Thread class - Thread name: Thread-0 - priority: 5 - group: main
Implementing the runnable interface - Thread name: Thread-1 - priority: 5 - group: main

Launching the main program you see that the threads are started asynchronously. Sometimes you see:
Implementing the runnable interface - Thread name: Thread-1
Extending the Thread class - Thread name: Thread-0

Sometimes it´s:
Extending the Thread class - Thread name: Thread-0
Implementing the runnable interface - Thread name: Thread-1

You can quickly proof it in Eclipse launching the program with the shortcut Ctlr+11.

To change this “random” behaviour you can set the priority with the setPriority() method, from 1, the lowest, to 10, the highest. The default is 5. They can be all retrieved by the static members:
– Thread.NORM_PRIORITY ;
– Thread.MAX_PRIORITY ;
– Thread.MIN_PRIORITY .

Threads can be in one of the following states:
– NEW (created)
– RUNNABLE (started)
– TERMINATED
– BLOCKED (waiting to acquire the lock)
– WAITING (waiting for notifications)
– TIMED_WAITING (sleep() invoked and the thread is sleeping or if wait() with timeout)

States are defined with the Thread.State enumeration.
You can get the state with the getState() method.

MySQL CHECK constraint alternative: triggers!

Yesterday I discovered the powerful hibernate check constraint (@org.hibernate.annotations.Check) , which can be directly added in your entity class like this:

@Entity
@XmlRootElement
@Check(constraints = "age IS NOT NULL")
public class Person{

String firstname;
String lastName;
Integer age;

// valid code
}

Unfortunately, as you can read in the official MySQL docs, “the CHECK clause is parsed but ignored by all storage engines”.
Yesterday I found out that MySql 5.X doesn´t support the SQL CHECK constraint.
It means that if you are using JPA und HIBERNATE you can´t take advantage of the Check annotation!

In the project I am working on we could successfully export the schema with the maven command “hibernate4:export”. The check constraint was added in the create table statement. So if your DBMS supports it you get the job done in a very elegant way.

The way out I could find by googling a bit is not so elegant, but it allowed me to achieve the same result. I have just written a trigger, like the (simple) following:

CREATE TRIGGER check_age
BEFORE INSERT ON person
FOR EACH ROW
BEGIN
IF age is NULL
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Age cannot be null';
END IF;
END;

MySQL has been bought by Oracle. Is this silent ignoring is a strategy to make people migrate to Oracle DBMS? I am afraid it´s so. Corporation games, my friends!

Selenium Web driver tests fail if run all together

If you write unit tests that must be run like if you had to open/refresh a new browser session each time, you can use a method with before annotations:

@Before
public void refreshPage() {
    driver.navigate().refresh();
}

If all tests are individually successful (green) but fail all together, the reason might also been that you need to wait for some resources to be available on the page, so you also need to handle it, setting the timeout like this:

 public WebElement getSaveButton() {
     return findDynamicElementByXPath(By.xpath("//*[@id=\"form:btnSave\"]"), 320);
 }

320 is a long time, but you must make sure that you give enough time to get all that it takes to test.

 

Ant task to execute a main class with command line args parameters

Yeah, once again another HelloWorld stuff on the web. I was just curious to try to execute a Main class with an Ant script and found out that I couldn´t find the straight working snippets online easily, because I couldn´t get to know how to set the classpath. So I have wasted some minutes to make it work.

Let´s consider a little more than the classic HelloWorld example and pass a command line parameter as well:

package de.demo;

public class HelloWorld {

public static void main(String[] args) {
System.out.println("Hello Main! \n"  + "Parameter: "+ args[0]);
}

}

Then run following build.xml script:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<project basedir="." default="run" name="Ant Hello World">
	
	<property name="src" value="." />

	<path id="classpath">
		<fileset dir="${src}">
		</fileset>
	</path>

	<target name="compile">
		<javac srcdir="." />
	</target>

	<target name="run" depends="compile">
		<!-- Print directly in the console -->
		<echo message="Hello World!" />
		<!-- Run main class with parameters-->
		<java classname="de.demo.HelloWorld">
			<arg value="10"/>
			<classpath refid="classpath">
			</classpath>
		</java>
	</target>

</project>

In the console you will see something like:

Buildfile: C:\Users\liparulol\workspace\AntDemos\buildHelloWorld.xml
compile:
[javac] C:\Users\liparulol\workspace\AntDemos\buildHelloWorld.xml:14: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 1 source file
run:
[echo] Hello World!
1 Hello Main!
1 Parameter10
BUILD SUCCESSFUL
Total time: 745 milliseconds

That´s it!

Arquillian testing with Maven in Eclipse: Deployment scenario issue (Glassfish embedded example)

To run a test with Arquillian using Maven you might consider embedded, local or remote containers. You can set them all in your pom.xml file and specify deployment profiles like the following:

 <profile>
			<id>arquillian-glassfish-embedded</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<dependencies>
				<dependency>
					<groupId>org.jboss.arquillian.container</groupId>
					<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
					<version>1.0.0.CR4</version>
					<scope>test</scope>
				</dependency>
				<dependency>
					<groupId>org.glassfish.main.extras</groupId>
					<artifactId>glassfish-embedded-all</artifactId>
					<version>4.0</version>
					<scope>provided</scope>
				</dependency>

				<dependency>
					<groupId>org.glassfish.jersey.core</groupId>
					<artifactId>jersey-client</artifactId>
					<version>2.4.1</version>
					<scope>provided</scope>
				</dependency>

				<dependency>
					<groupId>org.glassfish.jersey.containers</groupId>
					<artifactId>jersey-container-servlet</artifactId>
					<version>2.4.1</version>
					<scope>provided</scope>
				</dependency>

			</dependencies>
		</profile>

The container must be configured in the arquillian.xml file. A simple one might look like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://jboss.org/schema/arquillian"
   xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
   <!--  this is only needed if you want to override AS7  -->
   <!--  <defaultProtocol type="Servlet 3.0"/> -->
   <container qualifier="arquillian-glassfish-embedded">
   	<configuration>
   		<property name="bindHttpPort">9090</property>
   	</configuration>
   </container>
   <!-- 
   <container qualifier="tomcat">
   	<configuration>
   		<property name="user">tomcat</property>
   		<property name="pass">tomcat</property>
   	</configuration>
   </container>
    -->
</arquillian>

Setting the container in the pom.xml and the arquillian.xml files is not enough to run the test in Eclipse.
You would get the following error:

org.jboss.arquillian.container.test.impl.client.deployment.ValidationException: DeploymentScenario contains a target (_DEFAULT_) not matching any defined Container in the registry.
Please include at least 1 Deployable Container on your Classpath.
          at org.jboss.arquillian.container.test.impl.client.deployment.DeploymentGenerator.throwNoContainerFound(DeploymentGenerator.java:250)
          at org.jboss.arquillian.container.test.impl.client.deployment.DeploymentGenerator.throwTargetNotFoundValidationException(DeploymentGenerator.java:243)
          at org.jboss.arquillian.container.test.impl.client.deployment.DeploymentGenerator.validate(DeploymentGenerator.java:102)
          at org.jboss.arquillian.container.test.impl.client.deployment.DeploymentGenerator.generateDeployment(DeploymentGenerator.java:84)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          at java.lang.reflect.Method.invoke(Method.java:597)
          at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
          at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
          at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
          at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:135)
          at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:115)
          at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:67)
…

To make the test run you need to set the active maven profiles in Eclipse. Simply right click on the project and select Properties>Maven and add “arquillian-glassfish-embedded” in the Active maven profiles textbox.

Categories
Links: