org-netbeans-lib-cvsclient 的一些测试代码

邹普松
2023-12-01

写了一些cvsclient的测试代码,在这记录一下:

/**
 * Test CVSClient
 * @author linshutao
 * */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import org.netbeans.lib.cvsclient.CVSRoot;
import org.netbeans.lib.cvsclient.Client;
import org.netbeans.lib.cvsclient.admin.Entry;
import org.netbeans.lib.cvsclient.admin.StandardAdminHandler;
import org.netbeans.lib.cvsclient.command.Builder;
import org.netbeans.lib.cvsclient.command.CommandAbortedException;
import org.netbeans.lib.cvsclient.command.CommandException;
import org.netbeans.lib.cvsclient.command.GlobalOptions;
import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand;
import org.netbeans.lib.cvsclient.command.commit.CommitCommand;
import org.netbeans.lib.cvsclient.command.diff.DiffCommand;
import org.netbeans.lib.cvsclient.command.diff.DiffInformation;
import org.netbeans.lib.cvsclient.command.diff.SimpleDiffBuilder;
import org.netbeans.lib.cvsclient.command.log.LogCommand;
import org.netbeans.lib.cvsclient.commandLine.BasicListener;
import org.netbeans.lib.cvsclient.connection.AuthenticationException;
import org.netbeans.lib.cvsclient.connection.Connection;
import org.netbeans.lib.cvsclient.connection.ConnectionFactory;
import org.netbeans.lib.cvsclient.event.EventManager;

import junit.framework.TestCase;


public class CVSClientTest extends TestCase{
	/** Cvs clinet instance used to communicate with cvs server */
	 private static Client cvsclient = null;
	 
	 /** Cvs connect string */
	 private static CVSRoot cvsroot = null;
	 
	 /** Connection instance to keep connect with cvs server */
	 private static Connection connection = null;
	 
	 /** Global options to store the requied parameter for cvs server */
	 private static GlobalOptions globalOptions = null;
	 
	 /** The local path on ur local machine */
	 private static String LOCALPATH ="d:/cvs_checkout";
	/** 连接字符串*/
	 final static String  CONNECTION_STRING = ":pserver:linshutao:123456@168.168.0.77:/home/cvsroot";
	  		
	 public Connection openConnection() throws AuthenticationException,
	  IOException, CommandException {
		 cvsroot = CVSRoot.parse(CONNECTION_STRING);
		 connection = ConnectionFactory.getConnection(cvsroot);
		 cvsclient=new Client(connection, new StandardAdminHandler());
		 cvsclient.setLocalPath(LOCALPATH);
		 cvsclient.getEventManager().addCVSListener(new BasicListener());
		 connection.open();
		 
		 globalOptions=new GlobalOptions();
		 globalOptions.setCVSRoot(CVSRoot.parse(CONNECTION_STRING).getRepository());
		
		 return connection;
}
	 
	 public Client getCvsclient() throws AuthenticationException, IOException, CommandException{
		 if(cvsclient==null){
			 openConnection();			 
		 }
			 return cvsclient;
		 
	 }
	 
	 public void closeConnection() throws IOException{
		 if(connection!=null){
			 connection.close();
		 }
	 }
	 //重定向输出流
	 public static void systemSetOut() {
			try {
				File test = new File("D:/test.txt ");
				test.createNewFile();
				PrintStream out = new PrintStream(new FileOutputStream(test));
				//PrintStream out = new PrintStream(new String());
				System.setOut(out);
			} catch (Exception e) {
			}

		}
	
	/** Start test...*/
	 
	public void testLogCommand() throws CommandAbortedException,
		CommandException, AuthenticationException, IOException {
		openConnection();
		LogCommand logCommand = new LogCommand();
		logCommand.setFiles(new File[] { new File(
				"d:/cvs_checkout/IM800KBS/src/web/com/syni/im800/kb/attendant/webapp/action/KbsEntryAction.java") });
		cvsclient.executeCommand(logCommand, globalOptions);
		closeConnection();
	}
	
