Search in sources :

Example 1 with HistogramDiff

use of org.eclipse.jgit.diff.HistogramDiff in project nifi by apache.

the class TemplateSerializerTest method validateDiffWithChangingComponentIdAndAdditionalElements.

@Test
public void validateDiffWithChangingComponentIdAndAdditionalElements() throws Exception {
    // Create initial template (TemplateDTO)
    FlowSnippetDTO snippet = new FlowSnippetDTO();
    Set<ProcessorDTO> procs = new HashSet<>();
    for (int i = 4; i > 0; i--) {
        ProcessorDTO procDTO = new ProcessorDTO();
        procDTO.setType("Processor" + i + ".class");
        procDTO.setId(ComponentIdGenerator.generateId().toString());
        procs.add(procDTO);
    }
    snippet.setProcessors(procs);
    TemplateDTO origTemplate = new TemplateDTO();
    origTemplate.setDescription("MyTemplate");
    origTemplate.setId("MyTemplate");
    origTemplate.setSnippet(snippet);
    byte[] serTemplate = TemplateSerializer.serialize(origTemplate);
    // Deserialize Template into TemplateDTO
    ByteArrayInputStream in = new ByteArrayInputStream(serTemplate);
    JAXBContext context = JAXBContext.newInstance(TemplateDTO.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    XMLStreamReader xsr = XmlUtils.createSafeReader(in);
    JAXBElement<TemplateDTO> templateElement = unmarshaller.unmarshal(xsr, TemplateDTO.class);
    TemplateDTO deserTemplate = templateElement.getValue();
    // Modify deserialized template
    FlowSnippetDTO deserSnippet = deserTemplate.getSnippet();
    Set<ProcessorDTO> deserProcs = deserSnippet.getProcessors();
    int c = 0;
    for (ProcessorDTO processorDTO : deserProcs) {
        if (c % 2 == 0) {
            processorDTO.setName("Hello-" + c);
        }
        c++;
    }
    // add new Processor
    ProcessorDTO procDTO = new ProcessorDTO();
    procDTO.setType("ProcessorNew" + ".class");
    procDTO.setId(ComponentIdGenerator.generateId().toString());
    deserProcs.add(procDTO);
    // Serialize modified template
    byte[] serTemplate2 = TemplateSerializer.serialize(deserTemplate);
    RawText rt1 = new RawText(serTemplate);
    RawText rt2 = new RawText(serTemplate2);
    EditList diffList = new EditList();
    diffList.addAll(new HistogramDiff().diff(RawTextComparator.DEFAULT, rt1, rt2));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (DiffFormatter diff = new DiffFormatter(out)) {
        diff.format(diffList, rt1, rt2);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(out.toByteArray()), StandardCharsets.UTF_8));
        List<String> changes = reader.lines().peek(System.out::println).filter(line -> line.startsWith("+") || line.startsWith("-")).collect(Collectors.toList());
        assertEquals("+            <name>Hello-0</name>", changes.get(0));
        assertEquals("+            <name>Hello-2</name>", changes.get(1));
        assertEquals("+        <processors>", changes.get(2));
        assertEquals("+            <type>ProcessorNew.class</type>", changes.get(4));
        assertEquals("+        </processors>", changes.get(5));
    }
}
Also used : HistogramDiff(org.eclipse.jgit.diff.HistogramDiff) XmlUtils(org.apache.nifi.security.xml.XmlUtils) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RawTextComparator(org.eclipse.jgit.diff.RawTextComparator) HashSet(java.util.HashSet) ByteArrayInputStream(java.io.ByteArrayInputStream) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) XMLStreamReader(javax.xml.stream.XMLStreamReader) RawText(org.eclipse.jgit.diff.RawText) JAXBContext(javax.xml.bind.JAXBContext) ComponentIdGenerator(org.apache.nifi.util.ComponentIdGenerator) Unmarshaller(javax.xml.bind.Unmarshaller) EditList(org.eclipse.jgit.diff.EditList) JAXBElement(javax.xml.bind.JAXBElement) Set(java.util.Set) Test(org.junit.Test) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) BufferedReader(java.io.BufferedReader) Assert.assertEquals(org.junit.Assert.assertEquals) FlowSnippetDTO(org.apache.nifi.web.api.dto.FlowSnippetDTO) FlowSnippetDTO(org.apache.nifi.web.api.dto.FlowSnippetDTO) XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStreamReader(java.io.InputStreamReader) HistogramDiff(org.eclipse.jgit.diff.HistogramDiff) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RawText(org.eclipse.jgit.diff.RawText) ByteArrayInputStream(java.io.ByteArrayInputStream) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) BufferedReader(java.io.BufferedReader) EditList(org.eclipse.jgit.diff.EditList) Unmarshaller(javax.xml.bind.Unmarshaller) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with HistogramDiff

