<commit_before>/*-
 * #%L
 * Slice - Core
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2012 Cognifide Limited
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.cognifide.slice.core.internal.provider;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

import com.cognifide.slice.api.context.ContextFactory;
import com.cognifide.slice.api.context.ContextScope;
import com.cognifide.slice.api.provider.ClassToKeyMapper;
import com.cognifide.slice.core.internal.module.JcrModule;
import com.cognifide.slice.core.internal.module.LinkModule;
import com.cognifide.slice.core.internal.module.SliceModule;
import com.cognifide.slice.core.internal.module.SliceResourceModule;
import com.cognifide.slice.core.internal.module.SlingModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;

@RunWith(MockitoJUnitRunner.class)
public class SliceClassToKeyMapperTest {
	private static final String BUNDLE_NAME_FILTER = "com\\.cognifide\\.test\\.webapp\\..*";

	private static final String BASE_PACKAGE = "com.cognifide.test";

	private Injector injector;

	@Mock
	private BundleContext bundleContext;

	@Mock
	private Bundle bundle;

	@Mock
	private ContextScope contextScope;
	
	private ClassToKeyMapper mapper;

	@Before
	public void setUp() throws ClassNotFoundException {
		when(bundleContext.getBundles()).thenReturn(new Bundle[0]);
		when(bundle.loadClass(ClassToKeyMapper.class.getCanonicalName())).thenReturn(SliceClassToKeyMapper.class);
		
		List<Module> modules = new ArrayList<Module>();
		modules.add(new SliceModule(contextScope, bundle));
		modules.add(new SlingModule(contextScope));
		modules.add(new JcrModule());
		modules.add(new LinkModule());
		modules.add(new SliceResourceModule(bundleContext, BUNDLE_NAME_FILTER, BASE_PACKAGE));

		injector = Guice.createInjector(modules);
		mapper = injector.getInstance(ClassToKeyMapper.class);
	}

	@Test
	public void testGetKeyForSimpleType() {
		// use type not bound in a module - but with an obvious constructor 
		testForClassName(ClassToKeyMapper.class);
	}

	@Test
	public void testGetKeyForKnownBinding() {
		// use interface bound in a module - but with a simple constructor so it passes mocks
		testForClassName(ContextFactory.class);
	}

	private void testForClassName(Class<?> testedClass) {
		String className = testedClass.getName();
		Object instance1 = injector.getInstance(testedClass);
		assertNotNull("instance of " + className + " fetched by key cannot be null", instance1);

		Key<?> key = mapper.getKey(className);
		assertNotNull("key for " + className + " cannot be null", key);

		Object instance2 = injector.getInstance(key);
		assertNotNull("instance of " + className + " fetched by type cannot be null", instance2);

		assertEquals("Expected same class, never mind the method", instance1.getClass(), instance2.getClass());
	}<commit_msg>Added test for internal caching<commit_after>/*-
 * #%L
 * Slice - Core
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2012 Cognifide Limited
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.cognifide.slice.core.internal.provider;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

import com.cognifide.slice.api.context.ContextFactory;
import com.cognifide.slice.api.context.ContextScope;
import com.cognifide.slice.api.provider.ClassToKeyMapper;
import com.cognifide.slice.core.internal.module.JcrModule;
import com.cognifide.slice.core.internal.module.LinkModule;
import com.cognifide.slice.core.internal.module.SliceModule;
import com.cognifide.slice.core.internal.module.SliceResourceModule;
import com.cognifide.slice.core.internal.module.SlingModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;

@RunWith(MockitoJUnitRunner.class)
public class SliceClassToKeyMapperTest {
	private static final String BUNDLE_NAME_FILTER = "com\\.cognifide\\.test\\.webapp\\..*";

	private static final String BASE_PACKAGE = "com.cognifide.test";

	private Injector injector;

	@Mock
	private BundleContext bundleContext;

	@Mock
	private Bundle bundle;

	@Mock
	private ContextScope contextScope;

	private ClassToKeyMapper mapper;

	@Before
	public void setUp() throws ClassNotFoundException {
		when(bundleContext.getBundles()).thenReturn(new Bundle[0]);
		when(bundle.loadClass(ClassToKeyMapper.class.getCanonicalName())).thenReturn(
				SliceClassToKeyMapper.class);

		List<Module> modules = new ArrayList<Module>();
		modules.add(new SliceModule(contextScope, bundle));
		modules.add(new SlingModule(contextScope));
		modules.add(new JcrModule());
		modules.add(new LinkModule());
		modules.add(new SliceResourceModule(bundleContext, BUNDLE_NAME_FILTER, BASE_PACKAGE));

		injector = Guice.createInjector(modules);
		mapper = injector.getInstance(ClassToKeyMapper.class);
	}

	@Test
	public void testGetKeyForSimpleType() {
		// use type not bound in a module - but with an obvious constructor
		testForClassName(ClassToKeyMapper.class);
	}

	@Test
	public void testGetKeyForKnownBinding() {
		// use interface bound in a module - but with a simple constructor so it passes mocks
		testForClassName(ContextFactory.class);
	}

	@Test
	public void testInternalCaching() throws ClassNotFoundException {
		String className = ClassToKeyMapper.class.getName();

		// known class
		Key<?> key = mapper.getKey(className);
		Mockito.verify(bundle, Mockito.times(0)).loadClass(className);

		// some unknown class
		when(bundle.loadClass(SliceClassToKeyMapperTest.class.getCanonicalName())).thenReturn(
				SliceClassToKeyMapperTest.class);
		className = SliceClassToKeyMapperTest.class.getName();
		key = mapper.getKey(className);
		Key<?> secondInvocationKey = mapper.getKey(className);
		Mockito.verify(bundle, Mockito.times(1)).loadClass(className);
		assertEquals("Expecting the same key returned", key, secondInvocationKey);
	}

	private void testForClassName(Class<?> testedClass) {
		String className = testedClass.getName();
		Object instance1 = injector.getInstance(testedClass);
		assertNotNull("instance of " + className + " fetched by key cannot be null", instance1);

		Key<?> key = mapper.getKey(className);
		assertNotNull("key for " + className + " cannot be null", key);

		Object instance2 = injector.getInstance(key);
		assertNotNull("instance of " + className + " fetched by type cannot be null", instance2);

		assertEquals("Expected same class, never mind the method", instance1.getClass(), instance2.getClass());
	}
{'fraction_non_alphanumeric': 0.10698898994734322, 'fraction_numerical': 0.003829583532790809, 'mean_word_length': 5.172082717872969, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3442057', 'n_tokens_mistral': 2584, 'n_tokens_neox': 2474, 'n_words': 642}