View Javadoc
1   /*
2    * Copyright (c) 2021 Qualcomm Innovation Center, Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.internal.storage.file;
13  
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertThrows;
16  import static org.junit.Assert.assertTrue;
17  
18  import java.io.File;
19  import org.eclipse.jgit.internal.storage.pack.PackExt;
20  import org.eclipse.jgit.lib.ObjectId;
21  import org.junit.Test;
22  
23  public class PackFileTest {
24  	private static final ObjectId TEST_OID = ObjectId
25  			.fromString("0123456789012345678901234567890123456789");
26  
27  	private static final String TEST_ID = TEST_OID.name();
28  
29  	private static final String PREFIX = "pack-";
30  
31  	private static final String OLD_PREFIX = "old-";
32  
33  	private static final String OLD_PACK = PREFIX + TEST_ID + "." + OLD_PREFIX
34  			+ PackExt.PACK.getExtension();
35  
36  	private static final File TEST_PACK_DIR = new File(
37  			"/path/to/repo.git/objects/pack");
38  
39  	private static final File TEST_PRESERVED_DIR = new File(TEST_PACK_DIR,
40  			"preserved");
41  
42  	private static final PackFile TEST_PACKFILE_NO_EXT = new PackFile(
43  			new File(TEST_PACK_DIR, PREFIX + TEST_ID));
44  
45  	@Test
46  	public void objectsAreSameFromAnyConstructor() throws Exception {
47  		String name = PREFIX + TEST_ID + "." + PackExt.PACK.getExtension();
48  		File pack = new File(TEST_PACK_DIR, name);
49  		PackFile pf = new PackFile(pack);
50  		PackFile pfFromDirAndName = new PackFile(TEST_PACK_DIR, name);
51  		assertPackFilesEqual(pf, pfFromDirAndName);
52  
53  		PackFile pfFromOIdAndExt = new PackFile(TEST_PACK_DIR, TEST_OID,
54  				PackExt.PACK);
55  		assertPackFilesEqual(pf, pfFromOIdAndExt);
56  
57  		PackFile pfFromIdAndExt = new PackFile(TEST_PACK_DIR, TEST_ID,
58  				PackExt.PACK);
59  		assertPackFilesEqual(pf, pfFromIdAndExt);
60  	}
61  
62  	@Test
63  	public void idIsSameFromFileWithOrWithoutExt() throws Exception {
64  		PackFile packWithExt = new PackFile(new File(TEST_PACK_DIR,
65  				PREFIX + TEST_ID + "." + PackExt.PACK.getExtension()));
66  		assertEquals(packWithExt.getId(), TEST_PACKFILE_NO_EXT.getId());
67  	}
68  
69  	@Test
70  	public void idIsSameFromFileWithOrWithoutPrefix() throws Exception {
71  		PackFile packWithoutPrefix = new PackFile(
72  				new File(TEST_PACK_DIR, TEST_ID));
73  		assertEquals(packWithoutPrefix.getId(), TEST_PACKFILE_NO_EXT.getId());
74  	}
75  
76  	@Test
77  	public void canCreatePreservedFromFile() throws Exception {
78  		PackFile preserved = new PackFile(
79  				new File(TEST_PRESERVED_DIR, OLD_PACK));
80  		assertTrue(preserved.getName().contains(OLD_PACK));
81  		assertEquals(preserved.getId(), TEST_ID);
82  		assertEquals(preserved.getPackExt(), PackExt.PACK);
83  	}
84  
85  	@Test
86  	public void canCreatePreservedFromDirAndName() throws Exception {
87  		PackFile preserved = new PackFile(TEST_PRESERVED_DIR, OLD_PACK);
88  		assertTrue(preserved.getName().contains(OLD_PACK));
89  		assertEquals(preserved.getId(), TEST_ID);
90  		assertEquals(preserved.getPackExt(), PackExt.PACK);
91  	}
92  
93  	@Test
94  	public void cannotCreatePreservedNoExtFromNonPreservedNoExt()
95  			throws Exception {
96  		assertThrows(IllegalArgumentException.class, () -> TEST_PACKFILE_NO_EXT
97  				.createPreservedForDirectory(TEST_PRESERVED_DIR));
98  	}
99  
100 	@Test
101 	public void canCreateAnyExtFromAnyExt() throws Exception {
102 		for (PackExt from : PackExt.values()) {
103 			PackFile dotFrom = TEST_PACKFILE_NO_EXT.create(from);
104 			for (PackExt to : PackExt.values()) {
105 				PackFile dotTo = dotFrom.create(to);
106 				File expected = new File(TEST_PACK_DIR,
107 						PREFIX + TEST_ID + "." + to.getExtension());
108 				assertEquals(dotTo.getPackExt(), to);
109 				assertEquals(dotFrom.getId(), dotTo.getId());
110 				assertEquals(expected.getName(), dotTo.getName());
111 			}
112 		}
113 	}
114 
115 	@Test
116 	public void canCreatePreservedFromAnyExt() throws Exception {
117 		for (PackExt ext : PackExt.values()) {
118 			PackFile nonPreserved = TEST_PACKFILE_NO_EXT.create(ext);
119 			PackFile preserved = nonPreserved
120 					.createPreservedForDirectory(TEST_PRESERVED_DIR);
121 			File expected = new File(TEST_PRESERVED_DIR,
122 					PREFIX + TEST_ID + "." + OLD_PREFIX + ext.getExtension());
123 			assertEquals(preserved.getName(), expected.getName());
124 			assertEquals(preserved.getId(), TEST_ID);
125 			assertEquals(preserved.getPackExt(), nonPreserved.getPackExt());
126 		}
127 	}
128 
129 	@Test
130 	public void canCreateAnyPreservedExtFromAnyPreservedExt() throws Exception {
131 		// Preserved PackFiles must have an extension
132 		PackFile preserved = new PackFile(TEST_PRESERVED_DIR, OLD_PACK);
133 		for (PackExt from : PackExt.values()) {
134 			PackFile preservedWithExt = preserved.create(from);
135 			for (PackExt to : PackExt.values()) {
136 				PackFile preservedNewExt = preservedWithExt.create(to);
137 				File expected = new File(TEST_PRESERVED_DIR, PREFIX + TEST_ID
138 						+ "." + OLD_PREFIX + to.getExtension());
139 				assertEquals(preservedNewExt.getPackExt(), to);
140 				assertEquals(preservedWithExt.getId(), preservedNewExt.getId());
141 				assertEquals(preservedNewExt.getName(), expected.getName());
142 			}
143 		}
144 	}
145 
146 	@Test
147 	public void canCreateNonPreservedFromAnyPreservedExt() throws Exception {
148 		// Preserved PackFiles must have an extension
149 		PackFile preserved = new PackFile(TEST_PRESERVED_DIR, OLD_PACK);
150 		for (PackExt ext : PackExt.values()) {
151 			PackFile preservedWithExt = preserved.create(ext);
152 			PackFile nonPreserved = preservedWithExt
153 					.createForDirectory(TEST_PACK_DIR);
154 			File expected = new File(TEST_PACK_DIR,
155 					PREFIX + TEST_ID + "." + ext.getExtension());
156 			assertEquals(nonPreserved.getName(), expected.getName());
157 			assertEquals(nonPreserved.getId(), TEST_ID);
158 			assertEquals(nonPreserved.getPackExt(),
159 					preservedWithExt.getPackExt());
160 		}
161 	}
162 
163 	private void assertPackFilesEqual(PackFile p1, PackFile p2) {
164 		// for test purposes, considered equal if id, name, and ext are equal
165 		assertEquals(p1.getId(), p2.getId());
166 		assertEquals(p1.getPackExt(), p2.getPackExt());
167 		assertEquals(p1.getName(), p2.getName());
168 	}
169 }