<commit_before>/*
 * Copyright (C) 2004-2007  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope.pattern;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.io.IOException;

import com.exedio.cope.AbstractLibTest;
import com.exedio.cope.Item;
import com.exedio.cope.Model;
import com.exedio.cope.Type;
import com.exedio.cope.pattern.Dispatcher.Failure;

public class DispatcherTest extends AbstractLibTest
{
	public/*for web.xml*/ static final Model MODEL = new Model(DispatcherItem.TYPE);
	
	public DispatcherTest()
	{
		super(MODEL);
	}

	DispatcherItem item;
	DispatcherItem item1;
	DispatcherItem item2;
	DispatcherItem item3;
	DispatcherItem item4;
	
	@Override
	public void setUp() throws Exception
	{
		super.setUp();
		item1 = deleteOnTearDown(new DispatcherItem("item1", false));
		item2 = deleteOnTearDown(new DispatcherItem("item2", true));
		item3 = deleteOnTearDown(new DispatcherItem("item3", false));
		item4 = deleteOnTearDown(new DispatcherItem("item4", true));
	}
	
	public void testIt()
	{
		final Type<?> failureType = item.upload.getFailureType();
		
		// test model
		assertEqualsUnmodifiable(list(
				item.TYPE,
				failureType
			), model.getTypes());
		assertEqualsUnmodifiable(list(
				item.TYPE,
				failureType
			), model.getTypesSortedByHierarchy());
		assertEquals(DispatcherItem.class, item.TYPE.getJavaClass());
		assertEquals(true, item.TYPE.hasUniqueJavaClass());

		assertEqualsUnmodifiable(list(
				item.TYPE.getThis(),
				item.body,
				item.fail,
				item.dispatchCount,
				item.upload,
				item.upload.getPending(),
				item.upload.getSuccessDate(),
				item.upload.getSuccessElapsed()
			), item.TYPE.getFeatures());
		assertEqualsUnmodifiable(list(
				failureType.getThis(),
				item.uploadFailureParent(),
				item.upload.getFailureDate(),
				item.upload.getFailureElapsed(),
				item.upload.getFailureCause()
			), failureType.getFeatures());

		assertEquals(item.TYPE, item.upload.getType());
		assertEquals("upload", item.upload.getName());
		assertEquals(3, item.upload.getFailureLimit());
		assertEquals(2, item.upload.getSearchSize());

		assertEquals("DispatcherItem.uploadFailure", failureType.getID());
		assertEquals(Item.class, failureType.getJavaClass().getSuperclass());
		assertEquals(false, failureType.hasUniqueJavaClass());
		assertEquals(null, failureType.getSupertype());
		assertEqualsUnmodifiable(list(), failureType.getSubTypes());
		assertEquals(false, failureType.isAbstract());
		assertEquals(Item.class, failureType.getThis().getValueClass().getSuperclass());
		assertEquals(failureType, failureType.getThis().getValueType());
		assertEquals(model, failureType.getModel());

		assertEquals(failureType, item.uploadFailureParent().getType());
		assertEquals(failureType, item.upload.getFailureDate().getType());
		assertEquals(failureType, item.upload.getFailureCause().getType());

		assertEquals("parent", item.uploadFailureParent().getName());
		assertEquals("date", item.upload.getFailureDate().getName());
		assertEquals("cause", item.upload.getFailureCause().getName());
		
		try
		{
			new Dispatcher(0, 0);
			fail();
		}
		catch(IllegalArgumentException e)
		{
			assertEquals("failureLimit must be greater zero, but was 0.", e.getMessage());
		}
		try
		{
			new Dispatcher(-10, 0);
			fail();
		}
		catch(IllegalArgumentException e)
		{
			assertEquals("failureLimit must be greater zero, but was -10.", e.getMessage());
		}
		try
		{
			new Dispatcher(1000, 0);
			fail();
		}
		catch(IllegalArgumentException e)
		{
			assertEquals("searchSize must be greater zero, but was 0.", e.getMessage());
		}
		try
		{
			new Dispatcher(1000, -10);
			fail();
		}
		catch(IllegalArgumentException e)
		{
			assertEquals("searchSize must be greater zero, but was -10.", e.getMessage());
		}
		
		// test persistence
		assertNotDone(list(), item1);
		assertNotDone(list(), item2);
		assertNotDone(list(), item3);
		assertNotDone(list(), item4);
		
		final DateRange d1 = dispatch();
		assertDone(d1, list(), item1);
		assertNotDone(list(d1), item2);
		assertDone(d1, list(), item3);
		assertNotDone(list(d1), item4);
		
		final DateRange d2 = dispatch();
		assertDone(d1, list(), item1);
		assertNotDone(list(d1, d2), item2);
		assertDone(d1, list(), item3);
		assertNotDone(list(d1, d2), item4);
		
		item2.setFail(false);
		final DateRange d3 = dispatch();
		assertDone(d1, list(), item1);
		assertDone(d3, list(d1, d2), item2);
		assertDone(d1, list(), item3);
		assertNotDone(list(d1, d2, d3), item4);
		
		dispatch();
		assertDone(d1, list(), item1);
		assertDone(d3, list(d1, d2), item2);
		assertDone(d1, list(), item3);
		assertNotDone(list(d1, d2, d3), item4);
		
	}
	
	private static class DateRange
	{
		final Date before;
		final Date after;
		
		DateRange(final Date before, final Date after)
		{
			this.before = before;
			this.after = after;
		}
	}
	
	private DateRange dispatch()
	{
		model.commit();<commit_msg>test with non-Dispatchable<commit_after>/*
 * Copyright (C) 2004-2007  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope.pattern;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.io.IOException;

import com.exedio.cope.AbstractLibTest;
import com.exedio.cope.Item;
import com.exedio.cope.Model;
import com.exedio.cope.Type;
import com.exedio.cope.pattern.Dispatcher.Failure;

public class DispatcherTest extends AbstractLibTest
{
	public/*for web.xml*/ static final Model MODEL = new Model(DispatcherItem.TYPE);
	
	public DispatcherTest()
	{
		super(MODEL);
	}

	DispatcherItem item;
	DispatcherItem item1;
	DispatcherItem item2;
	DispatcherItem item3;
	DispatcherItem item4;
	
	@Override
	public void setUp() throws Exception
	{
		super.setUp();
		item1 = deleteOnTearDown(new DispatcherItem("item1", false));
		item2 = deleteOnTearDown(new DispatcherItem("item2", true));
		item3 = deleteOnTearDown(new DispatcherItem("item3", false));
		item4 = deleteOnTearDown(new DispatcherItem("item4", true));
	}
	
	public void testIt()
	{
		final Type<?> failureType = item.upload.getFailureType();
		
		// test model
		assertEqualsUnmodifiable(list(
				item.TYPE,
				failureType
			), model.getTypes());
		assertEqualsUnmodifiable(list(
				item.TYPE,
				failureType
			), model.getTypesSortedByHierarchy());
		assertEquals(DispatcherItem.class, item.TYPE.getJavaClass());
		assertEquals(true, item.TYPE.hasUniqueJavaClass());

		assertEqualsUnmodifiable(list(
				item.TYPE.getThis(),
				item.body,
				item.fail,
				item.dispatchCount,
				item.upload,
				item.upload.getPending(),
				item.upload.getSuccessDate(),
				item.upload.getSuccessElapsed()
			), item.TYPE.getFeatures());
		assertEqualsUnmodifiable(list(
				failureType.getThis(),
				item.uploadFailureParent(),
				item.upload.getFailureDate(),
				item.upload.getFailureElapsed(),
				item.upload.getFailureCause()
			), failureType.getFeatures());

		assertEquals(item.TYPE, item.upload.getType());
		assertEquals("upload", item.upload.getName());
		assertEquals(3, item.upload.getFailureLimit());
		assertEquals(2, item.upload.getSearchSize());

		assertEquals("DispatcherItem.uploadFailure", failureType.getID());
		assertEquals(Item.class, failureType.getJavaClass().getSuperclass());
		assertEquals(false, failureType.hasUniqueJavaClass());
		assertEquals(null, failureType.getSupertype());
		assertEqualsUnmodifiable(list(), failureType.getSubTypes());
		assertEquals(false, failureType.isAbstract());
		assertEquals(Item.class, failureType.getThis().getValueClass().getSuperclass());
		assertEquals(failureType, failureType.getThis().getValueType());
		assertEquals(model, failureType.getModel());

		assertEquals(failureType, item.uploadFailureParent().getType());
		assertEquals(failureType, item.upload.getFailureDate().getType());
		assertEquals(failureType, item.upload.getFailureCause().getType());

		assertEquals("parent", item.uploadFailureParent().getName());
		assertEquals("date", item.upload.getFailureDate().getName());
		assertEquals("cause", item.upload.getFailureCause().getName());
		
		try
		{
			new Dispatcher(0, 0);
			fail();
		}
		catch(IllegalArgumentException e)
		{
			assertEquals("failureLimit must be greater zero, but was 0.", e.getMessage());
		}
		try
		{
			new Dispatcher(-10, 0);
			fail();
		}
		catch(IllegalArgumentException e)
		{
			assertEquals("failureLimit must be greater zero, but was -10.", e.getMessage());
		}
		try
		{
			new Dispatcher(1000, 0);
			fail();
		}
		catch(IllegalArgumentException e)
		{
			assertEquals("searchSize must be greater zero, but was 0.", e.getMessage());
		}
		try
		{
			new Dispatcher(1000, -10);
			fail();
		}
		catch(IllegalArgumentException e)
		{
			assertEquals("searchSize must be greater zero, but was -10.", e.getMessage());
		}
		
		// test persistence
		assertNotDone(list(), item1);
		assertNotDone(list(), item2);
		assertNotDone(list(), item3);
		assertNotDone(list(), item4);
		
		final DateRange d1 = dispatch();
		assertDone(d1, list(), item1);
		assertNotDone(list(d1), item2);
		assertDone(d1, list(), item3);
		assertNotDone(list(d1), item4);
		
		final DateRange d2 = dispatch();
		assertDone(d1, list(), item1);
		assertNotDone(list(d1, d2), item2);
		assertDone(d1, list(), item3);
		assertNotDone(list(d1, d2), item4);
		
		item2.setFail(false);
		final DateRange d3 = dispatch();
		assertDone(d1, list(), item1);
		assertDone(d3, list(d1, d2), item2);
		assertDone(d1, list(), item3);
		assertNotDone(list(d1, d2, d3), item4);
		
		dispatch();
		assertDone(d1, list(), item1);
		assertDone(d3, list(d1, d2), item2);
		assertDone(d1, list(), item3);
		assertNotDone(list(d1, d2, d3), item4);
		
		try
		{
			DispatcherItem.upload.dispatch(HashItem.class);
			fail();
		}
		catch(ClassCastException e)
		{
			assertEquals("expected " + HashItem.class.getName() + ", but was " + DispatcherItem.class.getName(), e.getMessage());
		}
	}
	
	private static class DateRange
	{
		final Date before;
		final Date after;
		
		DateRange(final Date before, final Date after)
		{
			this.before = before;
			this.after = after;
		}
	}
	
	private DateRange dispatch()
	{
		model.commit();
{'fraction_non_alphanumeric': 0.13173292032352435, 'fraction_numerical': 0.018929616245052485, 'mean_word_length': 4.561244019138756, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '7485422', 'n_tokens_mistral': 4110, 'n_tokens_neox': 3563, 'n_words': 872}