use of org.eclipse.jgit.diff.HistogramDiff in project oozie by apache.

the class CoordUpdateXCommand method getDiffinGitFormat.

/**
 * Get the differences in git format.
 *
 * @param string1 the string1
 * @param string2 the string2
 * @return the diff
 * @throws IOException Signals that an I/O exception has occurred.
 */
private String getDiffinGitFormat(String string1, String string2) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    RawText rt1 = new RawText(string1.getBytes());
    RawText rt2 = new RawText(string2.getBytes());
    EditList diffList = new EditList();
    diffList.addAll(new HistogramDiff().diff(RawTextComparator.DEFAULT, rt1, rt2));
    new DiffFormatter(out).format(diffList, rt1, rt2);
    return out.toString();
}
Also used : HistogramDiff(org.eclipse.jgit.diff.HistogramDiff) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EditList(org.eclipse.jgit.diff.EditList) RawText(org.eclipse.jgit.diff.RawText) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter)

Example 3 with HistogramDiff

use of org.eclipse.jgit.diff.HistogramDiff in project gerrit by GerritCodeReview.

the class PatchListLoader method toFileHeaderWithoutMyersDiff.

private FileHeader toFileHeaderWithoutMyersDiff(DiffFormatter diffFormatter, DiffEntry diffEntry) throws IOException {
    HistogramDiff histogramDiff = new HistogramDiff();
    histogramDiff.setFallbackAlgorithm(null);
    diffFormatter.setDiffAlgorithm(histogramDiff);
    return diffFormatter.toFileHeader(diffEntry);
}
Also used : HistogramDiff(org.eclipse.jgit.diff.HistogramDiff)

Example 4 with HistogramDiff

use of org.eclipse.jgit.diff.HistogramDiff in project gerrit by GerritCodeReview.

the class PatchListLoader method createPatchListEntry.

private static PatchListEntry createPatchListEntry(RawTextComparator cmp, RevCommit aCommit, Text aText, Text bText, String fileName) {
    byte[] rawHdr = getRawHeader(aCommit != null, fileName);
    byte[] aContent = aText.getContent();
    byte[] bContent = bText.getContent();
    long size = bContent.length;
    long sizeDelta = bContent.length - aContent.length;
    RawText aRawText = new RawText(aContent);
    RawText bRawText = new RawText(bContent);
    EditList edits = new HistogramDiff().diff(cmp, aRawText, bRawText);
    FileHeader fh = new FileHeader(rawHdr, edits, PatchType.UNIFIED);
    return new PatchListEntry(fh, edits, size, sizeDelta);
}
Also used : HistogramDiff(org.eclipse.jgit.diff.HistogramDiff) EditList(org.eclipse.jgit.diff.EditList) RawText(org.eclipse.jgit.diff.RawText) FileHeader(org.eclipse.jgit.patch.FileHeader)

Aggregations

HistogramDiff (org.eclipse.jgit.diff.HistogramDiff)4 EditList (org.eclipse.jgit.diff.EditList)3 RawText (org.eclipse.jgit.diff.RawText)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBElement (javax.xml.bind.JAXBElement)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 XmlUtils (org.apache.nifi.security.xml.XmlUtils)1 ComponentIdGenerator (org.apache.nifi.util.ComponentIdGenerator)1 FlowSnippetDTO (org.apache.nifi.web.api.dto.FlowSnippetDTO)1