View Javadoc
1   /*
2    * Copyright (C) 2018, David Pursehouse <david.pursehouse@gmail.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.lib;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNull;
15  
16  import java.io.File;
17  import java.io.FileInputStream;
18  import java.io.FileOutputStream;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  
22  import org.junit.Test;
23  
24  public class ObjectIdSerializerTest {
25  	@Test
26  	public void serialize() throws Exception {
27  		ObjectId original = new ObjectId(1, 2, 3, 4, 5);
28  		ObjectId deserialized = writeAndReadBackFromTempFile(original);
29  		assertEquals(original, deserialized);
30  	}
31  
32  	@Test
33  	public void serializeZeroId() throws Exception {
34  		ObjectId original = ObjectId.zeroId();
35  		ObjectId deserialized = writeAndReadBackFromTempFile(original);
36  		assertEquals(original, deserialized);
37  	}
38  
39  	@Test
40  	public void serializeNull() throws Exception {
41  		ObjectId deserialized = writeAndReadBackFromTempFile(null);
42  		assertNull(deserialized);
43  	}
44  
45  	private ObjectId writeAndReadBackFromTempFile(ObjectId objectId)
46  			throws Exception {
47  		File file = File.createTempFile("ObjectIdSerializerTest_", "");
48  		try (OutputStream out = new FileOutputStream(file)) {
49  			if (objectId == null) {
50  				ObjectIdSerializer.write(out, objectId);
51  			} else {
52  				ObjectIdSerializer.writeWithoutMarker(out, objectId);
53  			}
54  		}
55  		try (InputStream in = new FileInputStream(file)) {
56  			if (objectId == null) {
57  				return ObjectIdSerializer.read(in);
58  			}
59  			return ObjectIdSerializer.readWithoutMarker(in);
60  		}
61  	}
62  }