You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ace.apache.org by ja...@apache.org on 2012/04/12 11:44:29 UTC

svn commit: r1325172 - in /ace/trunk/ace-client-repository-helper-base/src: main/java/org/apache/ace/client/repository/helper/base/ test/java/org/apache/ace/client/repository/helper/base/

Author: jawi
Date: Thu Apr 12 09:44:29 2012
New Revision: 1325172

URL: http://svn.apache.org/viewvc?rev=1325172&view=rev
Log:
ACE-257: fixed possible NPE in VelocityArtifactPreprocessor.

Modified:
    ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessor.java
    ace/trunk/ace-client-repository-helper-base/src/test/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessorTest.java

Modified: ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessor.java
URL: http://svn.apache.org/viewvc/ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessor.java?rev=1325172&r1=1325171&r2=1325172&view=diff
==============================================================================
--- ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessor.java (original)
+++ ace/trunk/ace-client-repository-helper-base/src/main/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessor.java Thu Apr 12 09:44:29 2012
@@ -272,11 +272,11 @@ public class VelocityArtifactPreprocesso
             byte[] buf = new byte[BUFFER_SIZE];
             for (int count = in.read(buf); count != -1; count = in.read(buf)) {
                 baos.write(buf, 0, count);
-
-                result = baos.toByteArray();
-
-                m_cachedArtifacts.put(url, new WeakReference<byte[]>(result));
             }
