mailmonk.client.MessageSerializerTests.java Source code

Java tutorial

Introduction

Here is the source code for mailmonk.client.MessageSerializerTests.java

Source

/*
* Copyright 2009-2012 Alma Media Corporation
*
* 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 mailmonk.client;

import java.util.HashMap;
import java.util.Map;

import org.json.*;

import junit.framework.*;

/**
 * @author Kai Kousa
 * */
public class MessageSerializerTests extends TestCase {

    public void testSerialize() throws JSONException {
        Message msg = new Message();
        msg.setFrom("jane.doe@example.com");
        msg.addToRecipient("john.doe@example.com");
        msg.setSubject("test");
        msg.setTemplateId("helloworld");

        Map<String, String> dataMap = new HashMap<String, String>();
        dataMap.put("name", "John Doe");
        msg.setData(dataMap);

        String jsonString = MessageSerializer.serialize(msg);
        JSONObject json = new JSONObject(jsonString);
        JSONObject data = json.getJSONObject("data");

        assertEquals("John Doe", (String) data.get("name"));
    }

    public void testSerialize_messageData() throws JSONException {
        Message msg = new Message();
        msg.setFrom("jane.doe@example.com");
        msg.addToRecipient("john.doe@example.com");
        msg.setSubject("test");
        msg.setTemplateId("helloworld");

        msg.setData(new MessageData("John Doe"));

        String jsonString = MessageSerializer.serialize(msg);
        JSONObject json = new JSONObject(jsonString);
        JSONObject data = json.getJSONObject("data");

        assertEquals("John Doe", (String) data.get("name"));
    }

    public void testSerialize_messageSubClass() throws JSONException {
        MessageSubClass msg = new MessageSubClass("John Doe");
        msg.setFrom("jane.doe@example.com");
        msg.addToRecipient("john.doe@example.com");
        msg.setSubject("test");
        msg.setTemplateId("helloworld");

        String jsonString = MessageSerializer.serialize(msg);
        JSONObject json = new JSONObject(jsonString);
        JSONObject data = json.getJSONObject("data");

        assertEquals("John Doe", (String) data.get("name"));
    }

    class MessageData {

        private String name;

        public MessageData(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

    }

    class MessageSubClass extends Message {

        private String name;

        public MessageSubClass(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public Object getData() {
            return this;
        }

        public void setData(Object data) {
        }

    }

}