package moe.pine.bottler;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.*;
import java.util.List;

/**
 * Test for CookieStoreUtils
 * Created by pine on 2017/04/08
 */
public class CookieStoreUtilsTest {
    @Test
    public void testConstructor() {
        // Bean needs default constructor
        new CookieStoreUtils();
    }

    @Test
    public void testSaveTo() throws URISyntaxException, IOException {
        CookieManager cookieManager =
                new CookieManager(null, CookiePolicy.ACCEPT_ALL);
        CookieManager newCookieManager =
                new CookieManager(null, CookiePolicy.ACCEPT_ALL);
        CookieStore cookieStore = cookieManager.getCookieStore();
        CookieStore newCookieStore = newCookieManager.getCookieStore();

        URI uri = new URI("http://www.example.com");
        HttpCookie cookie = new HttpCookie("hello", "world");
        cookieStore.add(uri, cookie);

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        CookieStoreUtils.writeTo(cookieStore, os);
        cookieStore.removeAll();

        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        IOUtils.closeQuietly(os);
        CookieStoreUtils.readFrom(newCookieStore, is);
        IOUtils.closeQuietly(is);

        List<HttpCookie> saveCookie = newCookieStore.get(uri);
        Assert.assertEquals("hello", saveCookie.get(0).getName());
        Assert.assertEquals("world", saveCookie.get(0).getValue());
    }
}

@@ -126,113 +126,56 @@
 .io.
-ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.net.*
+*;%0Aimport java.net.*;%0Aimport java.util.ArrayList
 ;%0Aim
@@ -1567,11 +1567,634 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testReadFrom_nonHashMap() throws IOException%7B%0A        CookieManager cookieManager =%0A                new CookieManager(null, CookiePolicy.ACCEPT_ALL);%0A        CookieStore cookieStore = cookieManager.getCookieStore();%0A%0A        ByteArrayOutputStream os = new ByteArrayOutputStream();%0A        ObjectOutputStream oos = new ObjectOutputStream(os);%0A        oos.writeObject(new ArrayList%3CInteger%3E());%0A%0A        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());%0A        CookieStoreUtils.readFrom(cookieStore, is);%0A%0A        Assert.assertEquals(0, cookieStore.getCookies().size());%0A    %7D
 %0A%7D%0A

add no hashmap deserialize tests