Skip to content

Commit 56dd514

Browse files
andrzejj0slawekjaranowski
authored andcommitted
Fixing #320: Correcting the delta computation
1 parent a440adc commit 56dd514

File tree

2 files changed

+140
-2
lines changed

2 files changed

+140
-2
lines changed

src/main/java/org/codehaus/mojo/versions/rewriting/ModifiedPomXMLEventReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ public void replace( String replacement )
462462
return;
463463
}
464464
pom.replace( start, end, replacement );
465-
int delta = replacement.length() - lastEnd - lastStart;
465+
int delta = replacement.length() - ( lastEnd - lastStart );
466466
nextDelta += delta;
467467
for ( int i = 0; i < MAX_MARKS; i++ )
468468
{
@@ -563,7 +563,7 @@ public void replaceMark( int index, String replacement )
563563
return;
564564
}
565565
pom.replace( start, end, replacement );
566-
int delta = replacement.length() - markEnd[index] - markStart[index];
566+
int delta = replacement.length() - ( markEnd[index] - markStart[index] );
567567
nextDelta += delta;
568568
if ( lastStart == markStart[index] && lastEnd == markEnd[index] )
569569
{
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package org.codehaus.mojo.versions.rewriting;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import javax.xml.stream.Location;
23+
import javax.xml.stream.XMLEventReader;
24+
import javax.xml.stream.XMLInputFactory;
25+
import javax.xml.stream.XMLStreamException;
26+
import javax.xml.stream.events.XMLEvent;
27+
28+
import java.io.StringReader;
29+
30+
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
31+
import org.junit.Before;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.mockito.Mock;
35+
import org.mockito.junit.MockitoJUnitRunner;
36+
37+
import static org.hamcrest.MatcherAssert.assertThat;
38+
import static org.hamcrest.core.Is.is;
39+
import static org.mockito.ArgumentMatchers.any;
40+
import static org.mockito.Mockito.when;
41+
42+
/**
43+
* Unit tests for {@link ModifiedPomXMLEventReaderTest}
44+
*
45+
* @author Andrzej Jarmoniuk
46+
*/
47+
48+
@RunWith( MockitoJUnitRunner.class )
49+
public class ModifiedPomXMLEventReaderTest extends AbstractMojoTestCase
50+
{
51+
private static final String[] STR = {"xyz", "0123456789abcdef"};
52+
private static final String REPLACEMENT = "abcdef";
53+
54+
@Mock
55+
private Location location;
56+
57+
@Mock
58+
private XMLEvent xmlEvent;
59+
60+
@Mock
61+
private XMLEventReader xmlEventReader;
62+
63+
@Mock
64+
private XMLInputFactory xmlInputFactory;
65+
66+
private ModifiedPomXMLEventReader pomXMLEventReader;
67+
68+
@Before
69+
public void setUp() throws Exception
70+
{
71+
super.setUp();
72+
73+
when( location.getCharacterOffset() )
74+
.thenReturn( STR[0].length() )
75+
.thenReturn( STR[0].length() + STR[1].length() );
76+
77+
when( xmlEvent.isCharacters() ).thenReturn( true );
78+
when( xmlEvent.getLocation() ).thenReturn( location );
79+
80+
when( xmlEventReader.hasNext() )
81+
.thenReturn( true ).thenReturn( true ) // str[0]
82+
.thenReturn( true ).thenReturn( true ) // str[1]
83+
.thenReturn( false ); // ∅
84+
when( xmlEventReader.nextEvent() )
85+
.thenReturn( xmlEvent )
86+
.thenReturn( xmlEvent );
87+
when( xmlEventReader.peek() )
88+
.thenReturn( xmlEvent );
89+
90+
when( xmlInputFactory.createXMLEventReader( any( StringReader.class ) ) )
91+
.thenReturn( xmlEventReader );
92+
93+
pomXMLEventReader =
94+
new ModifiedPomXMLEventReader( new StringBuilder( STR[0] ).append( STR[1] ), xmlInputFactory, "" );
95+
}
96+
97+
@Test
98+
public void testReplace() throws XMLStreamException, IllegalAccessException
99+
{
100+
assertThat( pomXMLEventReader.hasNext(), is( true ) );
101+
assertThat( pomXMLEventReader.nextEvent(), is( xmlEvent ) );
102+
103+
assertThat( pomXMLEventReader.hasNext(), is( true ) );
104+
assertThat( pomXMLEventReader.nextEvent(), is( xmlEvent ) );
105+
106+
pomXMLEventReader.replace( REPLACEMENT );
107+
assertThat( pomXMLEventReader.asStringBuilder().toString(), is( STR[0] + REPLACEMENT ) );
108+
109+
pomXMLEventReader.mark( 0 );
110+
assertThat( pomXMLEventReader.getMarkVerbatim( 0 ), is( REPLACEMENT ) );
111+
112+
// more dangerous test since this touches the implementation
113+
assertThat( getVariableValueFromObject( pomXMLEventReader, "lastEnd" ),
114+
is( ( STR[0] + REPLACEMENT ).length() ) );
115+
}
116+
117+
@Test
118+
public void testReplaceMark() throws XMLStreamException, IllegalAccessException
119+
{
120+
assertThat( pomXMLEventReader.hasNext(), is( true ) );
121+
assertThat( pomXMLEventReader.nextEvent(), is( xmlEvent ) );
122+
123+
assertThat( pomXMLEventReader.hasNext(), is( true ) );
124+
assertThat( pomXMLEventReader.nextEvent(), is( xmlEvent ) );
125+
126+
pomXMLEventReader.mark( 0 );
127+
128+
pomXMLEventReader.replaceMark( 0, REPLACEMENT );
129+
assertThat( pomXMLEventReader.asStringBuilder().toString(), is( STR[0] + REPLACEMENT ) );
130+
131+
pomXMLEventReader.mark( 0 );
132+
assertThat( pomXMLEventReader.getMarkVerbatim( 0 ), is( REPLACEMENT ) );
133+
134+
// more dangerous test since this touches the implementation
135+
assertThat( getVariableValueFromObject( pomXMLEventReader, "lastEnd" ),
136+
is( ( STR[0] + REPLACEMENT ).length() ) );
137+
}
138+
}

0 commit comments

Comments
 (0)