GeneratePreferencesFile { public void run( String[] args ) throws Exception { if( args.length != 1 ) throw new RuntimeException( "A file path was expected as an argument." ); File targetFile = new File( args[ 0 ]); if( ! targetFile.isFile()) throw new RuntimeException( "File " + targetFile + " does not exist." ); Field[] fields = IPreferencesMngr.class.getDeclaredFields(); Map<String,String> propertyNameToComment = new HashMap<> (); for( Field field : fields ) { String constantName = (String) field.get( null ); PreferenceDescription annotation = field.getAnnotation( PreferenceDescription.class ); if( annotation == null ) throw new RuntimeException( "Documentation was not written for the " + field.getName() + " constant in IPreferencesMngr.java." ); StringBuilder sb = new StringBuilder(); sb.append( annotation.desc()); String[] values = annotation.values(); if( values.length > 0 ) { sb.append( "\n\nPossible values:\n" ); for( String value : values ) { sb.append( " - " ); sb.append( value ); sb.append( "\n" ); } } propertyNameToComment.put( constantName, sb.toString()); } Defaults def = new Defaults(); StringBuilder sb = new StringBuilder(); for( PreferenceKeyCategory cat : PreferenceKeyCategory.values()) { sb.append( "\n###\n# " ); sb.append( cat.getDescription()); sb.append( "\n###\n" ); for( Map.Entry<String,PreferenceKeyCategory> entry : def.keyToCategory.entrySet()) { if( cat != entry.getValue()) continue; String comment = propertyNameToComment.get( entry.getKey()); comment = comment.replaceAll( "^", "# " ).replace( "\n", "\n# " ); sb.append( "\n" ); sb.append( comment ); sb.append( "\n" ); sb.append( entry.getKey()); sb.append( " = " ); String defaultValue = def.keyToDefaultValue.get( entry.getKey()); if( ! Utils.isEmptyOrWhitespaces( defaultValue )) sb.append( defaultValue.trim()); sb.append( "\n" ); } sb.append( "\n" ); } Utils.appendStringInto( sb.toString(), targetFile ); }  static void main( String[] args ); void run( String[] args );  }
@Test public void testGeneratedFile_success() throws Exception { File targetFile = this.folder.newFile(); Assert.assertEquals( 0, targetFile.length()); new GeneratePreferencesFile().run( new String[] { targetFile.getAbsolutePath()}); Assert.assertNotEquals( 0, targetFile.length()); String content = Utils.readFileContent( targetFile ); for( String line : content.split( "\n" )) { if( Utils.isEmptyOrWhitespaces( line )) continue; if( ! line.startsWith( "##" ) && ! line.startsWith( "# " ) && ! line.matches( "\\w[\\w.]+\\w = .*" )) throw new Exception( "Invalid line: " + line ); } }
@Test( expected = RuntimeException.class ) public void testGeneratedFile_invalidNumberOfArguments() throws Exception { File targetFile = this.folder.newFile(); Assert.assertEquals( 0, targetFile.length()); new GeneratePreferencesFile().run( new String[] { "a", "b", targetFile.getAbsolutePath()}); }
@Test( expected = RuntimeException.class ) public void testGeneratedFile_invalidArguments() throws Exception { File targetFile = this.folder.newFolder(); new GeneratePreferencesFile().run( new String[] { targetFile.getAbsolutePath()}); }