+            
+            result = baos.toByteArray();
+            
+            m_cachedArtifacts.put(url, new WeakReference<byte[]>(result));
         }
         finally {
             silentlyClose(in);

Modified: ace/trunk/ace-client-repository-helper-base/src/test/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessorTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/ace-client-repository-helper-base/src/test/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessorTest.java?rev=1325172&r1=1325171&r2=1325172&view=diff
==============================================================================
--- ace/trunk/ace-client-repository-helper-base/src/test/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessorTest.java (original)
+++ ace/trunk/ace-client-repository-helper-base/src/test/java/org/apache/ace/client/repository/helper/base/VelocityArtifactPreprocessorTest.java Thu Apr 12 09:44:29 2012
@@ -38,7 +38,7 @@ import org.junit.Test;
 public class VelocityArtifactPreprocessorTest {
     
     private static final String TARGET = "target";
-    private static final String VERSION = "1.0.0";
+    private static final String VERSION1 = "1.0.0";
     
     private URL m_obrUrl;
     private PropertyResolver m_resolver;
@@ -55,42 +55,54 @@ public class VelocityArtifactPreprocesso
     }
 
     /**
-     * Test case for {@link VelocityArtifactPreprocessor#preprocess(String, PropertyResolver, String, String, java.net.URL)}
+     * Test case for {@link VelocityArtifactPreprocessor#needsNewVersion(String, PropertyResolver, String, String)}
      */
-    @Test(expected = IOException.class)
-    public void testPreprocessNonExistingTemplateOk() throws Exception {
-        // Should be something that really doesn't exist somehow...
-        String url = "file:///path/to/nowhere-" + System.currentTimeMillis();
+    @Test
+    public void testNeedsNewVersionChangedTemplateOk() throws Exception {
+        final VelocityArtifactPreprocessor vap = new VelocityArtifactPreprocessor();
+        
+        String url = createArtifact("Message: [$context.msg]");
+
+        // "upload" a new version...
+        vap.preprocess(url, m_resolver, TARGET, VERSION1, m_obrUrl);
+        
+        boolean result = vap.needsNewVersion(url, m_resolver, TARGET, VERSION1);
+        assertFalse(result); // no new version is needed...
+        
+        updateArtifact(url, "Another message: [$context.msg2]");
         
-        new VelocityArtifactPreprocessor().preprocess(url, m_resolver, TARGET, VERSION, m_obrUrl);
+        result = vap.needsNewVersion(url, m_resolver, TARGET, VERSION1);
+        assertFalse(result); // no new version is needed; original artifact is cached indefinitely...
     }
 
     /**
-     * Test case for {@link VelocityArtifactPreprocessor#preprocess(String, PropertyResolver, String, String, java.net.URL)}
+     * Test case for {@link VelocityArtifactPreprocessor#needsNewVersion(String, PropertyResolver, String, String)}
      */
     @Test
-    public void testPreprocessExistingRealTemplateOk() throws Exception {
-        String url = createArtifact("Message: [$context.msg]");
+    public void testNeedsNewVersionEmptyTemplateOk() throws Exception {
+        final VelocityArtifactPreprocessor vap = new VelocityArtifactPreprocessor();
         
-        String newUrl = new VelocityArtifactPreprocessor().preprocess(url, m_resolver, TARGET, VERSION, m_obrUrl);
-        assertNotNull(newUrl);
-        // Verify that it is actually uploaded...
-        assertFalse(newUrl.equals(url));
-        // Verify that it is actually uploaded to our (fake) OBR...
-        assertTrue(newUrl.startsWith(m_obrUrl.toExternalForm()));
+        String url = createArtifact("");
+
+        // "upload" a new version...
+        vap.preprocess(url, m_resolver, TARGET, VERSION1, m_obrUrl);
+
+        boolean result = vap.needsNewVersion(url, m_resolver, TARGET, VERSION1);
+        assertFalse(result); // no new version is needed...
     }
 
     /**
-     * Test case for {@link VelocityArtifactPreprocessor#preprocess(String, PropertyResolver, String, String, java.net.URL)}
+     * Test case for {@link VelocityArtifactPreprocessor#needsNewVersion(String, PropertyResolver, String, String)}
      */
     @Test
-    public void testPreprocessExistingNoTemplateOk() throws Exception {
-        String url = createArtifact("Message: [context.msg]");
+    public void testNeedsNewVersionNonExistingTemplateOk() throws Exception {
+        final VelocityArtifactPreprocessor vap = new VelocityArtifactPreprocessor();
         
-        String newUrl = new VelocityArtifactPreprocessor().preprocess(url, m_resolver, TARGET, VERSION, m_obrUrl);
-        assertNotNull(newUrl);
-        // Verify that it is *not* uploaded...
-        assertEquals(url, newUrl);
+        // Should be something that really doesn't exist somehow...
+        String url = "file:///path/to/nowhere-" + System.currentTimeMillis();
+
+        boolean result = vap.needsNewVersion(url, m_resolver, TARGET, VERSION1);
+        assertTrue(result); // always true for non-existing templates...
     }
 
     /**
@@ -102,49 +114,53 @@ public class VelocityArtifactPreprocesso
         
         String url = createArtifact("Message: [$context.msg]");
         
-        boolean result = vap.needsNewVersion(url, m_resolver, TARGET, VERSION);
+        boolean result = vap.needsNewVersion(url, m_resolver, TARGET, VERSION1);
         assertTrue(result); // nothing uploaded yet; new version is needed...
-        
+
         // "upload" a new version...
-        vap.preprocess(url, m_resolver, TARGET, VERSION, m_obrUrl);
-        
-        result = vap.needsNewVersion(url, m_resolver, TARGET, VERSION);
+        vap.preprocess(url, m_resolver, TARGET, VERSION1, m_obrUrl);
+
+        result = vap.needsNewVersion(url, m_resolver, TARGET, VERSION1);
         assertFalse(result); // no new version is needed...
     }
 
     /**
-     * Test case for {@link VelocityArtifactPreprocessor#needsNewVersion(String, PropertyResolver, String, String)}
+     * Test case for {@link VelocityArtifactPreprocessor#preprocess(String, PropertyResolver, String, String, java.net.URL)}
      */
     @Test
-    public void testNeedsNewVersionChangedTemplateOk() throws Exception {
-        final VelocityArtifactPreprocessor vap = new VelocityArtifactPreprocessor();
-        
-        String url = createArtifact("Message: [$context.msg]");
-
-        // "upload" a new version...
-        vap.preprocess(url, m_resolver, TARGET, VERSION, m_obrUrl);
-        
-        boolean result = vap.needsNewVersion(url, m_resolver, TARGET, VERSION);
-        assertFalse(result); // no new version is needed...
-        
-        updateArtifact(url, "Another message: [$context.msg2]");
+    public void testPreprocessExistingNoTemplateOk() throws Exception {
+        String url = createArtifact("Message: [context.msg]");
         
-        result = vap.needsNewVersion(url, m_resolver, TARGET, VERSION);
-        assertFalse(result); // no new version is needed; original artifact is cached indefinitely...
+        String newUrl = new VelocityArtifactPreprocessor().preprocess(url, m_resolver, TARGET, VERSION1, m_obrUrl);
+        assertNotNull(newUrl);
+        // Verify that it is *not* uploaded...
+        assertEquals(url, newUrl);
     }
 
     /**
-     * Test case for {@link VelocityArtifactPreprocessor#needsNewVersion(String, PropertyResolver, String, String)}
+     * Test case for {@link VelocityArtifactPreprocessor#preprocess(String, PropertyResolver, String, String, java.net.URL)}
      */
     @Test
-    public void testNeedsNewVersionNonExistingTemplateOk() throws Exception {
-        final VelocityArtifactPreprocessor vap = new VelocityArtifactPreprocessor();
+    public void testPreprocessExistingRealTemplateOk() throws Exception {
+        String url = createArtifact("Message: [$context.msg]");
         
+        String newUrl = new VelocityArtifactPreprocessor().preprocess(url, m_resolver, TARGET, VERSION1, m_obrUrl);
+        assertNotNull(newUrl);
+        // Verify that it is actually uploaded...
+        assertFalse(newUrl.equals(url));
+        // Verify that it is actually uploaded to our (fake) OBR...
+        assertTrue(newUrl.startsWith(m_obrUrl.toExternalForm()));
+    }
+
+    /**
+     * Test case for {@link VelocityArtifactPreprocessor#preprocess(String, PropertyResolver, String, String, java.net.URL)}
+     */
+    @Test(expected = IOException.class)
+    public void testPreprocessNonExistingTemplateOk() throws Exception {
         // Should be something that really doesn't exist somehow...
         String url = "file:///path/to/nowhere-" + System.currentTimeMillis();
-
-        boolean result = vap.needsNewVersion(url, m_resolver, TARGET, VERSION);
-        assertTrue(result); // always true for non-existing templates...
+        
+        new VelocityArtifactPreprocessor().preprocess(url, m_resolver, TARGET, VERSION1, m_obrUrl);
     }
 
     private String createArtifact(String string) throws IOException {
@@ -154,6 +170,7 @@ public class VelocityArtifactPreprocesso
         
         FileWriter writer = new FileWriter(tmpFile);
         writer.write(string);
+        writer.flush();
         writer.close();
 
         return tmpFile.toURI().toURL().toExternalForm();
@@ -164,6 +181,7 @@ public class VelocityArtifactPreprocesso
         
         FileWriter writer = new FileWriter(uri.getFile());
         writer.write(string);
+        writer.flush();
         writer.close();
 
         return url;