@Test public void chunkFromString() { Function<Node, ContentChunk> chunkGenerator = FileContentReplace.chunkFromString("Test chunk."); Node ourNode = new NodeImpl(new RevisionImpl(2)); ourNode.getHeaders().put(NodeHeader.ACTION, "add"); ourNode.getHeaders().put(NodeHeader.PATH, "/somepath/is/here.txt"); assertThat(new String(chunkGenerator.apply(ourNode).getContent()), is(equalTo("Test chunk."))); }
@Test public void nodematch() { Predicate<Node> nodeMatch = FileContentReplace.nodeMatch(2, "add", "/somepath/is/here.txt"); { Node ourNode = new NodeImpl(new RevisionImpl(2)); ourNode.getHeaders().put(NodeHeader.ACTION, "add"); ourNode.getHeaders().put(NodeHeader.PATH, "/somepath/is/here.txt"); assertTrue(nodeMatch.test(ourNode)); } { Node ourNode = new NodeImpl(new RevisionImpl(2)); ourNode.getHeaders().put(NodeHeader.ACTION, "add"); ourNode.getHeaders().put(NodeHeader.PATH, "/somepath/is/here1.txt"); assertFalse(nodeMatch.test(ourNode)); } { Node ourNode = new NodeImpl(new RevisionImpl(2)); ourNode.getHeaders().put(NodeHeader.ACTION, "remove"); ourNode.getHeaders().put(NodeHeader.PATH, "/somepath/is/here.txt"); assertFalse(nodeMatch.test(ourNode)); } { Node ourNode = new NodeImpl(new RevisionImpl(3)); ourNode.getHeaders().put(NodeHeader.ACTION, "add"); ourNode.getHeaders().put(NodeHeader.PATH, "/somepath/is/here.txt"); assertFalse(nodeMatch.test(ourNode)); } }
@Test public void simple_replace() throws ParseException, NoSuchAlgorithmException { final String newFileContent = "No content.\n"; Predicate<Node> predicate = n -> n.getRevision().get().getNumber() == 1 && n.getHeaders().get(NodeHeader.PATH).equals("README.txt"); FileContentReplace fileContentReplace = new FileContentReplace(predicate, n -> new ContentChunkImpl(newFileContent.getBytes())); RepositoryInMemory inMemory = new RepositoryInMemory(); fileContentReplace.continueTo(inMemory); SvnDumpParser.consume(TestUtil.openResource("dumps/add_file.dump"), fileContentReplace); assertThat(inMemory.getRepo().getRevisions().size(), is(2)); Revision r1 = inMemory.getRepo().getRevisions().get(1); assertThat(r1.getNodes().size(), is(1)); Node node = r1.getNodes().get(0); assertThat(new String(node.getContent().get(0).getContent()), is(equalTo(newFileContent))); assertThat(node.get(NodeHeader.TEXT_CONTENT_LENGTH), is(equalTo("12"))); assertThat(node.get(NodeHeader.CONTENT_LENGTH), is(equalTo("22"))); assertThat(node.get(NodeHeader.MD5), is(equalTo(new Md5().hash(newFileContent.getBytes())))); assertThat(node.get(NodeHeader.SHA1), is(equalTo(new Sha1().hash(newFileContent.getBytes())))); }
@Test public void copy_constructor_with_null_properties() { Node node = new NodeImpl(); { Map<NodeHeader, String> headers = new LinkedHashMap<>(); headers.put(NodeHeader.ACTION, "add"); headers.put(NodeHeader.KIND, "dir"); headers.put(NodeHeader.PATH, "dir1"); node.setHeaders(headers); node.setProperties(null); } Node duplicate = new NodeImpl(node); assertThat(duplicate.getProperties().isEmpty(), is(true)); assertFalse(duplicate.getRevision().isPresent()); assertThat(duplicate.getHeaders().size(), is(3)); assertThat(duplicate.get(NodeHeader.ACTION), is(equalTo("add"))); assertThat(duplicate.get(NodeHeader.KIND), is(equalTo("dir"))); assertThat(duplicate.get(NodeHeader.PATH), is(equalTo("dir1"))); assertTrue(duplicate.getContent().isEmpty()); }