Add tests to increase coverage of WindowCache
[jgit.git] / org.spearce.jgit.test / tst / org / spearce / jgit / lib / WindowCacheGetTest.java
blobc19bb2bcbb8cbde2c4fc9dcaa75515aae3ad5663
1 /*
2 * Copyright (C) 2009, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.lib;
40 import java.io.BufferedReader;
41 import java.io.FileInputStream;
42 import java.io.IOException;
43 import java.io.InputStreamReader;
44 import java.util.ArrayList;
45 import java.util.List;
47 import org.spearce.jgit.errors.CorruptObjectException;
48 import org.spearce.jgit.util.JGitTestUtil;
49 import org.spearce.jgit.util.MutableInteger;
51 public class WindowCacheGetTest extends RepositoryTestCase {
52 private List<TestObject> toLoad;
54 @Override
55 public void setUp() throws Exception {
56 super.setUp();
58 toLoad = new ArrayList<TestObject>();
59 final BufferedReader br = new BufferedReader(new InputStreamReader(
60 new FileInputStream(JGitTestUtil
61 .getTestResourceFile("all_packed_objects.txt")),
62 Constants.CHARSET));
63 try {
64 String line;
65 while ((line = br.readLine()) != null) {
66 final String[] parts = line.split(" {1,}");
67 final TestObject o = new TestObject();
68 o.id = ObjectId.fromString(parts[0]);
69 o.setType(parts[1]);
70 o.rawSize = Integer.parseInt(parts[2]);
71 // parts[3] is the size-in-pack
72 o.offset = Long.parseLong(parts[4]);
73 toLoad.add(o);
75 } finally {
76 br.close();
78 assertEquals(96, toLoad.size());
81 public void testCache_Defaults() throws IOException {
82 final WindowCacheConfig cfg = new WindowCacheConfig();
83 WindowCache.reconfigure(cfg);
84 doCacheTests();
85 checkLimits(cfg);
87 final WindowCache cache = WindowCache.getInstance();
88 assertEquals(6, cache.getOpenFiles());
89 assertEquals(17346, cache.getOpenBytes());
92 public void testCache_TooFewFiles() throws IOException {
93 final WindowCacheConfig cfg = new WindowCacheConfig();
94 cfg.setPackedGitOpenFiles(2);
95 WindowCache.reconfigure(cfg);
96 doCacheTests();
97 checkLimits(cfg);
100 public void testCache_TooSmallLimit() throws IOException {
101 final WindowCacheConfig cfg = new WindowCacheConfig();
102 cfg.setPackedGitWindowSize(4096);
103 cfg.setPackedGitLimit(4096);
104 WindowCache.reconfigure(cfg);
105 doCacheTests();
106 checkLimits(cfg);
109 private void checkLimits(final WindowCacheConfig cfg) {
110 final WindowCache cache = WindowCache.getInstance();
111 assertTrue(cache.getOpenFiles() <= cfg.getPackedGitOpenFiles());
112 assertTrue(cache.getOpenBytes() <= cfg.getPackedGitLimit());
113 assertTrue(0 < cache.getOpenFiles());
114 assertTrue(0 < cache.getOpenBytes());
117 private void doCacheTests() throws IOException {
118 for (final TestObject o : toLoad) {
119 final ObjectLoader or = db.openObject(o.id);
120 assertNotNull(or);
121 assertTrue(or instanceof PackedObjectLoader);
122 assertEquals(o.type, or.getType());
123 assertEquals(o.rawSize, or.getRawSize());
124 assertEquals(o.offset, ((PackedObjectLoader) or).getObjectOffset());
128 private class TestObject {
129 ObjectId id;
131 int type;
133 int rawSize;
135 long offset;
137 void setType(final String typeStr) throws CorruptObjectException {
138 final byte[] typeRaw = Constants.encode(typeStr + " ");
139 final MutableInteger ptr = new MutableInteger();
140 type = Constants.decodeTypeString(id, typeRaw, (byte) ' ', ptr);