/*******************************************************************************
 * Copyright (c) 2016 Sebastian Stenzel and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the accompanying LICENSE.txt.
 *
 * Contributors:
 *     Sebastian Stenzel - initial API and implementation
 *******************************************************************************/
package org.cryptomator.cryptofs.ch;

import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;

import static org.hamcrest.CoreMatchers.is;

public class ByteSourceTest {

	@Nested
	public class FromByteBuffer {

		@Test
		public void testHasRemainingDelegatesToBufferHasRemainingIfTrue() {
			ByteBuffer buffer = ByteBuffer.allocate(1);
			ByteSource inTest = ByteSource.from(buffer);

			MatcherAssert.assertThat(inTest.hasRemaining(), is(true));
		}

		@Test
		public void testHasRemainingDelegatesToBufferHasRemainingIfFalse() {
			ByteBuffer buffer = ByteBuffer.allocate(0);
			ByteSource inTest = ByteSource.from(buffer);

			MatcherAssert.assertThat(inTest.hasRemaining(), is(false));
		}

		@Test
		public void testRemainingDelegatesToBuffersRemaining() {
			int expectedResult = 3;
			ByteBuffer buffer = ByteBuffer.allocate(expectedResult);
			ByteSource inTest = ByteSource.from(buffer);

			MatcherAssert.assertThat(inTest.remaining(), is((long) expectedResult));
		}

		@Test
		public void testCopyToWithLessRemainingInSourceThanInTarget() {
			ByteBuffer sourceBuffer = ByteBuffer.allocate(10);
			sourceBuffer.position(5);
			ByteBuffer targetBuffer = ByteBuffer.allocate(20);
			targetBuffer.position(3);
			ByteSource inTest = ByteSource.from(sourceBuffer);

			inTest.copyTo(targetBuffer);

			MatcherAssert.assertThat(sourceBuffer.position(), is(10));
			MatcherAssert.assertThat(sourceBuffer.limit(), is(10));
			MatcherAssert.assertThat(targetBuffer.position(), is(8));
			MatcherAssert.assertThat(targetBuffer.limit(), is(20));
		}

		@Test
		public void testCopyToWithLessRemainingInTargetThanInSource() {
			ByteBuffer sourceBuffer = ByteBuffer.allocate(20);
			sourceBuffer.position(3);
			ByteBuffer targetBuffer = ByteBuffer.allocate(10);
			targetBuffer.position(5);
			ByteSource inTest = ByteSource.from(sourceBuffer);

			inTest.copyTo(targetBuffer);

			MatcherAssert.assertThat(sourceBuffer.position(), is(8));
			MatcherAssert.assertThat(sourceBuffer.limit(), is(20));
			MatcherAssert.assertThat(targetBuffer.position(), is(10));
			MatcherAssert.assertThat(targetBuffer.limit(), is(10));
		}

	}

}

@@ -498,16 +498,57 @@
 Assert;%0A
+import org.junit.jupiter.api.Assertions;%0A
 import o
@@ -639,16 +639,41 @@
 eBuffer;
+%0Aimport java.util.Arrays;
 %0A%0Aimport
@@ -2703,10 +2703,845 @@
 %09%7D%0A%0A%09%7D%0A%0A
+%09@Nested%0A%09public class RepeatingZeros %7B%0A%0A%09%09@Test%0A%09%09public void testRemainingCombinesZerosWithBuffer() %7B%0A%09%09%09ByteBuffer buffer = ByteBuffer.wrap(new byte%5B%5D%7B(byte) 0xFF%7D);%0A%09%09%09ByteSource inTest = ByteSource.repeatingZeroes(41).followedBy(buffer);%0A%0A%09%09%09Assertions.assertTrue(inTest.hasRemaining());%0A%09%09%09Assertions.assertEquals(42, inTest.remaining());%0A%09%09%7D%0A%0A%09%09@Test%0A%09%09public void testCopyToWritesZeros() %7B%0A%09%09%09ByteBuffer buffer = ByteBuffer.wrap(new byte%5B%5D%7B(byte) 0x77%7D);%0A%09%09%09ByteSource inTest = ByteSource.repeatingZeroes(41).followedBy(buffer);%0A%09%09%09byte%5B%5D target = new byte%5B50%5D;%0A%09%09%09Arrays.fill(target, (byte) 0xFF); // pre-fill target to check whether data gets zero'ed%0A%0A%09%09%09inTest.copyTo(ByteBuffer.wrap(target));%0A%0A%09%09%09Assertions.assertArrayEquals(new byte%5B41%5D, Arrays.copyOf(target, 41));%0A%09%09%09Assertions.assertEquals(0x77, target%5B41%5D);%0A%09%09%7D%0A%0A%09%7D%0A%0A
 %7D%0A

add unit test to make sure to write 0x00 bytes