From 7bd34da6e6330ef536a4768b5e677a17490ddd55 Mon Sep 17 00:00:00 2001 From: Huang Xiao Date: Thu, 27 Jun 2024 14:54:01 +0800 Subject: [PATCH] refactor: Optimize exception information --- .../maven/plugins/deploy/DeployMojo.java | 14 +++---- .../maven/plugins/deploy/DeployMojoTest.java | 37 +++++++++---------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java b/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java index 79ee1d87..3287b5f1 100644 --- a/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java +++ b/src/main/java/org/apache/maven/plugins/deploy/DeployMojo.java @@ -381,20 +381,16 @@ RemoteRepository getDeploymentRepository( + "\" instead."); repo = getRemoteRepository(id, url); } else { - throw new MojoExecutionException( - altDeploymentRepo, - "Invalid legacy syntax and layout for repository.", - "Invalid legacy syntax and layout for alternative repository. Use \"" + id + "::" + url - + "\" instead, and only default layout is supported."); + throw new MojoExecutionException("Invalid legacy syntax and layout for alternative repository: \"" + + altDeploymentRepo + "\". Use \"" + id + "::" + url + + "\" instead, and only default layout is supported."); } } else { matcher = ALT_REPO_SYNTAX_PATTERN.matcher(altDeploymentRepo); if (!matcher.matches()) { - throw new MojoExecutionException( - altDeploymentRepo, - "Invalid syntax for repository.", - "Invalid syntax for alternative repository. Use \"id::url\"."); + throw new MojoExecutionException("Invalid syntax for alternative repository: \"" + altDeploymentRepo + + "\". Use \"id::url\"."); } else { String id = matcher.group(1).trim(); String url = matcher.group(2).trim(); diff --git a/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java b/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java index 52964cc5..ce128e4f 100644 --- a/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java @@ -711,17 +711,18 @@ public void testLegacyAltDeploymentRepositoryWithLegacyLayout() throws Exception setVariableValueToObject(mojo, "project", project); setVariableValueToObject(mojo, "session", session); - setVariableValueToObject(mojo, "altDeploymentRepository", "altDeploymentRepository::legacy::http://localhost"); + String altDeploymentRepository = "altDeploymentRepository::legacy::http://localhost"; + setVariableValueToObject(mojo, "altDeploymentRepository", altDeploymentRepository); project.setVersion("1.0-SNAPSHOT"); try { - mojo.getDeploymentRepository(project, null, null, "altDeploymentRepository::legacy::http://localhost"); + mojo.getDeploymentRepository(project, null, null, altDeploymentRepository); fail("Should throw: Invalid legacy syntax and layout for repository."); } catch (MojoExecutionException e) { - assertEquals(e.getMessage(), "Invalid legacy syntax and layout for repository."); assertEquals( - e.getLongMessage(), - "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::http://localhost\" instead, and only default layout is supported."); + e.getMessage(), + "Invalid legacy syntax and layout for alternative repository: \"" + altDeploymentRepository + + "\". Use \"altDeploymentRepository::http://localhost\" instead, and only default layout is supported."); } } @@ -730,19 +731,18 @@ public void testInsaneAltDeploymentRepository() throws Exception { setVariableValueToObject(mojo, "project", project); setVariableValueToObject(mojo, "session", session); - setVariableValueToObject( - mojo, "altDeploymentRepository", "altDeploymentRepository::hey::wow::foo::http://localhost"); + String altDeploymentRepository = "altDeploymentRepository::hey::wow::foo::http://localhost"; + setVariableValueToObject(mojo, "altDeploymentRepository", altDeploymentRepository); project.setVersion("1.0-SNAPSHOT"); try { - mojo.getDeploymentRepository( - project, null, null, "altDeploymentRepository::hey::wow::foo::http://localhost"); + mojo.getDeploymentRepository(project, null, null, altDeploymentRepository); fail("Should throw: Invalid legacy syntax and layout for repository."); } catch (MojoExecutionException e) { - assertEquals(e.getMessage(), "Invalid legacy syntax and layout for repository."); assertEquals( - e.getLongMessage(), - "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::wow::foo::http://localhost\" instead, and only default layout is supported."); + e.getMessage(), + "Invalid legacy syntax and layout for alternative repository: \"" + altDeploymentRepository + + "\". Use \"altDeploymentRepository::wow::foo::http://localhost\" instead, and only default layout is supported."); } } @@ -766,19 +766,18 @@ public void testLegacyScmSvnAltDeploymentRepository() throws Exception { mojo = new DeployMojo(); setVariableValueToObject(mojo, "project", project); - setVariableValueToObject( - mojo, "altDeploymentRepository", "altDeploymentRepository::legacy::scm:svn:http://localhost"); + String altDeploymentRepository = "altDeploymentRepository::legacy::scm:svn:http://localhost"; + setVariableValueToObject(mojo, "altDeploymentRepository", altDeploymentRepository); project.setVersion("1.0-SNAPSHOT"); try { - mojo.getDeploymentRepository( - project, null, null, "altDeploymentRepository::legacy::scm:svn:http://localhost"); + mojo.getDeploymentRepository(project, null, null, altDeploymentRepository); fail("Should throw: Invalid legacy syntax and layout for repository."); } catch (MojoExecutionException e) { - assertEquals(e.getMessage(), "Invalid legacy syntax and layout for repository."); assertEquals( - e.getLongMessage(), - "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::scm:svn:http://localhost\" instead, and only default layout is supported."); + e.getMessage(), + "Invalid legacy syntax and layout for alternative repository: \"" + altDeploymentRepository + + "\". Use \"altDeploymentRepository::scm:svn:http://localhost\" instead, and only default layout is supported."); } }