пятница, 11 июля 2014 г.

Unit test. Java vs Groovy

What do You think What is a more convenient for writing Unit test for a java classes? Simple Java or Grooovy. Let's see on following examples:
Java example:
public class DataAdapterTest {

    @Test
    public void testGetColumns() throws BaseException {
        FieldSetsEntry fieldsSet = new FieldSetsEntry();
        fieldsSet.setSetName("data");
        FieldsEntry fe1 = new FieldsEntry();
        fe1.setFieldName("first_column");
        FieldsEntry fe2 = new FieldsEntry();
        fe2.setFieldName("second_column");
        FieldsEntry fe3 = new FieldsEntry();
        fe3.setFieldName("third_column");
        List fieldsEntries = new ArrayList<>();
        fieldsEntries.add(fe1);
        fieldsEntries.add(fe2);
        fieldsEntries.add(fe3);
        fieldsSet.setFields(fieldsEntries);
        List fieldsSets = new ArrayList<>();
        fieldsSets.add(fieldsSet);

        RulesEntry rule = new RulesEntry();
        rule.setFieldSets(fieldsSets);

        List columns = new DataAdapter(new Message(rule)).getColumns();

        Assert.assertEquals(3, columns.size());
    }
}
Groovy example:

class DataAdapterGroovyTest {

    @Test
    public void testGetColumns() {
        FieldSetsEntry fieldsSet = 
        new FieldSetsEntry(setName:"data", fields:[new FieldsEntry(fieldName:"first_column"),
                                                   new FieldsEntry(fieldName:"second_column"),
                                                   new FieldsEntry(fieldName:"third_column")]);

        DataAdapter adapter = new DataAdapter(new Message(new RulesEntry(fieldSets:[fieldsSet])));

        def columns = adapter.getColumns();
        Assert.assertEquals(3, columns.size());
    }
}
I think answer is obvious :) link to full project

суббота, 5 июля 2014 г.

How to run and debug a simple web application on Java (Tomcat) through Intellij IDEA

At first we will need the best tool for java developers Intellij IDEA, simple java web application (will be better if we either will have a Maven Project Object Model file) and Tomcat as servlet container.
Example of my POM file

    4.0.0
    ua.home.webmvc
    WebMVC
    war
    0.0.0.1
    WebMVC

    
        
            public
            http://mvnrepository.com
        
    
    
        
            plugins.public
            http://mvnrepository.com
        
    

    
        WebMVC
    

    
        4.0.3.RELEASE
    

    
        
        
            org.springframework
            spring-core
            ${spring.version}
        

        
            org.springframework
            spring-web
            ${spring.version}
        

        
            org.springframework
            spring-webmvc
            ${spring.version}
        

        
            org.springframework
            spring-context
            ${spring.version}
        

        
            org.springframework
            spring-beans
            ${spring.version}
        

        
            javax.servlet
            jstl
            1.2
            runtime
        

    



Let's open our IDE and choose the option "Open project"

And after that we need open a pom.xml (ofc if we have a Maven Project Object Model file)

As reasult we will see our project in Intellij IDEA

IDEA are able to setup project from maven pom.xml file but for following work You need download a maven distributive. To IDEA is able to work with maven You have to create M2_HOVE variable environement with Maven home path (for example M2_HOME: D:\Install\build_tools\apache-maven-3.2.1) or using IDEA right tool bar as You can see on picture

Since our application is web application we need some web server like a JBoss or just simple servlet container like a Tomcat. Let's download a Tomcat from official site and unpack to some folder We are ready to start our application. Take a look please on next picture and do the same

For the next step we need to adjust a tomcat

On bottom of configuration window appeared warniong message: Warning: No artifacts marked for deployment, for fixing this problem You just need to push a button "Fix" choose any option (I recommend to choose "exploded" option). As You will see IDEA will offer to fill context param and name of run up configuration

That's all :) Our application is ready to start :) Let's try

Example of link for enterence for our application: http://localhost:8080/webmvc/view.do Actually if someone needs example of my application please leave me message in comments
  • OS: Windows 8.1
  • IDEA: 13.1.3
  • Tomcat: 7.0.50
  • Maven: 3.2.1

среда, 2 июля 2014 г.

How to start using the mongo db (install as windows service, create db, create user)


This post doesn't contain a piece of some special information. It's just attempt to share the most important things with beginners to start using mongo db much easier and faster. At first You need to download a mongo db distributive from official site. These are my steps for installing db:

  1. I unpacked files from archive to working folder (for example in my case it was d:\mongodb )
  2. I create a config file (mongo.config) with following content:

  3. ##store data here
    dbpath=D:\mongodb\mongodb_data
     
    ##all output go here
    logpath=D:\mongodb\mongodb_log\mongo.log
    # Enable journaling
    journal=true
    
  4. I created two folder (as You can see in config) the one was mongodb_data and the second one was mongodb_log
  5. I invoked the next script for creating windows service which will start or stop db server
    D:\mongodb\bin\mongod.exe --config "D:\mongodb\mongo.config"
         --install
         --serviceDisplayName "MongoDB"
         --serviceDescription "MongoDB Server Instance"
    
    Have a look on one interesting moment. Result of Your invoking You are able to found in mongo.log and this result might be failure. For example:
    2014-07-01T21:30:41.371+0300 Trying to install Windows service 'MongoDB'
    2014-07-01T21:30:41.371+0300 Error connecting to the Service Control Manager: Access is denied. (5)
    
    The reason of this error is very simple I just forgotten to start Far manager as Administrator :) And the success message will be:
    2014-07-01T21:32:46.175+0300 Trying to install Windows service 'MongoDB'
    2014-07-01T21:32:46.208+0300 Service 'MongoDB' (MongoDB) installed with command line 'D:\mongodb\bin\mongod.exe --config D:\mongodb\mongo.config --service'
    2014-07-01T21:32:46.208+0300 Service can be started from the command line with 'net start MongoDB'
    
  6. As result was created a service.
By default mongodb use a database with name test (more details here)
Few examples
-- create database with name mydb
use mydb

-- create user
db.createUser({user: "customuser", pwd: "custompwd", roles: [ "readWrite", "dbAdmin" ]})

-- check user details
db.getUsers()
  • OS: Windows 7; Windows 8.1
  • Mongo db: 2.6.3