Skip to content

Commit

Permalink
Added test to ensure property name can contain '_' characters. (apach…
Browse files Browse the repository at this point in the history
  • Loading branch information
merlimat authored Apr 6, 2018
1 parent 1eb8068 commit 3ae5115
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -768,4 +768,41 @@ public void testPublishConsumerStats() throws Exception {
producer.close();
consumer.close();
}

@Test
public void testTenantNameWithUnderscore() throws Exception {
PropertyAdmin propertyAdmin = new PropertyAdmin(Sets.newHashSet("role1", "role2"), Sets.newHashSet("use"));
admin.properties().createProperty("prop_xyz", propertyAdmin);

admin.namespaces().createNamespace("prop_xyz/use/my-namespace");

String topic = "persistent://prop_xyz/use/my-namespace/my-topic";

Producer<byte[]> producer = pulsarClient.newProducer().topic(topic)
.create();

PersistentTopicStats stats = admin.persistentTopics().getStats(topic);
assertEquals(stats.publishers.size(), 1);
producer.close();
}

@Test
public void testTenantNameWithInvalidCharacters() throws Exception {
PropertyAdmin propertyAdmin = new PropertyAdmin(Sets.newHashSet("role1", "role2"), Sets.newHashSet("use"));

// If we try to create property with invalid characters, it should fail immediately
try {
admin.properties().createProperty("prop xyz", propertyAdmin);
fail("Should have failed");
} catch (PulsarAdminException e) {
// Expected
}

try {
admin.properties().createProperty("prop&xyz", propertyAdmin);
fail("Should have failed");
} catch (PulsarAdminException e) {
// Expected
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
*/
package org.apache.pulsar.common.naming;

import java.net.URI;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lombok.experimental.UtilityClass;

/**
*/
@UtilityClass
public class NamedEntity {

// allowed characters for property, namespace, cluster and topic names are
Expand All @@ -37,11 +39,4 @@ public static void checkName(String name) throws IllegalArgumentException {
throw new IllegalArgumentException("Invalid named entity: " + name);
}
}

public static void checkURI(URI uri, String name) {
if (!String.format("%s://%s%s", uri.getScheme(), uri.getHost(), uri.getPath()).equals(name)) {
throw new IllegalArgumentException("Invalid trailing chars in named entity: " + name);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ private TopicDomain(String value) {
public String value() {
return this.value;
}

public static TopicDomain getEnum(String value) {
for (TopicDomain e : values()) {
if (e.value.equalsIgnoreCase(value)) {
return e;
}
}
throw new IllegalArgumentException("Invalid enum value " + value);
throw new IllegalArgumentException("Invalid topic domain: '" + value + "'");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private TopicName(String completeTopicName) {
// legacy: persistent://property/cluster/namespace/topic
if (!completeTopicName.contains("://")) {
throw new IllegalArgumentException(
"Invalid completeTopicName name: " + completeTopicName + " -- Domain is missing");
"Invalid topic name: " + completeTopicName + " -- Domain is missing");
}

List<String> parts = Splitter.on("://").limit(2).splitToList(completeTopicName);
Expand Down Expand Up @@ -139,15 +139,15 @@ private TopicName(String completeTopicName) {
this.partitionIndex = getPartitionIndex(completeTopicName);
this.namespaceName = NamespaceName.get(property, cluster, namespacePortion);
} else {
throw new IllegalArgumentException("Invalid completeTopicName name: " + completeTopicName);
throw new IllegalArgumentException("Invalid topic name: " + completeTopicName);
}


if (localName == null || localName.isEmpty()) {
throw new IllegalArgumentException("Invalid completeTopicName name: " + completeTopicName);
throw new IllegalArgumentException("Invalid topic name: " + completeTopicName);
}
} catch (NullPointerException e) {
throw new IllegalArgumentException("Invalid completeTopicName name: " + completeTopicName, e);
throw new IllegalArgumentException("Invalid topic name: " + completeTopicName, e);
}

}
Expand Down

0 comments on commit 3ae5115

Please sign in to comment.