/*
* Copyright 2009 Red Hat, Inc.
* Red Hat licenses this file to you 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 org.hornetq.tests.unit.core.journal.impl;
import java.nio.ByteBuffer;
import junit.framework.Assert;
import org.hornetq.core.asyncio.impl.AsynchronousFileImpl;
import org.hornetq.core.journal.SequentialFileFactory;
import org.hornetq.core.journal.impl.AIOSequentialFileFactory;
import org.hornetq.core.journal.impl.NIOSequentialFileFactory;
import org.hornetq.tests.unit.core.journal.impl.fakes.FakeSequentialFileFactory;
import org.hornetq.tests.util.UnitTestCase;
/**
*
* @author <a href="mailto:clebert.suconic@jboss.com">Clebert Suconic</a>
*
*/
public class CleanBufferTest extends UnitTestCase
{
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
public void testCleanOnNIO()
{
SequentialFileFactory factory = new NIOSequentialFileFactory("Whatever");
testBuffer(factory);
}
public void testCleanOnAIO()
{
if (AsynchronousFileImpl.isLoaded())
{
SequentialFileFactory factory = new AIOSequentialFileFactory("Whatever");
testBuffer(factory);
}
}
public void testCleanOnFake()
{
SequentialFileFactory factory = new FakeSequentialFileFactory();
testBuffer(factory);
}
private void testBuffer(final SequentialFileFactory factory)
{
ByteBuffer buffer = factory.newBuffer(100);
try
{
for (byte b = 0; b < 100; b++)
{
buffer.put(b);
}
buffer.rewind();
for (byte b = 0; b < 100; b++)
{
Assert.assertEquals(b, buffer.get());
}
buffer.limit(10);
factory.clearBuffer(buffer);
buffer.limit(100);
buffer.rewind();
for (byte b = 0; b < 100; b++)
{
if (b < 10)
{
Assert.assertEquals(0, buffer.get());
}
else
{
Assert.assertEquals(b, buffer.get());
}
}
}
finally
{
factory.releaseBuffer(buffer);
}
}
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
}