You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by zh...@apache.org on 2020/03/13 06:10:45 UTC
[pulsar] 04/17: Add verification for
SchemaDefinitionBuilderImpl.java (#6405)
This is an automated email from the ASF dual-hosted git repository.
zhaijia pushed a commit to branch branch-2.5
in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit f2a4a8d352104b6e275efc38c7a9c4322284f4ef
Author: lipenghui <pe...@apache.org>
AuthorDate: Wed Feb 26 02:41:31 2020 +0800
Add verification for SchemaDefinitionBuilderImpl.java (#6405)
### Motivation
Add verification for SchemaDefinitionBuilderImpl.java
### Verifying this change
Added a new unit test.
(cherry picked from commit 848ad30f1cfd4dd731a6c6ab31ffb2e19b1280d5)
---
.../impl/schema/SchemaDefinitionBuilderImpl.java | 9 +++++
.../client/api/SchemaDefinitionBuilderTest.java | 42 ++++++++++++++++++++++
2 files changed, 51 insertions(+)
diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/SchemaDefinitionBuilderImpl.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/SchemaDefinitionBuilderImpl.java
index ea9ede5..2a0cab7 100644
--- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/SchemaDefinitionBuilderImpl.java
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/SchemaDefinitionBuilderImpl.java
@@ -18,6 +18,9 @@
*/
package org.apache.pulsar.client.impl.schema;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import org.apache.commons.lang3.StringUtils;
import org.apache.pulsar.client.api.schema.SchemaDefinition;
import org.apache.pulsar.client.api.schema.SchemaDefinitionBuilder;
@@ -98,6 +101,12 @@ public class SchemaDefinitionBuilderImpl<T> implements SchemaDefinitionBuilder<T
@Override
public SchemaDefinition<T> build() {
+ checkArgument(StringUtils.isNotBlank(jsonDef) || clazz != null,
+ "Must specify one of the pojo or jsonDef for the schema definition.");
+
+ checkArgument(!(StringUtils.isNotBlank(jsonDef) && clazz != null),
+ "Not allowed to set pojo and jsonDef both for the schema definition.");
+
properties.put(ALWAYS_ALLOW_NULL, this.alwaysAllowNull ? "true" : "false");
return new SchemaDefinitionImpl(clazz, jsonDef, alwaysAllowNull, properties, supportSchemaVersioning);
diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/api/SchemaDefinitionBuilderTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/api/SchemaDefinitionBuilderTest.java
new file mode 100644
index 0000000..90d3a27
--- /dev/null
+++ b/pulsar-client/src/test/java/org/apache/pulsar/client/api/SchemaDefinitionBuilderTest.java
@@ -0,0 +1,42 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.client.api;
+
+import org.apache.pulsar.client.api.schema.SchemaDefinition;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class SchemaDefinitionBuilderTest {
+
+ @Test
+ public void testVerification() {
+ try {
+ SchemaDefinition.builder().build();
+ Assert.fail("should failed");
+ } catch (IllegalArgumentException ignore) {
+ }
+ try {
+ SchemaDefinition.builder().withJsonDef("{}").withPojo(Object.class).build();
+ Assert.fail("should failed");
+ } catch (IllegalArgumentException ignore) {
+ }
+ SchemaDefinition.builder().withJsonDef("{}").build();
+ SchemaDefinition.builder().withPojo(Object.class).build();
+ }
+}