comparison test/org/tmatesoft/hg/test/TestKeywordFilter.java @ 711:a62079bc422b

Keyword filtering that doesn't depend on input buffer size and the way input lines got split between filter() calls. KewordFilter got state to keep processed suspicious ...$ lines
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 11 Oct 2013 21:35:41 +0200
parents
children
comparison
equal deleted inserted replaced
710:cf200271439a 711:a62079bc422b
1 /*
2 * Copyright (c) 2013 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.test;
18
19 import java.io.File;
20 import java.nio.ByteBuffer;
21
22 import org.junit.Assert;
23 import org.junit.Test;
24 import org.tmatesoft.hg.internal.ByteArrayChannel;
25 import org.tmatesoft.hg.internal.Filter;
26 import org.tmatesoft.hg.internal.Filter.Direction;
27 import org.tmatesoft.hg.internal.KeywordFilter;
28 import org.tmatesoft.hg.repo.HgLookup;
29 import org.tmatesoft.hg.repo.HgRepository;
30 import org.tmatesoft.hg.repo.HgRepositoryFiles;
31 import org.tmatesoft.hg.util.Path;
32
33 /**
34 *
35 * @author Artem Tikhomirov
36 * @author TMate Software Ltd.
37 */
38 public class TestKeywordFilter {
39
40 private HgRepository repo;
41
42 @Test
43 public void testSmallBuffer() throws Exception {
44 initRepo();
45 final Filter kwFilter = createFilter(Filter.Direction.ToRepo);
46 final byte[] in = "1\n2\n3\n".getBytes();
47 ByteBuffer bb = kwFilter.filter(ByteBuffer.wrap(in));
48 final byte[] out = new byte[bb.remaining()];
49 bb.get(out);
50 Assert.assertArrayEquals(in, out);
51 }
52
53 @Test
54 public void testKeywordDrop() throws Exception {
55 initRepo();
56 final Filter kwFilter = createFilter(Filter.Direction.ToRepo);
57 final byte[] in = "1\n$Revision: cf200271439a7ec256151b30bc4360b21db3542e$\n3\n".getBytes();
58 ByteBuffer bb = kwFilter.filter(ByteBuffer.wrap(in));
59 final byte[] out = new byte[bb.remaining()];
60 bb.get(out);
61 Assert.assertArrayEquals("1\n$Revision$\n3\n".getBytes(), out);
62 }
63
64 @Test
65 public void testKeywordExpansion() throws Exception {
66 Assert.fail("Need real repo with changeset to check kw expansion");
67 }
68
69 /**
70 * what if keyword is split between two input buffers
71 */
72 @Test
73 public void testKeywordSplitInBuffer() throws Exception {
74 initRepo();
75 final byte[] in1 = "1\n$Id:whatever".getBytes();
76 final byte[] in2 = " continues here$\n3\n".getBytes();
77 ByteArrayChannel out = new ByteArrayChannel();
78 final Filter kwFilter = createFilter(Filter.Direction.ToRepo);
79 out.write(kwFilter.filter(ByteBuffer.wrap(in1)));
80 out.write(kwFilter.filter(ByteBuffer.wrap(in2)));
81 Assert.assertEquals("1\n$Id$\n3\n", new String(out.toArray()));
82 // Same as above, to extreme - only $ in the first buffer
83 final Filter kwFilter2 = createFilter(Filter.Direction.ToRepo);
84 out = new ByteArrayChannel();
85 out.write(kwFilter2.filter(ByteBuffer.wrap("1\n$".getBytes())));
86 out.write(kwFilter2.filter(ByteBuffer.wrap("Id:whatever continues here$\n3\n".getBytes())));
87 Assert.assertEquals("1\n$Id$\n3\n", new String(out.toArray()));
88 }
89
90 /**
91 * what if input contains smth similar to keyword but unless the second part of the buffer
92 * comes in, it's impossible to tell
93 */
94 @Test
95 public void testIncompleteKeyword() throws Exception {
96 initRepo();
97 final byte[] in1 = "1\n$Id:whatever".getBytes();
98 final byte[] in2 = " id doesn't close here\n3\n".getBytes();
99 ByteArrayChannel out = new ByteArrayChannel();
100 final Filter kwFilter = createFilter(Filter.Direction.ToRepo);
101 out.write(kwFilter.filter(ByteBuffer.wrap(in1)));
102 out.write(kwFilter.filter(ByteBuffer.wrap(in2)));
103 byte[] expected = new byte[in1.length + in2.length];
104 System.arraycopy(in1, 0, expected, 0, in1.length);
105 System.arraycopy(in2, 0, expected, in1.length, in2.length);
106 Assert.assertEquals(new String(expected), new String(out.toArray()));
107 }
108
109 @Test
110 public void testIncompleteKeywordAtEOF() throws Exception {
111 initRepo();
112 final byte[] in = "1\n$Id:whatever\n".getBytes();
113 final Filter kwFilter = createFilter(Filter.Direction.ToRepo);
114 ByteBuffer outBuf = kwFilter.filter(ByteBuffer.wrap(in));
115 byte[] out = new byte[outBuf.remaining()];
116 outBuf.get(out);
117 Assert.assertEquals(new String(in), new String(out));
118 //
119 // incomplete $kw is stripped of in case of EOF
120 final Filter kwFilter2 = createFilter(Filter.Direction.ToRepo);
121 outBuf = kwFilter2.filter(ByteBuffer.wrap("1\n$Id:whatever".getBytes()));
122 out = new byte[outBuf.remaining()];
123 outBuf.get(out);
124 Assert.assertEquals("1\n", new String(out));
125 }
126
127 private Filter createFilter(Direction dir) {
128 final KeywordFilter.Factory kwFactory = new KeywordFilter.Factory();
129 kwFactory.initialize(repo);
130 return kwFactory.create(Path.create("a/b"), new Filter.Options(dir));
131 }
132
133 private HgRepository initRepo() throws Exception {
134 final File repoLoc = RepoUtils.initEmptyTempRepo("test-kw-filter");
135 final File hgrc = new File(repoLoc, HgRepositoryFiles.RepoConfig.getPath());
136 RepoUtils.createFile(hgrc, "[extensions]\nkeyword=\n\n[keyword]\n**.*=\n");
137 repo = new HgLookup().detect(repoLoc);
138 return repo;
139 }
140 }