From 6f1571a1a69995baee230da60647892fb501f975 Mon Sep 17 00:00:00 2001 From: Robert Scholte Date: Fri, 26 Dec 2014 18:52:19 +0100 Subject: [PATCH] Add unittests for Problem classes, improve javadoc --- .../apache/maven/building/DefaultProblem.java | 3 +- .../building/DefaultProblemCollector.java | 1 + .../maven/building/ProblemCollector.java | 3 +- .../building/DefaultProblemCollectorTest.java | 76 ++++++++++ .../maven/building/DefaultProblemTest.java | 136 ++++++++++++++++++ .../building/ProblemCollectorFactoryTest.java | 45 ++++++ 6 files changed, 262 insertions(+), 2 deletions(-) create mode 100644 maven-builder-support/src/test/java/org/apache/maven/building/DefaultProblemCollectorTest.java create mode 100644 maven-builder-support/src/test/java/org/apache/maven/building/DefaultProblemTest.java create mode 100644 maven-builder-support/src/test/java/org/apache/maven/building/ProblemCollectorFactoryTest.java diff --git a/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblem.java b/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblem.java index 08db68c73a..ad1fc40fb9 100644 --- a/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblem.java +++ b/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblem.java @@ -27,7 +27,7 @@ package org.apache.maven.building; * @author Benjamin Bentmann * @author Robert Scholte */ -public class DefaultProblem +class DefaultProblem implements Problem { @@ -45,6 +45,7 @@ public class DefaultProblem /** * Creates a new problem with the specified message and exception. + * Either {@code message} or {@code exception} is required * * @param message The message describing the problem, may be {@code null}. * @param severity The severity level of the problem, may be {@code null} to default to diff --git a/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblemCollector.java b/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblemCollector.java index ef32010307..567f620e7e 100644 --- a/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblemCollector.java +++ b/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblemCollector.java @@ -53,6 +53,7 @@ class DefaultProblemCollector this.source = source; } + @Override public void add( Problem.Severity severity, String message, int line, int column, Exception cause ) { Problem problem = new DefaultProblem( message, severity, source, line, column, cause ); diff --git a/maven-builder-support/src/main/java/org/apache/maven/building/ProblemCollector.java b/maven-builder-support/src/main/java/org/apache/maven/building/ProblemCollector.java index 98b7b007a7..e7015676ce 100644 --- a/maven-builder-support/src/main/java/org/apache/maven/building/ProblemCollector.java +++ b/maven-builder-support/src/main/java/org/apache/maven/building/ProblemCollector.java @@ -32,6 +32,7 @@ public interface ProblemCollector /** * Adds the specified problem. + * Either message or exception is required * * @param severity The severity of the problem, must not be {@code null}. * @param message The detail message of the problem, may be {@code null}. @@ -51,7 +52,7 @@ public interface ProblemCollector /** * - * @return the collected Problems + * @return the collected Problems, never {@code null} */ List getProblems(); diff --git a/maven-builder-support/src/test/java/org/apache/maven/building/DefaultProblemCollectorTest.java b/maven-builder-support/src/test/java/org/apache/maven/building/DefaultProblemCollectorTest.java new file mode 100644 index 0000000000..bf0f847710 --- /dev/null +++ b/maven-builder-support/src/test/java/org/apache/maven/building/DefaultProblemCollectorTest.java @@ -0,0 +1,76 @@ +package org.apache.maven.building; + +/* + * 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. + */ + +import static org.junit.Assert.*; + +import org.apache.maven.building.Problem.Severity; +import org.junit.Test; + +public class DefaultProblemCollectorTest +{ + + @Test + public void testGetProblems() + { + DefaultProblemCollector collector = new DefaultProblemCollector( null ); + assertNotNull( collector.getProblems() ); + assertEquals( 0, collector.getProblems().size() ); + + collector.add( null, "MESSAGE1", -1, -1, null ); + + Exception e2 = new Exception(); + collector.add( Severity.WARNING, null, 42, 127, e2 ); + + assertEquals( 2, collector.getProblems().size() ); + + Problem p1 = collector.getProblems().get(0); + assertEquals( Severity.ERROR, p1.getSeverity() ); + assertEquals( "MESSAGE1",p1.getMessage() ); + assertEquals( -1, p1.getLineNumber() ); + assertEquals( -1, p1.getColumnNumber() ); + assertEquals( null, p1.getException() ); + + Problem p2 = collector.getProblems().get(1); + assertEquals( Severity.WARNING, p2.getSeverity() ); + assertEquals( "",p2.getMessage() ); + assertEquals( 42, p2.getLineNumber() ); + assertEquals( 127, p2.getColumnNumber() ); + assertEquals( e2, p2.getException() ); + } + + @Test + public void testSetSource() + { + DefaultProblemCollector collector = new DefaultProblemCollector( null ); + + collector.add( null, "PROBLEM1", -1, -1, null ); + + collector.setSource( "SOURCE_PROBLEM2" ); + collector.add( null, "PROBLEM2", -1, -1, null ); + + collector.setSource( "SOURCE_PROBLEM3" ); + collector.add( null, "PROBLEM3", -1, -1, null ); + + assertEquals( "", collector.getProblems().get( 0 ).getSource() ); + assertEquals( "SOURCE_PROBLEM2", collector.getProblems().get( 1 ).getSource() ); + assertEquals( "SOURCE_PROBLEM3", collector.getProblems().get( 2 ).getSource() ); + } +} diff --git a/maven-builder-support/src/test/java/org/apache/maven/building/DefaultProblemTest.java b/maven-builder-support/src/test/java/org/apache/maven/building/DefaultProblemTest.java new file mode 100644 index 0000000000..80a20da999 --- /dev/null +++ b/maven-builder-support/src/test/java/org/apache/maven/building/DefaultProblemTest.java @@ -0,0 +1,136 @@ +package org.apache.maven.building; + +/* + * 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. + */ + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; + +import org.apache.maven.building.Problem.Severity; +import org.junit.Test; + +public class DefaultProblemTest +{ + + @Test + public void testGetSeverity() + { + DefaultProblem problem = new DefaultProblem( null, null, null, -1, -1, null ); + assertEquals( Severity.ERROR, problem.getSeverity() ); + + problem = new DefaultProblem( null, Severity.FATAL, null, -1, -1, null ); + assertEquals( Severity.FATAL, problem.getSeverity() ); + + problem = new DefaultProblem( null, Severity.ERROR, null, -1, -1, null ); + assertEquals( Severity.ERROR, problem.getSeverity() ); + + problem = new DefaultProblem( null, Severity.WARNING, null, -1, -1, null ); + assertEquals( Severity.WARNING, problem.getSeverity() ); + } + + @Test + public void testGetLineNumber() + { + DefaultProblem problem = new DefaultProblem( null, null, null, -1, -1, null ); + assertEquals( -1, problem.getLineNumber() ); + + problem = new DefaultProblem( null, null, null, 42, -1, null ); + assertEquals( 42, problem.getLineNumber() ); + + problem = new DefaultProblem( null, null, null, Integer.MAX_VALUE, -1, null ); + assertEquals( Integer.MAX_VALUE, problem.getLineNumber() ); + + // this case is not specified, might also return -1 + problem = new DefaultProblem( null, null, null, Integer.MIN_VALUE, -1, null ); + assertEquals( Integer.MIN_VALUE, problem.getLineNumber() ); + } + + @Test + public void testGetColumnNumber() + { + DefaultProblem problem = new DefaultProblem( null, null, null, -1, -1, null ); + assertEquals( -1, problem.getColumnNumber() ); + + problem = new DefaultProblem( null, null, null, -1, 42, null ); + assertEquals( 42, problem.getColumnNumber() ); + + problem = new DefaultProblem( null, null, null, -1, Integer.MAX_VALUE, null ); + assertEquals( Integer.MAX_VALUE, problem.getColumnNumber() ); + + // this case is not specified, might also return -1 + problem = new DefaultProblem( null, null, null, -1, Integer.MIN_VALUE, null ); + assertEquals( Integer.MIN_VALUE, problem.getColumnNumber() ); + } + + @Test + public void testGetException() + { + DefaultProblem problem = new DefaultProblem( null, null, null, -1, -1, null ); + assertEquals( null, problem.getException() ); + + Exception e = new Exception(); + problem = new DefaultProblem( null, null, null, -1, -1, e ); + assertSame( e, problem.getException() ); + } + + @Test + public void testGetSource() + { + DefaultProblem problem = new DefaultProblem( null, null, null, -1, -1, null ); + assertEquals( "", problem.getSource() ); + + problem = new DefaultProblem( null, null, "", -1, -1, null ); + assertEquals( "", problem.getSource() ); + + problem = new DefaultProblem( null, null, "SOURCE", -1, -1, null ); + assertEquals( "SOURCE", problem.getSource() ); + } + + @Test + public void testGetLocation() + { + DefaultProblem problem = new DefaultProblem( null, null, null, -1, -1, null ); + assertEquals( "", problem.getLocation() ); + + problem = new DefaultProblem( null, null, "SOURCE", -1, -1, null ); + assertEquals( "SOURCE", problem.getLocation() ); + + problem = new DefaultProblem( null, null, null, 42, -1, null ); + assertEquals( "line 42", problem.getLocation() ); + + problem = new DefaultProblem( null, null, null, -1, 127, null ); + assertEquals( "column 127", problem.getLocation() ); + + problem = new DefaultProblem( null, null, "SOURCE", 42, 127, null ); + assertEquals( "SOURCE, line 42, column 127", problem.getLocation() ); + } + + @Test + public void testGetMessage() + { + DefaultProblem problem = new DefaultProblem( "MESSAGE", null, null, -1, -1, null ); + assertEquals( "MESSAGE", problem.getMessage() ); + + problem = new DefaultProblem( null, null, null, -1, -1, new Exception() ); + assertEquals( "", problem.getMessage() ); + + problem = new DefaultProblem( null, null, null, -1, -1, new Exception( "EXCEPTION MESSAGE" ) ); + assertEquals( "EXCEPTION MESSAGE", problem.getMessage() ); + } +} diff --git a/maven-builder-support/src/test/java/org/apache/maven/building/ProblemCollectorFactoryTest.java b/maven-builder-support/src/test/java/org/apache/maven/building/ProblemCollectorFactoryTest.java new file mode 100644 index 0000000000..44c7906c7c --- /dev/null +++ b/maven-builder-support/src/test/java/org/apache/maven/building/ProblemCollectorFactoryTest.java @@ -0,0 +1,45 @@ +package org.apache.maven.building; + +/* + * 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. + */ + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; + +import java.util.Collections; + +import org.junit.Test; + +public class ProblemCollectorFactoryTest +{ + + @Test + public void testNewInstance() + { + ProblemCollector collector1 = ProblemCollectorFactory.newInstance( null ); + + Problem problem = new DefaultProblem( "MESSAGE1", null, null, -1, -1, null ); + ProblemCollector collector2 = ProblemCollectorFactory.newInstance( Collections.singletonList( problem ) ); + + assertNotSame( collector1, collector2 ); + assertEquals( 0, collector1.getProblems().size() ); + assertEquals( 1, collector2.getProblems().size() ); + } + +}