/**
  * Copyright 2014 Google Inc. All rights reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  *     http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
*/

package com.google.mr4c.sources;

import java.io.File;
import java.io.RandomAccessFile;
import java.net.URI;
import java.util.Arrays;

import org.junit.*;
import static org.junit.Assert.*;

public class RandomAccessFileSinkTest {

	private byte[] m_data = new byte[] { 33, 44, 55, 66};

	@Test public void testLocal() throws Exception {
		File file = new File("output/rafsink/test1.bin");
		URI uri = file.toURI();
		URIDataFileSink sink = new URIDataFileSink(uri, "test1.bin", file);
		doTest(sink, false);
		URIDataFileSource src = new URIDataFileSource(uri);
		assertTrue("data", Arrays.equals(m_data, src.getFileBytes()));
		
	}

	@Test public void testStaged() throws Exception {
		BytesDataFileSink sink = new BytesDataFileSink();
		doTest(sink, true);
		assertTrue("data", Arrays.equals(m_data, sink.getWrittenBytes()));
	}

	private void doTest(DataFileSink sink, boolean staged) throws Exception {
		RandomAccessFileSink rafSink = new RandomAccessFileSink(sink);
		RandomAccessFile raf = rafSink.getRandomAccess();
		assertEquals("staged flag", staged, rafSink.isStaged());
		raf.write(m_data);
		rafSink.close();
	}

}

