maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [3/3] maven-indexer git commit: format and use try resource
Date Mon, 24 Jul 2017 03:58:57 GMT
format and use try resource

Signed-off-by: olivier lamy <olamy@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/maven-indexer/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-indexer/commit/0bbcad1f
Tree: http://git-wip-us.apache.org/repos/asf/maven-indexer/tree/0bbcad1f
Diff: http://git-wip-us.apache.org/repos/asf/maven-indexer/diff/0bbcad1f

Branch: refs/heads/master
Commit: 0bbcad1fb65642f83003fea6c4bf9ddd217bfb12
Parents: 75cc5ac
Author: olivier lamy <olamy@apache.org>
Authored: Mon Jul 24 13:57:57 2017 +1000
Committer: olivier lamy <olamy@apache.org>
Committed: Mon Jul 24 13:57:57 2017 +1000

----------------------------------------------------------------------
 .../org/apache/maven/index/reader/Utils.java    | 246 ++++++++++---------
 .../maven/index/reader/ChunkReaderTest.java     |  98 ++++----
 2 files changed, 181 insertions(+), 163 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-indexer/blob/0bbcad1f/indexer-reader/src/main/java/org/apache/maven/index/reader/Utils.java
----------------------------------------------------------------------
diff --git a/indexer-reader/src/main/java/org/apache/maven/index/reader/Utils.java b/indexer-reader/src/main/java/org/apache/maven/index/reader/Utils.java
index c5b210a..db4d58b 100644
--- a/indexer-reader/src/main/java/org/apache/maven/index/reader/Utils.java
+++ b/indexer-reader/src/main/java/org/apache/maven/index/reader/Utils.java
@@ -19,6 +19,11 @@ package org.apache.maven.index.reader;
  * under the License.
  */
 
+import org.apache.maven.index.reader.Record.EntryKey;
+import org.apache.maven.index.reader.Record.Type;
+import org.apache.maven.index.reader.ResourceHandler.Resource;
+import org.apache.maven.index.reader.WritableResourceHandler.WritableResource;
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -30,11 +35,6 @@ import java.util.Properties;
 import java.util.TimeZone;
 import java.util.regex.Pattern;
 
