diff --git a/pom.xml b/pom.xml index 35917f4..852aeae 100644 --- a/pom.xml +++ b/pom.xml @@ -70,7 +70,7 @@ 2.2.1 2.9.1 - 1.5 + 1.8 target/staging/${project.artifactId} diff --git a/src/main/java/org/codehaus/mojo/jdepend/AbstractJDependMojo.java b/src/main/java/org/codehaus/mojo/jdepend/AbstractJDependMojo.java index 6c1c1fe..9b4ea57 100644 --- a/src/main/java/org/codehaus/mojo/jdepend/AbstractJDependMojo.java +++ b/src/main/java/org/codehaus/mojo/jdepend/AbstractJDependMojo.java @@ -125,12 +125,7 @@ public void executeReport( Locale locale ) */ public boolean canGenerateReport() { - File srcDir = new File( classDirectory ); - if ( !srcDir.exists() ) - { - return false; - } - return true; + return new File( classDirectory ).exists(); } /** @@ -143,7 +138,7 @@ public boolean canGenerateReport() */ private String[] getArgumentList( String argument, String locationXMLreportFile, String classDir ) { - List argList = new ArrayList(); + List argList = new ArrayList<>(); argList.add( argument ); @@ -151,7 +146,7 @@ private String[] getArgumentList( String argument, String locationXMLreportFile, argList.add( classDir ); - return (String[]) argList.toArray( new String[argList.size()] ); + return argList.toArray( new String[0] ); } public void generateReport( Locale locale ) diff --git a/src/main/java/org/codehaus/mojo/jdepend/JDependMojo.java b/src/main/java/org/codehaus/mojo/jdepend/JDependMojo.java index 0fb8eb7..d6cd427 100644 --- a/src/main/java/org/codehaus/mojo/jdepend/JDependMojo.java +++ b/src/main/java/org/codehaus/mojo/jdepend/JDependMojo.java @@ -30,7 +30,7 @@ * @author aramirez@exist.com * @version $Id$ */ -@Mojo( name = "generate", requiresProject = true ) +@Mojo( name = "generate" ) @Execute( phase = LifecyclePhase.COMPILE ) public class JDependMojo extends AbstractJDependMojo diff --git a/src/main/java/org/codehaus/mojo/jdepend/JDependNoForkMojo.java b/src/main/java/org/codehaus/mojo/jdepend/JDependNoForkMojo.java index 9ca408f..6d75d4c 100644 --- a/src/main/java/org/codehaus/mojo/jdepend/JDependNoForkMojo.java +++ b/src/main/java/org/codehaus/mojo/jdepend/JDependNoForkMojo.java @@ -28,7 +28,7 @@ * * @author Karl-Heinz Marbaise */ -@Mojo( name = "generate-no-fork", requiresProject = true, defaultPhase = LifecyclePhase.SITE ) +@Mojo( name = "generate-no-fork", defaultPhase = LifecyclePhase.SITE ) public class JDependNoForkMojo extends AbstractJDependMojo { diff --git a/src/main/java/org/codehaus/mojo/jdepend/JDependXMLReportParser.java b/src/main/java/org/codehaus/mojo/jdepend/JDependXMLReportParser.java index 6c780c4..68d6503 100644 --- a/src/main/java/org/codehaus/mojo/jdepend/JDependXMLReportParser.java +++ b/src/main/java/org/codehaus/mojo/jdepend/JDependXMLReportParser.java @@ -75,7 +75,7 @@ public JDependXMLReportParser( File xmlFile ) SAXParserFactory factory = SAXParserFactory.newInstance(); /* Create an empty stack */ - stack = new Stack(); + stack = new Stack<>(); SAXParser saxParser = factory.newSAXParser(); @@ -89,7 +89,6 @@ public JDependXMLReportParser( File xmlFile ) * org.xml.sax.Attributes) */ public void startElement( String namespaceURI, String sName, String qName, Attributes attrs ) - throws SAXException { /* Push element name into stack */ @@ -99,38 +98,37 @@ public void startElement( String namespaceURI, String sName, String qName, Attri // text buffer = new StringBuffer(); - if ( qName.equals( "Packages" ) ) + switch ( qName ) { - packages = new ArrayList(); - } - else if ( qName.equals( "Package" ) ) - { - if ( isParentElement( "Packages" ) ) - { - jdpackage = new JDPackage(); - - if ( attrs != null ) + case "Packages": + packages = new ArrayList<>(); + break; + case "Package": + if ( isParentElement( "Packages" ) ) { - jdpackage.setPackageName( attrs.getValue( 0 ) ); - } - } - else if ( isParentElement( "Cycles" ) ) - { - cyclePackage = new CyclePackage(); + jdpackage = new JDPackage(); - if ( attrs != null ) + if ( attrs != null ) + { + jdpackage.setPackageName( attrs.getValue( 0 ) ); + } + } + else if ( isParentElement( "Cycles" ) ) { - cyclePackage.setName( attrs.getValue( 0 ) ); + cyclePackage = new CyclePackage(); + + if ( attrs != null ) + { + cyclePackage.setName( attrs.getValue( 0 ) ); + } } - } - } - else if ( qName.equals( "Stats" ) ) - { - stats = new Stats(); - } - else if ( qName.equals( "Cycles" ) ) - { - cycles = new ArrayList(); + break; + case "Stats": + stats = new Stats(); + break; + case "Cycles": + cycles = new ArrayList<>(); + break; } } @@ -139,99 +137,89 @@ else if ( qName.equals( "Cycles" ) ) * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String) */ public void endElement( String namespaceURI, String sName, String qName ) - throws SAXException { String elementValue = buffer != null ? buffer.toString().trim() : null; - if ( qName.equals( "Package" ) ) + switch ( qName ) { - if ( isParentElement( "Packages" ) ) - { - if ( errFlag == false ) + case "Package": + if ( isParentElement( "Packages" ) ) { - jdpackage.setStats( stats ); + if ( !errFlag ) + { + jdpackage.setStats( stats ); - packages.add( jdpackage ); + packages.add( jdpackage ); + } + errFlag = false; } - errFlag = false; - } - else if ( isParentElement( "DependsUpon" ) ) - { - jdpackage.addDependsUpon( elementValue ); - } - else if ( isParentElement( "UsedBy" ) ) - { - jdpackage.addUsedBy( elementValue ); - } - else if ( isParentElement( "Package" ) ) - { - cyclePackage.addPackageList( elementValue ); - } - else if ( isParentElement( "Cycles" ) ) - { - cycles.add( cyclePackage ); - } - } - else if ( qName.equals( "TotalClasses" ) ) - { - stats.setTotalClasses( elementValue ); - } - else if ( qName.equals( "ConcreteClasses" ) ) - { - if ( isParentElement( "Stats" ) ) - { - stats.setConcreteClasses( elementValue ); - } - } - else if ( qName.equals( "AbstractClasses" ) ) - { - if ( isParentElement( "Stats" ) ) - { - stats.setAbstractClasses( elementValue ); - } - } - else if ( qName.equals( "Ca" ) ) - { - stats.setCa( elementValue ); - } - else if ( qName.equals( "Ce" ) ) - { - stats.setCe( elementValue ); - } - else if ( qName.equals( "A" ) ) - { - stats.setA( elementValue ); - } - else if ( qName.equals( "I" ) ) - { - stats.setI( elementValue ); - } - else if ( qName.equals( "D" ) ) - { - stats.setD( elementValue ); - } - else if ( qName.equals( "V" ) ) - { - stats.setV( elementValue ); - } - else if ( qName.equals( "Class" ) ) - { - if ( isParentElement( "AbstractClasses" ) ) - { - jdpackage.addAbstractClasses( elementValue ); - } - else if ( isParentElement( "ConcreteClasses" ) ) - { - jdpackage.addConcreteClasses( elementValue ); - } - } - else if ( qName.equals( "error" ) ) - { - if ( isParentElement( "Package" ) ) - { - errFlag = true; - } + else if ( isParentElement( "DependsUpon" ) ) + { + jdpackage.addDependsUpon( elementValue ); + } + else if ( isParentElement( "UsedBy" ) ) + { + jdpackage.addUsedBy( elementValue ); + } + else if ( isParentElement( "Package" ) ) + { + cyclePackage.addPackageList( elementValue ); + } + else if ( isParentElement( "Cycles" ) ) + { + cycles.add( cyclePackage ); + } + break; + case "TotalClasses": + stats.setTotalClasses( elementValue ); + break; + case "ConcreteClasses": + if ( isParentElement( "Stats" ) ) + { + stats.setConcreteClasses( elementValue ); + } + break; + case "AbstractClasses": + if ( isParentElement( "Stats" ) ) + { + stats.setAbstractClasses( elementValue ); + } + break; + case "Ca": + stats.setCa( elementValue ); + break; + case "Ce": + stats.setCe( elementValue ); + break; + case "A": + stats.setA( elementValue ); + break; + case "I": + stats.setI( elementValue ); + break; + case "D": + stats.setD( elementValue ); + break; + case "V": + stats.setV( elementValue ); + break; + case "Class": + if ( isParentElement( "AbstractClasses" ) ) + { + jdpackage.addAbstractClasses( elementValue ); + } + else if ( isParentElement( "ConcreteClasses" ) ) + { + jdpackage.addConcreteClasses( elementValue ); + } + break; + case "error": + if ( isParentElement( "Package" ) ) + { + errFlag = true; + } + break; } if ( stack.size() != 0 ) @@ -248,7 +236,6 @@ else if ( qName.equals( "error" ) ) * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int) */ public void characters( char[] buff, int offset, int len ) - throws SAXException { if ( buffer != null ) { @@ -277,11 +264,7 @@ public Stats getStats() */ private int getParentIndex() { - int parentIndex = 0; - - parentIndex = stack.size() - 2; - - return parentIndex; + return stack.size() - 2; } /** @@ -290,10 +273,6 @@ private int getParentIndex() */ private boolean isParentElement( String parentElement ) { - boolean isParent = false; - - isParent = stack.get( getParentIndex() ).toString().equals( parentElement ); - - return isParent; + return stack.get( getParentIndex() ).equals( parentElement ); } } diff --git a/src/main/java/org/codehaus/mojo/jdepend/ReportGenerator.java b/src/main/java/org/codehaus/mojo/jdepend/ReportGenerator.java index 88efe2b..fb738a6 100644 --- a/src/main/java/org/codehaus/mojo/jdepend/ReportGenerator.java +++ b/src/main/java/org/codehaus/mojo/jdepend/ReportGenerator.java @@ -32,13 +32,9 @@ public class ReportGenerator { private JDependXMLReportParser jdepend; - private JDPackage jdpackage; - private Stats stats; - private List list; - - private CyclePackage cyclepackage; + private List list; /** * Creates a new instance of ReportGenerator. @@ -138,16 +134,15 @@ public void doSummarySection( ResourceBundle bundle, Sink sink ) list = jdepend.packages; - for ( int i = 0; i < list.size(); i++ ) + for ( JDPackage o : list ) { - jdpackage = (JDPackage) list.get( i ); - stats = jdpackage.getStats(); + stats = o.getStats(); sink.tableRow(); sink.tableCell(); - sink.link( bundle.getString( "report.dash" ) + jdpackage.getPackageName() ); //$NON-NLS-1$ - sink.text( jdpackage.getPackageName() ); + sink.link( bundle.getString( "report.dash" ) + o.getPackageName() ); //$NON-NLS-1$ + sink.text( o.getPackageName() ); sink.link_(); sink.tableCell_(); @@ -270,10 +265,8 @@ public void doPackage( ResourceBundle bundle, Sink sink ) } else { - for ( int i = 0; i < list.size(); i++ ) + for ( JDPackage jdpackage : list ) { - jdpackage = (JDPackage) list.get( i ); - stats = jdpackage.getStats(); sink.anchor( jdpackage.getPackageName() ); @@ -373,7 +366,7 @@ public void doPackage( ResourceBundle bundle, Sink sink ) sink.tableRow(); /* Abstract Classes */ - List abstractList = jdpackage.getAbstractClasses(); + List abstractList = jdpackage.getAbstractClasses(); sink.tableCell(); @@ -385,16 +378,16 @@ public void doPackage( ResourceBundle bundle, Sink sink ) } else { - for ( int j = 0; j < abstractList.size(); j++ ) + for ( String value : abstractList ) { - sink.text( (String) abstractList.get( j ) ); + sink.text( value ); sink.lineBreak(); } } sink.tableCell_(); /* Concrete Classes */ - java.util.List concreteList = jdpackage.getConcreteClasses(); + List concreteList = jdpackage.getConcreteClasses(); sink.tableCell(); @@ -406,16 +399,16 @@ public void doPackage( ResourceBundle bundle, Sink sink ) } else { - for ( int j = 0; j < concreteList.size(); j++ ) + for ( String s : concreteList ) { - sink.text( (String) concreteList.get( j ) ); + sink.text( s ); sink.lineBreak(); } } sink.tableCell_(); /* Used By Packages */ - List aList = jdpackage.getUsedBy(); + List aList = jdpackage.getUsedBy(); sink.tableCell(); @@ -427,16 +420,14 @@ public void doPackage( ResourceBundle bundle, Sink sink ) } else { - for ( int j = 0; j < aList.size(); j++ ) + for ( String s : aList ) { - sink.text( (String) aList.get( j ) ); + sink.text( s ); sink.lineBreak(); } } sink.tableCell_(); - aList = null; - /* Uses Package */ aList = jdpackage.getDependsUpon(); @@ -450,9 +441,9 @@ public void doPackage( ResourceBundle bundle, Sink sink ) } else { - for ( int j = 0; j < aList.size(); j++ ) + for ( String s : aList ) { - sink.text( (String) aList.get( j ) ); + sink.text( s ); sink.lineBreak(); } } @@ -488,7 +479,7 @@ public void doCycleSection( ResourceBundle bundle, Sink sink ) public void doCycles( ResourceBundle bundle, Sink sink ) { - List cycleList = jdepend.cycles; + List cycleList = jdepend.cycles; if ( cycleList.size() <= 0 ) { @@ -509,18 +500,17 @@ public void doCycles( ResourceBundle bundle, Sink sink ) sink.tableHeaderCell_(); sink.tableRow_(); - for ( int i = 0; i < cycleList.size(); i++ ) + for ( CyclePackage cyclePackage : cycleList ) { - cyclepackage = (CyclePackage) cycleList.get( i ); sink.tableRow(); sink.tableCell(); - sink.text( cyclepackage.getName() ); + sink.text( cyclePackage.getName() ); sink.tableCell_(); /* Package Dependencies List */ - List packageList = cyclepackage.getPackageList(); + List packageList = cyclePackage.getPackageList(); sink.tableCell(); if ( packageList.size() <= 0 ) { @@ -530,9 +520,9 @@ public void doCycles( ResourceBundle bundle, Sink sink ) } else { - for ( int j = 0; j < packageList.size(); j++ ) + for ( String s : packageList ) { - sink.text( (String) packageList.get( j ) ); + sink.text( s ); sink.lineBreak(); } } @@ -653,12 +643,6 @@ public void doExplanationSection( ResourceBundle bundle, Sink sink ) private String convertToPercent( String value ) { - float ival = 0; - - ival = Float.parseFloat( value ); - ival = ival * 100; - value = String.valueOf( ival ) + "%"; //$NON-NLS-1$ - - return value; + return Float.parseFloat( value ) * 100 + "%"; //$NON-NLS-1$ } } diff --git a/src/main/java/org/codehaus/mojo/jdepend/objects/CyclePackage.java b/src/main/java/org/codehaus/mojo/jdepend/objects/CyclePackage.java index 01e0fda..0695e72 100644 --- a/src/main/java/org/codehaus/mojo/jdepend/objects/CyclePackage.java +++ b/src/main/java/org/codehaus/mojo/jdepend/objects/CyclePackage.java @@ -43,7 +43,7 @@ public List getPackageList() { if ( packageList == null ) { - packageList = new ArrayList(); + packageList = new ArrayList<>(); } return this.packageList; } diff --git a/src/main/java/org/codehaus/mojo/jdepend/objects/JDPackage.java b/src/main/java/org/codehaus/mojo/jdepend/objects/JDPackage.java index 2b1947e..dd4fef1 100644 --- a/src/main/java/org/codehaus/mojo/jdepend/objects/JDPackage.java +++ b/src/main/java/org/codehaus/mojo/jdepend/objects/JDPackage.java @@ -72,7 +72,7 @@ public List getAbstractClasses() { if ( abstractClasses == null ) { - abstractClasses = new ArrayList(); + abstractClasses = new ArrayList<>(); } return abstractClasses; } @@ -86,7 +86,7 @@ public List getConcreteClasses() { if ( concreteClasses == null ) { - concreteClasses = new ArrayList(); + concreteClasses = new ArrayList<>(); } return concreteClasses; } @@ -100,7 +100,7 @@ public List getDependsUpon() { if ( dependsUpon == null ) { - dependsUpon = new ArrayList(); + dependsUpon = new ArrayList<>(); } return dependsUpon; } @@ -119,7 +119,7 @@ public List getUsedBy() { if ( usedBy == null ) { - usedBy = new ArrayList(); + usedBy = new ArrayList<>(); } return usedBy; } diff --git a/src/main/java/org/codehaus/mojo/jdepend/objects/Packages.java b/src/main/java/org/codehaus/mojo/jdepend/objects/Packages.java index 9ab18d6..b75bcdb 100644 --- a/src/main/java/org/codehaus/mojo/jdepend/objects/Packages.java +++ b/src/main/java/org/codehaus/mojo/jdepend/objects/Packages.java @@ -44,7 +44,7 @@ public List getPackages() { if ( this.jdpackage == null ) { - this.jdpackage = new ArrayList(); + this.jdpackage = new ArrayList<>(); } return this.jdpackage; diff --git a/src/test/java/org/codehaus/mojo/jdepend/JDependMojoTest.java b/src/test/java/org/codehaus/mojo/jdepend/JDependMojoTest.java index 15553b6..9070b98 100644 --- a/src/test/java/org/codehaus/mojo/jdepend/JDependMojoTest.java +++ b/src/test/java/org/codehaus/mojo/jdepend/JDependMojoTest.java @@ -25,27 +25,31 @@ import java.io.IOException; import jdepend.xmlui.JDepend; -import junit.framework.TestCase; import org.codehaus.plexus.util.IOUtil; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; public class JDependMojoTest - extends TestCase { JDependXMLReportParser parser; public static final String REPORT_PATH = "target/test-classes/jdepend-report.xml"; - private String basedir = System.getProperty( "basedir" ); + private final String basedir = System.getProperty( "basedir" ); - private File reportXML = new File( basedir, REPORT_PATH ); + private final File reportXML = new File( basedir, REPORT_PATH ); + @Before public void setUp() throws Exception { parser = new JDependXMLReportParser( reportXML ); } + @Test public void testJDependReportContent() throws IOException { @@ -59,7 +63,7 @@ public void testJDependReportContent() args[2] = classDirectory.getCanonicalPath(); JDepend.main( args ); - ; + assertTrue( "Generated report xml from " + generatedReport + " is not equal to expected output " + reportXML, IOUtil.contentEquals( new FileInputStream( generatedReport ), new FileInputStream( reportXML ) ) ); } diff --git a/src/test/java/org/codehaus/mojo/jdepend/JDependReportParserTest.java b/src/test/java/org/codehaus/mojo/jdepend/JDependReportParserTest.java index 40c46db..d0b7698 100644 --- a/src/test/java/org/codehaus/mojo/jdepend/JDependReportParserTest.java +++ b/src/test/java/org/codehaus/mojo/jdepend/JDependReportParserTest.java @@ -20,22 +20,28 @@ * #L% */ -import junit.framework.TestCase; import org.codehaus.mojo.jdepend.objects.JDPackage; import org.codehaus.mojo.jdepend.objects.Stats; +import org.junit.Before; +import org.junit.Test; + import java.io.File; import java.util.ArrayList; import java.util.List; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + public class JDependReportParserTest - extends TestCase { private JDependXMLReportParser parser; private List packages; - private String basedir = System.getProperty( "basedir" ); + private final String basedir = System.getProperty( "basedir" ); + @Before public void setUp() throws Exception { @@ -45,14 +51,16 @@ public void setUp() packages = parser.packages; } + @Test public void testTotalNumberPackages() { assertEquals( "Total number of packages is not equal to expected output", 2, parser.packages.size() ); } + @Test public void testPackageNames() { - List packageNames = new ArrayList(); + List packageNames = new ArrayList<>(); for (JDPackage jdpackage : packages) { @@ -62,9 +70,10 @@ public void testPackageNames() assertTrue( packageNames.contains( "org.codehaus.mojo.jdepend.objects" ) ); } + @Test public void testPackageNamesNotContainingInList() { - List packageNames = new ArrayList(); + List packageNames = new ArrayList<>(); for (JDPackage jdpackage : packages) { @@ -74,6 +83,7 @@ public void testPackageNamesNotContainingInList() assertFalse( packageNames.contains( "test2" ) ); } + @Test public void testStats() { for (JDPackage jdpackage : packages) @@ -109,13 +119,14 @@ public void testStats() } } + @Test public void testConcreteClasses() { for (JDPackage jdpackage : packages) { if ( jdpackage.getPackageName().equals( "org.codehaus.mojo.jdepend" ) ) { - List concretes = jdpackage.getConcreteClasses(); + List concretes = jdpackage.getConcreteClasses(); assertTrue( concretes.contains( "org.codehaus.mojo.jdepend.JDependMojo" ) ); assertTrue( concretes.contains( "org.codehaus.mojo.jdepend.JDependXMLReportParser" ) ); @@ -123,7 +134,7 @@ public void testConcreteClasses() } if ( jdpackage.getPackageName().equals( "org.codehaus.mojo.jdepend.objects" ) ) { - List concretes = jdpackage.getConcreteClasses(); + List concretes = jdpackage.getConcreteClasses(); assertTrue( concretes.contains( "org.codehaus.mojo.jdepend.objects.CyclePackage" ) ); assertTrue( concretes.contains( "org.codehaus.mojo.jdepend.objects.JDPackage" ) ); @@ -137,6 +148,7 @@ public void testConcreteClasses() } } + @Test public void testCountOfDependsUpon() { for (JDPackage jdpackage : packages)