mirror of
https://github.com/EngineHub/WorldGuard.git
synced 2024-11-16 23:55:23 +01:00
Added some misisng methods to DataValuedAttribute and added a test.
This commit is contained in:
parent
919c03a57b
commit
040e35f65b
@ -22,6 +22,8 @@
|
|||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.Validate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple implementation of {@link Attribute} that saves the raw binary
|
* A simple implementation of {@link Attribute} that saves the raw binary
|
||||||
* data stream for exact recall later during runtime and during serialization.
|
* data stream for exact recall later during runtime and during serialization.
|
||||||
@ -38,6 +40,56 @@ public final class DataValuedAttribute extends Attribute {
|
|||||||
|
|
||||||
private byte[] buffer;
|
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
|
@Override
|
||||||
public void read(DataInputStream in, int len) throws IOException {
|
public void read(DataInputStream in, int len) throws IOException {
|
||||||
byte[] buffer = new byte[len];
|
byte[] buffer = new byte[len];
|
||||||
|
@ -29,49 +29,53 @@
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class AttributeTest {
|
public class AttributeTest {
|
||||||
|
|
||||||
@Test
|
private Attribute makeAttribute(String name) {
|
||||||
public void testHashCode() {
|
return new TestAttribute(name);
|
||||||
assertEquals(new TestAttribute("testing").hashCode(), "testing".hashCode());
|
|
||||||
assertFalse(new TestAttribute("broken").hashCode() == "testing".hashCode());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAttribute() {
|
public final void testHashCode() {
|
||||||
new TestAttribute("testing");
|
assertEquals(makeAttribute("testing").hashCode(), "testing".hashCode());
|
||||||
|
assertFalse(makeAttribute("broken").hashCode() == "testing".hashCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetName() {
|
public final void testAttribute() {
|
||||||
assertEquals(new TestAttribute("testing").getName(), "testing");
|
makeAttribute("testing");
|
||||||
assertFalse(new TestAttribute("testing").getName().equals("broken"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetName() {
|
public final void testGetName() {
|
||||||
assertEquals(new TestAttribute("testing").getName(), "testing");
|
assertEquals(makeAttribute("testing").getName(), "testing");
|
||||||
assertFalse(new TestAttribute("testing").getName().equals("broken"));
|
assertFalse(makeAttribute("testing").getName().equals("broken"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void testSetName() {
|
||||||
|
assertEquals(makeAttribute("testing").getName(), "testing");
|
||||||
|
assertFalse(makeAttribute("testing").getName().equals("broken"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRead() throws IOException {
|
public void testRead() throws IOException {
|
||||||
Attribute attribute = new TestAttribute("testing");
|
Attribute attribute = makeAttribute("testing");
|
||||||
byte[] data = new byte[] { 1, 2, 3, 4, 5, 6 };
|
byte[] data = new byte[] { 1, 2, 3, 4, 5, 6 };
|
||||||
attribute.read(new DataInputStream(new ByteArrayInputStream(data)), data.length); // Do nothing
|
attribute.read(new DataInputStream(new ByteArrayInputStream(data)), data.length); // Do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWrite() throws IOException {
|
public void testWrite() throws IOException {
|
||||||
Attribute attribute = new TestAttribute("testing");
|
Attribute attribute = makeAttribute("testing");
|
||||||
ByteArrayOutputStream stream = new ByteArrayOutputStream(100);
|
ByteArrayOutputStream stream = new ByteArrayOutputStream(100);
|
||||||
attribute.write(new DataOutputStream(stream)); // Do nothing
|
attribute.write(new DataOutputStream(stream)); // Do nothing
|
||||||
assertEquals(stream.size(), 0);
|
assertEquals(stream.size(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEqualsObject() {
|
public final void testEqualsObject() {
|
||||||
assertTrue(new TestAttribute("testing").equals(new TestAttribute("testing")));
|
assertTrue(makeAttribute("testing").equals(makeAttribute("testing")));
|
||||||
assertFalse(new TestAttribute("testing").equals(new TestAttribute("broken")));
|
assertFalse(makeAttribute("testing").equals(makeAttribute("broken")));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user