Total Pageviews

2014/12/22

市場乘數 (平均本益比)

無論是漫步華爾街或是彼得學林的學以致富的書籍中,作者都有提到不要買價格超過其實際價值的股票,強調用本益比來挑選股票的重要性。

如果想知道目前本隻本益比是偏高或偏低,可以看過去的歷史紀錄來判斷。
以中華電信為例,目前中華電信(2412)的本益比是18.05

我們可以去查一下過去五年來的最高平均、最低平均以及平均本益比的值
根據下圖的資訊,可以得知99~103年的歷年最高、最低本益比資訊,故:
  • 平均最高本益比 = (19 + 20 + 18 + 18 + 16) / 5 = 18.2
  • 平均最低本益比 =  (17 + 17 + 14 + 14 + 13) / 5 = 15
  • 平均本益比 = 16.6



由上述資訊可以看出目前中華電信本益比為18.05,屬於接近偏高

然而以上是用單支股票的方式來看,在學以致富此本書中有提到,我們也可以一次挑選多家公司,把他們的股價加起來,然後除以它們的盈餘,就可以得出一個平均本益比。在華爾街,人們用此方式計算道瓊工業指數、S&P 500指數的本益比,得出所謂的市場乘數(Market Multiple),也就是平均本益比,其代表著人們願意以多高的價格買下多少盈餘的公司。

在台灣的話,我們可以參考台灣50的成分股,以下是50檔台灣50的本日股價與EPS相關資訊:


代號 公司名稱 股價 EPS
3474 華亞科 47.60 7.10
4938 和碩 72.50 4.94
3481 群創 14.95 0.97
2330 台積電 138.50 8.82
2303 聯電 14.45 0.66
2882 國泰金 46.15 3.63
2357 華碩 335.00 27.68
1303 南亞 61.60 4.43
2883 開發金 9.83 0.68
1301 台塑 68.00 3.79
2002 中鋼 26.10 1.22
2311 日月光 39.10 2.55
2317 鴻海 88.00 8.21
1402 遠東新 30.05 2.23
2892 第一金 18.45 1.44
2880 華南金 17.40 1.30
2801 彰銀 18.00 1.36
1216 統一 48.60 2.00
1101 台泥 42.45 2.89
1102 亞泥 37.85 2.76
2382 廣達 78.50 4.88
2308 台達電 186.00 8.31
1326 台化 64.50 3.39
2886 兆豐金 24.10 2.28
2891 中信金 20.40 2.66
2325 矽品 48.20 3.50
2105 正新 71.60 5.13
2395 研華 216.00 7.44
2408 南科 73.90 10.30
2412 中華電 92.60 5.12
2409 友達 15.10 1.30
2207 和泰車 463.00 16.63
2301 光寶科 36.00 3.24
9904 寶成 38.10 2.94
2912 統一超 245.00 8.76
2354 鴻準 87.60 5.21
2474 可成 242.00 19.40
3045 台灣大 99.90 5.78
2454 聯發科 455.00 29.64
2881 富邦金 49.45 5.81
2887 台新金 12.90 1.62
4904 遠傳 69.90 3.71
2885 元大金 15.20 1.53
3008 大立光 2,325.00 113.67
2498 宏達電 136.50 1.62
2884 玉山金 19.55 1.50
2890 永豐金 12.80 1.53
6505 台塑化 66.20 3.02
5880 合庫金 18.45 1.26
2227 裕日車 327.00 20.60

上述50檔台灣50的股價total為 6785.03,盈餘的total為 390.44,把股價 (6785.03) 除以盈餘 (390.44) ,可以得到這50檔的平均本益比為17.38


Total 股價 Total 盈餘 市場乘數 (市場平均本益比)
6785.03 390.44 17.38

我們就可以拿17.38這個數字做為參考,人們願意購買的本益比的股票約為17.38,代表著人們願意以多高的價格買下多少盈餘的公司股票,若過高的話就要考慮一下了,這個或許也可以作為一個股票買賣的一個參考


Reference

[1] http://www.twse.com.tw/ch/trading/indices/twco/tai50i.php
[2] http://www.cnyes.com/twstock/profile/2412.htm
[3] http://jsjustweb.jihsun.com.tw/z/zc/zca/zca_2412.djhtm

How to Determine if the Date Format is Correct or Not in Java

Problem
We hope the date parameter should be yyyyMMdd format and only accept Western calendar system. 
If user provide a date parameter with Minguo calendar, it should return error to user.

Solution
This DateUtils class provides an isCorrectDateFormatWithWesternYear method, it will

  • return false if date format is incorrect
  • return true if date format is correct

We define the expected format in Line 24.
Set lenient to false with strict parsing, and call parse method to check the parse 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
package gov.nta.dbm.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import lombok.extern.slf4j.Slf4j;

/**
 * The Class DateUtils.
 */
@Slf4j
public class DateUtils {

    /**
     * Checks if is correct date format with western year.
     * 
     * @param dateStr
     *            the date str
     * @return the boolean
     */
    public static Boolean isCorrectDateFormatWithWesternYear(String dateStr) {
        Boolean isCollect = Boolean.FALSE;
        // instantiate SimpleDateFormat object with specific date format
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        try {
            // Specify whether or not date/time parsing is to be lenient. With lenient parsing, the
            // parser may use heuristics to interpret inputs that do not precisely match this
            // object's format. With strict parsing, inputs must match this object's format
            dateFormat.setLenient(false);
            // call parse method, return true if parse successfully, return false if fail to parse.
            dateFormat.parse(dateStr);
            isCollect = Boolean.TRUE;
        } catch (ParseException e) {
            isCollect = Boolean.FALSE;
            e.printStackTrace();
        }
        return isCollect;
    }

    /**
     * The main method.
     * 
     * @param args
     *            the arguments
     */
    public static void main(String[] args) {
        log.debug("test1=" + DateUtils.isCorrectDateFormatWithWesternYear("20141231"));
        log.debug("test1=" + DateUtils.isCorrectDateFormatWithWesternYear("1031222"));
    }

}

In main method, we input "20141231" and "1031222" to do test, and here is the test result:
1
2
3
4
5
6
15:34:00.446 [main] DEBUG gov.nta.dbm.utils.DateUtils - test1=true
java.text.ParseException: Unparseable date: "1031222"
 at java.text.DateFormat.parse(DateFormat.java:357)
 at gov.nta.dbm.utils.DateUtils.isCorrectDateFormatWithWesternYear(DateUtils.java:31)
 at gov.nta.dbm.utils.DateUtils.main(DateUtils.java:48)
15:34:00.453 [main] DEBUG gov.nta.dbm.utils.DateUtils - test1=false


Reference
[1] http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html

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