Make sure we get the right storage for loose/pack/loose and packed refs
[egit/chris.git] / org.spearce.jgit.test / tst / org / spearce / jgit / lib / RefTest.java
blob440686d3e4729d20efb45c1eeb4477be5bdd96ba
1 /*
2 * Copyright (C) 2009, Robin Rosenberg
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.
37 package org.spearce.jgit.lib;
39 import java.io.File;
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42 import java.util.Map;
44 import org.spearce.jgit.lib.Ref.Storage;
45 import org.spearce.jgit.lib.RefUpdate.Result;
47 /**
48 * Misc tests for refs. A lot of things are tested elsewhere so not having a
49 * test for a ref related method, does not mean it is untested.
51 public class RefTest extends RepositoryTestCase {
53 public void testReadAllIncludingSymrefs() throws Exception {
54 ObjectId masterId = db.resolve("refs/heads/master");
55 RefUpdate updateRef = db.updateRef("refs/remotes/origin/master");
56 updateRef.setNewObjectId(masterId);
57 updateRef.setForceUpdate(true);
58 updateRef.update();
60 .writeSymref("refs/remotes/origin/HEAD",
61 "refs/remotes/origin/master");
63 ObjectId r = db.resolve("refs/remotes/origin/HEAD");
64 assertEquals(masterId, r);
66 Map<String, Ref> allRefs = db.getAllRefs();
67 Ref refHEAD = allRefs.get("refs/remotes/origin/HEAD");
68 assertNotNull(refHEAD);
69 assertEquals(masterId, refHEAD.getObjectId());
70 assertTrue(refHEAD.isPeeled());
71 assertNull(refHEAD.getPeeledObjectId());
73 Ref refmaster = allRefs.get("refs/remotes/origin/master");
74 assertEquals(masterId, refmaster.getObjectId());
75 assertFalse(refmaster.isPeeled());
76 assertNull(refmaster.getPeeledObjectId());
79 public void testReadSymRefToPacked() throws IOException {
80 db.writeSymref("HEAD", "refs/heads/b");
81 Ref ref = db.getRef("HEAD");
82 assertEquals(Ref.Storage.LOOSE_PACKED, ref.getStorage());
85 public void testReadSymRefToLoosePacked() throws IOException {
86 ObjectId pid = db.resolve("refs/heads/master^");
87 RefUpdate updateRef = db.updateRef("refs/heads/master");
88 updateRef.setNewObjectId(pid);
89 updateRef.setForceUpdate(true);
90 Result update = updateRef.update();
91 assertEquals(Result.FORCED, update); // internal
93 db.writeSymref("HEAD", "refs/heads/master");
94 Ref ref = db.getRef("HEAD");
95 assertEquals(Ref.Storage.LOOSE_PACKED, ref.getStorage());
98 public void testReadLooseRef() throws IOException {
99 RefUpdate updateRef = db.updateRef("ref/heads/new");
100 updateRef.setNewObjectId(db.resolve("refs/heads/master"));
101 Result update = updateRef.update();
102 assertEquals(Result.NEW, update);
103 Ref ref = db.getRef("ref/heads/new");
104 assertEquals(Storage.LOOSE, ref.getStorage());
108 * Let an "outsider" create a loose ref with the same name as a packed one
110 * @throws IOException
111 * @throws InterruptedException
113 public void testReadLoosePackedRef() throws IOException,
114 InterruptedException {
115 Ref ref = db.getRef("refs/heads/master");
116 assertEquals(Storage.PACKED, ref.getStorage());
117 FileOutputStream os = new FileOutputStream(new File(db.getDirectory(),
118 "refs/heads/master"));
119 os.write(ref.getObjectId().name().getBytes());
120 os.write('\n');
121 os.close();
123 ref = db.getRef("refs/heads/master");
124 assertEquals(Storage.LOOSE_PACKED, ref.getStorage());
128 * Modify a packed ref using the API. This creates a loose ref too, ie.
129 * LOOSE_PACKED
131 * @throws IOException
133 public void testReadSimplePackedRefSameRepo() throws IOException {
134 Ref ref = db.getRef("refs/heads/master");
135 ObjectId pid = db.resolve("refs/heads/master^");
136 assertEquals(Storage.PACKED, ref.getStorage());
137 RefUpdate updateRef = db.updateRef("refs/heads/master");
138 updateRef.setNewObjectId(pid);
139 updateRef.setForceUpdate(true);
140 Result update = updateRef.update();
141 assertEquals(Result.FORCED, update);
143 ref = db.getRef("refs/heads/master");
144 assertEquals(Storage.LOOSE_PACKED, ref.getStorage());