package org.tmatesoft.svn.test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.junit.Test;
import org.tmatesoft.svn.core.ISVNDirEntryHandler;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.wc.SVNLogClient;
import org.tmatesoft.svn.core.wc.SVNRevision;

public class SVNLogClientTest {
	@Test
	public void testListSubversionTagsParameter() throws Exception {
		// read tags from a repository
		String tagsDir = "http://svn.codehaus.org/sxc";

		SimpleSVNDirEntryHandler dirEntryHandler = new SimpleSVNDirEntryHandler();
		List<String> dirs = new ArrayList<String>();

		SVNURL repoURL = SVNURL.parseURIDecoded(tagsDir);

		ISVNAuthenticationManager authManager = null;
		SVNLogClient logClient = new SVNLogClient(authManager, null);

		System.out.println("Listing sub-svn-tags via " + repoURL);
		logClient.doList(repoURL, SVNRevision.HEAD, SVNRevision.HEAD, false,
				false, dirEntryHandler);
		dirs = dirEntryHandler.getDirs();

		assertNotNull(dirs);

		assertTrue("Had: " + dirs, dirs.size() > 0);
		assertTrue("Had: " + dirs, dirs.contains("sxc-0.7"));
		assertTrue("Had: " + dirs, dirs.contains("sxc-0.7.1"));
		assertTrue("Had: " + dirs, dirs.contains("trunk"));
	}

	public class SimpleSVNDirEntryHandler implements ISVNDirEntryHandler {

		private final List<SVNDirEntry> dirs = new ArrayList<SVNDirEntry>();

		public SimpleSVNDirEntryHandler() {
		}

		public List<String> getDirs() {
			Collections.sort(dirs, new Comparator<SVNDirEntry>() {
				public int compare(SVNDirEntry dir1, SVNDirEntry dir2) {
					return dir1.getName().compareTo(dir2.getName());
				}
			});

			List<String> sortedDirs = new ArrayList<String>();
			for (SVNDirEntry dirEntry : dirs) {
				sortedDirs.add(dirEntry.getName());
			}

			return sortedDirs;
		}

		public void handleDirEntry(SVNDirEntry dirEntry) throws SVNException {
			dirs.add(dirEntry);
		}
	}
}
