<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.microbrowser;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.hobsoft.microbrowser.support.FakeHypermediaContainer;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static java.util.Arrays.asList;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;

/**
 * Tests {@code AbstractHypermediaContainer}.
 */
public class AbstractHypermediaContainerTest
{
	// ----------------------------------------------------------------------------------------------------------------
	// fields
	// ----------------------------------------------------------------------------------------------------------------

	private AbstractHypermediaContainer container;
	
	private ExpectedException thrown = ExpectedException.none();
	
	// ----------------------------------------------------------------------------------------------------------------
	// test methods
	// ----------------------------------------------------------------------------------------------------------------

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

	@Test
	public void getLinkReturnsFirstLink()
	{
		final Link link = mock(Link.class);
		container = new FakeHypermediaContainer()
		{
			@Override
			public List<Link> getLinks(String rel)
			{
				return "x".equals(rel) ? asList(link, mock(Link.class)) : null;
			}
		};
		
		assertThat(container.getLink("x"), is(link));
	}
	
	@Test
	public void getLinkWhenNotFoundThrowsException()
	{
		container = new FakeHypermediaContainer()
		{
			@Override
			public List<Link> getLinks(String rel)
			{
				return "x".equals(rel) ? Collections.<Link>emptyList() : null;
			}
		};
		
		thrown.expect(LinkNotFoundException.class);
		thrown.expectMessage("x");
		
		container.getLink("x");
	}
	
	@Test
	public void getFormReturnsForm()
	{
		final Form form = mock(Form.class);
		container = new FakeHypermediaContainer()
		{
			@Override
			protected Form newForm(String name)
			{
				return "x".equals(name) ? form : null;
			}
		};
		
		assertThat(container.getForm("x"), is(form));
	}
	
	@Test
	public void getFormCachesForm()
	{
		Form form = mock(Form.class);
		final Iterator<Form> forms = asList(form, mock(Form.class)).iterator();
		container = new FakeHypermediaContainer()
		{
			@Override
			protected Form newForm(String name)
			{
				return "x".equals(name) ? forms.next() : null;
			}
		};
		
		container.getForm("x");
		assertThat(container.getForm("x"), is(form));
	}
	
	@Test
	public void getFormWhenNotFoundThrowsException()
	{
		container = new FakeHypermediaContainer()
		{
			@Override
			protected Form newForm(String name)
			{
				if ("x".equals(name))
				{
					throw new FormNotFoundException(name);
				}
				
				return null;
			}
		};
		
		thrown.expect(FormNotFoundException.class);
		thrown.expectMessage("x");
		
		container.getForm("x");
	}
}
<commit_msg>Use Mockito where possible in AbstractHypermediaContainerTest<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.microbrowser;

import java.util.Collections;
import java.util.Iterator;

import org.hobsoft.microbrowser.support.FakeHypermediaContainer;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static java.util.Arrays.asList;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * Tests {@code AbstractHypermediaContainer}.
 */
public class AbstractHypermediaContainerTest
{
	// ----------------------------------------------------------------------------------------------------------------
	// fields
	// ----------------------------------------------------------------------------------------------------------------

	private AbstractHypermediaContainer container;
	
	private ExpectedException thrown = ExpectedException.none();
	
	// ----------------------------------------------------------------------------------------------------------------
	// test methods
	// ----------------------------------------------------------------------------------------------------------------

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

	@Test
	public void getLinkReturnsFirstLink()
	{
		Link link = mock(Link.class);
		container = mock(AbstractHypermediaContainer.class);
		when(container.getLinks("x")).thenReturn(asList(link, mock(Link.class)));
		
		assertThat(container.getLink("x"), is(link));
	}
	
	@Test
	public void getLinkWhenNotFoundThrowsException()
	{
		container = mock(AbstractHypermediaContainer.class);
		when(container.getLinks("x")).thenReturn(Collections.<Link>emptyList());
		
		thrown.expect(LinkNotFoundException.class);
		thrown.expectMessage("x");
		
		container.getLink("x");
	}
	
	@Test
	public void getFormReturnsForm()
	{
		final Form form = mock(Form.class);
		container = new FakeHypermediaContainer()
		{
			@Override
			protected Form newForm(String name)
			{
				return "x".equals(name) ? form : null;
			}
		};
		
		assertThat(container.getForm("x"), is(form));
	}
	
	@Test
	public void getFormCachesForm()
	{
		Form form = mock(Form.class);
		final Iterator<Form> forms = asList(form, mock(Form.class)).iterator();
		container = new FakeHypermediaContainer()
		{
			@Override
			protected Form newForm(String name)
			{
				return "x".equals(name) ? forms.next() : null;
			}
		};
		
		container.getForm("x");
		assertThat(container.getForm("x"), is(form));
	}
	
	@Test
	public void getFormWhenNotFoundThrowsException()
	{
		container = new FakeHypermediaContainer()
		{
			@Override
			protected Form newForm(String name)
			{
				if ("x".equals(name))
				{
					throw new FormNotFoundException(name);
				}
				
				return null;
			}
		};
		
		thrown.expect(FormNotFoundException.class);
		thrown.expectMessage("x");
		
		container.getForm("x");
	}
}

{'fraction_non_alphanumeric': 0.2731816388267789, 'fraction_numerical': 0.0010522162304353545, 'mean_word_length': 1.8726860596902153, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2772775', 'n_tokens_mistral': 2194, 'n_tokens_neox': 1910, 'n_words': 473}