<commit_before>/**********************************************************************************
 * Copyright 2010 Sakai Foundation
 *
 * Licensed under the Educational Community 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.osedu.org/licenses/ECL-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.sakaiproject.mailsender.logic.impl;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.File;
import java.util.Properties;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.sakaiproject.component.api.ServerConfigurationService;
import org.sakaiproject.mailsender.logic.ConfigLogic;
import org.sakaiproject.mailsender.logic.ExternalLogic;
import org.sakaiproject.mailsender.model.ConfigEntry;
import org.sakaiproject.tool.api.ToolManager;

@RunWith(value = MockitoJUnitRunner.class)
public class ConfigLogicImplTest {

	@Mock
	ExternalLogic externalLogic;

	@Mock
	ServerConfigurationService configService;

	@Mock(answer = Answers.RETURNS_DEEP_STUBS)
	ToolManager toolManager;

	Properties props;
	ConfigLogicImpl logic;

	@Before
	public void setUp() {
		props = new Properties();

		when(toolManager.getCurrentPlacement().getConfig()).thenReturn(props);

		logic = new ConfigLogicImpl();
		logic.setExternalLogic(externalLogic);
		logic.setServerConfigurationService(configService);
		logic.setToolManager(toolManager);
	}

	@Test
	public void getConfig() {
		ConfigEntry config = logic.getConfig();
		assertNotNull(config);
	}

	@Test
	public void allowSubjectPrefixChange() {
		assertFalse(logic.allowSubjectPrefixChange());

		when(
				configService.getBoolean(ConfigLogic.ALLOW_PREFIX_CHANGE_PROP,
						false)).thenReturn(true);
		assertTrue(logic.allowSubjectPrefixChange());
	}

	@Test
	public void useRTE() {
		assertTrue(logic.useRichTextEditor());

		when(configService.getString(ConfigLogic.WSYIWYG_EDITOR_PROP))
				.thenReturn("htmlarea");
		assertFalse(logic.useRichTextEditor());
	}

	@Test
	public void isEmailTestMode() {
		assertFalse(logic.isEmailTestMode());

		when(configService.getBoolean(ConfigLogic.EMAIL_TEST_MODE_PROP, false))
				.thenReturn(true);
		assertTrue(logic.isEmailTestMode());
	}

	@Test
	public void getUploadDirectory() {
		String tmp = System.getProperty("java.io.tmpdir");
		String newDir = tmp + "/configLogicImplTest";
		File f = new File(newDir);
		if (!f.exists()) {
			if (f.mkdir()) {
				f.deleteOnExit();
			} else {
				System.err.println("Unable to create test directory.");
			}
		}

		assertEquals(tmp, logic.getUploadDirectory());

		when(configService.getString(ConfigLogic.UPLOAD_DIRECTORY_PROP))
				.thenReturn("/missing/dir").thenReturn(newDir);
		assertEquals(tmp, logic.getUploadDirectory());<commit_msg>NOJIRA Corrected test to match logic in tested module.<commit_after>/**********************************************************************************
 * Copyright 2010 Sakai Foundation
 *
 * Licensed under the Educational Community 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.osedu.org/licenses/ECL-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.sakaiproject.mailsender.logic.impl;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.File;
import java.util.Properties;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.sakaiproject.component.api.ServerConfigurationService;
import org.sakaiproject.mailsender.logic.ConfigLogic;
import org.sakaiproject.mailsender.logic.ExternalLogic;
import org.sakaiproject.mailsender.model.ConfigEntry;
import org.sakaiproject.tool.api.ToolManager;

@RunWith(value = MockitoJUnitRunner.class)
public class ConfigLogicImplTest {

	@Mock
	ExternalLogic externalLogic;

	@Mock
	ServerConfigurationService configService;

	@Mock(answer = Answers.RETURNS_DEEP_STUBS)
	ToolManager toolManager;

	Properties props;
	ConfigLogicImpl logic;

	@Before
	public void setUp() {
		props = new Properties();

		when(toolManager.getCurrentPlacement().getConfig()).thenReturn(props);

		logic = new ConfigLogicImpl();
		logic.setExternalLogic(externalLogic);
		logic.setServerConfigurationService(configService);
		logic.setToolManager(toolManager);
	}

	@Test
	public void getConfig() {
		ConfigEntry config = logic.getConfig();
		assertNotNull(config);
	}

	@Test
	public void allowSubjectPrefixChange() {
		assertFalse(logic.allowSubjectPrefixChange());

		when(
				configService.getBoolean(ConfigLogic.ALLOW_PREFIX_CHANGE_PROP,
						false)).thenReturn(true);
		assertTrue(logic.allowSubjectPrefixChange());
	}

	@Test
	public void useRTE() {
		assertTrue(logic.useRichTextEditor());

		when(configService.getString(ConfigLogic.WSYIWYG_EDITOR_PROP))
				.thenReturn("htmlarea");
		assertFalse(logic.useRichTextEditor());
	}

	@Test
	public void isEmailTestMode() {
		assertFalse(logic.isEmailTestMode());

		when(configService.getBoolean(ConfigLogic.EMAIL_TEST_MODE_PROP, false))
				.thenReturn(true);
		assertTrue(logic.isEmailTestMode());
	}

	@Test
	public void getUploadDirectory() {
		String tmp = System.getProperty("java.io.tmpdir");
		// trim ending / to match what is done in ConfigLogicImpl
		if (tmp.endsWith("/")) {
			tmp = tmp.substring(0, tmp.length() - 1);
		}

		String newDir = tmp + "/configLogicImplTest";
		File f = new File(newDir);
		if (!f.exists()) {
			if (f.mkdir()) {
				f.deleteOnExit();
			} else {
				System.err.println("Unable to create test directory.");
			}
		}

		assertEquals(tmp, logic.getUploadDirectory());

		when(configService.getString(ConfigLogic.UPLOAD_DIRECTORY_PROP))
				.thenReturn("/missing/dir").thenReturn(newDir);
		assertEquals(tmp, logic.getUploadDirectory());
{'fraction_non_alphanumeric': 0.14640734737979472, 'fraction_numerical': 0.0024311183144246355, 'mean_word_length': 5.489921121822962, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1120768', 'n_tokens_mistral': 2360, 'n_tokens_neox': 2153, 'n_words': 490}