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; package org.dna.mqtt.moquette.messaging.spi.impl;
import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -11,7 +12,21 @@ import java.util.List;
public class SubscriptionsStore { public class SubscriptionsStore {
protected static class Token { 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 @Override
public int hashCode() { public int hashCode() {
int hash = 7; int hash = 7;
@@ -33,14 +48,6 @@ public class SubscriptionsStore {
} }
return true; return true;
} }
String name;
protected Token(String s) {
name = s;
}
protected String name() {
return name;
}
} }
private List<Subscription> subscriptions = new ArrayList<Subscription>(); private List<Subscription> subscriptions = new ArrayList<Subscription>();
@@ -70,10 +77,20 @@ public class SubscriptionsStore {
return subscriptions.size(); return subscriptions.size();
} }
protected List<Token> splitTopic(String topic) { protected List<Token> splitTopic(String topic) throws ParseException {
List res = new ArrayList<Token>(); List res = new ArrayList<Token>();
for (String s : topic.split("/")) { String[] splitted = topic.split("/");
res.add(new Token(s));
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; return res;
@@ -1,11 +1,12 @@
package org.dna.mqtt.moquette.messaging.spi.impl; package org.dna.mqtt.moquette.messaging.spi.impl;
import java.text.ParseException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.dna.mqtt.moquette.messaging.spi.impl.SubscriptionsStore.Token; import org.dna.mqtt.moquette.messaging.spi.impl.SubscriptionsStore.Token;
import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Before; import org.junit.Before;
import org.junit.Test;
/** /**
* *
@@ -24,9 +25,18 @@ public class SubscriptionsStoreTest {
} }
@Test @Test
public void testSplitTopic() { public void testSplitTopic() throws ParseException {
List<Token> result = store.splitTopic("finance/stock/ibm"); List tokens = store.splitTopic("finance/stock/ibm");
assertEqualsSeq(asArray("finance", "stock", "ibm"), result); 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) { private static Token[] asArray(Object... l) {