This is an automated email from the ASF dual-hosted git repository. mmerli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-pulsar.git
The following commit(s) were added to refs/heads/master by this push: new 3ae5115 Added test to ensure property name can contain '_' characters. (#1516) 3ae5115 is described below commit 3ae511518558499729ea9ac2154b183718fc0668 Author: Matteo Merli <mme...@apache.org> AuthorDate: Fri Apr 6 16:33:10 2018 -0700 Added test to ensure property name can contain '_' characters. (#1516) --- .../apache/pulsar/broker/admin/AdminApiTest2.java | 37 ++++++++++++++++++++++ .../apache/pulsar/common/naming/NamedEntity.java | 11 ++----- .../apache/pulsar/common/naming/TopicDomain.java | 4 +-- .../org/apache/pulsar/common/naming/TopicName.java | 8 ++--- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest2.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest2.java index 2bc1652..c8c3b75 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest2.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest2.java @@ -768,4 +768,41 @@ public class AdminApiTest2 extends MockedPulsarServiceBaseTest { 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 + } + } } diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/naming/NamedEntity.java b/pulsar-common/src/main/java/org/apache/pulsar/common/naming/NamedEntity.java index 165fd07..e5180d6 100644 --- a/pulsar-common/src/main/java/org/apache/pulsar/common/naming/NamedEntity.java +++ b/pulsar-common/src/main/java/org/apache/pulsar/common/naming/NamedEntity.java @@ -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 @@ -37,11 +39,4 @@ public class NamedEntity { 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); - } - } - } \ No newline at end of file diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicDomain.java b/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicDomain.java index 744efea..26b382f 100644 --- a/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicDomain.java +++ b/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicDomain.java @@ -30,14 +30,14 @@ public enum TopicDomain { 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 diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java b/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java index 6bb2693..aa7dd4a 100644 --- a/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java +++ b/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java @@ -105,7 +105,7 @@ public class TopicName implements ServiceUnitId { // 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); @@ -139,15 +139,15 @@ public class TopicName implements ServiceUnitId { 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); } } -- To stop receiving notification emails like this one, please contact mme...@apache.org.