-import org.apache.maven.index.reader.Record.EntryKey;
-import org.apache.maven.index.reader.Record.Type;
-import org.apache.maven.index.reader.ResourceHandler.Resource;
-import org.apache.maven.index.reader.WritableResourceHandler.WritableResource;
-
 /**
  * Reusable code snippets and constants.
  *
@@ -42,129 +42,153 @@ import org.apache.maven.index.reader.WritableResourceHandler.WritableResource;
  */
 public final class Utils
 {
-  private Utils() {
-    // nothing
-  }
+    private Utils()
+    {
+        // nothing
+    }
 
-  public static final String INDEX_FILE_PREFIX = "nexus-maven-repository-index";
+    public static final String INDEX_FILE_PREFIX = "nexus-maven-repository-index";
 
-  public static final DateFormat INDEX_DATE_FORMAT;
+    public static final DateFormat INDEX_DATE_FORMAT;
 
-  static {
-    INDEX_DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss.SSS Z");
-    INDEX_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
-  }
+    static
+    {
+        INDEX_DATE_FORMAT = new SimpleDateFormat( "yyyyMMddHHmmss.SSS Z" );
+        INDEX_DATE_FORMAT.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
+    }
 
-  public static final String FIELD_SEPARATOR = "|";
+    public static final String FIELD_SEPARATOR = "|";
 
-  public static final String NOT_AVAILABLE = "NA";
+    public static final String NOT_AVAILABLE = "NA";
 
-  public static final String UINFO = "u";
+    public static final String UINFO = "u";
 
-  public static final String INFO = "i";
+    public static final String INFO = "i";
 
-  public static final Pattern FS_PATTERN = Pattern.compile(Pattern.quote(FIELD_SEPARATOR));
+    public static final Pattern FS_PATTERN = Pattern.compile( Pattern.quote( FIELD_SEPARATOR
) );
 
-  /**
-   * Creates and loads {@link Properties} from provided {@link InputStream} and closes the
stream.
-   */
-  public static Properties loadProperties(final InputStream inputStream) throws IOException
{
-    try {
-      final Properties properties = new Properties();
-      properties.load(inputStream);
-      return properties;
+    /**
+     * Creates and loads {@link Properties} from provided {@link InputStream} and closes
the stream.
+     */
+    public static Properties loadProperties( final InputStream inputStream )
+        throws IOException
+    {
+        try
+        {
+            final Properties properties = new Properties();
+            properties.load( inputStream );
+            return properties;
+        }
+        finally
+        {
+            inputStream.close();
+        }
     }
-    finally {
-      inputStream.close();
+
+    /**
+     * Creates and loads {@link Properties} from provided {@link Resource} if exists, and
closes the resource. If not
+     * exists, returns {@code null}.
+     */
+    public static Properties loadProperties( final Resource resource )
+        throws IOException
+    {
+        final InputStream inputStream = resource.read();
+        if ( inputStream == null )
+        {
+            return null;
+        }
+        return loadProperties( resource.read() );
     }
-  }
-
-  /**
-   * Creates and loads {@link Properties} from provided {@link Resource} if exists, and closes
the resource. If not
-   * exists, returns {@code null}.
-   */
-  public static Properties loadProperties(final Resource resource) throws IOException {
-    final InputStream inputStream = resource.read();
-    if (inputStream == null) {
-      return null;
+
+    /**
+     * Saves {@link Properties} to provided {@link OutputStream} and closes the stream.
+     */
+    public static void storeProperties( final OutputStream outputStream, final Properties
properties )
+        throws IOException
+    {
+        try
+        {
+            properties.store( outputStream, "Maven Indexer Writer" );
+        }
+        finally
+        {
+            outputStream.close();
+        }
     }
-    return loadProperties(resource.read());
-  }
-
-  /**
-   * Saves {@link Properties} to provided {@link OutputStream} and closes the stream.
-   */
-  public static void storeProperties(final OutputStream outputStream, final Properties properties)
throws IOException {
-    try {
-      properties.store(outputStream, "Maven Indexer Writer");
+
+
+    /**
+     * Saves {@link Properties} to provided {@link WritableResource} and closes the resource.
+     */
+    public static void storeProperties( final WritableResource writableResource, final Properties
properties )
+        throws IOException
+    {
+        try
+        {
+            storeProperties( writableResource.write(), properties );
+        }
+        finally
+        {
+            writableResource.close();
+        }
     }
-    finally {
-      outputStream.close();
+
+    /**
+     * Creates a record of type {@link Type#DESCRIPTOR}.
+     */
+    public static Record descriptor( final String repoId )
+    {
+        HashMap<EntryKey, Object> entries = new HashMap<EntryKey, Object>();
+        entries.put( Record.REPOSITORY_ID, repoId );
+        return new Record( Type.DESCRIPTOR, entries );
     }
-  }
 
+    /**
+     * Creates a record of type {@link Type#ALL_GROUPS}.
+     */
+    public static Record allGroups( final Collection<String> allGroups )
+    {
+        HashMap<EntryKey, Object> entries = new HashMap<EntryKey, Object>();
+        entries.put( Record.ALL_GROUPS, allGroups.toArray( new String[allGroups.size()] )
);
+        return new Record( Type.ALL_GROUPS, entries );
+    }
 
-  /**
-   * Saves {@link Properties} to provided {@link WritableResource} and closes the resource.
-   */
-  public static void storeProperties(final WritableResource writableResource, final Properties
properties) throws IOException {
-    try {
-      storeProperties(writableResource.write(), properties);
+    /**
+     * Creates a record of type {@link Type#ROOT_GROUPS}.
+     */
+    public static Record rootGroups( final Collection<String> rootGroups )
+    {
+        HashMap<EntryKey, Object> entries = new HashMap<EntryKey, Object>();
+        entries.put( Record.ROOT_GROUPS, rootGroups.toArray( new String[rootGroups.size()]
) );
+        return new Record( Type.ROOT_GROUPS, entries );
     }
-    finally {
-      writableResource.close();
+
+    /**
+     * Helper to translate the "NA" (not available) input into {@code null} value.
+     */
+    public static String renvl( final String v )
+    {
+        return NOT_AVAILABLE.equals( v ) ? null : v;
     }
-  }
-
-  /**
-   * Creates a record of type {@link Type#DESCRIPTOR}.
-   */
-  public static Record descriptor(final String repoId) {
-    HashMap<EntryKey, Object> entries = new HashMap<EntryKey, Object>();
-    entries.put(Record.REPOSITORY_ID, repoId);
-    return new Record(Type.DESCRIPTOR, entries);
-  }
-
-  /**
-   * Creates a record of type {@link Type#ALL_GROUPS}.
-   */
-  public static Record allGroups(final Collection<String> allGroups) {
-    HashMap<EntryKey, Object> entries = new HashMap<EntryKey, Object>();
-    entries.put(Record.ALL_GROUPS, allGroups.toArray(new String[allGroups.size()]));
-    return new Record(Type.ALL_GROUPS, entries);
-  }
-
-  /**
-   * Creates a record of type {@link Type#ROOT_GROUPS}.
-   */
-  public static Record rootGroups(final Collection<String> rootGroups) {
-    HashMap<EntryKey, Object> entries = new HashMap<EntryKey, Object>();
-    entries.put(Record.ROOT_GROUPS, rootGroups.toArray(new String[rootGroups.size()]));
-    return new Record(Type.ROOT_GROUPS, entries);
-  }
-
-  /**
-   * Helper to translate the "NA" (not available) input into {@code null} value.
-   */
-  public static String renvl(final String v) {
-    return NOT_AVAILABLE.equals(v) ? null : v;
-  }
-
-  /**
-   * Helper to translate {@code null} into "NA" (not available) value.
-   */
-  public static String nvl(final String v) {
-    return v == null ? NOT_AVAILABLE : v;
-  }
-
-  /**
-   * Returns the "root group" of given groupId.
-   */
-  public static String rootGroup(final String groupId) {
-    int n = groupId.indexOf('.');
-    if (n > -1) {
-      return groupId.substring(0, n);
+
+    /**
+     * Helper to translate {@code null} into "NA" (not available) value.
+     */
+    public static String nvl( final String v )
+    {
+        return v == null ? NOT_AVAILABLE : v;
+    }
+
+    /**
+     * Returns the "root group" of given groupId.
+     */
+    public static String rootGroup( final String groupId )
+    {
+        int n = groupId.indexOf( '.' );
+        if ( n > -1 )
+        {
+            return groupId.substring( 0, n );
+        }
+        return groupId;
     }
-    return groupId;
-  }
 }

http://git-wip-us.apache.org/repos/asf/maven-indexer/blob/0bbcad1f/indexer-reader/src/test/java/org/apache/maven/index/reader/ChunkReaderTest.java
----------------------------------------------------------------------
diff --git a/indexer-reader/src/test/java/org/apache/maven/index/reader/ChunkReaderTest.java
b/indexer-reader/src/test/java/org/apache/maven/index/reader/ChunkReaderTest.java
index 7d5cc33..eb6a393 100644
--- a/indexer-reader/src/test/java/org/apache/maven/index/reader/ChunkReaderTest.java
+++ b/indexer-reader/src/test/java/org/apache/maven/index/reader/ChunkReaderTest.java
@@ -19,6 +19,10 @@ package org.apache.maven.index.reader;
  * under the License.
  */
 
+import org.apache.maven.index.reader.Record.Type;
+import org.apache.maven.index.reader.ResourceHandler.Resource;
+import org.junit.Test;
+
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -27,10 +31,6 @@ import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.maven.index.reader.Record.Type;
-import org.apache.maven.index.reader.ResourceHandler.Resource;
-import org.junit.Test;
-
 import static org.hamcrest.CoreMatchers.nullValue;
 import static org.hamcrest.core.IsEqual.equalTo;
 import static org.junit.Assert.assertThat;
@@ -41,55 +41,49 @@ import static org.junit.Assert.assertThat;
 public class ChunkReaderTest
     extends TestSupport
 {
-  @Test
-  public void simple() throws IOException {
-    final ChunkReader chunkReader = new ChunkReader(
-        "full",
-        testResourceHandler("simple").locate("nexus-maven-repository-index.gz").read()
-    );
-    final Map<Type, List<Record>> recordTypes = loadRecordsByType(chunkReader);
-    assertThat(recordTypes.get(Type.DESCRIPTOR).size(), equalTo(1));
-    assertThat(recordTypes.get(Type.ROOT_GROUPS).size(), equalTo(1));
-    assertThat(recordTypes.get(Type.ALL_GROUPS).size(), equalTo(1));
-    assertThat(recordTypes.get(Type.ARTIFACT_ADD).size(), equalTo(2));
-    assertThat(recordTypes.get(Type.ARTIFACT_REMOVE), nullValue());
-  }
-
-  @Test
-  public void roundtrip() throws IOException {
-    final Date published;
-    File tempChunkFile = createTempFile("nexus-maven-repository-index.gz");
+    @Test
+    public void simple()
+        throws IOException
     {
-      final Resource resource = testResourceHandler("simple").locate("nexus-maven-repository-index.gz");
-      final ChunkReader chunkReader = new ChunkReader(
-          "full",
-          resource.read()
-      );
-      final ChunkWriter chunkWriter = new ChunkWriter(
-          chunkReader.getName(),
-          new FileOutputStream(tempChunkFile), 1, new Date()
-      );
-      try {
-        chunkWriter.writeChunk(chunkReader.iterator());
-      }
-      finally {
-        chunkWriter.close();
-        chunkReader.close();
-      }
-      published = chunkWriter.getTimestamp();
+        final ChunkReader chunkReader = new ChunkReader( "full", testResourceHandler( "simple"
) //
+            .locate( "nexus-maven-repository-index.gz" ).read() );
+        final Map<Type, List<Record>> recordTypes = loadRecordsByType( chunkReader
);
+        assertThat( recordTypes.get( Type.DESCRIPTOR ).size(), equalTo( 1 ) );
+        assertThat( recordTypes.get( Type.ROOT_GROUPS ).size(), equalTo( 1 ) );
+        assertThat( recordTypes.get( Type.ALL_GROUPS ).size(), equalTo( 1 ) );
+        assertThat( recordTypes.get( Type.ARTIFACT_ADD ).size(), equalTo( 2 ) );
+        assertThat( recordTypes.get( Type.ARTIFACT_REMOVE ), nullValue() );
     }
 
-    final ChunkReader chunkReader = new ChunkReader(
-        "full",
-        new FileInputStream(tempChunkFile)
-    );
-    assertThat(chunkReader.getVersion(), equalTo(1));
-    assertThat(chunkReader.getTimestamp().getTime(), equalTo(published.getTime()));
-    final Map<Type, List<Record>> recordTypes = loadRecordsByType(chunkReader);
-    assertThat(recordTypes.get(Type.DESCRIPTOR).size(), equalTo(1));
-    assertThat(recordTypes.get(Type.ROOT_GROUPS).size(), equalTo(1));
-    assertThat(recordTypes.get(Type.ALL_GROUPS).size(), equalTo(1));
-    assertThat(recordTypes.get(Type.ARTIFACT_ADD).size(), equalTo(2));
-    assertThat(recordTypes.get(Type.ARTIFACT_REMOVE), nullValue());
-  }
+    @Test
+    public void roundtrip()
+        throws IOException
+    {
+        final Date published;
+        File tempChunkFile = createTempFile( "nexus-maven-repository-index.gz" );
+        {
+            final Resource resource = testResourceHandler( "simple" ) //
+                .locate( "nexus-maven-repository-index.gz" );
+
+            try (ChunkReader chunkReader = new ChunkReader( "full",
+                                                            resource.read() ); ChunkWriter
chunkWriter = new ChunkWriter(
+                chunkReader.getName(), //
+                new FileOutputStream( tempChunkFile ), 1, new Date() ))
+            {
+                chunkWriter.writeChunk( chunkReader.iterator() );
+                published = chunkWriter.getTimestamp();
+            }
+
+        }
+
+        final ChunkReader chunkReader = new ChunkReader( "full", new FileInputStream( tempChunkFile
) );
+        assertThat( chunkReader.getVersion(), equalTo( 1 ) );
+        assertThat( chunkReader.getTimestamp().getTime(), equalTo( published.getTime() )
);
+        final Map<Type, List<Record>> recordTypes = loadRecordsByType( chunkReader
);
+        assertThat( recordTypes.get( Type.DESCRIPTOR ).size(), equalTo( 1 ) );
+        assertThat( recordTypes.get( Type.ROOT_GROUPS ).size(), equalTo( 1 ) );
+        assertThat( recordTypes.get( Type.ALL_GROUPS ).size(), equalTo( 1 ) );
+        assertThat( recordTypes.get( Type.ARTIFACT_ADD ).size(), equalTo( 2 ) );
+        assertThat( recordTypes.get( Type.ARTIFACT_REMOVE ), nullValue() );
+    }
 }


Mime
View raw message