<commit_before>package de.terrestris.shogun2.dao;

import static org.junit.Assert.*;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.RandomStringUtils;
import org.joda.time.DateTime;
import org.joda.time.ReadableDateTime;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import de.terrestris.shogun2.dao.ApplicationDao;
import de.terrestris.shogun2.model.Application;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:META-INF/spring/test-context-dao.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class ApplicationDaoTest {

	@Autowired
	ApplicationDao applicationDao;

	private Set<String> usedRandomStrings = new HashSet<String>();

	/**
	 * @return
	 */
	private String getRandomStr() {
		String candidate;
		while (true) {
			candidate = RandomStringUtils.random(10);
			if (usedRandomStrings.contains(candidate) == false) {
				// stop it, we found one
				usedRandomStrings.add(candidate);
				break;
			}
		}
		return candidate;
	}

	/**
	 * Small helper for getting a new unsaved mock Application with a random
	 * name.
	 *
	 * @return
	 */
	private Application getRandomUnsavedMockApp() {
		String randomName = "A name " + getRandomStr();
		return getMockApp(randomName);
	}

	/**
	 * Small helper for getting a new saved mock Application with a random
	 * name.
	 *
	 * @return
	 */
	private Application getRandomSavedMockApp() {
		String randomName = "A name " + getRandomStr();
		return getSavedMockApp(randomName);
	}

	/**
	 * Small helper for getting a new unsaved mock Application.
	 *
	 * @return
	 */
	private Application getMockApp(String name) {
		Application app = new Application();
		app.setName(name);
		app.setDescription("A description");
		return app;
	}

	/**
	 * Small helper for getting a new saved mock Application.
	 *
	 * @return
	 */
	private Application getSavedMockApp(String name) {
		String appName = name;
		if (name.isEmpty()) {
			appName = "A name";
		}

		Application app = getMockApp(appName);

		applicationDao.saveOrUpdate(app);

		return app;
	}

	/**
	 * Tests whether we can both CREATE and UPDATE with saveOrUpdate
	 */
	@Test
	public void saveOrUpdate_shouldSaveOrUpdateApplication() {
		Application app = getRandomUnsavedMockApp();

		assertNull(app.getId());

		// Test CREATE
		applicationDao.saveOrUpdate(app);

		Integer id = app.getId();

		assertNotNull(id);
		assertTrue(id > 0);

		// Test UPDATE
		String changedNameOfApp = "Some other name";
		String changedDescOfApp = "Changed description";

		app.setName("Some other name");
		app.setDescription("Changed description");

		applicationDao.saveOrUpdate(app);

		assertEquals(id, app.getId());
		assertEquals(changedNameOfApp, app.getName());
		assertEquals(changedDescOfApp, app.getDescription());
	}

	/**
	 * Tests whether the automatic setting of field modified happens when
	 * creating.
	 */
	@Test
	public void saveOrUpdate_shouldSetModified() {
		// first create an application and save it.
		Application app = getRandomUnsavedMockApp();

		// Model tests should ensure that the constructor populates modified
		ReadableDateTime before = app.getModified();

		try {
			// ... wait ...
			Thread.sleep(50);

			// ... then save
			applicationDao.saveOrUpdate(app);

			ReadableDateTime after = app.getModified();

			// They should be different
			assertNotEquals(before, after);<commit_msg>Using variables instead of hardcoded strings.<commit_after>package de.terrestris.shogun2.dao;

import static org.junit.Assert.*;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.RandomStringUtils;
import org.joda.time.DateTime;
import org.joda.time.ReadableDateTime;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import de.terrestris.shogun2.dao.ApplicationDao;
import de.terrestris.shogun2.model.Application;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:META-INF/spring/test-context-dao.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class ApplicationDaoTest {

	@Autowired
	ApplicationDao applicationDao;

	private Set<String> usedRandomStrings = new HashSet<String>();

	/**
	 * @return
	 */
	private String getRandomStr() {
		String candidate;
		while (true) {
			candidate = RandomStringUtils.random(10);
			if (usedRandomStrings.contains(candidate) == false) {
				// stop it, we found one
				usedRandomStrings.add(candidate);
				break;
			}
		}
		return candidate;
	}

	/**
	 * Small helper for getting a new unsaved mock Application with a random
	 * name.
	 *
	 * @return
	 */
	private Application getRandomUnsavedMockApp() {
		String randomName = "A name " + getRandomStr();
		return getMockApp(randomName);
	}

	/**
	 * Small helper for getting a new saved mock Application with a random
	 * name.
	 *
	 * @return
	 */
	private Application getRandomSavedMockApp() {
		String randomName = "A name " + getRandomStr();
		return getSavedMockApp(randomName);
	}

	/**
	 * Small helper for getting a new unsaved mock Application.
	 *
	 * @return
	 */
	private Application getMockApp(String name) {
		Application app = new Application();
		app.setName(name);
		app.setDescription("A description");
		return app;
	}

	/**
	 * Small helper for getting a new saved mock Application.
	 *
	 * @return
	 */
	private Application getSavedMockApp(String name) {
		String appName = name;
		if (name.isEmpty()) {
			appName = "A name";
		}

		Application app = getMockApp(appName);

		applicationDao.saveOrUpdate(app);

		return app;
	}

	/**
	 * Tests whether we can both CREATE and UPDATE with saveOrUpdate
	 */
	@Test
	public void saveOrUpdate_shouldSaveOrUpdateApplication() {
		Application app = getRandomUnsavedMockApp();

		assertNull(app.getId());

		// Test CREATE
		applicationDao.saveOrUpdate(app);

		Integer id = app.getId();

		assertNotNull(id);
		assertTrue(id > 0);

		// Test UPDATE
		String changedNameOfApp = "Some other name";
		String changedDescOfApp = "Changed description";

		app.setName(changedNameOfApp);
		app.setDescription(changedDescOfApp);

		applicationDao.saveOrUpdate(app);

		assertEquals(id, app.getId());
		assertEquals(changedNameOfApp, app.getName());
		assertEquals(changedDescOfApp, app.getDescription());
	}

	/**
	 * Tests whether the automatic setting of field modified happens when
	 * creating.
	 */
	@Test
	public void saveOrUpdate_shouldSetModified() {
		// first create an application and save it.
		Application app = getRandomUnsavedMockApp();

		// Model tests should ensure that the constructor populates modified
		ReadableDateTime before = app.getModified();

		try {
			// ... wait ...
			Thread.sleep(50);

			// ... then save
			applicationDao.saveOrUpdate(app);

			ReadableDateTime after = app.getModified();

			// They should be different
			assertNotEquals(before, after);
{'fraction_non_alphanumeric': 0.10839068290100583, 'fraction_numerical': 0.002911593435680254, 'mean_word_length': 4.255215577190542, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5456524', 'n_tokens_mistral': 2423, 'n_tokens_neox': 2191, 'n_words': 547}