<commit_before>package com.sys1yagi.indirectinjector;

import junit.framework.TestCase;

import java.lang.ref.WeakReference;

import javax.inject.Inject;

public class IndirectInjectorTest extends TestCase {

    interface Listener {

        public void onFinish();
    }

    public static class Target {

        @Inject
        private Integer mNo = -1;

        @Inject
        private Listener mListener;

        public Target(Object context) {
            IndirectInjector.inject(context, this);
        }

        public Integer getNo() {
            return mNo;
        }

        public Listener getListener() {
            return mListener;
        }
    }

    public void testInject() {
        Object context = new Object();
        Integer dependency = new Integer(10);
        IndirectInjector.addDependency(context, dependency);

        Target target = new Target(context);

        assertEquals(dependency, target.getNo());
    }

    public void testInjectInterface() {
        Object context = new Object();
        Listener dependency = new Listener() {
            @Override
            public void onFinish() {

            }
        };
        IndirectInjector.addDependency(context, dependency);
        Target target = new Target(context);

        assertEquals(dependency, target.getListener());
    }

    public void testLeak() {
        Object context = new Object();
        Listener dependency = new Listener() {
            @Override
            public void onFinish() {

            }
        };

        WeakReference<Object> reference = new WeakReference<Object>(context);
        WeakReference<Object> reference2 = new WeakReference<Object>(dependency);

        IndirectInjector.addDependency(context, dependency);
        Target target = new Target(context);

        assertEquals(dependency, target.getListener());

        context = null;
        dependency = null;
        target.mListener = null;
        System.gc();
        assertNull(reference.get());
        assertNull(reference2.get());
    }
}<commit_msg>added test for strong ref leak<commit_after>package com.sys1yagi.indirectinjector;

import junit.framework.TestCase;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;

import javax.inject.Inject;

public class IndirectInjectorTest extends TestCase {

    interface Listener {

        public void onFinish();
    }

    public static class Target {

        @Inject
        private Integer mNo = -1;

        @Inject
        private Listener mListener;

        public Target(Object context) {
            IndirectInjector.inject(context, this);
        }

        public Integer getNo() {
            return mNo;
        }

        public Listener getListener() {
            return mListener;
        }
    }

    public void testInject() {
        Object context = new Object();
        Integer dependency = new Integer(10);
        IndirectInjector.addDependency(context, dependency);

        Target target = new Target(context);

        assertEquals(dependency, target.getNo());
    }

    public void testInjectInterface() {
        Object context = new Object();
        Listener dependency = new Listener() {
            @Override
            public void onFinish() {

            }
        };
        IndirectInjector.addDependency(context, dependency);
        Target target = new Target(context);

        assertEquals(dependency, target.getListener());
    }

    public void testLeak() {
        Object context = new Object();
        Listener dependency = new Listener() {
            @Override
            public void onFinish() {

            }
        };

        WeakReference<Object> reference = new WeakReference<Object>(context);
        WeakReference<Object> reference2 = new WeakReference<Object>(dependency);

        IndirectInjector.addDependency(context, dependency);
        Target target = new Target(context);

        assertEquals(dependency, target.getListener());

        context = null;
        dependency = null;
        target.mListener = null;
        System.gc();
        assertNull(reference.get());
        assertNull(reference2.get());
    }

    public void testStrongRef() throws Exception {
        Object context = new Object();
        Object dependency = new Object();

        final int initialCount = getStrongRefCount();

        IndirectInjector.addDependency(context, dependency, true);

        assertEquals(initialCount + 1, getStrongRefCount());

        context = null;
        System.gc();
        IndirectInjector.sweep();

        assertEquals(initialCount, getStrongRefCount());
    }

    private static int getStrongRefCount() throws Exception {
        final Field listField = IndirectInjector.class.getDeclaredField("STRONG_REFERENCE_LIST");
        assert (listField.getModifiers() & Modifier.STATIC) != 0;
        try {
            listField.setAccessible(true);

            final List<?> list = (List<?>) listField.get(null);
            return list.size();
        } finally {
            listField.setAccessible(false);
        }
    }
}
{'fraction_non_alphanumeric': 0.09350699844479005, 'fraction_numerical': 0.0027216174183514776, 'mean_word_length': 2.2481060606060606, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '144281', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1266, 'n_words': 321}