	public void testCheckoutCommand() throws AuthenticationException, IOException, CommandException{
		openConnection();
		CheckoutCommand checkoutCommand  =new CheckoutCommand();
		checkoutCommand.setModule("IM800KBS");
		//只输出信息不输出文件
		//checkoutCommand.setPipeToOutput(true);
		checkoutCommand.setRecursive(true);
		checkoutCommand.setCheckoutByRevision("v1-3-1-1000");
		cvsclient.executeCommand(checkoutCommand, globalOptions);
		closeConnection();
	}
	
	public void testCommitCommand() throws AuthenticationException, IOException, CommandException{
		openConnection();
		CommitCommand commitCommand = new CommitCommand();
		File files[] = {
				new File("d:/cvs_checkout/IM800KBS/src/web/com/syni/im800/kb/attendant/webapp/action/Abc.java")};
		commitCommand.setFiles(files);
		commitCommand.setBuilder(null);
		commitCommand.setForceCommit(true);
		commitCommand.setRecursive(true);
		cvsclient.executeCommand(commitCommand, globalOptions);
		closeConnection();
	}
	
	public void testDiffCommand() throws AuthenticationException, IOException, CommandException{
		systemSetOut();
		openConnection();
		DiffCommand diffCommand = new DiffCommand();
		/** 
		 * 某个文件两个不同版本的比较
		 * setRevision1:设置某一个版本,如果不设置就是拿本地的文件做比较
		 * setRevision2:设置另一个版本
		 * */
	//	diffCommand.setRevision1("1.64.2.229");
		diffCommand.setRevision2("1.64.2.230");
		diffCommand.setFiles(new File[]{new File(
				  "d:/cvs_checkout/IM800KBS/src/web/com/syni/im800/kb/attendant/webapp/action/KbsEntryAction.java")});	
		cvsclient.executeCommand(diffCommand, globalOptions);				
		closeConnection();
	}
	
	public void testDiffInformation() throws AuthenticationException, IOException, CommandException{
		openConnection();
		EventManager em = new EventManager(cvsclient);
		em.addCVSListener(new BasicListener());
		DiffCommand diffCommand = new DiffCommand();
		diffCommand.setFiles(new File[]{new File(			  
				"d:/cvs_checkout/IM800KBS/src/web/com/syni/im800/kb/attendant/webapp/action/KbsEntryAction.java")});			
	//	diffCommand.setRevision1("1.64.2.229");
		diffCommand.setRevision2("1.64.2.230");
		SimpleDiffBuilder sdb = new SimpleDiffBuilder(em,diffCommand);	
	//	cvsclient.executeCommand(diffCommand, globalOptions);
		DiffInformation diffInfo = sdb.createDiffInformation();
		diffInfo.setRepositoryFileName("d:/cvs_checkout/IM800KBS/src/web/com/syni/im800/kb/attendant/webapp/action/KbsEntryAction.java");
		diffInfo.setLeftRevision("1.64.2.229");
		diffInfo.setRightRevision("1.64.2.230");
		
		System.out.println(diffInfo.getFirstChange());
		
		
		
	/*	File diffFile = new File("d:/diff.txt");
		diffFile.createNewFile();	
		diffInfo.setFile(diffFile);
		diffInfo.setRepositoryFileName("d:/cvs_checkout/IM800KBS/src/web/com/syni/im800/kb/attendant/webapp/action/KbsEntryAction.java");
		diffInfo.setLeftRevision("1.64.2.220");
		diffInfo.setRightRevision("1.64.2.230");*/

		
		closeConnection();
	}
	
	
	public void testEntry()throws AuthenticationException, IOException, CommandException{
		systemSetOut();
		openConnection();
		File file = new File
				("d:/cvs_checkout/IM800KBS/src/web/com/syni/im800/kb/attendant/webapp/form/KbsEntryForm.java");
		Entry entry = cvsclient.getEntry(file);
		System.out.println(entry.getRevision());
		closeConnection();
	}
}


 类似资料: