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 .IOUtils ;
47
+ import org .opensolaris .opengrok .util .TestRepository ;
38
48
39
49
/**
40
50
*
@@ -59,14 +69,122 @@ public static void setUpClass() throws Exception {
59
69
public static void tearDownClass () throws Exception {
60
70
}
61
71
62
- @ Before
63
- public void setUp () {
64
- instance = new CVSRepository ();
72
+ private TestRepository repository ;
73
+
74
+ /**
75
+ * Set up a test repository. Should be called by the tests that need it. The
76
+ * test repository will be destroyed automatically when the test finishes.
77
+ */
78
+ private void setUpTestRepository () throws IOException {
79
+ repository = new TestRepository ();
80
+ repository .create (getClass ().getResourceAsStream ("repositories.zip" ));
81
+
82
+ // Checkout cvsrepo anew in order to get the CVS/Root files point to
83
+ // the temporary directory rather than the OpenGrok workspace directory
84
+ // it was created from. This is necessary since 'cvs update' changes
85
+ // the CVS parent directory after branch has been created.
86
+ File root = new File (repository .getSourceRoot (), "cvs_test" );
87
+ File cvsrepodir = new File (root , "cvsrepo" );
88
+ IOUtils .removeRecursive (cvsrepodir .toPath ());
89
+ File cvsroot = new File (root , "cvsroot" );
90
+ runCvsCommand (root , "-d" , cvsroot .getAbsolutePath (), "checkout" , "cvsrepo" );
65
91
}
66
92
67
93
@ After
68
94
public void tearDown () {
69
95
instance = null ;
96
+
97
+ if (repository != null ) {
98
+ repository .destroy ();
99
+ repository = null ;
100
+ }
101
+ }
102
+
103
+ @ Before
104
+ public void setUp () {
105
+ instance = new CVSRepository ();
106
+ }
107
+
108
+ /**
109
+ * Run the 'cvs' command with some arguments.
110
+ *
111
+ * @param reposRoot directory of the repository root
112
+ * @param args arguments to use for the command
113
+ */
114
+ private static void runCvsCommand (File reposRoot , String ... args ) {
115
+ List <String > cmdargs = new ArrayList <>();
116
+ cmdargs .add (CVSRepository .CMD_FALLBACK );
117
+ for (String arg : args ) {
118
+ cmdargs .add (arg );
119
+ }
120
+ Executor exec = new Executor (cmdargs , reposRoot );
121
+ int exitCode = exec .exec ();
122
+ if (exitCode != 0 ) {
123
+ fail ("cvs command '" + cmdargs .toString () + "'failed."
124
+ + "\n exit code: " + exitCode
125
+ + "\n stdout:\n " + exec .getOutputString ()
126
+ + "\n stderr:\n " + exec .getErrorString ());
127
+ }
128
+ }
129
+
130
+ /**
131
+ * Get the CVS repository, test that getBranch() returns null if there is
132
+ * no branch.
133
+ * @throws Exception
134
+ */
135
+ @ Test
136
+ public void testGetBranchNoBranch () throws Exception {
137
+ setUpTestRepository ();
138
+ File root = new File (repository .getSourceRoot (), "cvs_test/cvsrepo" );
139
+ CVSRepository cvsrepo
140
+ = (CVSRepository ) RepositoryFactory .getRepository (root );
141
+ assertEquals (null , cvsrepo .getBranch ());
142
+ }
143
+
144
+ /**
145
+ * Get the CVS repository, create new branch, change a file and verify that
146
+ * getBranch() returns the branch and check newly added commits annotate
147
+ * with branch revision numbers.
148
+ * Last, check that history entries of the file follow through before the
149
+ * branch was created.
150
+ * @throws Exception
151
+ */
152
+ @ Test
153
+ public void testNewBranch () throws Exception {
154
+ setUpTestRepository ();
155
+ File root = new File (repository .getSourceRoot (), "cvs_test/cvsrepo" );
156
+
157
+ // Create new branch and switch to it.
158
+ runCvsCommand (root , "tag" , "-b" , "mybranch" );
159
+ // Note that the 'update' command will change the entries in 'cvsroot' directory.
160
+ runCvsCommand (root , "update" , "-r" , "mybranch" );
161
+
162
+ // Now the repository object can be instantiated so that determineBranch()
163
+ // will be called.
164
+ CVSRepository cvsrepo
165
+ = (CVSRepository ) RepositoryFactory .getRepository (root );
166
+
167
+ assertEquals ("mybranch" , cvsrepo .getBranch ());
168
+
169
+ // Change the content and commit.
170
+ File mainC = new File (root , "main.c" );
171
+ FileChannel outChan = new FileOutputStream (mainC , true ).getChannel ();
172
+ outChan .truncate (0 );
173
+ outChan .close ();
174
+ FileWriter fw = new FileWriter (mainC );
175
+ fw .write ("#include <foo.h>\n " );
176
+ fw .close ();
177
+ runCvsCommand (root , "commit" , "-m" , "change on a branch" , "main.c" );
178
+
179
+ // Check that annotation for the changed line has branch revision.
180
+ Annotation annotation = cvsrepo .annotate (mainC , null );
181
+ assertEquals ("1.2.2.1" , annotation .getRevision (1 ));
182
+
183
+ History mainCHistory = cvsrepo .getHistory (mainC );
184
+ assertEquals (3 , mainCHistory .getHistoryEntries ().size ());
185
+ assertEquals ("1.2.2.1" , mainCHistory .getHistoryEntries ().get (0 ).getRevision ());
186
+ assertEquals ("1.2" , mainCHistory .getHistoryEntries ().get (1 ).getRevision ());
187
+ assertEquals ("1.1" , mainCHistory .getHistoryEntries ().get (2 ).getRevision ());
70
188
}
71
189
72
190
/**
0 commit comments