Total Pageviews

2014/12/19

How to delete a branch in EGit?

Question
Assume I created a branch, DBM_CommonService_getPaymentData, to implement a new function. 

After I finished this function and commit & push to GitLab,  and GitLab project owner merged this branch into master. 

If I had switch to master and would like to delete DBM_CommonService_getPaymentData branch, how to delete it?


Answer

Step1. right click => Team => Advanced => Delete branch

Step2. Selected the branch name you want to delete => Click "OK"

See..as you right click => Team => Switch to, you won't see DBM_CommonService_getPaymentData branch anymore.

Reference

2014/12/17

How to Run Stored Procedure in Oracle SQL Developer

Problem
Assume I created a stored procedure which named PROC_FMS406R_TAB5_RPT1_STEP1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
create or replace PROCEDURE PROC_FMS406R_TAB5_RPT1_STEP1 
(
  INPUT_YYY_MM IN VARCHAR2 
, INPUT_LAST_YEAR IN VARCHAR2 
, INPUT_USER_ID IN VARCHAR2 
) AS 
BEGIN
  
  --1.5.2 計算『歲入估測分析表(含特別預算)』上一年度的一月到12月的實際數
 -- ignore execution detail

  
  
 --1.5.3 計算『歲入估測分析表(含特別預算)』上一年度的合計數
 -- ignore execution detail
  
END PROC_FMS406R_TAB5_RPT1_STEP1;


I tried to execute PROC_FMS406R_TAB5_RPT1_STEP1 in SQL worksheet


It will fail to execute and show this error message:

1
2
3
4
命令的第 2 行開始發生錯誤 -
PROC_FMS406R_TAB5_RPT1_STEP1(:INPUT_YYY_MM, :INPUT_LAST_YEAR, :INPUT_USER_ID)
錯誤報告 -
不明的命令

How to Fix This Problem
Add BEGIN before the stored procedure and add END; after the stored procedure.

1
2
3
BEGIN
PROC_FMS406R_TAB5_RPT1_STEP1(:INPUT_YYY_MM, :INPUT_LAST_YEAR, :INPUT_USER_ID);
END;


Reference
[1] http://dba.stackexchange.com/questions/57163/how-to-run-procedure-in-oracle-sql-developer

2014/12/16

Utilize Styles to Change Font Color Based on Condition in iReport

Problem
Customer ask us to change font color to red if the amount is less than zero.

It should look like this:


Solution
  • Step1. Move cursor to "Styles" and right click => Add => Style


  • Step2. Right click on "style1" => Add "Conditional Style"


  • Step3. Edit "Conditional Expression" ( apply this style if $F(amount) value is less than zero)


  • Step4. Set font color to RED


  • Step5. Owning to set font color to red, it will set line color to red as well. Hence, we need to set line color to black manually.


  • Step6. Click on $F{amount} and set "Style" to "style1" which we create and configure in Step2 ~ Step5.



2014/12/15

How to revert changes in EGit

Assume I have a form bean in gov.nta.dbm.web.dto

The form bean is as following:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package gov.nta.dbm.web.dto;

import java.io.Serializable;

import org.apache.commons.lang.builder.ToStringBuilder;

import lombok.Data;

@Data
public class Dbm915rFormBean implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private int year = 0;
    private int month = 0;
    
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

If I add one more attribute in this form bean:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package gov.nta.dbm.web.dto;

import java.io.Serializable;

import org.apache.commons.lang.builder.ToStringBuilder;

import lombok.Data;

@Data
public class Dbm915rFormBean implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private int year = 0;
    private int month = 0;
    private String type;
    
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

We can see this form bean had been modified and add ">" icon before file name to remind us this file had been modified.

If I would like to revert its changes, just right click => Replace with => HEAD Revision

Click OK to make sure to discard changes.

We can see the ">" label is disappeared.

And the form bean had been revert to original version:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package gov.nta.dbm.web.dto;

import java.io.Serializable;

import org.apache.commons.lang.builder.ToStringBuilder;

import lombok.Data;

@Data
public class Dbm915rFormBean implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private int year = 0;
    private int month = 0;
    
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}


