forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix alibaba#3973 * 重复代码抽取到一个方法 * 删除私有方法的注释 * 处理namespace参数的方法提出到一个工具类中 * 修改注释 * 添加licences * 增加 TenantUtil 的测试 * TenantUtil 改名为 NamespaceUtil
- Loading branch information
1 parent
af29a2e
commit 59d6ad5
Showing
3 changed files
with
105 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
config/src/main/java/com/alibaba/nacos/config/server/utils/NamespaceUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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 com.alibaba.nacos.config.server.utils; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
/** | ||
* namespace(tenant) util. | ||
* Because config and naming treat namespace(tenant) differently, | ||
* this tool class can only be used by the config module. | ||
* @author klw(213539@qq.com) | ||
* @date 2020/10/12 17:56 | ||
*/ | ||
public class NamespaceUtil { | ||
|
||
private static final String NAMESPACE_PUBLIC_KEY = "public"; | ||
|
||
private static final String NAMESPACE_NULL_KEY = "null"; | ||
|
||
/** | ||
* Treat the namespace(tenant) parameters with values of "public" and "null" as an empty string. | ||
* @param tenant namespace(tenant) id | ||
* @return java.lang.String A namespace(tenant) string processed | ||
*/ | ||
public static String processNamespaceParameter(String tenant) { | ||
if (StringUtils.isBlank(tenant) || NAMESPACE_PUBLIC_KEY.equalsIgnoreCase(tenant) || NAMESPACE_NULL_KEY | ||
.equalsIgnoreCase(tenant)) { | ||
return ""; | ||
} | ||
return tenant.trim(); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
config/src/test/java/com/alibaba/nacos/config/server/utils/NamespaceUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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 com.alibaba.nacos.config.server.utils; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* test NamespaceUtil. | ||
* | ||
* @author klw(213539 @ qq.com) | ||
* @date 2020/10/13 9:46 | ||
*/ | ||
public class NamespaceUtilTest { | ||
|
||
@Test | ||
public void testProcessTenantParameter() { | ||
String strPublic = "public"; | ||
String strNull = "null"; | ||
String strEmpty = ""; | ||
String strAbc = "abc"; | ||
String strdef123 = "def123"; | ||
String strAbcHasSpace = " abc "; | ||
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strPublic)); | ||
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strNull)); | ||
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strEmpty)); | ||
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(null)); | ||
Assert.assertEquals(strAbc, NamespaceUtil.processNamespaceParameter(strAbc)); | ||
Assert.assertEquals(strdef123, NamespaceUtil.processNamespaceParameter(strdef123)); | ||
Assert.assertEquals(strAbc, NamespaceUtil.processNamespaceParameter(strAbcHasSpace)); | ||
} | ||
|
||
} |