summaryrefslogtreecommitdiff
path: root/subversion/bindings/javahl/tests/org/apache/subversion/javahl/SVNReposTests.java
blob: dcee0ffa6d070e1e32ab8bb443de511e0fa38015 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
 * @copyright
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 * @endcopyright
 */
package org.apache.subversion.javahl;

import org.apache.subversion.javahl.callback.*;
import org.apache.subversion.javahl.types.*;

import java.io.File;
import java.io.FileWriter;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Map;

/**
 * This class is used for testing the ISVNRepos interface
 *
 * More methodes for testing are still needed
 */
public class SVNReposTests extends SVNTests
{
    public SVNReposTests()
    {
    }

    public SVNReposTests(String name)
    {
        super(name);
    }

    /**
     * Test the basic ISVNRepos.create functionality
     * @throws SubversionException
     */
    public void testCreate()
        throws SubversionException, IOException
    {
        OneTest thisTest = new OneTest(false);
        assertTrue("repository exists", thisTest.getRepository().exists());
    }

    public void testSetRevProp()
        throws SubversionException, IOException
    {
        OneTest thisTest = new OneTest(false);
        final String MSG = "Initial repository creation";
        admin.setRevProp(thisTest.getRepository(), Revision.getInstance(0),
                         "svn:log", MSG, false, false);
        Map<String, byte[]> pdata = client.revProperties(
                           makeReposUrl(thisTest.getRepository()).toString(),
                           Revision.getInstance(0));
        assertNotNull("expect non null rev props");
        String logMessage = new String(pdata.get("svn:log"));
        assertEquals("expect rev prop change to take effect", MSG, logMessage);
    }

    /* This test only tests the call down to the C++ layer. */
    public void testVerify()
        throws SubversionException, IOException
    {
        OneTest thisTest = new OneTest(false, true);
        admin.verify(thisTest.getRepository(), Revision.getInstance(0),
                     Revision.HEAD, null);
    }

    private class VerifyCallback implements ReposVerifyCallback
    {
        public int mderr = 0;
        public int reverr = 0;
        public boolean keepGoing = false;

        public void onVerifyError(long revision, ClientException verifyError)
            throws ClientException
        {
            if (revision == Revision.SVN_INVALID_REVNUM) {
                ++mderr;
            }
            else {
                ++reverr;
            }
            if (keepGoing) {
                return;
            }
            else {
                throw verifyError;
            }
        }

    }

    private boolean tryToBreakRepo(OneTest test) throws IOException
    {
        File repo = test.getRepository();

        // Check for a sharded repo first
        File rev1 = new File(repo, "db/revs/0/1");
        if (!rev1.exists() || !rev1.setWritable(true))
        {
            // Try non-sharded
            rev1 = new File(repo, "db/revs/1");
        }
        if (!rev1.exists() || !rev1.setWritable(true))
            return false;

        FileWriter fd = new FileWriter(rev1);
        fd.write("inserting junk to corrupt the rev");
        fd.close();
        return true;
    }

    public void testVerifyBrokenRepo() throws Throwable
    {
        OneTest thisTest = new OneTest(false, true);

        if (!tryToBreakRepo(thisTest)) {
            // We don't support the repos format
            System.err.print("Cannot break repository for verify test.");
            return;
        }

        VerifyCallback cb = new VerifyCallback();
        cb.keepGoing = false;

        try {
            admin.verify(thisTest.getRepository(),
                         Revision.getInstance(0),
                         Revision.HEAD,
                         false, false, null, cb);
        }
        catch(ClientException ex) {
            assertEquals(cb.mderr, 1);
            assertEquals(cb.reverr, 0);
            return;
        }

        assert("Verify did not catch repository corruption." == "");
    }

    public void testVerifyBrokenRepo_KeepGoing() throws Throwable
    {
        OneTest thisTest = new OneTest(false, true);

        if (!tryToBreakRepo(thisTest)) {
            // We don't support the repos format
            System.err.print("Cannot break repository for verify test.");
            return;
        }

        VerifyCallback cb = new VerifyCallback();
        cb.keepGoing = true;

        admin.verify(thisTest.getRepository(),
                     Revision.getInstance(0),
                     Revision.HEAD,
                     false, false, null, cb);

        assertEquals(cb.mderr, 1);
        assertEquals(cb.reverr, 1);
    }

    /* this test only tests the call down to the C++ layer. */
    public void testUpgrade()
        throws SubversionException, IOException
    {
        OneTest thisTest = new OneTest(false);
        admin.upgrade(thisTest.getRepository(), null);
    }

    /* This test only tests the call down to the C++ layer. */
    public void testPack()
        throws SubversionException, IOException
    {
        OneTest thisTest = new OneTest(false);
        admin.pack(thisTest.getRepository(), null);
    }

    /* Check that the freeze() callback gets invoked. */
    private class FreezeAction implements ReposFreezeAction
    {
        int invoked = 0;
        public void invoke() { ++invoked; }
    }

    public void testFreeze()
        throws SubversionException, IOException
    {
        OneTest thisTest = new OneTest(false);
        FreezeAction action = new FreezeAction();
        admin.freeze(action, thisTest.getRepository());
        assertEquals("expect freeze callback to be invoked once", 1, action.invoked);
    }

    public void testLoadRepo()
        throws SubversionException, IOException
    {
        /* Make sure ISVNRepos.load() works, with a repo dump file known
         * to provoke bug 2979
         */
        // makes repos with nothing in it
        OneTest thisTest = new OneTest(false,false);
        // verify zero revisions in new repos
        URI repoUrl = makeReposUrl(thisTest.getRepository());
        final Info[] infoHolder = new Info[1];
        InfoCallback mycallback = new InfoCallback()
        {
            public void singleInfo(Info info)
            {
                infoHolder[0] = info;
            }
        };
        client.info2(repoUrl.toString(), Revision.HEAD, Revision.HEAD,
                Depth.immediates, null, mycallback);
        assertNotNull("expect info callback", infoHolder[0]);
        assertEquals("expect zero revisions in new repository",
                0L, infoHolder[0].getLastChangedRev());

        // locate dump file in test environment
        String testSrcdir = System.getProperty("test.srcdir",
                "subversion/bindings/javahl");
        File dump = new File(testSrcdir, "tests/data/issue2979.dump");
        InputStream input = new FileInputStream(dump);
        admin.load(thisTest.getRepository(),
                   input, true, true, false, false, null, null);
        // should have two revs after the load
        infoHolder[0] = null;
        client.info2(repoUrl.toString(), Revision.HEAD, Revision.HEAD,
                     Depth.immediates, null, mycallback);
        assertEquals("expect two revisions after load()",
                     2L, infoHolder[0].getLastChangedRev());
        // verify that the repos is faithful rep. of the dump file,
        // e.g., correct author
        assertEquals("expect 'svn4ant' as author of r2",
                     "svn4ant", infoHolder[0].getLastChangedAuthor());
    }
}