Add unittests for Problem classes, improve javadoc

This commit is contained in:
Robert Scholte 2014-12-26 18:52:19 +01:00
parent d37fbf6011
commit 6f1571a1a6
6 changed files with 262 additions and 2 deletions

View File

@ -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

View File

@ -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 );

View File

@ -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<Problem> getProblems();

View File

@ -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() );
}
}

View File

@ -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() );
}
}

View File

@ -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() );
}
}