From e669fdade100fb5ba55490d866e09c046031601b Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Mon, 20 Feb 2017 17:15:47 +0100 Subject: [PATCH] [java] module name validation --- .../rename/JavaModuleRenameValidator.java | 6 ++- .../rename/JavaModuleRenameValidatorTest.kt | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 java/java-tests/testSrc/com/intellij/refactoring/rename/JavaModuleRenameValidatorTest.kt diff --git a/java/java-impl/src/com/intellij/refactoring/rename/JavaModuleRenameValidator.java b/java/java-impl/src/com/intellij/refactoring/rename/JavaModuleRenameValidator.java index 79d9b374bbae..62ba08a3cf94 100644 --- a/java/java-impl/src/com/intellij/refactoring/rename/JavaModuleRenameValidator.java +++ b/java/java-impl/src/com/intellij/refactoring/rename/JavaModuleRenameValidator.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,7 +35,9 @@ public class JavaModuleRenameValidator implements RenameInputValidator { @Override public boolean isInputValid(@NotNull String newName, @NotNull PsiElement element, @NotNull ProcessingContext context) { + if (StringUtil.isEmptyOrSpaces(newName)) return false; + PsiNameHelper helper = PsiNameHelper.getInstance(element.getProject()); - return StringUtil.isEmptyOrSpaces(newName) || StringUtil.split(newName, ".").stream().allMatch(helper::isIdentifier); + return StringUtil.split(newName, ".", true, false).stream().allMatch(helper::isIdentifier); } } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/rename/JavaModuleRenameValidatorTest.kt b/java/java-tests/testSrc/com/intellij/refactoring/rename/JavaModuleRenameValidatorTest.kt new file mode 100644 index 000000000000..45e1a8b947a4 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/refactoring/rename/JavaModuleRenameValidatorTest.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * 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.intellij.refactoring.rename + +import com.intellij.psi.PsiJavaFile +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase +import com.intellij.util.ProcessingContext +import org.junit.Test + +class JavaModuleRenameValidatorTest : LightCodeInsightFixtureTestCase() { + @Test fun test() { + val validator = JavaModuleRenameValidator() + val module = (myFixture.configureByText("module-info.java", "module M { }") as PsiJavaFile).moduleDeclaration!! + val context = ProcessingContext() + + assertTrue(validator.isInputValid("M", module, context)) + assertTrue(validator.isInputValid("M.M.M", module, context)) + assertTrue(validator.isInputValid("M42", module, context)) + + assertFalse(validator.isInputValid("", module, context)) + assertFalse(validator.isInputValid(" ", module, context)) + assertFalse(validator.isInputValid("\n", module, context)) + assertFalse(validator.isInputValid("42", module, context)) + assertFalse(validator.isInputValid("M.42", module, context)) + assertFalse(validator.isInputValid("M.", module, context)) + assertFalse(validator.isInputValid(".M", module, context)) + assertFalse(validator.isInputValid("M.M.", module, context)) + assertFalse(validator.isInputValid("M..M", module, context)) + } +} \ No newline at end of file