<commit_before>/*
 * Copyright 2015 agwlvssainokuni
 *
 * 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.
 */

package cherry.foundation.invoker;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.fasterxml.jackson.databind.ObjectMapper;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:config/applicationContext-test.xml")
public class InvokerTest implements InitializingBean {

	@Autowired
	private ApplicationContext appCtx;

	@Autowired
	@Qualifier("objectMapper")
	private ObjectMapper objectMapper;

	private Invoker invoker;

	@Override
	public void afterPropertiesSet() {
		InvokerImpl impl = new InvokerImpl();
		impl.setApplicationContext(appCtx);
		impl.setObjectMapper(objectMapper);
		invoker = impl;
	}

	@Test
	public void testNoArgNoRet() throws Exception {
		assertEquals("null", invoker.invoke(null, ToBeInvoked.class.getName(), "method0", -1, 0, null, null));
		assertEquals("null",
				invoker.invoke("toBeInvokedImpl", ToBeInvoked.class.getName(), "method0", -1, 0, null, null));
	}

	@Test
	public void testPrimitive() throws Exception {
		assertEquals("579",
				invoker.invoke(null, ToBeInvoked.class.getName(), "method1", -1, 0, asList("123", "456"), null));
	}

	@Test
	public void testLong() throws Exception {
		assertEquals("579",
				invoker.invoke(null, ToBeInvoked.class.getName(), "method2", -1, 0, asList("123", "456"), null));
		assertEquals("null",
				invoker.invoke(null, ToBeInvoked.class.getName(), "method2", -1, 0, asList(null, "456"), null));
		assertEquals("null", invoker.invoke(null, ToBeInvoked.class.getName(), "method2", -1, 0, asList("123"), null));
		assertEquals("null", invoker.invoke(null, ToBeInvoked.class.getName(), "method2", -1, 0, null, null));
	}

	@Test
	public void testJodaTime() throws Exception {
		assertEquals(
				"\"2015-01-23T12:34:56.000\"",
				invoker.invoke(null, ToBeInvoked.class.getName(), "method3", -1, 0,
						asList("\"2015-01-23\"", "\"12:34:56\""), null));
	}

	@Test
	public void testFlatDto() throws Exception {
		assertEquals(
				"{\"val1\":68,\"val2\":112}",
				invoker.invoke(null, ToBeInvoked.class.getName(), "method4", -1, 0,
						asList("{\"val1\":12,\"val2\":34}", "{\"val1\":56,\"val2\":78}"),
						asList(ToBeInvoked.Dto1.class.getName())));
	}

	@Test
	public void testNestedDto() throws Exception {
		assertEquals("{\"val1\":{\"val1\":6,\"val2\":8},\"val2\":{\"val1\":10,\"val2\":12}}", invoker.invoke(
				null,
				ToBeInvoked.class.getName(),
				"method5",
				-1,
				0,
				asList("{\"val1\":{\"val1\":1,\"val2\":2},\"val2\":{\"val1\":3,\"val2\":4}}",
						"{\"val1\":{\"val1\":5,\"val2\":6},\"val2\":{\"val1\":7,\"val2\":8}}"), null));
	}

	@Test
	public void testMethodIndex() throws Exception {
		assertEquals("-1", invoker.invoke(null, ToBeInvoked.class.getName(), "method6", -1, 0, asList("1", "2"), null));
		assertEquals("1", invoker.invoke(null, ToBeInvoked.class.getName(), "method6", -1, 1, asList("1", "2"), null));<commit_msg>foundation: 汎用起動機能<commit_after>/*
 * Copyright 2015 agwlvssainokuni
 *
 * 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.
 */

package cherry.foundation.invoker;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.lang.reflect.Method;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.fasterxml.jackson.databind.ObjectMapper;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:config/applicationContext-test.xml")
public class InvokerTest implements InitializingBean {

	@Autowired
	private ApplicationContext appCtx;

	@Autowired
	@Qualifier("objectMapper")
	private ObjectMapper objectMapper;

	private Invoker invoker;

	@Override
	public void afterPropertiesSet() {
		InvokerImpl impl = new InvokerImpl();
		impl.setApplicationContext(appCtx);
		impl.setObjectMapper(objectMapper);
		invoker = impl;
	}

	@Test
	public void testNoArgNoRet() throws Exception {
		assertEquals("null", invoker.invoke(null, ToBeInvoked.class.getName(), "method0", -1, 0, null, null));
		assertEquals("null",
				invoker.invoke("toBeInvokedImpl", ToBeInvoked.class.getName(), "method0", -1, 0, null, null));
	}

	@Test
	public void testPrimitive() throws Exception {
		assertEquals("579",
				invoker.invoke(null, ToBeInvoked.class.getName(), "method1", -1, 0, asList("123", "456"), null));
	}

	@Test
	public void testLong() throws Exception {
		assertEquals("579",
				invoker.invoke(null, ToBeInvoked.class.getName(), "method2", -1, 0, asList("123", "456"), null));
		assertEquals("null",
				invoker.invoke(null, ToBeInvoked.class.getName(), "method2", -1, 0, asList(null, "456"), null));
		assertEquals("null", invoker.invoke(null, ToBeInvoked.class.getName(), "method2", -1, 0, asList("123"), null));
		assertEquals("null", invoker.invoke(null, ToBeInvoked.class.getName(), "method2", -1, 0, null, null));
	}

	@Test
	public void testJodaTime() throws Exception {
		assertEquals(
				"\"2015-01-23T12:34:56.000\"",
				invoker.invoke(null, ToBeInvoked.class.getName(), "method3", -1, 0,
						asList("\"2015-01-23\"", "\"12:34:56\""), null));
	}

	@Test
	public void testFlatDto() throws Exception {
		assertEquals(
				"{\"val1\":68,\"val2\":112}",
				invoker.invoke(null, ToBeInvoked.class.getName(), "method4", -1, 0,
						asList("{\"val1\":12,\"val2\":34}", "{\"val1\":56,\"val2\":78}"),
						asList(ToBeInvoked.Dto1.class.getName())));
	}

	@Test
	public void testNestedDto() throws Exception {
		assertEquals("{\"val1\":{\"val1\":6,\"val2\":8},\"val2\":{\"val1\":10,\"val2\":12}}", invoker.invoke(
				null,
				ToBeInvoked.class.getName(),
				"method5",
				-1,
				0,
				asList("{\"val1\":{\"val1\":1,\"val2\":2},\"val2\":{\"val1\":3,\"val2\":4}}",
						"{\"val1\":{\"val1\":5,\"val2\":6},\"val2\":{\"val1\":7,\"val2\":8}}"), null));
	}

	@Test
	public void testMethodIndex() throws Exception {
		List<Method> list = invoker.resolveMethod(ToBeInvoked.class.getName(), "method6", -1);
		int m0;
		int m1;
		if (list.get(0).getReturnType() == Long.TYPE) {
			m0 = 0;
			m1 = 1;
		} else {
			m0 = 1;
			m1 = 0;
		}
		assertEquals("-1", invoker.invoke(null, ToBeInvoked.class.getName(), "method6", -1, m0, asList("1", "2"), null));
		assertEquals("1", invoker.invoke(null, ToBeInvoked.class.getName(), "method6", -1, m1, asList("1", "2"), null));
{'fraction_non_alphanumeric': 0.17204430532145437, 'fraction_numerical': 0.0400915001203949, 'mean_word_length': 4.82538569424965, 'pattern_counts': {'":': 48, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '1792888', 'n_tokens_mistral': 3017, 'n_tokens_neox': 2719, 'n_words': 609}