Skip to content

Commit

Permalink
[Fix JENKINS-14205] Fix logic error checking for start token.
Browse files Browse the repository at this point in the history
If the start token is configured, but does not exist in the content,
the check for the start token's existence would fail, causing the content
to become corrupt.
  • Loading branch information
Joe Hansche committed Jun 25, 2012
1 parent fffb578 commit 2aa0a59
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 6 deletions.
1 change: 1 addition & 0 deletions .classpath
Expand Up @@ -3,6 +3,7 @@
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/classes" path="target/generated-sources/localizer"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
Expand Down
13 changes: 13 additions & 0 deletions pom.xml
Expand Up @@ -79,6 +79,19 @@
<type>jar</type>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<distributionManagement>
<repository>
Expand Down
Expand Up @@ -53,30 +53,31 @@ public String performEdits(final BuildListener listener, final String content,
final String generated, final boolean isNewFormat) throws TokenNotFoundException {
final StringBuffer sb = new StringBuffer(content);

final int start = content.indexOf(startMarkerToken) + startMarkerToken.length();
final int markerStart = content.indexOf(startMarkerToken);
final int contentStart = markerStart + startMarkerToken.length();

if (start < 0) {
if (markerStart < 0) {
throw new TokenNotFoundException(
"Start-marker token could not be found in the page content: "
+ startMarkerToken);
}

final int end = content.indexOf(endMarkerToken, start);
final int end = content.indexOf(endMarkerToken, contentStart);

if (end < 0) {
throw new TokenNotFoundException(
"End-marker token could not be found after the start-marker token: " + endMarkerToken);
}

// Remove the entire marked section (exclusive)
sb.delete(start, end);
sb.delete(contentStart, end);

// Then insert the new content:
if (isNewFormat) {
sb.insert(start, generated);
sb.insert(contentStart, generated);
} else {
// Surround in newlines
sb.insert(start, '\n').insert(start, generated).insert(start, '\n');
sb.insert(contentStart, '\n').insert(contentStart, generated).insert(contentStart, '\n');
}
return sb.toString();
}
Expand Down
@@ -0,0 +1,124 @@
package com.myyearbook.hudson.plugins.confluence.wiki.editors;

import hudson.model.BuildListener;
import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.mockito.Mock;

import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.Mockito.*;

import com.myyearbook.hudson.plugins.confluence.wiki.editors.MarkupEditor.TokenNotFoundException;
import com.myyearbook.hudson.plugins.confluence.wiki.generators.MarkupGenerator;

public class BetweenTokensEditorTest extends TestCase {

private static final String START_TOKEN = "%start%";
private static final String END_TOKEN = "%end%";

@Mock
BuildListener buildListener;
@Mock
MarkupGenerator markupGenerator;

@Before
protected void setUp() throws Exception {
super.setUp();
initMocks(this);
}

@After
protected void tearDown() throws Exception {
super.tearDown();
}

/**
* Tests that an error occurs if the START marker is not found.
*
* @throws TokenNotFoundException
*/
@Bug(14205)
@Test(expected = TokenNotFoundException.class)
public void testPerformEdits_startMarkerNotFound() throws TokenNotFoundException {
String testContent = "The start marker is nowhere to be found.%end%";
String toInsert = "New Content!";
String expectedMessage = "Start-marker token could not be found in the page content:";

BetweenTokensEditor obj = new BetweenTokensEditor(markupGenerator, START_TOKEN, END_TOKEN);

try {
obj.performEdits(buildListener, testContent, toInsert, false);
fail("Expected TokenNotFoundException");
} catch (TokenNotFoundException exc) {
assertTrue(exc.getMessage().startsWith(expectedMessage));
}
}

/**
* Tests that an error occurs if the END marker is not found.
*
* @throws TokenNotFoundException
*/
@Test(expected = TokenNotFoundException.class)
public void testPerformEdits_endMarkerNotFound() throws TokenNotFoundException {
String testContent = "%start%The end marker is nowhere to be found.";
String toInsert = "New Content!";
String expectedMessage = "End-marker token could not be found after the start-marker token:";

BetweenTokensEditor obj = new BetweenTokensEditor(markupGenerator, START_TOKEN, END_TOKEN);

try {
obj.performEdits(buildListener, testContent, toInsert, false);
fail("Expected TokenNotFoundException");
} catch (TokenNotFoundException exc) {
assertTrue(exc.getMessage().startsWith(expectedMessage));
}
}

/**
* This tests that you can replace two sections -- each with distinct START tokens, but sharing
* the same END token (e.g., "<tt>&lt;/div&gt;</tt>").
*
* @throws TokenNotFoundException
*/
@Bug(13896)
public void testPerformEdits_multipleMarkers() throws TokenNotFoundException {
String testContent = "%start1%First Section.%end%\n%start%Second Section.%end%";
String toInsert = "First replacement";

BetweenTokensEditor obj1 = new BetweenTokensEditor(markupGenerator, "%start1%", END_TOKEN);
String actual = obj1.performEdits(buildListener, testContent, toInsert, true);
assertEquals("%start1%First replacement%end%\n%start%Second Section.%end%", actual);

toInsert = "Second replacement";
BetweenTokensEditor obj2 = new BetweenTokensEditor(markupGenerator, START_TOKEN, END_TOKEN);
actual = obj2.performEdits(buildListener, actual, toInsert, true);
assertEquals("%start1%First replacement%end%\n%start%Second replacement%end%", actual);
}

/**
* This tests that the old wiki format (Confluence pre-4.0) inserts newlines around the content.
*
* @throws TokenNotFoundException
*/
public void testPerformEdits_oldFormat() throws TokenNotFoundException {
String testContent = "Header\n%start%Current Content%end%\nFooter";
String toInsert = "Replacement content";

BetweenTokensEditor obj = new BetweenTokensEditor(markupGenerator, START_TOKEN, END_TOKEN);
String actual = obj.performEdits(buildListener, testContent, toInsert, false);
assertEquals("Header\n%start%\nReplacement content\n%end%\nFooter", actual);
}

@Test
public void testCtor() {
BetweenTokensEditor obj = new BetweenTokensEditor(markupGenerator, START_TOKEN, END_TOKEN);
assertEquals(START_TOKEN, obj.startMarkerToken);
assertEquals(END_TOKEN, obj.endMarkerToken);
}

}

0 comments on commit 2aa0a59

Please sign in to comment.