<commit_before>/*
 * 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.hobsoft.symmetry.taglib;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.jsp.JspException;

import org.hobsoft.symmetry.Reflector;
import org.hobsoft.symmetry.ReflectorException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockPageContext;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
 * Tests {@code ComponentTag}.
 */
public class ComponentTagTest
{
	// ----------------------------------------------------------------------------------------------------------------
	// types
	// ----------------------------------------------------------------------------------------------------------------

	private static class DummyComponent
	{
		// dummy type
	}

	// ----------------------------------------------------------------------------------------------------------------
	// fields
	// ----------------------------------------------------------------------------------------------------------------

	private ExpectedException thrown = ExpectedException.none();

	private MockPageContext context;
	
	private ComponentTag tag;
	
	// ----------------------------------------------------------------------------------------------------------------
	// JUnit methods
	// ----------------------------------------------------------------------------------------------------------------

	@Before
	public void setUp()
	{
		context = new MockPageContext();
		
		tag = new ComponentTag();
		tag.setJspContext(context);
	}

	@Rule
	public ExpectedException getThrown()
	{
		return thrown;
	}

	// ----------------------------------------------------------------------------------------------------------------
	// tests
	// ----------------------------------------------------------------------------------------------------------------

	@Test
	public void doTagInvokesReflector() throws JspException, IOException, ReflectorException
	{
		DummyComponent component = new DummyComponent();
		context.setAttribute("x", component);
		tag.setName("x");
		
		Reflector<DummyComponent> reflector = mock(Reflector.class);
		context.setAttribute("y", reflector);
		tag.setReflectorName("y");
		
		tag.doTag();
		
		verify(reflector).reflect(component, context.getOut());
	}
	
	@Test
	public void doTagWritesHtml() throws JspException, IOException, ReflectorException
	{
		context.setAttribute("x", new DummyComponent());
		tag.setName("x");

		Reflector<DummyComponent> reflector = mock(Reflector.class);
		doAnswer(write(1, "z")).when(reflector).reflect(any(DummyComponent.class), any(Writer.class));
		context.setAttribute("y", reflector);
		tag.setReflectorName("y");
		
		tag.doTag();
		
		assertThat(getResponse().getContentAsString(), is("z"));
	}
	
	@Test
	public void doTagWhenIOExceptionThrowsException() throws JspException, IOException, ReflectorException
	{
		context.setAttribute("x", new DummyComponent());
		tag.setName("x");
		
		Reflector<DummyComponent> reflector = mock(Reflector.class);
		IOException exception = new IOException();
		doThrow(exception).when(reflector).reflect(any(DummyComponent.class), any(Writer.class));
		context.setAttribute("y", reflector);
		tag.setReflectorName("y");

		thrown.expect(is(exception));
		
		tag.doTag();
	}
	
	@Test
	public void setNameSetsProperty()
	{
		tag.setName("x");
		
		assertThat(tag.getName(), is("x"));
	}
	
	@Test
	public void setReflectorNameSetsProperty()
	{
		tag.setReflectorName("x");
		
		assertThat(tag.getReflectorName(), is("x"));
	}
	
	// ----------------------------------------------------------------------------------------------------------------
	// private methods
	// ----------------------------------------------------------------------------------------------------------------

	private MockHttpServletResponse getResponse()
	{
		return (MockHttpServletResponse) context.getResponse();
	}

	private static Answer<Object> write(final int writerIndex, final String string)
	{
		return new Answer<Object>()
		{
			@Override
			public Object answer(InvocationOnMock invocation) throws IOException
			{
				Writer writer = invocation.getArgumentAt(writerIndex, Writer.class);
				writer.write(string);
				return null;
			}
		};
	}
}
<commit_msg>Simplified ComponentTag tests with convenience method<commit_after>/*
 * 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.hobsoft.symmetry.taglib;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.jsp.JspException;

import org.hobsoft.symmetry.Reflector;
import org.hobsoft.symmetry.ReflectorException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockPageContext;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
 * Tests {@code ComponentTag}.
 */
public class ComponentTagTest
{
	// ----------------------------------------------------------------------------------------------------------------
	// types
	// ----------------------------------------------------------------------------------------------------------------

	private static class DummyComponent
	{
		// dummy type
	}

	// ----------------------------------------------------------------------------------------------------------------
	// fields
	// ----------------------------------------------------------------------------------------------------------------

	private ExpectedException thrown = ExpectedException.none();

	private MockPageContext context;
	
	private ComponentTag tag;
	
	// ----------------------------------------------------------------------------------------------------------------
	// JUnit methods
	// ----------------------------------------------------------------------------------------------------------------

	@Before
	public void setUp()
	{
		context = new MockPageContext();
		
		tag = new ComponentTag();
		tag.setJspContext(context);
	}

	@Rule
	public ExpectedException getThrown()
	{
		return thrown;
	}

	// ----------------------------------------------------------------------------------------------------------------
	// tests
	// ----------------------------------------------------------------------------------------------------------------

	@Test
	public void doTagInvokesReflector() throws JspException, IOException, ReflectorException
	{
		DummyComponent component = new DummyComponent();
		Reflector<DummyComponent> reflector = mock(Reflector.class);
		
		doTag(component, reflector);
		
		verify(reflector).reflect(component, context.getOut());
	}
	
	@Test
	public void doTagWritesHtml() throws JspException, IOException, ReflectorException
	{
		Reflector<DummyComponent> reflector = mock(Reflector.class);
		doAnswer(write(1, "z")).when(reflector).reflect(any(DummyComponent.class), any(Writer.class));
		
		doTag(new DummyComponent(), reflector);
		
		assertThat(getResponse().getContentAsString(), is("z"));
	}
	
	@Test
	public void doTagWhenIOExceptionThrowsException() throws JspException, IOException, ReflectorException
	{
		Reflector<DummyComponent> reflector = mock(Reflector.class);
		IOException exception = new IOException();
		doThrow(exception).when(reflector).reflect(any(DummyComponent.class), any(Writer.class));

		thrown.expect(is(exception));

		doTag(new DummyComponent(), reflector);
	}
	
	@Test
	public void setNameSetsProperty()
	{
		tag.setName("x");
		
		assertThat(tag.getName(), is("x"));
	}
	
	@Test
	public void setReflectorNameSetsProperty()
	{
		tag.setReflectorName("x");
		
		assertThat(tag.getReflectorName(), is("x"));
	}
	
	// ----------------------------------------------------------------------------------------------------------------
	// private methods
	// ----------------------------------------------------------------------------------------------------------------

	private <T> void doTag(T component, Reflector<T> reflector) throws JspException, IOException
	{
		context.setAttribute("_component", component);
		tag.setName("_component");
		
		context.setAttribute("_reflector", reflector);
		tag.setReflectorName("_reflector");
		
		tag.doTag();
	}
	
	private MockHttpServletResponse getResponse()
	{
		return (MockHttpServletResponse) context.getResponse();
	}

	private static Answer<Object> write(final int writerIndex, final String string)
	{
		return new Answer<Object>()
		{
			@Override
			public Object answer(InvocationOnMock invocation) throws IOException
			{
				Writer writer = invocation.getArgumentAt(writerIndex, Writer.class);
				writer.write(string);
				return null;
			}
		};
	}
}

{'fraction_non_alphanumeric': 0.3046800382043935, 'fraction_numerical': 0.0009551098376313276, 'mean_word_length': 1.801230604601391, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5833009', 'n_tokens_mistral': 2694, 'n_tokens_neox': 2514, 'n_words': 595}