001: package de.regnis.q.sequence.line;
002:
003: import java.io.*;
004: import java.util.*;
005: import junit.framework.*;
006:
007: import de.regnis.q.sequence.*;
008: import de.regnis.q.sequence.line.simplifier.*;
009: import de.regnis.q.sequence.core.*;
010: import de.regnis.q.sequence.media.*;
011:
012: /**
013: * @author Marc Strapetz
014: */
015: public class QSequenceLineMediaTest extends TestCase {
016:
017: // Fields =================================================================
018:
019: private Random random;
020:
021: // Implemented ============================================================
022:
023: protected void setUp() throws Exception {
024: super .setUp();
025: random = new Random(0);
026: }
027:
028: // Accessing ==============================================================
029:
030: public void testBasicMemoryVersusFileSystem() throws IOException,
031: QSequenceException {
032: test(new String[] { "a", "c", "b." }, new String[] { "a", "d",
033: "b." }, new QSequenceLineDummySimplifier(), null);
034: }
035:
036: public void testSimplifier() throws IOException, QSequenceException {
037: final String[] left = new String[] { "equal",
038: "number of ws only ", "all ws" };
039: final String[] right = new String[] { "equal",
040: "number\tof ws only ", " allws " };
041:
042: test(left, right, new QSequenceLineDummySimplifier(),
043: Collections.singletonList(new QSequenceDifferenceBlock(
044: 1, 2, 1, 2)));
045: test(left, right,
046: new QSequenceLineWhiteSpaceReducingSimplifier(),
047: Collections.singletonList(new QSequenceDifferenceBlock(
048: 2, 2, 2, 2)));
049: test(left, right,
050: new QSequenceLineWhiteSpaceSkippingSimplifier(),
051: Collections.EMPTY_LIST);
052: }
053:
054: public void testRandomMemoryVersusFileSystem() throws IOException,
055: QSequenceException {
056: for (int size = 1; size <= 50; size += 1) {
057: testRandomStrings(size, 0.3, 0.1);
058: }
059: }
060:
061: // Utils ==================================================================
062:
063: private void testRandomStrings(int lineCount, double pMod,
064: double pAddRemove) throws IOException, QSequenceException {
065: final String[] left = QSequenceDifferenceAssemblyTest
066: .createLines(lineCount, random);
067: final String[] right = QSequenceDifferenceAssemblyTest
068: .alterLines(left, pMod, pAddRemove, random);
069: test(left, right, new QSequenceLineDummySimplifier(), null);
070: }
071:
072: private void test(String[] left, String[] right,
073: QSequenceLineSimplifier simplifier, List expectedBlocks)
074: throws IOException, QSequenceException {
075: final File leftFile = createTestFile(left, true);
076: final File rightFile = createTestFile(right, false);
077: try {
078: final QSequenceLineResult memoryResult = createBlocksInMemory(
079: leftFile, rightFile, simplifier);
080: final QSequenceLineResult raFileResult1 = createRAFileBlocks(
081: leftFile, rightFile, 16, 16, simplifier);
082: final QSequenceLineResult raFileResult2 = createRAFileBlocks(
083: leftFile, rightFile, 256, 16, simplifier);
084: final QSequenceLineResult raFileResult3 = createRAFileBlocks(
085: leftFile, rightFile, 256, 64, simplifier);
086: final QSequenceLineResult raFileResult4 = createRAFileBlocks(
087: leftFile, rightFile, 256, 256, simplifier);
088: final QSequenceLineResult raFileResult5 = createRAFileBlocks(
089: leftFile, rightFile, Integer.MAX_VALUE, 48,
090: simplifier);
091: try {
092: if (expectedBlocks != null) {
093: compareBlocks(expectedBlocks, memoryResult
094: .getBlocks());
095: }
096: compareBlocks(memoryResult.getBlocks(), raFileResult1
097: .getBlocks());
098: compareBlocks(memoryResult.getBlocks(), raFileResult2
099: .getBlocks());
100: compareBlocks(memoryResult.getBlocks(), raFileResult3
101: .getBlocks());
102: compareBlocks(memoryResult.getBlocks(), raFileResult4
103: .getBlocks());
104: compareBlocks(memoryResult.getBlocks(), raFileResult5
105: .getBlocks());
106: } finally {
107: memoryResult.close();
108: raFileResult1.close();
109: raFileResult2.close();
110: raFileResult3.close();
111: raFileResult4.close();
112: raFileResult5.close();
113: }
114: } finally {
115: leftFile.delete();
116: rightFile.delete();
117: }
118: }
119:
120: private static QSequenceLineResult createBlocksInMemory(
121: final File leftFile, final File rightFile,
122: QSequenceLineSimplifier simplifier) throws IOException,
123: QSequenceException {
124: final FileInputStream left = new FileInputStream(leftFile);
125: final FileInputStream right = new FileInputStream(rightFile);
126: final QSequenceLineMemoryCache leftCache = QSequenceLineMemoryCache
127: .read(left, simplifier);
128: final QSequenceLineMemoryCache rightCache = QSequenceLineMemoryCache
129: .read(right, simplifier);
130: final QSequenceLineMedia lineMedia = new QSequenceLineMedia(
131: leftCache, rightCache);
132: final QSequenceCachingMedia cachingMedia = new QSequenceCachingMedia(
133: lineMedia, new QSequenceDummyCanceller());
134: final List blocks = new QSequenceDifference(cachingMedia,
135: new QSequenceMediaDummyIndexTransformer(cachingMedia))
136: .getBlocks();
137: new QSequenceDifferenceBlockShifter(cachingMedia, cachingMedia)
138: .shiftBlocks(blocks);
139: return new QSequenceLineResult(blocks, leftCache, rightCache);
140: }
141:
142: private static QSequenceLineResult createRAFileBlocks(
143: File leftFile, File rightFile, int maximumMemorySize,
144: int fileSegmentSize, QSequenceLineSimplifier simplifier)
145: throws IOException, QSequenceException {
146: final RandomAccessFile leftRAFile = new RandomAccessFile(
147: leftFile, "r");
148: final RandomAccessFile rightRAFile = new RandomAccessFile(
149: rightFile, "r");
150: try {
151: return QSequenceLineMedia.createBlocksInFilesystem(
152: new QSequenceLineRAFileData(leftRAFile),
153: new QSequenceLineRAFileData(rightRAFile),
154: new QSequenceLineSystemTempDirectoryFactory(), 1.0,
155: maximumMemorySize, fileSegmentSize, simplifier);
156: } finally {
157: leftRAFile.close();
158: rightRAFile.close();
159: }
160: }
161:
162: private static void compareBlocks(List expected, List actual) {
163: assertEquals(expected.size(), actual.size());
164:
165: for (int index = 0; index < expected.size(); index++) {
166: final QSequenceDifferenceBlock expectedBlock = (QSequenceDifferenceBlock) expected
167: .get(index);
168: final QSequenceDifferenceBlock actualBlock = (QSequenceDifferenceBlock) actual
169: .get(index);
170:
171: assertEquals(expectedBlock.getLeftFrom(), actualBlock
172: .getLeftFrom());
173: assertEquals(expectedBlock.getLeftTo(), actualBlock
174: .getLeftTo());
175: assertEquals(expectedBlock.getRightFrom(), actualBlock
176: .getRightFrom());
177: assertEquals(expectedBlock.getRightTo(), actualBlock
178: .getRightTo());
179: }
180: }
181:
182: private static File createTestFile(String[] content, boolean left)
183: throws IOException {
184: final File file = File.createTempFile("SequenceMediaTest",
185: left ? "left" : "right");
186: file.delete();
187:
188: final FileOutputStream stream = new FileOutputStream(file);
189: for (int index = 0; index < content.length; index++) {
190: stream.write(content[index].getBytes());
191: if (index < content.length - 1) {
192: stream.write("\n".getBytes());
193: }
194: }
195:
196: stream.close();
197: return file;
198: }
199: }
|