View Javadoc
1   /*
2    * Copyright (C) 2016, 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  package org.eclipse.jgit.lfs.server.fs;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertFalse;
47  
48  import java.nio.file.Files;
49  import java.nio.file.Path;
50  
51  import org.eclipse.jgit.api.Git;
52  import org.eclipse.jgit.api.errors.JGitInternalException;
53  import org.eclipse.jgit.junit.JGitTestUtil;
54  import org.eclipse.jgit.junit.TestRepository;
55  import org.eclipse.jgit.lfs.BuiltinLFS;
56  import org.eclipse.jgit.lfs.lib.LongObjectId;
57  import org.eclipse.jgit.lib.ConfigConstants;
58  import org.eclipse.jgit.lib.Repository;
59  import org.eclipse.jgit.lib.StoredConfig;
60  import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
61  import org.eclipse.jgit.util.FileUtils;
62  import org.junit.After;
63  import org.junit.Before;
64  import org.junit.Test;
65  
66  public class CheckoutTest extends LfsServerTest {
67  
68  	Git git;
69  	private TestRepository tdb;
70  
71  	@Override
72  	@Before
73  	public void setup() throws Exception {
74  		super.setup();
75  
76  		BuiltinLFS.register();
77  
78  		Path tmp = Files.createTempDirectory("jgit_test_");
79  		Repository db = FileRepositoryBuilder
80  				.create(tmp.resolve(".git").toFile());
81  		db.create();
82  		StoredConfig cfg = db.getConfig();
83  		cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
84  				ConfigConstants.CONFIG_SECTION_LFS,
85  				ConfigConstants.CONFIG_KEY_USEJGITBUILTIN, true);
86  		cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
87  				ConfigConstants.CONFIG_SECTION_LFS,
88  				ConfigConstants.CONFIG_KEY_REQUIRED, false);
89  		cfg.setString(ConfigConstants.CONFIG_SECTION_LFS, null, "url",
90  				server.getURI().toString() + "/lfs");
91  		cfg.save();
92  
93  		tdb = new TestRepository<>(db);
94  		tdb.branch("test").commit()
95  				.add(".gitattributes",
96  						"*.bin filter=lfs diff=lfs merge=lfs -text ")
97  				.add("a.bin",
98  						"version https://git-lfs.github.com/spec/v1\noid sha256:8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414\nsize 7\n")
99  				.create();
100 		git = Git.wrap(db);
101 		tdb.branch("test2").commit().add(".gitattributes",
102 				"*.bin filter=lfs diff=lfs merge=lfs -text ").create();
103 	}
104 
105 	@After
106 	public void cleanup() throws Exception {
107 		tdb.getRepository().close();
108 		FileUtils.delete(tdb.getRepository().getWorkTree(),
109 				FileUtils.RECURSIVE);
110 	}
111 
112 	@Test
113 	public void testUnknownContent() throws Exception {
114 		git.checkout().setName("test").call();
115 		// unknown content. We will see the pointer file
116 		assertEquals(
117 				"version https://git-lfs.github.com/spec/v1\noid sha256:8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414\nsize 7\n",
118 				JGitTestUtil.read(git.getRepository(), "a.bin"));
119 		assertEquals("[POST /lfs/objects/batch 200]",
120 				server.getRequests().toString());
121 	}
122 
123 	@Test(expected = JGitInternalException.class)
124 	public void testUnknownContentRequired() throws Exception {
125 		StoredConfig cfg = tdb.getRepository().getConfig();
126 		cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
127 				ConfigConstants.CONFIG_SECTION_LFS,
128 				ConfigConstants.CONFIG_KEY_REQUIRED, true);
129 		cfg.save();
130 
131 		// must throw
132 		git.checkout().setName("test").call();
133 	}
134 
135 	@Test
136 	public void testKnownContent() throws Exception {
137 		putContent(
138 				LongObjectId.fromString(
139 						"8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414"),
140 				"1234567");
141 		git.checkout().setName("test").call();
142 		// known content. we will see the actual content of the LFS blob.
143 		assertEquals(
144 				"1234567",
145 				JGitTestUtil.read(git.getRepository(), "a.bin"));
146 		assertEquals(
147 				"[PUT /lfs/objects/8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414 200"
148 						+ ", POST /lfs/objects/batch 200"
149 						+ ", GET /lfs/objects/8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414 200]",
150 				server.getRequests().toString());
151 
152 		git.checkout().setName("test2").call();
153 		assertFalse(JGitTestUtil.check(git.getRepository(), "a.bin"));
154 		git.checkout().setName("test").call();
155 		// unknown content. We will see the pointer file
156 		assertEquals("1234567",
157 				JGitTestUtil.read(git.getRepository(), "a.bin"));
158 		assertEquals(3, server.getRequests().size());
159 	}
160 
161 }