Skip to content

Commit

Permalink
Attempt to fix thread-safety of CharacterProtector
Browse files Browse the repository at this point in the history
* Add simple unit tests for class.

Fixes Issue # 20.

Signed-off-by: Alex Coles <alex@alexbcoles.com>
  • Loading branch information
myabc committed Feb 17, 2013
1 parent 6404f20 commit 2300634
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
18 changes: 11 additions & 7 deletions core/src/main/java/org/markdownj/CharacterProtector.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ liability, whether in contract, strict liability, or tort (including
package org.markdownj;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

class CharacterProtector {
private Map<String, String> protectMap = new HashMap<String, String>();
private Map<String, String> unprotectMap = new HashMap<String, String>();
public class CharacterProtector {
private final Map<String, String> protectMap = new HashMap<String, String>();
private final Map<String, String> unprotectMap = new HashMap<String, String>();
private static final String GOOD_CHARS = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
private Random rnd = new Random();

Expand All @@ -59,17 +60,20 @@ public String decode(String coded) {
}

public Collection<String> getAllEncodedTokens() {
return unprotectMap.keySet();
return Collections.unmodifiableSet(unprotectMap.keySet());
}

private void addToken(String literal) {
String encoded = longRandomString();
protectMap.put(literal, encoded);
unprotectMap.put(encoded, literal);

synchronized (this) {
protectMap.put(literal, encoded);
unprotectMap.put(encoded, literal);
}
}

private String longRandomString() {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
final int CHAR_MAX = GOOD_CHARS.length();
for (int i = 0; i < 20; i++) {
sb.append(GOOD_CHARS.charAt(rnd.nextInt(CHAR_MAX)));
Expand Down
52 changes: 52 additions & 0 deletions core/src/test/java/org/markdownj/test/CharacterProtectorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.markdownj.test;

import java.util.Collection;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.markdownj.CharacterProtector;

/**
*
* @author alexbcoles
*/
public class CharacterProtectorTest {

private CharacterProtector characterProtector;

@Before
public void createCharacterProtector() {
characterProtector = new CharacterProtector();
}

@Test
public void testEncodeAndDecodeRoundtrip() {
String encoded = characterProtector.encode("<h4>Warnemünde</h4>");
assertEquals("<h4>Warnemünde</h4>", characterProtector.decode(encoded));
}

@Test
public void testGetAllEncodedTokens() {
Collection tokens = characterProtector.getAllEncodedTokens();
assertEquals(0, tokens.size());

characterProtector.encode("<nav><div></div></nav>");
characterProtector.encode("<h1 id='heading'>Schifffahrt nach Warnemünde</h1>");
characterProtector.encode("<br/>");
assertEquals(3, tokens.size());
}

@Test(expected=UnsupportedOperationException.class)
public void testGetAllEncodedTokensCanNotModified1() {
Collection tokens = characterProtector.getAllEncodedTokens();
tokens.clear();
}

@Test(expected=UnsupportedOperationException.class)
public void testGetAllEncodedTokensCanNotModified2() {
Collection tokens = characterProtector.getAllEncodedTokens();
tokens.add("another_token");
tokens.remove("another_token");
}

}

0 comments on commit 2300634

Please sign in to comment.