otaconfigparsertest.java

来自「手机与服务器通过SyncML进行同步的客户端框架以及API」· Java 代码 · 共 378 行

JAVA
378
字号
/*
 * Copyright (C) 2006-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.syncml.client;

import com.funambol.syncml.spds.SyncSource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import jmunit.framework.cldc10.TestCase;

import com.funambol.util.Log;

/**
 * Testcase for OTAConfigParser
 */
public class OTAConfigParserTest extends TestCase {
    /** String for testing URL Configuration*/
    public static final String URL = "http://www.funambol.net";
    /** String for testing User Configuration*/
    public static final String USER = "user";
    /** String for testing Password Configuration*/
    public static final String PASSWORD = "password";
    /** String for testing Visible Name Configuration*/
    public static final String VISIBLENAME = "visibleName";
    /** String for testing Mail Address Configuration*/
    public static final String MAILADDRESS = "mailAddress";
    /** String for testing Remote Mail Source URI*/
    public static final String REMOTEMAILURI = "REMOTEMAILURI";
    /** String for testing Remote Contact Source URI*/
    public static final String REMOTECONTACTURI = "REMOTECONTACTURI";
    /** String for testing Remote Calendar Source URI*/
    public static final String REMOTECALENDARURI = "REMOTECALENDARURI";
    /** String for testing Remote task Source URI*/
    public static final String REMOTETASKURI = "REMOTETASKURI";
    /** String for testing Remote Note Source URI*/
    public static final String REMOTENOTEURI = "REMOTENOTEURI";
    /** String for testing Remote Briefcase Source URI*/
    public static final String REMOTEBRIEFCASEURI = "REMOTEBRIEFCASEURI";
    /** String for testing value for Format*/
    public static final String FORMAT = "FORMAT";
    //This constant won't be valorized in V1
    //public static final String LOCALURI = "LOCALURI";
    
    /** Expected sections to be parsed*/
    private static int EXPECTED_SECTION_NUMBER = 7;
    
    private ByteArrayInputStream bais;
    
    private byte[] msg;
    
    public OTAConfigMessageParser ocmp;
    private OTAConfigMessage message;

    public OTAConfigParserTest() throws Exception {
        super(1, "OTAConfig Parser Test");
    }
    
    /**
     * Configure TestCase Environment
     */
    public void setUp() throws Exception {
        Log.setLogLevel(Log.DEBUG);
        ocmp = new OTAConfigMessageParser();
        initParserTest();
    }

    /**
     * Clear TestCase Environment
     */
    public void tearDown() {
    }
    
    /**
     * Core test engine
     */
    public void test(int testNumber) throws Throwable {
        switch(testNumber) {
            case 0:
                testParseMessage();
                break;
            default:                    
                break;
        }
    }
    
    /**
     * Test Configuration value after Message parsing
     */
    private void testParseMessage() throws Exception {
        //Build complete message
        message = ocmp.parseMessage(msg);
        
        //Test message.conf container field
        assertEquals(message.getSyncUrl(), URL);
        Log.debug("Found: " + message.getSyncUrl() + " Expected: " + URL);
        assertEquals(message.getUserName(), USER);
        Log.debug("Found: " + message.getUserName() + " Expected: " + USER);
        assertEquals(message.getPassword(), PASSWORD);
        Log.debug("Found: " + message.getPassword() + " Expected: " + PASSWORD);
        assertEquals(message.getRemoteUri(message.MAIL), REMOTEMAILURI);
        Log.debug("Found: " + message.getRemoteUri(message.MAIL) 
                            + " Expected: " + REMOTEMAILURI);
        Log.debug("MAIL Section Correctly Parsed");
        assertEquals(message.getRemoteUri(message.CONTACT), REMOTECONTACTURI);
        Log.debug("Found: " + message.getRemoteUri(message.CONTACT) 
                            + " Expected: " + REMOTECONTACTURI);
        Log.debug("CONTACT Section Correctly Parsed");
        assertEquals(message.getRemoteUri(message.CALENDAR), REMOTECALENDARURI);
        Log.debug("Found: " + message.getRemoteUri(message.CALENDAR) 
                            + " Expected: " + REMOTECALENDARURI);
        Log.debug("CALENDAR Section Correctly Parsed");
        assertEquals(message.getRemoteUri(message.TASK), REMOTETASKURI);
        Log.debug("Found: " + message.getRemoteUri(message.TASK) 
                            + " Expected: " + REMOTETASKURI);
        Log.debug("TASK Section Correctly Parsed");
        assertEquals(message.getRemoteUri(message.NOTE), REMOTENOTEURI);
        Log.debug("Found: " + message.getRemoteUri(message.NOTE) 
                            + " Expected: " + REMOTENOTEURI);
        Log.debug("NOTE Section Correctly Parsed");
        assertEquals(message.getRemoteUri(message.BRIEFCASE), REMOTEBRIEFCASEURI);
        Log.debug("Found: " + message.getRemoteUri(message.BRIEFCASE) 
                            + " Expected: " + REMOTEBRIEFCASEURI);
        Log.debug("BRIEFCASE Section Correctly Parsed");
        
    }
        
