package org.sagebionetworks.javadoc;

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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;

import com.sun.javadoc.RootDoc;
import com.sun.tools.javadoc.Main;

/**
 * This utility helps create a real RootDoc from a source file for testing.
 * @author jmhill
 *
 */
public class JavaDocTestUtil {
	
	public static RootDoc buildRootDoc(String sourceFile) throws IOException {
		// Lookup the test files.
		File sampleSourceFile = findFileOnClasspath(sourceFile);
		// Find the classpath file generated by the maven-dependency-plugin
		String propertyValue = System.getProperty("auto.generated.classpath");
		if(propertyValue == null){
			// this occurs when run in eclipse.
			propertyValue = "target/gen/auto-generated-classpath.txt";
		}
		System.out.println(propertyValue);
		File classpathFile = new File(propertyValue);
		assertTrue("Classpath files does not exist: "+classpathFile.getAbsolutePath(), classpathFile.exists());
		// Lookup the output directory.
		propertyValue = System.getProperty("test.javadoc.output.directory");
		if(propertyValue == null){
			// this occurs when run in eclipse.
			propertyValue = "target/javadoc";
		}
		File outputDirectory = new File(propertyValue);

		File additionalClasspathDirectory = findFileOnClasspath("org/sagebionetworks/javadoc/testclasses/");
		additionalClasspathDirectory = additionalClasspathDirectory.getParentFile().getParentFile().getParentFile().getParentFile();

		BufferedReader reader = new BufferedReader(new FileReader(classpathFile));
		String classpath = reader.readLine();
		reader.close();
		classpath += File.pathSeparator + additionalClasspathDirectory.getAbsolutePath() + File.separator;

		int result = Main.execute(JavaDocTestUtil.class.getClassLoader(), new String[]{
			"-d",
			outputDirectory.getAbsolutePath(),
			"-doclet", TestDoclet.class.getName(),
			"-classpath",
			classpath,
			"-verbose",
			sampleSourceFile.getAbsolutePath()
		});
		return TestDoclet.getLastRoot();
	}
	
	public static File findFileOnClasspath(String fileName){
		URL url = JavaDocTestUtil.class.getClassLoader().getResource(fileName);
		assertNotNull("Failed to find: "+fileName+" on the classpath", url);
		File file = new File(url.getFile().replaceAll("%20", " "));
		assertTrue(file.exists());
		return file;
	}

}

@@ -1890,25 +1890,8 @@
 th()
- + File.separator
 ;%0D%0A%0D

Remove extra separator