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

📄 composerdecomposerreadertest.java

📁 JSMPP Examples to send and receive sms from java via smpp protocol.
💻 JAVA
字号:
/*
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package org.jsmpp.util;

import static org.testng.Assert.*;

import org.jsmpp.PDUStringException;
import org.jsmpp.SMPPConstant;
import org.jsmpp.bean.BindResp;
import org.jsmpp.bean.BindType;
import org.testng.annotations.Test;

/**
 * Test composing PDU, decompose and read.
 * @author uudashr
 *
 */
public class ComposerDecomposerReaderTest {
    private static final boolean DEBUG = false;
    
    @Test(groups="checkintest")
    public void lowLevel() {
        byte[] b = null;
        String systemId = "smsc";
        
        PDUByteBuffer buf = new PDUByteBuffer(BindType.BIND_TRX.responseCommandId(), 0, 1);
        buf.append(systemId);
        b = buf.toBytes();
        assertEquals(b.length, 16 + systemId.length() + 1);
        printLog("Length of bytes : " + b.length);
        
        
        SequentialBytesReader reader = new SequentialBytesReader(b);
        assertEquals(b.length, reader.remainBytesLength());
        
        int commandLength =  reader.readInt();
        assertEquals(commandLength, b.length);
        assertEquals(4, reader.cursor);
        assertEquals(b.length - 4, reader.remainBytesLength());
        
        int commandId =  reader.readInt();
        assertEquals(commandId, BindType.BIND_TRX.responseCommandId());
        assertEquals(8, reader.cursor);
        assertEquals(b.length - 8, reader.remainBytesLength());
        
        int commandStatus = reader.readInt();
        assertEquals(commandStatus, SMPPConstant.STAT_ESME_ROK);
        assertEquals(12, reader.cursor);
        assertEquals(b.length - 12, reader.remainBytesLength());
        
        int sequenceNumber = reader.readInt();
        assertEquals(sequenceNumber, 1);
        assertEquals(16, reader.cursor);
        assertEquals(b.length - 16, reader.remainBytesLength());
        
        String readedSystemId = reader.readCString();
        assertEquals(readedSystemId, systemId);
    }
    
    @Test(groups="checkintest")
    public void highLevel() {
        PDUComposer composer = new DefaultComposer();
        PDUDecomposer decomposer = new DefaultDecomposer();
        
        byte[] b = null;
        String systemId = "smsc";
        BindType bindType = BindType.BIND_TRX;
        try {
            b = composer.bindResp(bindType.responseCommandId(), 1, systemId);
            assertEquals(b.length, 16 + systemId.length() + 1);
            printLog("Length of bytes : " + b.length);
        } catch (PDUStringException e) {
            fail("Failed composing bind response", e);
        }
        
        try {
            BindResp resp = decomposer.bindResp(b);
            assertEquals(resp.getCommandLength(), b.length);
            assertEquals(resp.getCommandId(), bindType.responseCommandId());
            assertEquals(resp.getCommandStatus(), SMPPConstant.STAT_ESME_ROK);
            assertEquals(resp.getSequenceNumber(), 1);
            assertEquals(resp.getSystemId(), systemId);
        } catch (PDUStringException e) {
            fail("Failed decomposing bind response", e);
        }
    }
    
    private static void printLog(String message) {
        if (DEBUG) {
            System.out.println(message);
        }
    }
}

⌨️ 快捷键说明

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