    /**
     * Message builder: builds a message in byte[] format and shows its Hex Form
     * Useful to create URL for incoming SMS
     * @param msg is the message to be built
     */
    private void initParserTest() throws Exception {
        msg = buildMessage();
        
        //Print out a string useful for URL creation
        String out = bytesToHex(msg, '%');
        Log.debug(out);
    }
    
    /**
     * Build a complete message
     * @return byte[] message representation
     */
    private byte[] buildMessage() throws IOException {
        ByteArrayOutputStream msgByteArray =  new ByteArrayOutputStream();
        DataOutputStream msgData = new DataOutputStream(msgByteArray);
        // write message header %01%06%03%1F%01%B6
        msgData.write(0x01);
        msgData.write(0x06);
        msgData.write(0x03);
        msgData.write(0x1F);
        msgData.write(0x01);
        msgData.write(0xB6);
        
        // write message body
        msgData.write(buildSyncMLSection());
        msgData.write(buildMailSection());
        msgData.write(buildContactSection());
        msgData.write(buildCalendarSection());
        msgData.write(buildTaskSection());
        msgData.write(buildNoteSection());
        msgData.write(buildBriefcaseSection());
        return msgByteArray.toByteArray();
    }
    
    /**
     * Build SyncML Message Section
     * @return byte[] Section representation
     */
    private byte[] buildSyncMLSection() throws IOException {
        ByteArrayOutputStream msgByteArray =  new ByteArrayOutputStream();
        DataOutputStream msgData = new DataOutputStream(msgByteArray);
        //Section Type
        msgData.writeByte(message.SYNCML);
        //First Field length
        msgData.writeByte(URL.getBytes().length);
        //First Field Value
        msgData.write(URL.getBytes());
        //Second Field length
        msgData.writeByte(USER.getBytes().length);
        //Second Field Value
        msgData.write(USER.getBytes());
        //Third Field legth
        msgData.writeByte(PASSWORD.getBytes().length);
        //Third Field Value
        msgData.write(PASSWORD.getBytes());
        return msgByteArray.toByteArray();
    }

    /**
     * Build Mail Message Section
     * @return byte[] Section representation
     */
    private byte[] buildMailSection() throws IOException {
        ByteArrayOutputStream msgByteArray =  new ByteArrayOutputStream();
        DataOutputStream msgData = new DataOutputStream(msgByteArray);
        //Section Type
        msgData.writeByte(message.MAIL);
        //First Field length
        msgData.writeByte(REMOTEMAILURI.getBytes().length);
        //First Field Value
        msgData.write(REMOTEMAILURI.getBytes());
        //Second Field length
        msgData.writeByte(VISIBLENAME.getBytes().length);
        //Second Field Value
        msgData.write(VISIBLENAME.getBytes());
        //Third Field length
        msgData.writeByte(MAILADDRESS.getBytes().length);
        //Third Field Value
        msgData.write(MAILADDRESS.getBytes());
        
        return msgByteArray.toByteArray();
    }

    /**
     * Build Contact Message Section
     * @return byte[] Section representation
     */
    private byte[] buildContactSection() throws IOException {
        ByteArrayOutputStream msgByteArray =  new ByteArrayOutputStream();
        DataOutputStream msgData = new DataOutputStream(msgByteArray);
        //Section Type
        msgData.writeByte(message.CONTACT);
        //First Field length
        msgData.writeByte(REMOTECONTACTURI.getBytes().length);
        //First Field Value
        msgData.write(REMOTECONTACTURI.getBytes());
        //Second Field length
        msgData.writeByte(FORMAT.getBytes().length);
        //Second Field Value
        msgData.write(FORMAT.getBytes());
        
        return msgByteArray.toByteArray();
    }
    
