KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.netbeans.lib.cvsclient.connection.Connection;
24 import org.netbeans.lib.cvsclient.connection.PServerConnection;
25 import org.netbeans.lib.cvsclient.admin.StandardAdminHandler;
26 import org.netbeans.lib.cvsclient.admin.Entry;
27 import org.netbeans.lib.cvsclient.command.status.StatusCommand;
28 import org.netbeans.lib.cvsclient.command.GlobalOptions;
29 import org.netbeans.lib.cvsclient.command.FileInfoContainer;
30 import org.netbeans.lib.cvsclient.command.PipedFileInformation;
31 import org.netbeans.lib.cvsclient.command.CommandException;
32 import org.netbeans.lib.cvsclient.command.log.RlogCommand;
33 import org.netbeans.lib.cvsclient.command.add.AddCommand;
34 import org.netbeans.lib.cvsclient.command.commit.CommitCommand;
35 import org.netbeans.lib.cvsclient.command.update.UpdateCommand;
36 import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand;
37 import org.netbeans.lib.cvsclient.event.*;
38
39 import java.io.*;
40 import java.util.Date JavaDoc;
41
42 /**
43  * Compares output and input streams.
44  * <p>
45  * NON PORTABLE
46  *
47  * @author Petr Kuzel
48  */

49 public class ClientTest extends TestCase {
50
51     /**
52      * Tests single file status command for local file that is
53      * in repository Attic. It's should be Unknown = unversioned.
54      */

55     public void test50963() throws Exception JavaDoc {
56         File tmpDir = TestKit.createTmpFolder("localFileStatusTest");
57         File localCheckout = tmpDir;
58         String JavaDoc relativePath = "javacvs" + File.separator + "test" + File.separator + "data" +
59                 File.separator + "iz50963" + File.separator + "removed.txt";
60         String JavaDoc cvsRoot = ":pserver:anoncvs@netbeans.org:/cvs";
61
62         System.setProperty("cvsClientLog", new File(tmpDir, "protocol").getAbsolutePath());
63 // System.setProperty("socksProxyHost", "icsocks.holland.sun.com");
64

65         CVSRoot root = CVSRoot.parse(cvsRoot);
66         GlobalOptions gtx = new GlobalOptions();
67         gtx.setCVSRoot(cvsRoot);
68
69         // prepare local environment, checkout and create a local file
70

71         Connection connection = new PServerConnection(root);
72         Client client = new Client(connection, new StandardAdminHandler());
73         client.setLocalPath(tmpDir.getAbsolutePath());
74
75         CheckoutCommand checkout = new CheckoutCommand();
76         checkout.setModule("javacvs/test/data/iz50963");
77         checkout.setPruneDirectories(true);
78         checkout.setNotShortenPaths(true);
79         checkout.setRecursive(true);
80
81         client.executeCommand(checkout, gtx);
82
83         File localFile = new File(localCheckout, relativePath);
84         if (localFile.createNewFile() == false) {
85             throw new IOException("Cannot create " + localFile);
86         }
87
88         // run status on *single* local file that is Attic in repository
89

90         connection = new PServerConnection(root);
91         client = new Client(connection, new StandardAdminHandler());
92         client.setLocalPath(tmpDir.getAbsolutePath());
93
94
95         StatusCommand status = new StatusCommand();
96         File[] files = new File[] {new File(localCheckout, relativePath)};
97         status.setFiles(files);
98
99         client.getEventManager().addCVSListener(new CVSListener() {
100
101             public void messageSent(MessageEvent e) {
102             }
103
104             public void messageSent(BinaryMessageEvent e) {
105             }
106
107             public void fileAdded(FileAddedEvent e) {
108             }
109
110             public void fileToRemove(FileToRemoveEvent e) {
111             }
112
113             public void fileRemoved(FileRemovedEvent e) {
114             }
115
116             public void fileUpdated(FileUpdatedEvent e) {
117             }
118
119             public void fileInfoGenerated(FileInfoEvent e) {
120                 FileInfoContainer fic = e.getInfoContainer();
121                 System.err.println("Fic: " + fic);
122             }
123
124             public void commandTerminated(TerminationEvent e) {
125             }
126
127             public void moduleExpanded(ModuleExpansionEvent e) {
128             }
129         });
130         client.executeCommand(status, gtx);
131
132         // check result "Unknown" expected
133

134         assertTrue(false);
135
136         // all OK clean up
137

138         TestKit.deleteRecursively(tmpDir);
139
140     }
141
142     /**
143      * Tests checkou command prune empty directories option.
144      * It must eliminate pruned folder from Entries file.
145      */

146     public void test53239() throws Exception JavaDoc {
147
148         String JavaDoc checkoutModule = "javacvs/test/data";
149         File tmpDir = TestKit.createTmpFolder("pruneTest");
150         String JavaDoc localCheckout = tmpDir.getAbsolutePath();
151         String JavaDoc cvsRoot = ":pserver:anoncvs@netbeans.org/cvs";
152
153         System.setProperty("cvsClientLog", new File(tmpDir, "protocol").getAbsolutePath());
154 // System.setProperty("socksProxyHost", "icsocks.holland.sun.com");
155

156         CVSRoot root = CVSRoot.parse(cvsRoot);
157         Connection connection = new PServerConnection(root);
158         Client client = new Client(connection, new StandardAdminHandler());
159         client.setLocalPath(localCheckout);
160
161         CheckoutCommand checkout = new CheckoutCommand();
162         checkout.setModule(checkoutModule);
163         checkout.setPruneDirectories(true);
164         checkout.setNotShortenPaths(true);
165         checkout.setRecursive(true);
166
167         GlobalOptions gtx = new GlobalOptions();
168         gtx.setCVSRoot(cvsRoot);
169         client.executeCommand(checkout, gtx);
170
171         // check results, there must not be iz53239 entry
172

173         String JavaDoc relativePath = "javacvs" + File.separator + "test" + File.separator + "data" +
174                 File.separator + "CVS" + File.separator + "Entries";
175         FileReader reader = new FileReader(new File(localCheckout, relativePath));
176         BufferedReader buffy = new BufferedReader(reader);
177         String JavaDoc line = buffy.readLine();
178         do {
179             assertTrue(line.indexOf("iz53239") == -1);
180             line = buffy.readLine();
181         } while (line != null);
182
183         // all OK clean up
184

185         TestKit.deleteRecursively(tmpDir);
186     }
187
188     /**
189      * Test handling of binary file piped out to standard output
190      * (these are sent as as pure <tt>M</tt> responses instead of <tt>Mbinary</tt> responses)
191      */

192     public void test56710() throws Exception JavaDoc {
193         File tmpDir = TestKit.createTmpFolder("binaryToStdoutTest");
194         String JavaDoc relativePath = "javacvs" + File.separator + "test" + File.separator + "data" +
195                 File.separator + "iz56710" + File.separator + "binary.out";
196
197         String JavaDoc localCheckout = tmpDir.getAbsolutePath();
198         String JavaDoc cvsRoot = ":pserver:anoncvs@netbeans.org/cvs";
199
200         String JavaDoc protocolLog = new File(tmpDir, "protocol").getAbsolutePath();
201         System.setProperty("cvsClientLog", protocolLog);
202 // System.setProperty("socksProxyHost", "icsocks.holland.sun.com");
203

204         CVSRoot root = CVSRoot.parse(cvsRoot);
205         Connection connection = new PServerConnection(root);
206         Client client = new Client(connection, new StandardAdminHandler());
207         client.setLocalPath(localCheckout);
208
209         UpdateCommand update = new UpdateCommand();
210         update.setPipeToOutput(true);
211         File[] files = new File[] {new File(tmpDir, relativePath)};
212         update.setFiles(files);
213
214         final File[] pipedTmpFile = new File[1];
215         client.getEventManager().addCVSListener(new CVSListener() {
216
217             public void messageSent(MessageEvent e) {
218             }
219
220             public void messageSent(BinaryMessageEvent e) {
221             }
222
223             public void fileAdded(FileAddedEvent e) {
224             }
225
226             public void fileToRemove(FileToRemoveEvent e) {
227             }
228
229             public void fileRemoved(FileRemovedEvent e) {
230             }
231
232             public void fileUpdated(FileUpdatedEvent e) {
233             }
234
235             public void fileInfoGenerated(FileInfoEvent e) {
236                 PipedFileInformation pfi = (PipedFileInformation) e.getInfoContainer();
237                 pipedTmpFile[0] = pfi.getTempFile();
238             }
239
240             public void commandTerminated(TerminationEvent e) {
241             }
242
243             public void moduleExpanded(ModuleExpansionEvent e) {
244             }
245         });
246         GlobalOptions gtx = new GlobalOptions();
247         gtx.setCVSRoot(cvsRoot);
248         client.executeCommand(update, gtx);
249
250         FileInputStream in = new FileInputStream(pipedTmpFile[0]);
251         byte[] bytes = new byte[257];
252         int len = in.read(bytes);
253         assertEquals(len, 256);
254
255         for (int i = 0; i<256; i++) {
256             assertEquals(bytes[i], (byte)i);
257         }
258
259         TestKit.deleteRecursively(tmpDir);
260     }
261
262
263 }
264
Popular Tags