Increasing memory in idea

Hi,

if you have troubles like me in idea because of heap sizes etc. open ( for mac users of course 😉 )

/Applications/IntelliJ IDEA 9.0.1.app/Contents/Info.plist

and in the lower part of the document you will see

<key>VMOptions</key>

change it as you wish, mine is :

<key>VMOptions</key>
<string>-Xms256m -Xmx1024m -XX:MaxPermSize=768m -ea -Xbootclasspath/a:../lib/boot.jar</string>

and I am happy with it :).

Easy Selenium tests with Page Objects

Hi,

As you all know Selenium is a very nice tool while doing integration tests. You can simulate an user using your webpage for any kind of test cases.

While writing selenium tests in Java, it was very hard to trace the test code as you may come up with something like

	@Test
	public void testCreateUser() throws Exception {
		final String firstName = "firstNameSelenium";
		final String lastName = "lastNameSelenium";
		final String login = "loginSelenium";

		selenium.click( "link=Menu" );
		selenium.click( "usersMenuItem" );
		waitForPageToLoad();
		selenium.click( "registerNewUserLink" );
		waitForPageToLoad();
		Assert.assertTrue( selenium.isElementPresent( "userEditPage" ) );

		// Register a new user
		selenium.type( "form:firstName", firstName );
		selenium.type( "form:lastName", lastName );
		selenium.type( "form:login", login );
		selenium.type( "password", "secretPassSelenium" );
		selenium.type( "form:personalEmail", "perso@perso.com" );
		selenium.select( "form:homeCountry", "value=FR" );
		selenium.select( "form:access2Add", "label=Business Unit Manager" );

		selenium.click( "form:industries:industryToggleLink" );
	
		selenium.click( "form:saveButton" );
		waitForPageToLoad();
		Assert.assertTrue( selenium.isElementPresent( "userEditPage" ) );
	}

and this is not nice, as the code seems unmaintable and you cannot understand what you are testing by doing what. Also if you change the id of an input for example, you have to change that code every place.

With Page Object Pattern, you can represent your each page as an object and with this way you can seperate the html codes from your tests.

Let’s do the following example. A user will try to login to our page and then will list all the latest news from the main page. By the way I will not go over how to setup selenium etc. I will just try to show you can use the Page Objects.

We firstly create our login page as follows:

public class LoginPage{
	protected final DefaultSelenium selenium;

	public LoginPage(DefaultSelenium selenium) {
		this.selenium = selenium;
	}

	public DashBoardPage loginAs(String userName,String password) {
		selenium.open("/YourApplication/login.xhtml");
		selenium.click("j_username");
		selenium.type("j_username", userName);
		selenium.click("j_password");
		selenium.type("j_password", password);
		selenium.click("logbut");
		selenium.waitForPageToLoad("30000");
		return new DashBoardPage(selenium);
	}
}

this login page just logins to the system and returns the DashboardPage. let’s see what dashboard page looks like:


public class DashBoardPage {

	protected final DefaultSelenium selenium;

	public DashBoardPage(DefaultSelenium selenium) {
		this.selenium = selenium;
	}

	public NewsPage goToNewsPage() {
		selenium.click("link=news");
		return new NewsPage(selenium);
	}
}

and news page

public class NewsPage {

	protected final DefaultSelenium selenium;

	public NewsPage(DefaultSelenium selenium) {
		this.selenium = selenium;
	}

	public boolean isNewsDisplayed() {
		return selenium.getBodyText().contains("News for today");
	}
}

ok a basic setup is here, let’s see the a simple test

 public class DisplayNewsTest {
	protected DefaultSelenium selenium;
	
	@Before
	public void before() {
		selenium = new DefaultSelenium("localhost",4444,"*iexplore","http://localhost:8190");
		selenium.start();
	}
	@Test
	public void shouldLoginAndDisplayNews() {
		LoginPage loginPage = new LoginPage(selenium);
		Dashboard dashboard = loginPage.loginAs("admin", "admin");
		NewsPage newsPage = dashboard.goToNewsPage();
		assertThat(newsPage.isNewsDisplayed(), equalTo(true));
	}
}

See how easy and readable the test is?

it is a very basic example but you see it will make your code more readable and easier to maintain. For example we have a current test like:


	@Test
	public void shouldCreateAChannelOwner() {
		LoginPage loginPage = new LoginPage(selenium);
		NewUserPage newUser = loginPage.loginAs("admin", "admin").gotoAdministrator().goToUsers().goToCreateNewUser();

		assertThat(newUser.isUserCreateSuccessful(), equalTo(true));		
	}

