1 /*
2 * Copyright (C) 2012, Christian Halstrick <christian.halstrick@sap.com>
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 package org.eclipse.jgit.internal.storage.file;
45
46 import java.io.IOException;
47
48 import org.eclipse.jgit.internal.storage.file.GC.RepoStatistics;
49 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
50 import org.eclipse.jgit.junit.RepositoryTestCase;
51 import org.eclipse.jgit.junit.TestRepository;
52 import org.eclipse.jgit.junit.TestRepository.CommitBuilder;
53 import org.eclipse.jgit.lib.AnyObjectId;
54 import org.eclipse.jgit.revwalk.RevCommit;
55 import org.eclipse.jgit.revwalk.RevWalk;
56 import org.junit.After;
57 import org.junit.Before;
58
59 public abstract class GcTestCase extends LocalDiskRepositoryTestCase {
60 protected TestRepository<FileRepository> tr;
61 protected FileRepository repo;
62 protected GC gc;
63 protected RepoStatistics stats;
64
65 @Override
66 @Before
67 public void setUp() throws Exception {
68 super.setUp();
69 repo = createWorkRepository();
70 tr = new TestRepository<>(repo, new RevWalk(repo),
71 mockSystemReader);
72 gc = new GC(repo);
73 }
74
75 @Override
76 @After
77 public void tearDown() throws Exception {
78 super.tearDown();
79 }
80
81 /**
82 * Create a chain of commits of given depth.
83 * <p>
84 * Each commit contains one file named "a" containing the index of the
85 * commit in the chain as its content. The created commit chain is
86 * referenced from any ref.
87 * <p>
88 * A chain of depth = N will create 3*N objects in Gits object database. For
89 * each depth level three objects are created: the commit object, the
90 * top-level tree object and a blob for the content of the file "a".
91 *
92 * @param depth
93 * the depth of the commit chain.
94 * @return the commit that is the tip of the commit chain
95 * @throws Exception
96 */
97 protected RevCommit commitChain(int depth) throws Exception {
98 if (depth <= 0)
99 throw new IllegalArgumentException("Chain depth must be > 0");
100 CommitBuilder cb = tr.commit();
101 RevCommit tip;
102 do {
103 --depth;
104 tip = cb.add("a", "" + depth).message("" + depth).create();
105 cb = cb.child();
106 } while (depth > 0);
107 return tip;
108 }
109
110 /**
111 * Create a chain of commits of given depth with given number of added files
112 * per commit.
113 * <p>
114 * Each commit contains {@code files} files as its content. The created
115 * commit chain is referenced from any ref.
116 * <p>
117 * A chain will create {@code (2 + files) * depth} objects in Gits object
118 * database. For each depth level the following objects are created: the
119 * commit object, the top-level tree object and @code files} blobs for the
120 * content of the file "a".
121 *
122 * @param depth
123 * the depth of the commit chain.
124 * @param width
125 * number of files added per commit
126 * @return the commit that is the tip of the commit chain
127 * @throws Exception
128 */
129 protected RevCommit commitChain(int depth, int width) throws Exception {
130 if (depth <= 0) {
131 throw new IllegalArgumentException("Chain depth must be > 0");
132 }
133 if (width <= 0) {
134 throw new IllegalArgumentException("Number of files per commit must be > 0");
135 }
136 CommitBuilder cb = tr.commit();
137 RevCommit tip = null;
138 do {
139 --depth;
140 for (int i=0; i < width; i++) {
141 String id = depth + "-" + i;
142 cb.add("a" + id, id).message(id);
143 }
144 tip = cb.create();
145 cb = cb.child();
146 } while (depth > 0);
147 return tip;
148 }
149
150 protected long lastModified(AnyObjectId objectId) {
151 return repo.getFS()
152 .lastModifiedInstant(repo.getObjectDatabase().fileFor(objectId))
153 .toEpochMilli();
154 }
155
156 protected static void fsTick() throws InterruptedException, IOException {
157 RepositoryTestCase.fsTick(null);
158 }
159 }