    /**
     * Build Calendar Message Section
     * @return byte[] Section representation
     */
    private byte[] buildCalendarSection() throws IOException {
        ByteArrayOutputStream msgByteArray =  new ByteArrayOutputStream();
        DataOutputStream msgData = new DataOutputStream(msgByteArray);
        //Section Type
        msgData.writeByte(message.CALENDAR);
        //First Field length
        msgData.writeByte(REMOTECALENDARURI.getBytes().length);
        //First Field Value
        msgData.write(REMOTECALENDARURI.getBytes());
        //Second Field length
        msgData.writeByte(FORMAT.getBytes().length);
        //Second Field Value
        msgData.write(FORMAT.getBytes());
        
        return msgByteArray.toByteArray();
    }
    
    /**
     * Build Task Message Section
     * @return byte[] Section representation
     */
    private byte[] buildTaskSection() throws IOException {
        ByteArrayOutputStream msgByteArray =  new ByteArrayOutputStream();
        DataOutputStream msgData = new DataOutputStream(msgByteArray);
        //Section Type
        msgData.writeByte(message.TASK);
        //First Field length
        msgData.writeByte(REMOTETASKURI.getBytes().length);
        //First Field Value
        msgData.write(REMOTETASKURI.getBytes());
        //Second Field length
        msgData.writeByte(FORMAT.getBytes().length);
        //Second Field Value
        msgData.write(FORMAT.getBytes());
        
        return msgByteArray.toByteArray();
    }
    
    /**
     * Build Note Message Section
     * @return byte[] Section representation
     */
    private byte[] buildNoteSection() throws IOException {
        ByteArrayOutputStream msgByteArray =  new ByteArrayOutputStream();
        DataOutputStream msgData = new DataOutputStream(msgByteArray);
        //Section Type
        msgData.writeByte(message.NOTE);
        //First Field length
        msgData.writeByte(REMOTENOTEURI.getBytes().length);
        //First Field Value
        msgData.write(REMOTENOTEURI.getBytes());
        //OPEN POINT: LocalURI is unuseful for eMail Client Configuration
        //Second Field length
        //msgData.writeByte(LOCALURI.getBytes().length);
        //Second Field Value
        //msgData.write(LOCALURI.getBytes());
        
        return msgByteArray.toByteArray();
    }
    
    /**
     * Build Briefcase Message Section
     * @return byte[] Section representation
     */
    private byte[] buildBriefcaseSection() throws IOException {
        ByteArrayOutputStream msgByteArray =  new ByteArrayOutputStream();
        DataOutputStream msgData = new DataOutputStream(msgByteArray);
        //Section Type
        msgData.writeByte(message.BRIEFCASE);
        //First Field length
        msgData.writeByte(REMOTEBRIEFCASEURI.getBytes().length);
        //First Field Value
        msgData.write(REMOTEBRIEFCASEURI.getBytes());
        //OPEN POINT: LocalURI is unuseful for eMail Client Configuration
        //Second Field length
        //msgData.writeByte(LOCALURI.getBytes().length);
        //Second Field Value
        //msgData.write(LOCALURI.getBytes());
        
        return msgByteArray.toByteArray();
    }
    
    /**
     * Useful method to translate translate a byte array in HEX form
     * @param byte to be translate
     * @param delimitator is the output string separator to be used
     * @return String byte translated 
     */ 
    private static String bytesToHex(byte [] b, char delimitator) {
        StringBuffer buf = new StringBuffer("");
        for (int i=0; i< b.length;i++) {
            if (i != 0) {
                if (delimitator != 0) {
                    buf.append(delimitator);
                }
            }   
            buf.append(byteToHex(b[i]));

        }
        return buf.toString();
    }
    
    /**
     * Translate byte in HEX form
     * @param byte to be translate
     * @return String byte translated 
     */
    private static String byteToHex(byte  b) {
        // Returns hex String representation of byte b
        char hexDigit[] = {
            '0', '1', '2', '3', '4', '5', '6', '7',
                '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
        };
        char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
        return new String(array);
    }
}

⌨️ 快捷键说明

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