Skip to content

Commit

Permalink
fixed registryConfig equals (#4359)
Browse files Browse the repository at this point in the history
* fixed registryConfig equals

* port > 0
  • Loading branch information
Leishunyu authored and beiwei30 committed Jul 17, 2019
1 parent fba25d2 commit 85b96e2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import java.util.Map;

import static org.apache.dubbo.common.constants.CommonConstants.FILE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PASSWORD_KEY;
import static org.apache.dubbo.config.Constants.REGISTRIES_SUFFIX;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.USERNAME_KEY;
import static org.apache.dubbo.config.Constants.REGISTRIES_SUFFIX;
import static org.apache.dubbo.config.Constants.ZOOKEEPER_PROTOCOL;
import static org.apache.dubbo.registry.Constants.EXTRA_KEYS_KEY;

Expand Down Expand Up @@ -182,6 +182,11 @@ public void setAddress(String address) {
int i = address.indexOf("://");
if (i > 0) {
this.updateIdIfAbsent(address.substring(0, i));
this.updateProtocolIfAbsent(address.substring(0, i));
int port = address.lastIndexOf(":");
if (port > 0) {
this.updatePortIfAbsent(StringUtils.parseInteger(address.substring(port + 1)));
}
}
}
}
Expand Down Expand Up @@ -432,4 +437,15 @@ public boolean isValid() {
return !StringUtils.isEmpty(address);
}

protected void updatePortIfAbsent(Integer value) {
if (value != null && value > 0 && port == null) {
this.port = value;
}
}

protected void updateProtocolIfAbsent(String value) {
if (StringUtils.isNotEmpty(value) && StringUtils.isEmpty(protocol)) {
this.protocol = value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

package org.apache.dubbo.config;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.apache.dubbo.config.Constants.SHUTDOWN_TIMEOUT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_KEY;
import static org.apache.dubbo.config.Constants.SHUTDOWN_TIMEOUT_KEY;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -177,4 +178,13 @@ public void testDefault() throws Exception {
assertThat(registry.isDefault(), is(true));
}

@Test
public void testEquals() throws Exception {
RegistryConfig registry1 = new RegistryConfig();
RegistryConfig registry2 = new RegistryConfig();
registry1.setAddress("zookeeper://127.0.0.1:2182");
registry2.setAddress("zookeeper://127.0.0.1:2183");
Assertions.assertNotEquals(registry1, registry2);
}

}

0 comments on commit 85b96e2

Please sign in to comment.