All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nablarch.test.support.log.app.OnMemoryLogWriter Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
package nablarch.test.support.log.app;

import nablarch.core.log.basic.LogWriterSupport;
import nablarch.core.util.Builder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class OnMemoryLogWriter extends LogWriterSupport {
    
    private static final Map> messagesMap = new HashMap>();
    
    public static void clear() {
        messagesMap.clear();
    }
    
    public static List getMessages(String name) {
        if (!messagesMap.containsKey(name)) {
            messagesMap.put(name, new ArrayList());
        }
        return messagesMap.get(name);
    }


    public static void assertLogContains(String name, String... expected) {
        List origExpected = Arrays.asList(expected);

        // まだ発見されていない期待ログ
        Set expectedRest = new HashSet(origExpected);

        List actualLogs = OnMemoryLogWriter.getMessages(name);

        for (String actualLog : actualLogs) {
            Set ok = new HashSet();
            for (String expectedLog : expectedRest) {
                if (actualLog.contains(expectedLog)) {
                    ok.add(expectedLog);
                }
            }
            // 出現した文言を消し込み
            expectedRest.removeAll(ok);
            ok.clear();
        }
        // 期待したログ全てが、実際のログに含まれていること
        if (!expectedRest.isEmpty()) {
            throw new AssertionError(Builder.concat(
                    "expected log not found. \n",
                    "expected = ", origExpected.toString(), "\n",
                    "actual   = ", actualLogs.toString())
            );
        }
    }


    protected void onWrite(String formattedMessage) {
        if (formattedMessage.contains("initialized.")) {
            return;
        }
        getMessages(getName()).add(formattedMessage);
    }

    protected void onTerminate() {
        getMessages(getName()).add("@@@END@@@");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy