Add equals methods on objects, use for storetest

This commit is contained in:
Adam
2015-10-16 12:52:38 -04:00
parent bd5d57e94c
commit 298d623bbf
7 changed files with 257 additions and 22 deletions

View File

@@ -1,7 +1,9 @@
package net.runelite.cache.fs;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import net.runelite.cache.fs.io.InputStream;
public class Archive
@@ -19,6 +21,58 @@ public class Archive
this.index = index;
this.archiveId = id;
}
@Override
public int hashCode()
{
int hash = 7;
hash = 47 * hash + this.archiveId;
hash = 47 * hash + this.nameHash;
hash = 47 * hash + Arrays.hashCode(this.whirlpool);
hash = 47 * hash + this.crc;
hash = 47 * hash + this.revision;
hash = 47 * hash + Objects.hashCode(this.files);
return hash;
}
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final Archive other = (Archive) obj;
if (this.archiveId != other.archiveId)
{
return false;
}
if (this.nameHash != other.nameHash)
{
return false;
}
if (!Arrays.equals(this.whirlpool, other.whirlpool))
{
return false;
}
if (this.crc != other.crc)
{
return false;
}
if (this.revision != other.revision)
{
return false;
}
if (!Objects.equals(this.files, other.files))
{
return false;
}
return true;
}
public File addFile(int id)
{

View File

@@ -6,6 +6,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -16,11 +17,13 @@ public class DataFile implements Closeable
private static final int SECTOR_SIZE = 520;
private final Store store;
private final File file;
private final RandomAccessFile dat;
private final byte[] readCachedBuffer = new byte[SECTOR_SIZE];
public DataFile(Store store, File file) throws FileNotFoundException
{
this.file = file;
this.store = store;
dat = new RandomAccessFile(file, "rw");
}
@@ -30,6 +33,33 @@ public class DataFile implements Closeable
{
dat.close();
}
@Override
public int hashCode()
{
int hash = 7;
hash = 71 * hash + Objects.hashCode(this.file);
return hash;
}
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final DataFile other = (DataFile) obj;
if (!Objects.equals(this.file, other.file))
{
return false;
}
return true;
}
/**
*

View File

@@ -1,5 +1,7 @@
package net.runelite.cache.fs;
import java.util.Arrays;
public class File
{
private Archive archive;
@@ -13,6 +15,43 @@ public class File
this.fileId = fileId;
}
@Override
public int hashCode()
{
int hash = 7;
hash = 97 * hash + this.fileId;
hash = 97 * hash + this.nameHash;
hash = 97 * hash + Arrays.hashCode(this.contents);
return hash;
}
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final File other = (File) obj;
if (this.fileId != other.fileId)
{
return false;
}
if (this.nameHash != other.nameHash)
{
return false;
}
if (!Arrays.equals(this.contents, other.contents))
{
return false;
}
return true;
}
public Archive getArchive()
{
return archive;

View File

@@ -4,7 +4,9 @@ import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import net.runelite.cache.fs.io.InputStream;
import net.runelite.cache.fs.io.OutputStream;
import net.runelite.cache.fs.util.bzip2.BZip2Decompressor;
@@ -37,6 +39,58 @@ public class Index implements Closeable
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public int hashCode()
{
int hash = 3;
hash = 97 * hash + Objects.hashCode(this.index);
hash = 97 * hash + this.id;
hash = 97 * hash + this.revision;
hash = 97 * hash + this.crc;
hash = 97 * hash + Arrays.hashCode(this.whirlpool);
hash = 97 * hash + Objects.hashCode(this.archives);
return hash;
}
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final Index other = (Index) obj;
if (!Objects.equals(this.index, other.index))
{
return false;
}
if (this.id != other.id)
{
return false;
}
if (this.revision != other.revision)
{
return false;
}
if (this.crc != other.crc)
{
return false;
}
if (!Arrays.equals(this.whirlpool, other.whirlpool))
{
return false;
}
if (!Objects.equals(this.archives, other.archives))
{
return false;
}
return true;
}
public IndexFile getIndex()
{
return index;

View File

@@ -5,6 +5,7 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -15,6 +16,7 @@ public class IndexFile implements Closeable
private final Store store;
private final int indexFileId;
private final File file;
private final RandomAccessFile idx;
private final byte[] buffer = new byte[6];
@@ -22,6 +24,7 @@ public class IndexFile implements Closeable
{
this.store = store;
this.indexFileId = indexFileId;
this.file = file;
this.idx = new RandomAccessFile(file, "rw");
}
@@ -31,6 +34,33 @@ public class IndexFile implements Closeable
idx.close();
}
@Override
public int hashCode()
{
int hash = 3;
hash = 41 * hash + Objects.hashCode(this.file);
return hash;
}
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final IndexFile other = (IndexFile) obj;
if (!Objects.equals(this.file, other.file))
{
return false;
}
return true;
}
public Store getStore()
{
return store;

View File

@@ -6,6 +6,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class Store implements Closeable
{
@@ -43,6 +44,48 @@ public class Store implements Closeable
//for (IndexFile i : indexFiles)
i.close();
}
@Override
public int hashCode()
{
int hash = 5;
hash = 79 * hash + Objects.hashCode(this.folder);
hash = 79 * hash + Objects.hashCode(this.data);
hash = 79 * hash + Objects.hashCode(this.index255);
hash = 79 * hash + Objects.hashCode(this.indexes);
return hash;
}
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final Store other = (Store) obj;
if (!Objects.equals(this.folder, other.folder))
{
return false;
}
if (!Objects.equals(this.data, other.data))
{
return false;
}
if (!Objects.equals(this.index255, other.index255))
{
return false;
}
if (!Objects.equals(this.indexes, other.indexes))
{
return false;
}
return true;
}
public Index addIndex(int id) throws FileNotFoundException
{

View File

@@ -22,36 +22,21 @@ public class StoreTest
@Test
public void testOneFile() throws IOException
{
File file;
try (Store store = new Store(folder.getRoot()))
{
Index index = store.addIndex(0);
Archive archive = index.addArchive(0);
file = archive.addFile(0);
File file = archive.addFile(0);
file.setContents("test".getBytes());
store.save();
}
try (Store store = new Store(folder.getRoot()))
{
store.load();
List<Index> indexes = store.getIndexes();
Assert.assertEquals(1, indexes.size());
Index index = indexes.get(0);
List<Archive> archives = index.getArchives();
Assert.assertEquals(1, archives.size());
Archive archive = archives.get(0);
List<File> files = archive.getFiles();
// XXX just use equals methods on store duh
//archive.
File file2 = files.get(0);
Assert.assertArrayEquals(file.getContents(), file2.getContents());
try (Store store2 = new Store(folder.getRoot()))
{
store2.load();
Assert.assertEquals(store, store2);
}
}
}
}