You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectIdSerializerTest.java 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. package org.eclipse.jgit.lib;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertNull;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileOutputStream;
  16. import java.io.InputStream;
  17. import java.io.OutputStream;
  18. import org.junit.Test;
  19. public class ObjectIdSerializerTest {
  20. @Test
  21. public void serialize() throws Exception {
  22. ObjectId original = new ObjectId(1, 2, 3, 4, 5);
  23. ObjectId deserialized = writeAndReadBackFromTempFile(original);
  24. assertEquals(original, deserialized);
  25. }
  26. @Test
  27. public void serializeZeroId() throws Exception {
  28. ObjectId original = ObjectId.zeroId();
  29. ObjectId deserialized = writeAndReadBackFromTempFile(original);
  30. assertEquals(original, deserialized);
  31. }
  32. @Test
  33. public void serializeNull() throws Exception {
  34. ObjectId deserialized = writeAndReadBackFromTempFile(null);
  35. assertNull(deserialized);
  36. }
  37. private ObjectId writeAndReadBackFromTempFile(ObjectId objectId)
  38. throws Exception {
  39. File file = File.createTempFile("ObjectIdSerializerTest_", "");
  40. try (OutputStream out = new FileOutputStream(file)) {
  41. if (objectId == null) {
  42. ObjectIdSerializer.write(out, objectId);
  43. } else {
  44. ObjectIdSerializer.writeWithoutMarker(out, objectId);
  45. }
  46. }
  47. try (InputStream in = new FileInputStream(file)) {
  48. if (objectId == null) {
  49. return ObjectIdSerializer.read(in);
  50. }
  51. return ObjectIdSerializer.readWithoutMarker(in);
  52. }
  53. }
  54. }