<commit_before>package com.thoughtworks.proxy.toys.hotswap;

import com.thoughtworks.proxy.ProxyTestCase;
import static com.thoughtworks.proxy.toys.hotswap.HotSwapping.hotSwappable;
import static org.junit.Assert.*;
import org.junit.Test;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import java.util.List;
import java.util.Map;


/**
 * @author <NAME>&oslash;y
 */
public class HotSwappingTest extends ProxyTestCase {
    @Test
    public void shouldBeAbleToHotSwap() {
        List<String> firstList = new ArrayList<String>();
        firstList.add("first");

        List proxyList = hotSwappable(List.class).with(firstList).build(getFactory());
        assertTrue(proxyList.contains("first"));

        List<String> secondList = new ArrayList<String>();
        secondList.add("second");

        Swappable swappable = (Swappable) proxyList;
        assertSame(firstList, swappable.hotswap(secondList));

        assertFalse(proxyList.contains("first"));
        assertTrue(proxyList.contains("second"));
    }

    @Test
    public void shouldDiscoverCyclicReferences() {
        List firstList = new ArrayList();
        List hidingOne = hotSwappable(List.class).with(firstList).build(getFactory());
        List hidingTwo = hotSwappable(List.class).with(hidingOne).build(getFactory());
        List hidingThree = hotSwappable(List.class).with(hidingTwo).build(getFactory());

        try {
            ((Swappable) hidingOne).hotswap(hidingThree);
            fail();
        } catch (IllegalStateException e) {
            // expected
        }
    }

    @Test
    public void shouldNotHotswapRecursively() {
        List<Map<String, String>> list = singletonList(singletonMap("hello", "world"));
        List hidingList = hotSwappable(List.class).with(list).build(getFactory());
        Object shouldNotBeSwappableMap = hidingList.get(0);
        assertFalse(shouldNotBeSwappableMap instanceof Swappable);
    }

    @Test
    public void shouldWorkWithEquals() {
        List hotSwapList = hotSwappable(List.class).with(null).build(getFactory());
        assertFalse(hotSwapList.equals(new ArrayList()));
        assertTrue(hotSwapList.equals(hotSwapList));
    }

    @Test
    public void shouldWorkWithHashcode() {
        List hotSwapList = hotSwappable(List.class).with(null).build(getFactory());
        assertTrue(hotSwapList.hashCode() == hotSwapList.hashCode());
    }

    public static interface Screwdriver {
        void screw();
    }

    public static class ScredriverImpl implements Screwdriver, Serializable {
        public void screw() {
            fail("should not be called");
        }
    }

    public static class Person implements Serializable {
        public boolean wasScrewed;

        public void screw() {
            wasScrewed = true;
        }
    }

    @Test
    public void shouldForwardToObjectOfDifferentTypeIfTypeForgivingIsTrue() {<commit_msg>work on coverage<commit_after>package com.thoughtworks.proxy.toys.hotswap;

import com.thoughtworks.proxy.ProxyTestCase;
import static com.thoughtworks.proxy.toys.hotswap.HotSwapping.hotSwappable;
import static org.junit.Assert.*;
import org.junit.Test;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import java.util.List;
import java.util.Map;


/**
 * @author <NAME>&oslash;y
 */
public class HotSwappingTest extends ProxyTestCase {
    @Test
    public void shouldBeAbleToHotSwap() {
        List<String> firstList = new ArrayList<String>();
        firstList.add("first");

        List proxyList = hotSwappable(List.class).with(firstList).build(getFactory());
        assertTrue(proxyList.contains("first"));

        List<String> secondList = new ArrayList<String>();
        secondList.add("second");

        Swappable swappable = (Swappable) proxyList;
        assertSame(firstList, swappable.hotswap(secondList));

        assertFalse(proxyList.contains("first"));
        assertTrue(proxyList.contains("second"));
    }

    @Test
    public void shouldDiscoverCyclicReferences() {
        List firstList = new ArrayList();
        List hidingOne = hotSwappable(List.class).with(firstList).build(getFactory());
        List hidingTwo = hotSwappable(List.class).with(hidingOne).build(getFactory());
        List hidingThree = hotSwappable(List.class).with(hidingTwo).build(getFactory());

        try {
            ((Swappable) hidingOne).hotswap(hidingThree);
            fail();
        } catch (IllegalStateException e) {
            // expected
        }
    }

    @Test
    public void shouldNotHotswapRecursively() {
        List<Map<String, String>> list = singletonList(singletonMap("hello", "world"));
        List hidingList = hotSwappable(List.class).with(list).build(getFactory());
        Object shouldNotBeSwappableMap = hidingList.get(0);
        assertFalse(shouldNotBeSwappableMap instanceof Swappable);
    }

    @Test
    public void shouldWorkWithEquals() {
        List hotSwapList = hotSwappable(List.class).with(null).build(getFactory());
        assertFalse(hotSwapList.equals(new ArrayList()));
        assertTrue(hotSwapList.equals(hotSwapList));
    }

    @Test
    public void shouldWorkWithHashcode() {
        List hotSwapList = hotSwappable(List.class).with(null).build(getFactory());
        assertTrue(hotSwapList.hashCode() == hotSwapList.hashCode());
    }

    public static interface Screwdriver {
        void screw();
    }

    public static class ScrewdriverImpl implements Screwdriver, Serializable {
        public void screw() {
            fail("should not be called");
        }
    }

    public static class Person implements Serializable {
        public boolean wasScrewed;

        public void screw() {
            wasScrewed = true;
        }
    }

    @Test
    public void shouldForwardToObjectOfDifferentTypeIfTypeForgivingIsTrue() {
{'fraction_non_alphanumeric': 0.11275476660092044, 'fraction_numerical': 0.0003287310979618672, 'mean_word_length': 3.312544294826364, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6544407', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1708, 'n_words': 324}