18
18
*/
19
19
20
20
/*
21
- * Copyright (c) 2008, 2016 , Oracle and/or its affiliates. All rights reserved.
21
+ * Copyright (c) 2008, 2017 , Oracle and/or its affiliates. All rights reserved.
22
22
*/
23
23
package org .opensolaris .opengrok .history ;
24
24
25
+ import java .io .File ;
26
+ import java .io .FileOutputStream ;
27
+ import java .io .FileWriter ;
28
+ import java .io .IOException ;
25
29
import java .io .Reader ;
26
30
import java .io .StringReader ;
31
+ import java .nio .channels .FileChannel ;
32
+ import java .util .ArrayList ;
33
+ import java .util .List ;
27
34
import org .junit .After ;
28
35
import org .junit .AfterClass ;
29
36
import org .junit .Before ;
35
42
import org .opensolaris .opengrok .condition .RepositoryInstalled ;
36
43
37
44
import static org .junit .Assert .*;
45
+ import org .opensolaris .opengrok .util .Executor ;
46
+ import org .opensolaris .opengrok .util .TestRepository ;
38
47
39
48
/**
40
49
*
@@ -59,14 +68,109 @@ public static void setUpClass() throws Exception {
59
68
public static void tearDownClass () throws Exception {
60
69
}
61
70
62
- @ Before
63
- public void setUp () {
64
- instance = new CVSRepository ();
71
+ private TestRepository repository ;
72
+
73
+ /**
74
+ * Set up a test repository. Should be called by the tests that need it. The
75
+ * test repository will be destroyed automatically when the test finishes.
76
+ */
77
+ private void setUpTestRepository () throws IOException {
78
+ repository = new TestRepository ();
79
+ repository .create (getClass ().getResourceAsStream ("repositories.zip" ));
80
+
81
+ // Fix the CVS/Root so that it points to the temporary directory
82
+ // rather than the workspace directory.
83
+ // Normally this would be bad idea since every subdirectory includes
84
+ // CVS/Root file however in this case there is just one such file
85
+ // as the repository is flat in terms of directory structure.
86
+ // This is done so that 'cvs update' does not change the "upstream"
87
+ // cvsroot directory entries.
88
+ // The alternative would be to checkout cvsrepo from cvsroot.
89
+ // XXX no proper path separators + newline char below
90
+ File root = new File (repository .getSourceRoot (), "cvs_test/cvsrepo/CVS/Root" );
91
+ if (root .isFile ()) {
92
+ FileChannel outChan = new FileOutputStream (root , true ).getChannel ();
93
+ outChan .truncate (0 );
94
+ outChan .close ();
95
+ }
96
+ FileWriter fw = new FileWriter (root );
97
+ fw .write (repository .getSourceRoot () + "/cvs_test/cvsroot\n " );
98
+ fw .close ();
65
99
}
66
100
67
101
@ After
68
102
public void tearDown () {
69
103
instance = null ;
104
+
105
+ if (repository != null ) {
106
+ repository .destroy ();
107
+ repository = null ;
108
+ }
109
+ }
110
+
111
+ @ Before
112
+ public void setUp () {
113
+ instance = new CVSRepository ();
114
+ }
115
+
116
+ /**
117
+ * Run the 'cvs' command.
118
+ *
119
+ * @param reposRoot directory of the repository root
120
+ * @param command command to run
121
+ * @param arg argument to use for the command
122
+ */
123
+ private static void runCvsCommand (File reposRoot , String command , String ... args ) {
124
+ List <String > cmdargs = new ArrayList <>();
125
+ cmdargs .add (CVSRepository .CMD_FALLBACK );
126
+ cmdargs .add (command );
127
+ for (String arg : args ) {
128
+ cmdargs .add (arg );
129
+ }
130
+ Executor exec = new Executor (cmdargs , reposRoot );
131
+ int exitCode = exec .exec ();
132
+ if (exitCode != 0 ) {
133
+ fail ("cvs " + command + " failed."
134
+ + "\n exit code: " + exitCode
135
+ + "\n stdout:\n " + exec .getOutputString ()
136
+ + "\n stderr:\n " + exec .getErrorString ());
137
+ }
138
+ }
139
+
140
+ /**
141
+ * Get the CVS repository, test that getBranch() returns null if there is
142
+ * no branch.
143
+ * @throws Exception
144
+ */
145
+ @ Test
146
+ public void testGetBranchNoBranch () throws Exception {
147
+ setUpTestRepository ();
148
+ File root = new File (repository .getSourceRoot (), "cvs_test/cvsrepo" );
149
+ CVSRepository cvsrepo
150
+ = (CVSRepository ) RepositoryFactory .getRepository (root );
151
+ assertEquals (null , cvsrepo .getBranch ());
152
+ }
153
+
154
+ /**
155
+ * Get the CVS repository, create new branch and verify getBranch() returns it.
156
+ * @throws Exception
157
+ */
158
+ @ Test
159
+ public void testGetBranchNewBranch () throws Exception {
160
+ setUpTestRepository ();
161
+ File root = new File (repository .getSourceRoot (), "cvs_test/cvsrepo" );
162
+
163
+ // Create new branch and switch to it.
164
+ runCvsCommand (root , "tag" , "-b" , "mybranch" );
165
+ // Note that the 'update' command will change the entries in 'cvsroot' directory.
166
+ runCvsCommand (root , "update" , "-r" , "mybranch" );
167
+
168
+ // Now the repository object can be instantiated so that determineBranch()
169
+ // will be called.
170
+ CVSRepository cvsrepo
171
+ = (CVSRepository ) RepositoryFactory .getRepository (root );
172
+
173
+ assertEquals ("mybranch" , cvsrepo .getBranch ());
70
174
}
71
175
72
176
/**
0 commit comments