Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix JENKINS-14205] Fix logic error checking for start token.
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
Showing
4 changed files
with
145 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...t/java/com/myyearbook/hudson/plugins/confluence/wiki/editors/BetweenTokensEditorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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></div></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); | ||
} | ||
|
||
} |