Added some misisng methods to DataValuedAttribute and added a test.

This commit is contained in:
sk89q 2013-02-05 16:25:59 -08:00
parent 919c03a57b
commit 040e35f65b
3 changed files with 130 additions and 18 deletions

View File

@ -22,6 +22,8 @@
import java.io.DataOutputStream;
import java.io.IOException;
import org.apache.commons.lang.Validate;
/**
* A simple implementation of {@link Attribute} that saves the raw binary
* data stream for exact recall later during runtime and during serialization.
@ -38,6 +40,56 @@ public final class DataValuedAttribute extends Attribute {
private byte[] buffer;
/**
* Construct the attribute with a default name.
*/
public DataValuedAttribute() {
super();
}
/**
* Construct the attribute with a given name.
*/
public DataValuedAttribute(String name) {
super(name);
}
/**
* Get the raw byte array.
* <p>
* Modifying the returned byte array directly modifies the byte
* array stored within the instance of this class.
*
* @return data
*/
public byte[] getByteArray() {
return buffer;
}
/**
* Set the raw byte array.
* <p>
* The given byte array is not copied and so further modifications to the
* given byte array will have the changes reflect on the byte array
* inside this object.
*
* @param data new data
*/
public void setByteArray(byte[] data) {
Validate.notNull(data);
this.buffer = data;
}
/**
* Get the size in bytes of the data.
*
* @return length in bytes of data
*/
public int size() {
return buffer.length;
}
@Override
public void read(DataInputStream in, int len) throws IOException {
byte[] buffer = new byte[len];

View File

@ -29,49 +29,53 @@
import org.junit.Test;
public class AttributeTest {
@Test
public void testHashCode() {
assertEquals(new TestAttribute("testing").hashCode(), "testing".hashCode());
assertFalse(new TestAttribute("broken").hashCode() == "testing".hashCode());
private Attribute makeAttribute(String name) {
return new TestAttribute(name);
}
@Test
public void testAttribute() {
new TestAttribute("testing");
public final void testHashCode() {
assertEquals(makeAttribute("testing").hashCode(), "testing".hashCode());
assertFalse(makeAttribute("broken").hashCode() == "testing".hashCode());
}
@Test
public void testGetName() {
assertEquals(new TestAttribute("testing").getName(), "testing");
assertFalse(new TestAttribute("testing").getName().equals("broken"));
public final void testAttribute() {
makeAttribute("testing");
}
@Test
public void testSetName() {
assertEquals(new TestAttribute("testing").getName(), "testing");
assertFalse(new TestAttribute("testing").getName().equals("broken"));
public final void testGetName() {
assertEquals(makeAttribute("testing").getName(), "testing");
assertFalse(makeAttribute("testing").getName().equals("broken"));
}
@Test
public final void testSetName() {
assertEquals(makeAttribute("testing").getName(), "testing");
assertFalse(makeAttribute("testing").getName().equals("broken"));
}
@Test
public void testRead() throws IOException {
Attribute attribute = new TestAttribute("testing");
Attribute attribute = makeAttribute("testing");
byte[] data = new byte[] { 1, 2, 3, 4, 5, 6 };
attribute.read(new DataInputStream(new ByteArrayInputStream(data)), data.length); // Do nothing
}
@Test
public void testWrite() throws IOException {
Attribute attribute = new TestAttribute("testing");
Attribute attribute = makeAttribute("testing");
ByteArrayOutputStream stream = new ByteArrayOutputStream(100);
attribute.write(new DataOutputStream(stream)); // Do nothing
assertEquals(stream.size(), 0);
}
@Test
public void testEqualsObject() {
assertTrue(new TestAttribute("testing").equals(new TestAttribute("testing")));
assertFalse(new TestAttribute("testing").equals(new TestAttribute("broken")));
public final void testEqualsObject() {
assertTrue(makeAttribute("testing").equals(makeAttribute("testing")));
assertFalse(makeAttribute("testing").equals(makeAttribute("broken")));
}
}

View File

@ -0,0 +1,56 @@
// $Id$
/*
* This file is a part of WorldGuard.
* Copyright (c) sk89q <http://www.sk89q.com>
* Copyright (c) the WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY), without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldguard.region;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.junit.Test;
public class DataValuedAttributeTest extends AttributeTest {
private DataValuedAttribute makeAttribute(String name) {
return new DataValuedAttribute(name);
}
@Test
public void testRead() throws IOException {
DataValuedAttribute attribute = makeAttribute("testing");
byte[] data = new byte[] { 1, 2, 3, 4, 5, 6 };
attribute.read(new DataInputStream(new ByteArrayInputStream(data)), data.length);
assertArrayEquals(data, attribute.getByteArray());
}
@Test
public void testWrite() throws IOException {
byte[] data = new byte[] { 1, 2, 3, 4, 5, 6 };
DataValuedAttribute attribute = makeAttribute("testing");
attribute.setByteArray(data);
ByteArrayOutputStream stream = new ByteArrayOutputStream(100);
attribute.write(new DataOutputStream(stream));
assertArrayEquals(data, stream.toByteArray());
}
}