If you find out your revert action is success, but the ">" label still show in Eclipse.
Its EGit bug, go to its website ( http://eclipse.org/egit/download/ ) to update your EGit version.

2014/12/11

Checkout remote Git branch

Owing to our software development had been outsourced, here is the delivery process as function completion: 



As outsourced programmer completed his/her own function, he/she need to go to Gitlab to create a merge request to remind us:



Then we need to checkout his/her branch which he/she pushed to do code review.


Assume our initial branch is master, then there is the process to check out remote Git branch:



  • 1. Team => Remote => Fetch From...


  • 2. Click "Next"
  • 3. You can find out the branch name in merge request no matter in Gitlab or email. Hence, configure the source and destination ref. And click "Finish" button.

  •  4. Click OK


  • 5. Team => Switch To => Other..


  • 6. Remote Tracking => select specific branch name => Checkout.


  • 7. Checkout


  • 8. Click "Finish"


  • 9. We had changed from master to "DBM915R_new_function"







2014/12/09

Utilize CLOC(Count Lines of Code) to count lines of code

Requirement
If I would like to count lines of code for each Java program in my Java project, which tool can help me?

Solution
Go to http://cloc.sourceforge.net/ to download exe file (i.e. cloc-1.62.exe).

Assume cloc-1.62.exe had been saved into D:\software
Assume the Java project located in D:\workspace2\ave\ave-webapp\src\main\java
  • Step1. open command prompt.
  • Step2. Change directory to Java project directory, i.e. D:\workspace2\ave\ave-webapp\src\main\java
  • Step3. Execute this command: d:\software\cloc-1.62.exe . --by-file

Check the result:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
D:\workspace2\ave\ave-webapp\src\main\java>d:\software\cloc-1.62.exe . --by-file
      94 text files.
      94 unique files.
       8 files ignored.

http://cloc.sourceforge.net v 1.62  T=0.32 s (296.3 files/s, 60038.9 lines/s)
------------------------------------------------------------------------------------------------------------
File                                                                     blank        comment           code
------------------------------------------------------------------------------------------------------------
.\gov\nta\ave\web\rest\Ave102eResource.java                                369            299           1505
.\gov\nta\ave\web\rest\Ave104eResource.java                                195            235            907
.\gov\nta\ave\web\rest\Ave103eResource.java                                198            235            891
.\gov\nta\ave\web\rest\Ave101eResource.java                                199            242            890
.\gov\nta\ave\web\rest\Ave106eResource.java                                188            148            783
.\gov\nta\ave\web\rest\Ave401eResource.java                                212            234            772
.\gov\nta\ave\web\rest\Ave201eResource.java                                110             91            576
.\gov\nta\ave\web\rest\Ave105eResource.java                                 85             16            405
.\gov\nta\ave\web\controller\Ave608rReportController.java                  107            114            338
.\gov\nta\ave\web\controller\Ave201eReportController.java                   69             38            334
.\gov\nta\ave\web\rest\Ave904eResource.java                                 57             71            309
.\gov\nta\ave\web\rest\Ave903eResource.java                                 80             23            251
.\gov\nta\ave\web\controller\Ave114rReportController.java                   58             43            227
.\gov\nta\ave\web\controller\Ave613rReportController.java                   54             37            194
.\gov\nta\ave\web\controller\Ave401eReportController.java                   70             38            178
.\gov\nta\ave\web\rest\Ave121eResource.java                                 46             26            168
.\gov\nta\ave\web\rest\Ave115qResource.java                                 31             38            168
.\gov\nta\ave\web\rest\Ave907eResource.java                                 45             66            157
.\gov\nta\ave\web\rest\Ave117rResource.java                                 50             43            150
.\gov\nta\ave\web\controller\Ave609rReportController.java                   33             35            148
.\gov\nta\ave\web\controller\Ave108rReportController.java                   35             41            147
.\gov\nta\ave\web\controller\Ave116rReportController.java                   43             24            146
.\gov\nta\ave\web\rest\Ave909eResource.java                                 39             13            140
.\gov\nta\ave\web\controller\Ave611rReportController.java                   30             12            137
.\gov\nta\ave\web\controller\Ave502rReportController.java                   40             30            117
.\gov\nta\ave\web\controller\Ave105eReportController.java                   24              2            112
.\gov\nta\ave\web\controller\Ave610rReportController.java                   40             29            111
.\gov\nta\ave\web\rest\Ave905eResource.java                                 31              4            108
.\gov\nta\ave\web\controller\Ave605rReportController.java                   24             16            104
.\gov\nta\ave\web\rest\Ave112qResource.java                                 32             25            100
.\gov\nta\ave\web\controller\Ave117rReportController.java                   17             11             82
.\gov\nta\ave\web\rest\Ave501eResource.java                                 14              0             78
.\gov\nta\ave\web\controller\Ave607rReportController.java                   20             10             75
.\gov\nta\ave\web\dto\Ave102eFormBean.java                                  40             32             75
.\gov\nta\ave\web\rest\Ave114rResource.java                                 16             25             73
.\gov\nta\ave\web\controller\Ave601rReportController.java                   17              2             71
.\gov\nta\ave\web\rest\Ave901eResource.java                                 15              0             71
.\gov\nta\ave\web\controller\Ave602rReportController.java                   17              2             71
.\gov\nta\ave\web\rest\Ave110qResource.java                                 19              1             70
.\gov\nta\ave\web\dto\Ave106eFormBean.java                                  26             16             68
.\gov\nta\ave\web\controller\Ave405rReportController.java                   18             10             67
.\gov\nta\ave\web\rest\Ave122qResource.java                                 14              8             67
.\gov\nta\ave\web\controller\Ave406rReportController.java                   23              4             67
.\gov\nta\ave\web\rest\Ave111qResource.java                                 17              1             64
.\gov\nta\ave\web\controller\Ave612rReportController.java                   17             11             61
.\gov\nta\ave\web\controller\Ave604rReportController.java                   16              1             58
.\gov\nta\ave\web\controller\Ave403rReportController.java                   18             12             56
.\gov\nta\ave\web\dto\Ave101eFormBean.java                                  14              7             56
.\gov\nta\ave\web\dto\Ave103eFormBean.java                                  14              7             56
.\gov\nta\ave\web\dto\Ave104eFormBean.java                                  14              7             56
.\gov\nta\ave\web\controller\Ave408rReportController.java                   18             11             55
.\gov\nta\ave\web\controller\Ave603rReportController.java                   14              0             53
.\gov\nta\ave\web\controller\Ave410rReportController.java                   15             21             53
.\gov\nta\ave\web\controller\Ave606rReportController.java                   16             10             52
.\gov\nta\ave\web\controller\Ave115qReportController.java                   14              9             46
.\gov\nta\ave\web\controller\Ave107rReportController.java                   12              5             46
.\gov\nta\ave\web\controller\Ave904eReportController.java                   14              1             45
.\gov\nta\ave\web\rest\Ave902eResource.java                                 10              0             45
.\gov\nta\ave\web\controller\Ave113qReportController.java                   11              6             44
.\gov\nta\ave\web\dto\Ave201eFormBean.java                                  19             12             43
.\gov\nta\ave\web\controller\Ave203rReportController.java                   16             16             43
.\gov\nta\ave\web\controller\Ave501eReportController.java                   11              0             42
.\gov\nta\ave\web\controller\Ave903eReportController.java                   13              1             41
.\gov\nta\ave\web\controller\Ave901eReportController.java                   14              0             36
.\gov\nta\ave\web\controller\Ave402rReportController.java                   11              0             36
.\gov\nta\ave\web\controller\Ave303rReportController.java                   10              0             35
.\gov\nta\ave\web\controller\Ave302rReportController.java                   11              2             33
.\gov\nta\ave\web\rest\Ave113qResource.java                                  8             14             32
.\gov\nta\ave\web\controller\Ave404rReportController.java                    8              7             31
.\gov\nta\ave\web\controller\Ave204rReportController.java                    9              7             30
.\gov\nta\ave\web\controller\Ave202rReportController.java                    6              0             30
.\gov\nta\ave\web\controller\Ave407rReportController.java                    8              1             28
.\gov\nta\ave\web\controller\Ave301rReportController.java                    9              1             28
.\gov\nta\ave\web\controller\Ave409rReportController.java                    9              1             28
.\gov\nta\ave\web\dto\Ave608rFormBean.java                                  21              0             24
.\gov\nta\ave\web\dto\Ave609rFormBean.java                                  19              0             22
.\gov\nta\ave\web\dto\Ave401eFormBean.java                                  19              0             22
.\gov\nta\ave\web\dto\Ave903eFormBean.java                                  16              0             19
.\gov\nta\ave\web\dto\Ave121eFormBean.java                                  14              0             18
.\gov\nta\ave\web\dto\Ave613rFormBean.java                                   7              0             18
.\gov\nta\ave\web\dto\Ave610rFormBean.java                                  10              0             13
.\gov\nta\ave\web\dto\Ave105eFormBean.java                                   7              0             11
.\gov\nta\ave\web\dto\Ave122qFormBean.java                                   6              0             10
.\gov\nta\ave\web\dto\Ave907eFormBean.java                                   4              0              8
.\gov\nta\ave\web\dto\Ave113qFormBean.java                                   4              0              7
.\gov\nta\ave\web\dto\Ave905eFormBean.java                                   2              0              7
.\gov\nta\ave\web\dto\Ave203rFormBean.java                                   2              0              7
.\gov\nta\ave\web\dto\ComboboxDto.java                                       4              0              7
.\gov\nta\ave\web\dto\Ave606rFormBean.java                                   2              0              7
.\gov\nta\ave\web\dto\Ave404rFormBean.java                                   2              0              7
.\gov\nta\ave\web\dto\Ave403rFormBean.java                                   3              0              6
.\gov\nta\ave\web\rest\package-info.java                                     0              5              1
.\gov\nta\ave\web\controller\package-info.java                               0              3              1
.\gov\nta\ave\web\dto\package-info.java                                      0              3              1
------------------------------------------------------------------------------------------------------------
SUM:                                                                      3388           2571          13086
------------------------------------------------------------------------------------------------------------

D:\workspace2\ave\ave-webapp\src\main\java>


Reference
[1] http://cloc.sourceforge.net/

How to mock autowired fields via Mockito

Problem
Assume we wrote a calculation service which provide add and subtract service.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

/**
 * The Class CalculationService.
 */
@Service
@Slf4j
public class CalculationService {

    public int add(int num1, int num2) {
        return num1 + num2;
    }
    
    public int subtract(int num1, int num2) {
        return num1 - num2;
    }
    
}

If we would like to do unit test for CalculationService class, and autowire CalculationService into service class as bellows:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import static org.junit.Assert.assertEquals;
import gov.nta.nss.service.CalculationService;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class CalculationServiceTest {

    /* Inject CalculationService */
    @Autowired
    private CalculationService service;

    /* Test add function in CalculationService */
    @Test
    public void testAdd() {
        // arrange & act
        int result = service.add(6, 5);

        // assert
        assertEquals(11, result);
    }

    /* Test subtract function in CalculationService */
    @Test
    public void testSubtract() {
        // arrange & act
        int result = service.subtract(6, 5);

        // assert
        assertEquals(1, result);
    }

}

As I executed this test class, it returned NullPointerException. This exception result from failed to autowire CalculationService. How to overcome this problem?



Solution
We can mock autowired fields by Mockito to solve this problem. Here is the revised test code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import static org.junit.Assert.assertEquals;
import gov.nta.nss.service.CalculationService;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;

public class CalculationServiceTest {

    /* Inject CalculationService */
    @InjectMocks
    private CalculationService service;

    /* Initialized mocks */
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    /* Test add function in CalculationService */
    @Test
    public void testAdd() {
        // arrange & act
        int result = service.add(6, 5);

        // assert
        assertEquals(11, result);
    }

    /* Test subtract function in CalculationService */
    @Test
    public void testSubtract() {
        // arrange & act
        int result = service.subtract(6, 5);

        // assert
        assertEquals(1, result);
    }

}

Test result is bellowing:


Reference
[1] http://lkrnac.net/blog/2014/01/mock-autowired-fields/
[2] http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html


2014/12/05

How to implement indexOf in Oracle

Requirement
In Java, we can use indexOf method if we would like to return the index within this string of the first occurrence of the specified character. For instance, if we would like to get the characters which before left parenthesis, we can do this way. 

1
2
3
4
5
6
    public static void main(String[] args) {
        String str1 = "ABC(12345)";
        String str2 = "BC(34567)";
        System.out.println(str1.substring(0, str1.indexOf("(")));
        System.out.println(str2.substring(0, str2.indexOf("(")));
    }

But Oracle does not provide OOTB(Out of The Box) function to fulfill this requirement.

How to do 
We can make good use of INSTR function, the syntax is:

The INSTR functions search string for substring. The function returns an integer indicating the position of the character in string that is the first character of this occurrence. INSTR calculates strings using characters as defined by the input character set. INSTRB uses bytes instead of characters. INSTRC uses Unicode complete characters. INSTR2 uses UCS2 code points. INSTR4 uses UCS4 code points.

  • position is an nonzero integer indicating the character of string where Oracle Database begins the search. If position is negative, then Oracle counts backward from the end of string and then searches backward from the resulting position.
  • occurrence is an integer indicating which occurrence of string Oracle should search for. The value of occurrence must be positive.

Here has an example to fulfill this requirement:
1
2
SELECT SUBSTR('ABC(12345)', 0, INSTR('ABC(12345)', '(', 1, 1)-1) FROM DUAL;
SELECT SUBSTR('BC(34567)', 0, INSTR('BC(34567)', '(', 1, 1)-1 ) FROM DUAL;


Reference
[1] https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions068.htm#i77598
[2] http://fanli7.net/a/shujuku/Oracle/20120615/171956.html