org.echocat.nodoodle.transport.HandlerUnpackerTest.java Source code

Java tutorial

Introduction

Here is the source code for org.echocat.nodoodle.transport.HandlerUnpackerTest.java

Source

/*****************************************************************************************
 * *** BEGIN LICENSE BLOCK *****
 *
 * Version: MPL 2.0
 *
 * echocat NoDoodle, Copyright (c) 2010-2012 echocat
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * *** END LICENSE BLOCK *****
 ****************************************************************************************/

package org.echocat.nodoodle.transport;

import org.apache.commons.io.IOUtils;
import org.echocat.nodoodle.Handler;
import org.echocat.nodoodle.NodoodleInformationFactory;
import org.hamcrest.core.IsNull;
import org.junit.Test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import static org.junit.Assert.assertThat;

public class HandlerUnpackerTest {

    @Test
    public void testUnpack() throws Exception {
        final HandlerPacker packer = new HandlerPacker(new NodoodleInformationFactory());
        final HandlerUnpacker unpacker = new HandlerUnpacker(null);
        final File file = new File(System.getProperty("java.io.tmpdir"), "nodoodle/test.zip");
        file.getCanonicalFile().getParentFile().mkdirs();
        final FileOutputStream fos = new FileOutputStream(file);
        final ServletImpl servlet1 = new ServletImpl();
        // noinspection AccessingNonPublicFieldOfAnotherObject
        servlet1._test = "abc";
        servlet1.handle(null, null);
        try {
            packer.pack(servlet1, fos);
        } finally {
            IOUtils.closeQuietly(fos);
        }
        final FileInputStream fis = new FileInputStream(file);
        try {
            final HandlerUnpacker.Result result = unpacker.unpack("", fis);
            final Handler<?> servlet = result.getHandler();
            assertThat(servlet, IsNull.<Object>notNullValue());
            servlet.handle(null, null);
        } finally {
            IOUtils.closeQuietly(fis);
        }

    }

    public static class ServletImpl implements Handler<String> {

        private String _test;

        @Override
        public String handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
            System.err.println(Integer.toHexString(getClass().hashCode()) + "   " + _test + "    " + this);
            return "test";
        }

    }
}