<commit_before>/*
 * Copyright 2008-2012 the original author or authors.
 *
 * 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 org.springframework.plugin.core;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.core.annotation.Order;
import org.springframework.plugin.core.MutablePluginRegistry;
import org.springframework.plugin.core.OrderAwarePluginRegistry;
import org.springframework.plugin.core.Plugin;
import org.springframework.plugin.core.PluginRegistry;

/**
 * Unit test for {@link OrderAwarePluginRegistry} that especially concentrates on testing ordering functionality.
 * 
 * @author <NAME>
 */
public class OrderAwarePluginRegistryUnitTest extends SimplePluginRegistryUnitTest {

	TestPlugin firstPlugin;
	TestPlugin secondPlugin;

	@Override
	@Before
	public void setUp() {

		super.setUp();

		firstPlugin = new FirstImplementation();
		secondPlugin = new SecondImplementation();
	}

	@Override
	protected OrderAwarePluginRegistry<SamplePlugin, String> getRegistry() {

		return OrderAwarePluginRegistry.create();
	}

	@Test
	public void honorsOrderOnAddPlugins() throws Exception {

		PluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry.create(Arrays.asList(firstPlugin,
				secondPlugin));
		assertOrder(registry, secondPlugin, firstPlugin);
	}

	@Test
	@Ignore
	public void assertsOrderOnAddingPlugins() throws Exception {

		MutablePluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry.create(Arrays.asList(firstPlugin));
		registry.addPlugin(secondPlugin);

		assertOrder(registry, secondPlugin, firstPlugin);
	}

	private void assertOrder(PluginRegistry<TestPlugin, String> registry, TestPlugin... plugins) {

		List<TestPlugin> result = registry.getPluginsFor(null);

		assertThat(plugins.length, is(result.size()));

		for (int i = 0; i < plugins.length; i++) {
			assertThat(result.get(i), is(result.get(i)));
		}

		assertThat(registry.getPluginFor(null), is(plugins[0]));
	}

	@Test
	public void createsRevertedRegistryCorrectly() throws Exception {

		OrderAwarePluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry.create(Arrays.asList(firstPlugin,
				secondPlugin));
		PluginRegistry<TestPlugin, String> reverse = registry.reverse();

		assertOrder(registry, secondPlugin, firstPlugin);
		assertOrder(reverse, firstPlugin, secondPlugin);
	}

	private static interface TestPlugin extends Plugin<String> {

	}

	@Order(5)
	private static class FirstImplementation implements TestPlugin {

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.springframework.plugin.core.Plugin#supports(java.lang.Object)
		 */
		public boolean supports(String delimiter) {

			return true;
		}
	}

	@Order(1)
	private static class SecondImplementation implements TestPlugin {

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.springframework.plugin.core.Plugin#supports(java.lang.Object)
		 */
		public boolean supports(String delimiter) {

			return true;
		}
	}
}<commit_msg>#1 - Added test case to show implementing Ordered is considered on proxies as well.<commit_after>/*
 * Copyright 2008-2012 the original author or authors.
 *
 * 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 org.springframework.plugin.core;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.plugin.core.OrderAwarePluginRegistry.*;

import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
 * Unit test for {@link OrderAwarePluginRegistry} that especially concentrates on testing ordering functionality.
 * 
 * @author <NAME>
 */
public class OrderAwarePluginRegistryUnitTest extends SimplePluginRegistryUnitTest {

	TestPlugin firstPlugin;
	TestPlugin secondPlugin;

	@Override
	@Before
	public void setUp() {

		super.setUp();

		firstPlugin = new FirstImplementation();
		secondPlugin = new SecondImplementation();
	}

	@Override
	protected OrderAwarePluginRegistry<SamplePlugin, String> getRegistry() {

		return OrderAwarePluginRegistry.create();
	}

	@Test
	public void honorsOrderOnAddPlugins() throws Exception {

		PluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry.create(Arrays.asList(firstPlugin,
				secondPlugin));
		assertOrder(registry, secondPlugin, firstPlugin);
	}

	@Test
	@Ignore
	public void assertsOrderOnAddingPlugins() throws Exception {

		MutablePluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry.create(Arrays.asList(firstPlugin));
		registry.addPlugin(secondPlugin);

		assertOrder(registry, secondPlugin, firstPlugin);
	}

	private void assertOrder(PluginRegistry<TestPlugin, String> registry, TestPlugin... plugins) {

		List<TestPlugin> result = registry.getPluginsFor(null);

		assertThat(plugins.length, is(result.size()));

		for (int i = 0; i < plugins.length; i++) {
			assertThat(result.get(i), is(result.get(i)));
		}

		assertThat(registry.getPluginFor(null), is(plugins[0]));
	}

	@Test
	public void createsRevertedRegistryCorrectly() throws Exception {

		OrderAwarePluginRegistry<TestPlugin, String> registry = OrderAwarePluginRegistry.create(Arrays.asList(firstPlugin,
				secondPlugin));
		PluginRegistry<TestPlugin, String> reverse = registry.reverse();

		assertOrder(registry, secondPlugin, firstPlugin);
		assertOrder(reverse, firstPlugin, secondPlugin);
	}

	/**
	 * @see #1
	 */
	@Test
	public void considersJdkProxiedOrderedImplementation() {

		ThirdImplementation plugin = new ThirdImplementation();
		TestPlugin thirdPlugin = (TestPlugin) new ProxyFactory(plugin).getProxy();

		OrderAwarePluginRegistry<TestPlugin, String> registry = create(Arrays
				.asList(firstPlugin, secondPlugin, thirdPlugin));
		assertOrder(registry, secondPlugin, thirdPlugin, firstPlugin);
		assertOrder(registry.reverse(), firstPlugin, thirdPlugin, secondPlugin);
	}

	private static interface TestPlugin extends Plugin<String> {

	}

	@Order(5)
	private static class FirstImplementation implements TestPlugin {

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.springframework.plugin.core.Plugin#supports(java.lang.Object)
		 */
		public boolean supports(String delimiter) {

			return true;
		}
	}

	@Order(1)
	private static class SecondImplementation implements TestPlugin {

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.springframework.plugin.core.Plugin#supports(java.lang.Object)
		 */
		public boolean supports(String delimiter) {

			return true;
		}
	}

	private static class ThirdImplementation implements TestPlugin, Ordered {

		@Override
		public int getOrder() {
			return 3;
		}

		@Override
		public boolean supports(String delimiter) {
			return true;
		}
	}
}
{'fraction_non_alphanumeric': 0.09832977521034786, 'fraction_numerical': 0.004395328393821424, 'mean_word_length': 4.983471074380165, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 22, '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': '2897439', 'n_tokens_mistral': 2399, 'n_tokens_neox': 2181, 'n_words': 596}