package pl.designpatterns.structural.flyweight;

import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;

public class InsectFlyweightFactoryTest {

    private static final Logger logger = Logger.getLogger(InsectFlyweightFactoryTest.class);

    @Test
    public void shouldFlyweightInsects() {
        // given
        InsectFlyweightFactory flyweight = new InsectFlyweightFactory();

        // when
        Insectable locust = flyweight.getInsect(InsectType.LOCUST, ExtrinsicState.FROST);
        Insectable cockroach = flyweight.getInsect(InsectType.COCKROACH, ExtrinsicState.NEARLY_EXTINCT);
        logger.info(locust + System.lineSeparator() + cockroach);
        logger.info("Flyweight factory size: " + flyweight.getInsectsNo());

        // then
        Assert.assertEquals(locust, flyweight.getInsect(InsectType.LOCUST, ExtrinsicState.EMPTY));
        Assert.assertEquals(cockroach, flyweight.getInsect(InsectType.COCKROACH, ExtrinsicState.EMPTY));
        Assert.assertTrue(locust.getIntrinsicState() == flyweight.getInsect(InsectType.LOCUST, ExtrinsicState.EMPTY).getIntrinsicState());
        Assert.assertTrue(cockroach.getIntrinsicState() == flyweight.getInsect(InsectType.COCKROACH, ExtrinsicState.EMPTY).getIntrinsicState());
    }

}
