Thymeleafを試す

Thymeleafはテンプレートの一種。
Springとの相性が良く簡単に使用出来る。

・top.html

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="Hello, Thymeleaf!">Hello, hide6644!</p>
</body>
</html>

View Resolverを設定する。

<!-- View Resolver for Thymeleaf -->
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".html" />
    <property name="templateMode" value="HTML" />
    <property name="cacheable" value="false" />
</bean>
 
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver" />
    <property name="enableSpringELCompiler" value="true" />
</bean>
 
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine" />
    <property name="characterEncoding" value="UTF-8" />
    <property name="order" value="4" />
</bean>

Veiwを呼び出す。

@RequestMapping(value = "top", method = RequestMethod.GET)
public String topRequest() {
    return "top";
}

YOMOU CRAWLERについて

小説を読もう!の更新を自動でチェックしたかったのだが、自分の好みに合ったサービスがなかった。
じゃあ、自分で作っちゃえってことで以下の通り。

  1. 主要機能
    • 小説の各話の登録更新状況をメールで通知する
    • 更新チェックではアクセス頻度を減らしサーバーに負荷を掛けないようにする
    • 小説が消されても、後で読めるように更新チェックと同時に、更新内容を保存する
  2. 稼働環境
    • 言語:Java11
    • データベース:MySQL
  3. 副題
    • Hibernateを学ぶ
    • オブジェクト指向を学ぶ
  4. 参考情報(小説を読もう!のページの構造)
    • 小説のトップページにはタイトル、作者名、概要、目次がある
    • 目次のリンクからは各話に遷移出来る
    • 各話にはタイトルと本文がある
  5. 詳細機能
    • htmlを取得しパースする
    • メール送信する

PowerMockitoでUnit Test

下記のインスタンスを生成する部分をMockにしたい。

NovelSource novelSource = new NovelSource(url);

そこで、PowerMockitoを使用してみる。

<!-- https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito -->
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.6.6</version>
</dependency>

使用方法はこんな感じ。

@RunWith(PowerMockRunner.class)
@PrepareForTest({ NovelManagerImpl.class })
@PowerMockIgnore("javax.management.*")
public class NovelManagerImplTest extends BaseManagerMockTestCase {
    @Mock
    private NovelSource novelSource;
 
    @Mock
    private Logger log;
 
    @Mock
    private NovelDao novelDao;
 
    @Mock
    private NovelInfoManager novelInfoManager;
 
    @Mock
    private NovelChapterManager novelChapterManager;
 
    @InjectMocks
    private NovelManagerImpl novelManager = new NovelManagerImpl();
 
    @Test
    public void testAdd() throws Exception {
        String fileName = this.getClass().getClassLoader().getResource("novel/20160924/test.html").getPath();
        File file = new File(fileName);
        String url = "http://www.foo.bar/20160924/";
        NovelSource novelSource = new NovelSource(new Source(file));
 
        {
            // 初期化
            MockitoAnnotations.initMocks(this);
            // NovelSourceをnewするとき、thenReturn(~)のインスタンスを返却する
            PowerMockito.whenNew(NovelSource.class).withArguments(url).thenReturn(novelSource);
        }
    }

コンストラクタに引数を渡したいときは、withArguments()を使用する。

しかし、カバレッジが測定出来ないので注意。