<commit_before>package org.modeshape.jcr;

import static org.junit.Assert.assertTrue;
import static org.modeshape.common.logging.Logger.getLogger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.jcr.Node;
import javax.jcr.Session;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.modeshape.common.logging.Logger;
import org.modeshape.common.util.FileUtil;
import org.modeshape.jcr.api.JcrTools;
import org.modeshape.jcr.api.value.DateTime;
import org.modeshape.jcr.bus.ChangeBus;
import org.modeshape.jcr.cache.NodeKey;
import org.modeshape.jcr.cache.change.Change;
import org.modeshape.jcr.cache.change.ChangeSet;
import org.modeshape.jcr.cache.change.ChangeSetListener;
import org.modeshape.jcr.value.basic.JodaDateTime;

public class ConnectorChangesTest extends SingleUseAbstractTest {

	private ChangeBus changeBus;
	private TestListener listener;

	private Logger logger = getLogger(getClass());

	@Before
	public void beforeEach() throws Exception {
		startRepositoryWithConfiguration(resource("config/repo-config-filesystem-federation-with-persistence.json"));
		logger.debug("Started repository...");
		changeBus = repository().changeBus();
		listener = new TestListener();
		changeBus.register(listener);
	}

	@After
	public void afterEach() throws Exception {
		stopRepository();
		logger.debug("Stopped repository.");
	}

	@Test
	public void testChangesEmittedWhenNodeCreated() throws Exception {
		logger.debug("Executing testChangesEmittedWhenNodeCreated()...");
		FileUtil.delete("target/federation_persistent_repository");
		final Session session = session();
		final Node root = session.getRootNode();
		logger.debug("Root node is: ");
		new JcrTools(true).printSubgraph(root, 2);
		final Node federation = session.getNode("/federation");
		federation.addNode("testNode");
		session.save();
		assertTrue("Didn't receive changes after node creation!",
				listener.receivedChangeSet.size() > 0);
		logger.debug("Executed testChangesEmittedWhenNodeCreated().");

	}

	protected static class TestListener implements ChangeSetListener {
		private final List<TestChangeSet> receivedChangeSet;
		private CountDownLatch latch;

		public TestListener() {
			this(0);
		}

		protected TestListener(int expectedNumberOfChangeSet) {
			latch = new CountDownLatch(expectedNumberOfChangeSet);
			receivedChangeSet = new ArrayList<TestChangeSet>();
		}<commit_msg>MODE-1710 Added creation of repo directory to test case<commit_after>package org.modeshape.jcr;

import static org.junit.Assert.assertTrue;
import static org.modeshape.common.logging.Logger.getLogger;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.jcr.Node;
import javax.jcr.Session;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.modeshape.common.logging.Logger;
import org.modeshape.common.util.FileUtil;
import org.modeshape.jcr.api.JcrTools;
import org.modeshape.jcr.api.value.DateTime;
import org.modeshape.jcr.bus.ChangeBus;
import org.modeshape.jcr.cache.NodeKey;
import org.modeshape.jcr.cache.change.Change;
import org.modeshape.jcr.cache.change.ChangeSet;
import org.modeshape.jcr.cache.change.ChangeSetListener;
import org.modeshape.jcr.value.basic.JodaDateTime;

public class ConnectorChangesTest extends SingleUseAbstractTest {

	private ChangeBus changeBus;
	private TestListener listener;

	private Logger logger = getLogger(getClass());

	@Before
	public void beforeEach() throws Exception {
		startRepositoryWithConfiguration(resource("config/repo-config-filesystem-federation-with-persistence.json"));
		logger.debug("Started repository...");
		changeBus = repository().changeBus();
		listener = new TestListener();
		changeBus.register(listener);
	}

	@After
	public void afterEach() throws Exception {
		stopRepository();
		logger.debug("Stopped repository.");
	}

	@Test
	public void testChangesEmittedWhenNodeCreated() throws Exception {
		logger.debug("Executing testChangesEmittedWhenNodeCreated()...");
		/* hmm I guess the deletion is there to remove previously generated state, so I'll leave it in */
		FileUtil.delete("target/federation_persistent_repository");
		/* but for the tests to run the following directory must exists it seems */
		new File("target/federation_persistent_repository/store/persistentRepository").mkdirs();
		final Session session = session();
		final Node root = session.getRootNode();
		logger.debug("Root node is: ");
		new JcrTools(true).printSubgraph(root, 2);
		final Node federation = session.getNode("/federation");
		federation.addNode("testNode");
		session.save();
		assertTrue("Didn't receive changes after node creation!",
				listener.receivedChangeSet.size() > 0);
		logger.debug("Executed testChangesEmittedWhenNodeCreated().");

	}

	protected static class TestListener implements ChangeSetListener {
		private final List<TestChangeSet> receivedChangeSet;
		private CountDownLatch latch;

		public TestListener() {
			this(0);
		}

		protected TestListener(int expectedNumberOfChangeSet) {
			latch = new CountDownLatch(expectedNumberOfChangeSet);
			receivedChangeSet = new ArrayList<TestChangeSet>();
		}
{'fraction_non_alphanumeric': 0.10960635608522933, 'fraction_numerical': 0.0018057060310581437, 'mean_word_length': 5.7058111380145276, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6890726', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1620, 'n_words': 356}