You can understand what the test is doing by looking at here.

So use page objects while developing with selenium. A more detailed info can be found here:

http://code.google.com/p/selenium/wiki/PageObjects

Powerful Ajax with Update and Process attributes in PrimeFaces

Hi,

PrimeFaces has a very easy and powerful ajax infrastructure using update and process attributes in ajaxified components. Let’s have this following condition:

There is a required inputText field, and also below that you have a dependent 2 h:selectOneMenu components that needs to be ajaxified. Let’s say those two selectOneMenu’s are for country – city, and last requirement is you have to add the selected city to a datatable. A little complicated screen maybe, but which requirement is not? :).Here is the final output we want :

Let’s do the coding. We need a required inputText for the description and a county city selector as follows :


<h:form>

   Description:<br/>
  <h:inputText label="description" required="true" id="description" requiredMessage="Description is required" />

  <p:panel header="Location Selection" id="locationSelector">
   <h:panelGrid columns="2">
      <h:outputText value="Select Country" />
      <h:selectOneMenu id="country" value="#{locationController.country}">
          <f:selectItem itemLabel="Select a Country" itemValue="" />
          <f:selectItems value="#{locationController.allCountries}" />
          <p:ajax update="city" process="country" event="change" actionListener="#{locationController.chooseCountry}" />
      </h:selectOneMenu>

      <h:outputText value="Select City" />
      <h:selectOneMenu id="city" value="#{locationController.city}" >
         <f:selectItem itemLabel="Select a City" itemValue="" />
         <f:selectItems value="#{locationController.cities}" />
      </h:selectOneMenu>
   </h:panelGrid>
   <br />

   <p:commandLink actionListener="#{locationController.selectLocation}" update="selectedCities" process="locationSelector" >
      <h:outputText value="select city" />
   </p:commandLink>
  </p:panel>
</h:form>

The tricky part here is the <p:ajax update="city" process="country" event="change" actionListener="#{locationController.chooseCountry}" /> part. This one does an ajax request when the h:selectOneMenu is changed ( event=”change” ), the selectOneMenu with the id country is processed ( process=”country” ), as only the country part is important while filling the city selector selectOneMenu ( note that we do not have a required message error due to description field, as we never process it even if they are in the same form), and finally refresh city selectOneMenu ( update=”city” ) to refresh the city selector. This way we have minimum load on the server, and we do not fight with validation errors.

Let’s move on to the p:datatable part, think we have a very basic table below for selected Cities :

<p:panel id="selectedCities" header="Selected Cities">
   <p:dataTable value="#{locationController.selectedCities}" var="city">
      <p:column>
          <f:facet name="header">
              City Name
          </f:facet>
         <h:outputText value="#{city.name} ( #{city.country.name} )"/>
     </p:column>
  </p:dataTable>
</p:panel>

Same way as p:ajax works, the p:commandLink will work as follow, update the table ( update=”selectedCities” ), but process the location selector div ( process=”locationSelector” ) as we need to use city and country in our action listener. Easy huh?

I think, this is one of the most powerful side of the PrimeFaces. You can do ajax request without having any validations errors.

I am leaving you the delete command link in the datatable as an exercise 😉

execute method after all dependencies are injected

Hi,

In Spring, if you want to execute a method after all the dependencies are injected, you can use InitializingBean interface.

A Sample code is as follows :

import org.springframework.beans.factory.InitializingBean;

public class MyViewClass implements InitializingBean {

@Autowired

private AnotherBean anotherBean;

@Autowired

private SomeOtherBean someOtherBean;
.....
@Override
public void afterPropertiesSet() throws Exception {
someOtherBean.someMethod();
// any other thing you want as everything is injected...
}

}

Compare Enums

Hi,

compareTo method is declared as final in Enums so you cannot override it. The default compareTo method is comparing the ordinals ( the order they are defined ) in the Enum. If you want to compare your enums based on name what you can do is, writing your own comparator and use it. For example:

public enum Category {
Technology,
Banking,

....

}

public class CategoryComparator implements Comparator<Category> {

public int compare(Category o1, Category o2) {
Collator collator = Collator.getInstance(new Locale("tr"));
return collator.compare(o1.name(), o2.name());
}
}

and use it in your sort method like :

Collections.sort(categories,new CategoryComparator());

and that is it.

if you want to know why I used Collator to compare check here