Added check of not empty subtopics in splitTopics method

This commit is contained in:
andsel
2012-08-03 17:21:16 +02:00
parent c54b35c79c
commit 3be3f614c2
2 changed files with 43 additions and 16 deletions
@@ -1,5 +1,6 @@
package org.dna.mqtt.moquette.messaging.spi.impl;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
@@ -11,7 +12,21 @@ import java.util.List;
public class SubscriptionsStore {
protected static class Token {
static final Token EMPTY = new Token("");
static final Token MULTI = new Token("#");
static final Token SINGLE = new Token("+");
String name;
protected Token(String s) {
name = s;
}
protected String name() {
return name;
}
@Override
public int hashCode() {
int hash = 7;
@@ -33,14 +48,6 @@ public class SubscriptionsStore {
}
return true;
}
String name;
protected Token(String s) {
name = s;
}
protected String name() {
return name;
}
}
private List<Subscription> subscriptions = new ArrayList<Subscription>();
@@ -70,10 +77,20 @@ public class SubscriptionsStore {
return subscriptions.size();
}
protected List<Token> splitTopic(String topic) {
protected List<Token> splitTopic(String topic) throws ParseException {
List res = new ArrayList<Token>();
for (String s : topic.split("/")) {
res.add(new Token(s));
String[] splitted = topic.split("/");
for (int i=0; i<splitted.length; i++) {
String s = splitted[i];
if (s.isEmpty()) {
if (i != 0){
throw new ParseException("Bad format of topic, expetec toppic name between separators", i);
}
res.add(Token.EMPTY);
} else {
res.add(new Token(s));
}
}
return res;
@@ -1,11 +1,12 @@
package org.dna.mqtt.moquette.messaging.spi.impl;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;
import org.dna.mqtt.moquette.messaging.spi.impl.SubscriptionsStore.Token;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
/**
*
@@ -24,9 +25,18 @@ public class SubscriptionsStoreTest {
}
@Test
public void testSplitTopic() {
List<Token> result = store.splitTopic("finance/stock/ibm");
assertEqualsSeq(asArray("finance", "stock", "ibm"), result);
public void testSplitTopic() throws ParseException {
List tokens = store.splitTopic("finance/stock/ibm");
assertEqualsSeq(asArray("finance", "stock", "ibm"), tokens);
tokens = store.splitTopic("/finance/stock/ibm");
assertEqualsSeq(asArray(Token.EMPTY, "finance", "stock", "ibm"), tokens);
}
@Test(expected = ParseException.class)
public void testSplitTopicTwinsSlashAvoided() throws ParseException {
store.splitTopic("/finance//stock/ibm");
}
private static Token[] asArray(Object... l) {