KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > CommitTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.cvsclient;
21
22 import junit.framework.TestCase;
23
24 import java.io.*;
25 import java.util.Date JavaDoc;
26
27 import org.netbeans.lib.cvsclient.admin.Entry;
28 import org.netbeans.lib.cvsclient.admin.StandardAdminHandler;
29 import org.netbeans.lib.cvsclient.command.GlobalOptions;
30 import org.netbeans.lib.cvsclient.command.commit.CommitCommand;
31 import org.netbeans.lib.cvsclient.connection.Connection;
32 import org.netbeans.lib.cvsclient.connection.PServerConnection;
33
34 /**
35  * Commit command test suite.
36  *
37  * @author Petr Kuzel
38  */

39 public class CommitTest extends TestCase {
40
41     /**
42      * Client must checks conflicted files timestamps. Until
43      * it changes it should not commit the file (it actually
44      * decides server by testing sent entry).
45      * <p>
46      * Uses fake PseudoCvsServer.
47      */

48     public void test36288() throws Exception JavaDoc {
49         File tmpDir = TestKit.createTmpFolder("commitConflictTest");
50         String JavaDoc protocolLog = new File(tmpDir, "protocol").getAbsolutePath();
51         System.setProperty("cvsClientLog", protocolLog);
52         System.out.println(protocolLog);
53
54         // prepare working directory
55
File CVSdir = new File(tmpDir, "CVS");
56         CVSdir.mkdirs();
57         File entries = new File(CVSdir, "Entries");
58         OutputStream out = new FileOutputStream(entries);
59         String JavaDoc dateString = "Thu Mar 24 15:14:27 2005";
60         String JavaDoc data = "/conflict.txt/1.2/Result of merge+" + dateString + "//\nD";
61         out.write(data.getBytes("utf8"));
62         out.flush();
63         out.close();
64
65         File conflict_txt = new File(tmpDir, "conflict.txt");
66         out = new FileOutputStream(conflict_txt);
67         data = "AAA\n" +
68                 "BBB\n" +
69                 "<<<<<<< conflict.txt\n" +
70                 "YYY <= fix\n" +
71                 "=======\n" +
72                 "222 <= fix\n" +
73                 ">>>>>>> 1.2\n" +
74                 "DDD\n" +
75                 "EEE\n";
76         out.write(data.getBytes("utf8"));
77         out.flush();
78         out.close();
79         Date JavaDoc date = Entry.getLastModifiedDateFormatter().parse(dateString);
80         conflict_txt.setLastModified(date.getTime());
81
82         InputStream in = getClass().getResourceAsStream("protocol/iz36288.in");
83         if (in == null) {
84             System.err.println(getClass().getProtectionDomain().getCodeSource().getLocation().toExternalForm());
85             in.markSupported();
86         }
87         PseudoCvsServer cvss = new PseudoCvsServer(in);
88
89         File requestsLog = File.createTempFile("requests", null, tmpDir);
90         cvss.logRequests(new FileOutputStream(requestsLog));
91         new Thread JavaDoc(cvss).start();
92         String JavaDoc cvsRoot = cvss.getCvsRoot();
93
94         File root = new File(CVSdir, "Root");
95         out = new FileOutputStream(root);
96         out.write(cvsRoot.getBytes("utf8"));
97         out.flush();
98         out.close();
99
100         File repo = new File(CVSdir, "Repository");
101         out = new FileOutputStream(repo);
102         out.write("/cvs".getBytes("utf8"));
103         out.flush();
104         out.close();
105
106         // commit command
107
CVSRoot CvsRoot = CVSRoot.parse(cvsRoot);
108         GlobalOptions gtx = new GlobalOptions();
109         gtx.setCVSRoot(cvsRoot);
110         Connection connection = new PServerConnection(CvsRoot);
111         Client client = new Client(connection, new StandardAdminHandler());
112         client.setLocalPath(tmpDir.getAbsolutePath());
113
114         CommitCommand commit = new CommitCommand();
115         File[] files = new File[] {new File(tmpDir, "conflict.txt")};
116         commit.setFiles(files);
117
118         client.executeCommand(commit, gtx);
119         cvss.stop();
120
121         // check test matching golden file (here critical line from iz36288.out)
122

123         InputStream actual = new FileInputStream(requestsLog);
124         LineNumberReader lineReader = new LineNumberReader(new InputStreamReader(actual, "utf8"));
125         boolean foundConflictLine = false;
126         String JavaDoc line = lineReader.readLine();
127         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
128         while (foundConflictLine == false && line != null) {
129             sb.append(line + "\n");
130             foundConflictLine |= "Entry /conflict.txt/1.2/+=//".equals(line);
131             line = lineReader.readLine();
132         }
133         assertTrue("Missing 'Entry /conflict.txt/1.2/+=//' in:\n" + sb.toString(), foundConflictLine);
134
135         TestKit.deleteRecursively(tmpDir);
136     }
137
138 }
139
Popular Tags