Tag Archives: java

Modelmapper

In clean software development, with clean nice layers, there is one small problem relating entities. Every layer has own, most of the time similar entities. So there is a DB entity, an BusinessObject (BO) and there could be a TransferObject (TO) and so on. And mapping is a pain.

Modelmapper is the solution.

This framework simply mappes to objects and first hands on, this framework is great. Ok my entities where simple… DB Entity -> EntityBO both mainly similar, but with Lists of Objects and actually it worked out of the box. I didnt need any special MapperStrategy.

Love it.

svn and eclipse… and maven

ok one of the problems of eclipse with svn is that no eclipse specific file should be commited to the repository… mainly because they are developers computer specific and kinda crap on others computers… additionally not every one uses eclipse… so that the hell should he be doing with the eclipse files? so goal is to delete and ignore them… simple… but now every body needs to set his project localy correctly…

with maven it is actually a bit more confusing… which folders to add? how to add the Maven Dependencies?

Right Click the Project -> Maven -> Update Project Configuration

Done 🙂

@Autowired

after thinking a lot about DIP, it was kinda clear, that we need to refactor some stuff… ok not really the thinking part, more the discusting part… a big beans.xml file sucks… and analysing my beanx.xml showed, that one small component is used everywhere and a lot of injects only where this one class…

first, something about a concept… in my opinion there are two ways for dependency injection… the one where a config file injects classes to others and the one where the class, wishes to get an injection… secondly is @Resource, @Autowired…

and in my project, i thought the one class, which is used almost everywhere should be get by @Autowired… and actually it was an static class, where the key-value configuration out of the db is loaded… so simply refactored it…

and the main problem was: tell spring, that it should autowire!

<context:annotation-config />
<context:component-scan base-package=“[class name]” />

after adding this few lines to the beans.xml, it actually found the [class name] in time and could inject it 🙂

crap config!

powermock

PowerMock is an extension for Mockito or EasyMock, which can mock really special things… static classes, Constructors, partial mocks,…

cool thing…. http://code.google.com/p/powermock/

read from classpath

in our project we have a few templates… so the biggest question is, where to put them… there are a lot of simple answers, but consider this: 4 productive instances, 2 test and 1 dev… these platforms dont know each others… so the solution database, which they share, or localy on each machine… db is not a solution, because the main user has no access to it, and changing templates might happen at the beginning… so file system it is… and we dicided to put them in classpath… easier to roll out for the devs… so how access a file from the classpath?

String filenameOnClasspath = “/myfile.txt” // Located in a directory which is on the classpath
URL url = getClass().getResource(filename);
String fullyQualifiedFilename = url.getFile();

Source

JSF AlertBox

So once again i m working on a frontend… my main goal was not to work on frontends again… not that i dont want to do these kind of work, but creating customer facing stuff is demanding (which is a good thing) and a pain, because of the Business… a good idea is death, when the Business thinks they have a good idea aswell… so frontend pixel moving is really not my enjoyable work… but as i said… im doing it again… 

this time with JSF (JavaServer Faces)… in general, i like JSF ans its concepts… but there are still some really confusing things, including communication frontend to backend. But this might be a ajax problem… 

and now to the topic related stuff. I needed to implement an AlertBox. We use PrimeFaces, therefore i tried to do it as a dialog. But dialogs cant be updated, so its inside an outputPanel… 

xhtml

<p:outputPanel id=”alertBoxPanel”>
  <p:dialog header=”#{alertBoxBean.header}” widgetVar=”alertBoxDialog” visible=”#{alertBoxBean.show}”>
    <h:form>
      <table cellpadding=”0″ cellspacing=”0″ border=”0″>
      <tr>
        <td align=”center”><h:outputText value=”#{alertBoxBean.text}” escape=”false” /></td>
       </tr>
      <tr>
        <td align=”center”><p:commandButton value=”Ok” oncomplete=”alertBoxDialog.hide();” actionListener=”#{alertBoxBean.handleClose}” /></td>
     </tr>
    </table>
  </h:form>
</p:dialog>
</p:outputPanel>

java

Mainly there is the bean and its handles.

jpa type safe entityManager calls

testing our jpa daos always worked successful… but i think this was mainly, because we filled during test the data on the database and got the data and tested it in the same transaction. therefore the data might be still stored in cache and never an actuall db call was made… this needs to be analysed and tested in near future… but thats a different blog entry… hopefully…

this blog entry is about the problem, which occures, when the db actuall is called 🙂

We have a GenericDao and all find all calls worked… and we have object based daos… following example, with an random object… lets take Person:

    public List<Person> getPersonByName(final String name) {
        @SuppressWarnings(“unchecked”)
        final List<Person> list = (List<Person>) entityManager
                .createNamedQuery(Person.GET_PERSON_BY_NAME)
                .setParameter(“name”, name)
                .getResultList();
        return list;
    }

This resulted in an exception… dont have the Exception here, but was something with an Object[] missmatch… and something about an non initialitzed EntityManager… the problem was the @SuppressWarnings(“unchecked”): or the warrning, which we just ignored… getResultList() returns a List… and casting List to List<Person> just doesnt work… whats the solution? Simple… just tell the named query, that kind of type it is… so it returns an List<Person> instead of the List…

 .createNamedQuery(Person.GET_PERSON_BY_NAME, Person.class)

and the @SuppressWarnings(“unchecked”) isnt used anymore 🙂

mockito matchers

mocking stuff with mockito is cool… take a function, setup expected input values and define a return result… easy when the input value is fix… and can not be bigger then…

ok what was my problem… i had a simple function, which i needed to mock: (a little more complex, with acutally complex objects and enums, but it would have in a problem with following aswell)

int methodName(String s, int i);

the problem was that the return int is similar then the i is greater then or equal a value… lets say 3 (which actually was the case in my case, but who cares…)… so the mocks:

Mockito.when(this.myMock.methodName(“Hello”, 1)).thenReturn(7);
Mockito.when(this.myMock.methodName(“Hello”, 2)).thenReturn(23);

Greater then is:

Mockito.when(this.myMock.methodName(Mockito.eq(“Hello”), AdditionalMathcer.geq(3))).thenReturn(63456);

The problem or the confusing thing is, when you add an Matcher, all parameters need to have a Matcher… :/

date and calendar

java.util.date was a really  logic thing for me… but now, all cool functions on Date are deprecated… crap… ok with Calendar it makes object-oriented-wise more sense… but how the hell do i get from an Date to the Calendar? Calendar offers only an getInstance() function, which actually sets the Calendar to current datetime… not really the thing i wanted…

Calender cal = Calender.getInstance();
cal.setTime(date);

is the solution… crap…

mockito

mockito is a pretty cool mocking framework. It is simple and logic… actually i am using it with interfaces, which makes it really logic and simple, dont know, who it works against normal classes… will test that soon 🙂

but first, how does it work with a simple calculator class:

HelloWorld helloWorld = Mockito.mock(HelloWorld.class);
Mockito.when(helloWorld.hello(Mockito.anyString())).thenReturn(“Hello”);

  • So first line initializes the mock.
  • Second line defines the return value of the function hello(String) when called with any string. So it allways returns Hello.