⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rmsappendertest.java

📁 利用SyncML开发客户端程序的中间件
💻 JAVA
字号:
/*
 * Copyright (C) 2003-2007 Funambol
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

package com.funambol.util;

import com.funambol.util.LogViewer;
import javax.microedition.rms.RecordStore;
import jmunit.framework.cldc10.TestCase;
import jmunit.framework.cldc10.AssertionFailedException;

/**
 * Test Class for RMS Log
 */
public class RMSAppenderTest extends TestCase {

    //---------------------------------------------------------------- Constants
    public static final int LOGMESSAGESNUMBER = 50;

    //------------------------------------------------------------- Constructors 
    /**
     * New instance of LogTest class
     */
    public RMSAppenderTest() {
        super(3, "RMS Log Store Test");
        try {
            InitLogFile();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    //----------------------------------------------------------- Public Methods 
    /**
     * Test Cases
     */
    public void test(int testNumber) throws Throwable {
    
        switch(testNumber) {
            case 0:
                writeLogFile();
                testWriteLogFileBeforeRotation();
                break;
            case 1:
                int numRecords = writeAndRotate();
                testWriteLogFileAfterRotation();
                break;
            case 2:
                writeBigLogMessage();
                testWriteLastMessage();
                break;
            default:                    
                break;
        }
    }

    /**
     * Create RMS Log RecordStore
     */ 
    private void InitLogFile() throws Exception {
        Log.initLog(new RMSAppender(RMSAppender.LOGDBNAME));
    }
    
    /**
     * Write one message on RMS Log RecordStore: test if the last part of log
     * message is the message written to the log RecordStore.
     * first part of the message is the timestamp
     */
    private void testWriteLastMessage() throws Exception  {
        RecordStore rs = 
                       RecordStore.openRecordStore(RMSAppender.LOGDBNAME, true);
        assertTrue(new String(rs.getRecord((rs.getNextRecordID()-1)))
                                              .endsWith("Last Record Written"));
    }
    
    /**
     * Writes some dummy log messages into the log recordstore using
     * RMSAppender
     */
    private void writeBigLogMessage() {
        int defaultSize = RMSAppender.DEFAULTLOGFILESIZE;
        int msgDim = defaultSize-(50);
        int logMsgSize = 0;
        byte[] msg = new byte[msgDim];  
        for (int i=0; i< msgDim; i++) {
            msg[i] = 1;
        }
        Log.setLogLevel(Log.DEBUG);
        logMsgSize += msg.length;
        //Writes 2 big log message:
        System.out.println("Log Message Size: " + logMsgSize);
        Log.debug(new String(msg));
        Log.debug(new String(msg));
        Log.debug("Last Record Written");
    }
    
    /**
     * Test if the last log messages have been correctly rotated
     * when default log file size was reached
     */
    private void testWriteLogFileAfterRotation() throws Exception  {
        LogViewer lv = new LogViewer();
        String[] actualLog = lv.getLogEntries(lv.RMSLOG);
        RecordStore rs = RecordStore.openRecordStore(RMSAppender.LOGDBNAME, true);
        assertTrue(rs.getNumRecords()!=0);
        assertTrue(rs.getNumRecords() == actualLog.length);
        
    }
    
    private void testWriteLogFileBeforeRotation() throws Exception  {
        LogViewer lv = new LogViewer();
        String[] actualLog = lv.getLogEntries(lv.RMSLOG);
        String objectName = "["+ this.getClass().getName() + "] ";
        assertTrue(actualLog[7].endsWith("This is an ERROR Message"));
        assertTrue(actualLog[6].endsWith("This is a INFO Message"));
        assertTrue(actualLog[5].endsWith("This is a TRACE Message"));
        assertTrue(actualLog[4].endsWith("This is a DEBUG Message"));
        assertTrue(actualLog[3].endsWith(objectName + "This is an ERROR Message"));
        assertTrue(actualLog[2].endsWith(objectName + "This is a INFO Message"));
        assertTrue(actualLog[1].endsWith(objectName + "This is a TRACE Message"));
        assertTrue(actualLog[0].endsWith(objectName + "This is a DEBUG Message"));
    }
    
    /**
     * Writes some dummy log messages into the log recordstore using
     * RMSAppender
     */
    private void writeLogFile() {
        Log.setLogLevel(Log.ERROR);
        Log.error("This is an ERROR Message");
        Log.setLogLevel(Log.INFO);
        Log.info("This is a INFO Message");
        Log.setLogLevel(Log.TRACE);
        Log.trace("This is a TRACE Message");
        Log.setLogLevel(Log.DEBUG);
        Log.debug("This is a DEBUG Message");
        Log.setLogLevel(Log.ERROR);
        Log.error(this, "This is an ERROR Message");
        Log.setLogLevel(Log.INFO);
        Log.info(this, "This is a INFO Message");
        Log.setLogLevel(Log.TRACE);
        Log.trace(this, "This is a TRACE Message");
        Log.setLogLevel(Log.DEBUG);
        Log.debug(this, "This is a DEBUG Message");
    }
    
    /**
     * Writes some dummy log messages into the log recordstore using
     * RMSAppender
     */
    private int writeAndRotate() {
        int counter = 0;
        for (int i=0; i<LOGMESSAGESNUMBER; i++) {
            writeLogFile();
            counter+=8;
            System.out.println("Total number of log messages: " + counter);
        }
    System.out.println(counter + " written.");
    return counter;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -