You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2019/10/11 16:20:46 UTC

[jspwiki] branch master updated: replace Thread.sleep calls with Awaility calls in order to ensure order of files being stored - hopefully fixes CI build; no version bump

This is an automated email from the ASF dual-hosted git repository.

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git


The following commit(s) were added to refs/heads/master by this push:
     new e3714c0  replace Thread.sleep calls with Awaility calls in order to ensure order of files being stored - hopefully fixes CI build; no version bump
e3714c0 is described below

commit e3714c0f37e3ce7da33ff69860b22833a341092d
Author: juanpablo <ju...@apache.org>
AuthorDate: Fri Oct 11 18:20:25 2019 +0200

    replace Thread.sleep calls with Awaility calls in order to ensure order of files being stored - hopefully fixes CI build; no version bump
---
 .../providers/BasicAttachmentProviderTest.java     | 310 ++++++++-------------
 1 file changed, 113 insertions(+), 197 deletions(-)

diff --git a/jspwiki-main/src/test/java/org/apache/wiki/providers/BasicAttachmentProviderTest.java b/jspwiki-main/src/test/java/org/apache/wiki/providers/BasicAttachmentProviderTest.java
index c6b5675..3810554 100644
--- a/jspwiki-main/src/test/java/org/apache/wiki/providers/BasicAttachmentProviderTest.java
+++ b/jspwiki-main/src/test/java/org/apache/wiki/providers/BasicAttachmentProviderTest.java
@@ -22,6 +22,7 @@ package org.apache.wiki.providers;
 import org.apache.wiki.TestEngine;
 import org.apache.wiki.attachment.Attachment;
 import org.apache.wiki.util.FileUtil;
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
@@ -35,29 +36,23 @@ import java.util.Date;
 import java.util.List;
 import java.util.Properties;
 
-public class BasicAttachmentProviderTest
-{
+public class BasicAttachmentProviderTest {
+
     public static final String NAME1 = "TestPage";
     public static final String NAME2 = "TestPageToo";
 
     Properties props = TestEngine.getTestProperties();
-
     TestEngine m_engine;
-
     BasicAttachmentProvider m_provider;
 
-    /**
-     *  This is the sound of my head hitting the keyboard.
-     */
-    private static final String c_fileContents = "gy th tgyhgthygyth tgyfgftrfgvtgfgtr";
+    /** This is the sound of my head hitting the keyboard. */
+    private static final String FILE_CONTENTS = "gy th tgyhgthygyth tgyfgftrfgvtgfgtr";
 
     @BeforeEach
-    public void setUp()
-        throws Exception
-    {
-        m_engine  = new TestEngine(props);
+    public void setUp() throws Exception {
+        m_engine  = new TestEngine( props );
 
-        TestEngine.deleteAll( new File(m_engine.getRequiredProperty( props, BasicAttachmentProvider.PROP_STORAGEDIR )) );
+        TestEngine.deleteAll( new File( m_engine.getRequiredProperty( props, BasicAttachmentProvider.PROP_STORAGEDIR ) ) );
 
         m_provider = new BasicAttachmentProvider();
         m_provider.initialize( m_engine, props );
@@ -66,100 +61,73 @@ public class BasicAttachmentProviderTest
         m_engine.saveText( NAME2, "Foobar2" );
     }
 
-    private File makeAttachmentFile()
-        throws Exception
-    {
-        File tmpFile = File.createTempFile("test","txt");
-        tmpFile.deleteOnExit();
-
-        FileWriter out = new FileWriter( tmpFile );
-
-        FileUtil.copyContents( new StringReader( c_fileContents ), out );
-
-        out.close();
-
-        return tmpFile;
-    }
-
-    private File makeExtraFile( File directory, String name )
-        throws Exception
-    {
-        File tmpFile = new File( directory, name );
-        FileWriter out = new FileWriter( tmpFile );
-
-        FileUtil.copyContents( new StringReader( c_fileContents ), out );
-
-        out.close();
-
-        return tmpFile;
-    }
-
-
     @AfterEach
-    public void tearDown()
-    {
+    public void tearDown() {
         m_engine.deleteTestPage( NAME1 );
         m_engine.deleteTestPage( NAME2 );
 
-        String tmpfiles = props.getProperty( BasicAttachmentProvider.PROP_STORAGEDIR );
-
-        File f = new File( tmpfiles, NAME1+BasicAttachmentProvider.DIR_EXTENSION );
-
+        final String tmpfiles = props.getProperty( BasicAttachmentProvider.PROP_STORAGEDIR );
+        File f = new File( tmpfiles, NAME1 + BasicAttachmentProvider.DIR_EXTENSION );
         TestEngine.deleteAll( f );
 
-        f = new File( tmpfiles, NAME2+BasicAttachmentProvider.DIR_EXTENSION );
-
+        f = new File( tmpfiles, NAME2 + BasicAttachmentProvider.DIR_EXTENSION );
         TestEngine.deleteAll( f );
 
         TestEngine.emptyWorkDir();
     }
 
-    @Test
-    public void testExtension()
-    {
-        String s = "test.png";
+    private File makeAttachmentFile() throws Exception {
+        final File tmpFile = File.createTempFile("test","txt");
+        return copyContents( tmpFile );
+    }
 
-        Assertions.assertEquals( BasicAttachmentProvider.getFileExtension(s), "png" );
+    private File makeExtraFile( final File directory, final String name ) throws Exception {
+        final File tmpFile = new File( directory, name );
+        return copyContents( tmpFile );
+    }
+
+    private File copyContents( final File to ) throws Exception {
+        to.deleteOnExit();
+        try( final FileWriter out = new FileWriter( to ) ) {
+            FileUtil.copyContents( new StringReader( FILE_CONTENTS ), out );
+        }
+
+        return to;
     }
 
     @Test
-    public void testExtension2()
-    {
-        String s = ".foo";
+    public void testExtension() {
+        final String s = "test.png";
+        Assertions.assertEquals( "png", BasicAttachmentProvider.getFileExtension(s) );
+    }
 
+    @Test
+    public void testExtension2() {
+        final String s = ".foo";
         Assertions.assertEquals( "foo", BasicAttachmentProvider.getFileExtension(s) );
     }
 
     @Test
-    public void testExtension3()
-    {
-        String s = "test.png.3";
-
+    public void testExtension3() {
+        final String s = "test.png.3";
         Assertions.assertEquals( "3", BasicAttachmentProvider.getFileExtension(s) );
     }
 
     @Test
-    public void testExtension4()
-    {
-        String s = "testpng";
-
+    public void testExtension4() {
+        final String s = "testpng";
         Assertions.assertEquals( "bin", BasicAttachmentProvider.getFileExtension(s) );
     }
 
-
     @Test
-    public void testExtension5()
-    {
-        String s = "test.";
-
+    public void testExtension5() {
+        final String s = "test.";
         Assertions.assertEquals( "bin", BasicAttachmentProvider.getFileExtension(s) );
     }
 
     @Test
-    public void testExtension6()
-    {
-        String s = "test.a";
-
+    public void testExtension6() {
+        final String s = "test.a";
         Assertions.assertEquals( "a", BasicAttachmentProvider.getFileExtension(s) );
     }
 
@@ -167,44 +135,34 @@ public class BasicAttachmentProviderTest
      *  Can we save attachments with names in UTF-8 range?
      */
     @Test
-    public void testPutAttachmentUTF8()
-        throws Exception
-    {
-        File in = makeAttachmentFile();
-
-        Attachment att = new Attachment( m_engine, NAME1, "\u3072\u3048\u308b\u00e5\u00e4\u00f6test.f\u00fc\u00fc" );
+    public void testPutAttachmentUTF8() throws Exception {
+        final File in = makeAttachmentFile();
+        final Attachment att = new Attachment( m_engine, NAME1, "\u3072\u3048\u308b\u00e5\u00e4\u00f6test.f\u00fc\u00fc" );
 
         m_provider.putAttachmentData( att, new FileInputStream(in) );
-
-        List< Attachment > res = m_provider.listAllChanged( new Date(0L) );
-
-        Attachment a0 = res.get(0);
+        final List< Attachment > res = m_provider.listAllChanged( new Date(0L) );
+        final Attachment a0 = res.get(0);
 
         Assertions.assertEquals( att.getName(), a0.getName(), "name" );
     }
 
     @Test
-    public void testListAll()
-        throws Exception
-    {
-        File in = makeAttachmentFile();
-
-        Attachment att = new Attachment( m_engine, NAME1, "test1.txt" );
-
+    public void testListAll() throws Exception {
+        final File in = makeAttachmentFile();
+        final Attachment att = new Attachment( m_engine, NAME1, "test1.txt" );
         m_provider.putAttachmentData( att, new FileInputStream(in) );
 
-        Thread.sleep( 200L ); // So that we get a bit of granularity.
-
-        Attachment att2 = new Attachment( m_engine, NAME2, "test2.txt" );
+        Awaitility.await( "testListAll" ).until( () -> m_provider.listAllChanged( new Date( 0L ) ).size() > 0 );
 
+        final Attachment att2 = new Attachment( m_engine, NAME2, "test2.txt" );
         m_provider.putAttachmentData( att2, new FileInputStream(in) );
 
-        List< Attachment > res = m_provider.listAllChanged( new Date(0L) );
+        final List< Attachment > res = m_provider.listAllChanged( new Date(0L) );
 
         Assertions.assertEquals( 2, res.size(), "list size" );
 
-        Attachment a2 = res.get(0);  // Most recently changed
-        Attachment a1 = res.get(1);  // Least recently changed
+        final Attachment a2 = res.get(0);  // Most recently changed
+        final Attachment a1 = res.get(1);  // Least recently changed
 
         Assertions.assertEquals( att.getName(), a1.getName(), "a1 name" );
         Assertions.assertEquals( att2.getName(), a2.getName(), "a2 name" );
@@ -215,153 +173,111 @@ public class BasicAttachmentProviderTest
      *  Check that the system does not Assertions.fail if there are extra files in the directory.
      */
     @Test
-    public void testListAllExtrafile()
-        throws Exception
-    {
-        File in = makeAttachmentFile();
-
-        File sDir = new File(m_engine.getWikiProperties().getProperty( BasicAttachmentProvider.PROP_STORAGEDIR ));
-        File extrafile = makeExtraFile( sDir, "foobar.blob" );
-
-        try
-        {
-            Attachment att = new Attachment( m_engine, NAME1, "test1.txt" );
+    public void testListAllExtrafile() throws Exception {
+        final File in = makeAttachmentFile();
+        final File sDir = new File(m_engine.getWikiProperties().getProperty( BasicAttachmentProvider.PROP_STORAGEDIR ) );
+        makeExtraFile( sDir, "foobar.blob" );
 
-            m_provider.putAttachmentData( att, new FileInputStream(in) );
-
-            Thread.sleep( 200L ); // So that we get a bit of granularity.
+        final Attachment att = new Attachment( m_engine, NAME1, "test1.txt" );
+        m_provider.putAttachmentData( att, new FileInputStream(in) );
 
-            Attachment att2 = new Attachment( m_engine, NAME2, "test2.txt" );
+        Awaitility.await( "testListAllExtrafile" ).until( () -> m_provider.listAllChanged( new Date( 0L ) ).size() > 0 );
 
-            m_provider.putAttachmentData( att2, new FileInputStream(in) );
+        final Attachment att2 = new Attachment( m_engine, NAME2, "test2.txt" );
+        m_provider.putAttachmentData( att2, new FileInputStream(in) );
 
-            List< Attachment > res = m_provider.listAllChanged( new Date(0L) );
+        final List< Attachment > res = m_provider.listAllChanged( new Date(0L) );
 
-            Assertions.assertEquals( 2, res.size(), "list size" );
+        Assertions.assertEquals( 2, res.size(), "list size" );
 
-            Attachment a2 = res.get(0);  // Most recently changed
-            Attachment a1 = res.get(1);  // Least recently changed
+        final Attachment a2 = res.get(0);  // Most recently changed
+        final Attachment a1 = res.get(1);  // Least recently changed
 
-            Assertions.assertEquals( att.getName(), a1.getName(), "a1 name" );
-            Assertions.assertEquals( att2.getName(), a2.getName(), "a2 name" );
-        }
-        finally
-        {
-            extrafile.delete();
-        }
+        Assertions.assertEquals( att.getName(), a1.getName(), "a1 name" );
+        Assertions.assertEquals( att2.getName(), a2.getName(), "a2 name" );
     }
 
     /**
-     *  Check that the system does not Assertions.fail if there are extra files in the
-     *  attachment directory.
+     *  Check that the system does not Assertions.fail if there are extra files in the attachment directory.
      */
     @Test
-    public void testListAllExtrafileInAttachmentDir()
-        throws Exception
-    {
-        File in = makeAttachmentFile();
-
-        File sDir = new File(m_engine.getWikiProperties().getProperty( BasicAttachmentProvider.PROP_STORAGEDIR ));
-        File attDir = new File( sDir, NAME1+"-att" );
-
-
-        Attachment att = new Attachment( m_engine, NAME1, "test1.txt" );
+    public void testListAllExtrafileInAttachmentDir() throws Exception {
+        final File in = makeAttachmentFile();
+        final File sDir = new File(m_engine.getWikiProperties().getProperty( BasicAttachmentProvider.PROP_STORAGEDIR ));
+        final File attDir = new File( sDir, NAME1+"-att" );
 
+        final Attachment att = new Attachment( m_engine, NAME1, "test1.txt" );
         m_provider.putAttachmentData( att, new FileInputStream(in) );
+        makeExtraFile( attDir, "ping.pong" );
 
-        File extrafile = makeExtraFile( attDir, "ping.pong" );
+        Awaitility.await( "testListAllExtrafileInAttachmentDir" )
+                  .until( () -> m_provider.listAllChanged( new Date( 0L ) ).size() > 0 );
 
-        try
-        {
-            Thread.sleep( 200L ); // So that we get a bit of granularity.
+        final Attachment att2 = new Attachment( m_engine, NAME2, "test2.txt" );
 
-            Attachment att2 = new Attachment( m_engine, NAME2, "test2.txt" );
-
-            m_provider.putAttachmentData( att2, new FileInputStream(in) );
+        m_provider.putAttachmentData( att2, new FileInputStream(in) );
 
-            List< Attachment > res = m_provider.listAllChanged( new Date(0L) );
+        final List< Attachment > res = m_provider.listAllChanged( new Date(0L) );
 
-            Assertions.assertEquals( 2, res.size(), "list size" );
+        Assertions.assertEquals( 2, res.size(), "list size" );
 
-            Attachment a2 = res.get(0);  // Most recently changed
-            Attachment a1 = res.get(1);  // Least recently changed
+        final Attachment a2 = res.get(0);  // Most recently changed
+        final Attachment a1 = res.get(1);  // Least recently changed
 
-            Assertions.assertEquals( att.getName(), a1.getName(), "a1 name" );
-            Assertions.assertEquals( att2.getName(), a2.getName(), "a2 name" );
-        }
-        finally
-        {
-            extrafile.delete();
-        }
+        Assertions.assertEquals( att.getName(), a1.getName(), "a1 name" );
+        Assertions.assertEquals( att2.getName(), a2.getName(), "a2 name" );
     }
 
     /**
-     *  Check that the system does not Assertions.fail if there are extra dirs in the
-     *  attachment directory.
+     *  Check that the system does not Assertions.fail if there are extra dirs in the attachment directory.
      */
     @Test
-    public void testListAllExtradirInAttachmentDir()
-        throws Exception
-    {
-        File in = makeAttachmentFile();
-
-        File sDir = new File(m_engine.getWikiProperties().getProperty( BasicAttachmentProvider.PROP_STORAGEDIR ));
-        File attDir = new File( sDir, NAME1+"-att" );
-
-        Attachment att = new Attachment( m_engine, NAME1, "test1.txt" );
-
+    public void testListAllExtradirInAttachmentDir() throws Exception {
+        final File in = makeAttachmentFile();
+        final File sDir = new File(m_engine.getWikiProperties().getProperty( BasicAttachmentProvider.PROP_STORAGEDIR ));
+        final File attDir = new File( sDir, NAME1+"-att" );
+        final Attachment att = new Attachment( m_engine, NAME1, "test1.txt" );
         m_provider.putAttachmentData( att, new FileInputStream(in) );
 
         // This is our extraneous directory.
-        File extrafile = new File( attDir, "ping.pong" );
+        final File extrafile = new File( attDir, "ping.pong" );
         extrafile.mkdir();
 
-        try
-        {
-            Thread.sleep( 200L ); // So that we get a bit of granularity.
+        Awaitility.await( "testListAllExtradirInAttachmentDir" )
+                  .until( () -> m_provider.listAllChanged( new Date( 0L ) ).size() > 0 );
 
-            Attachment att2 = new Attachment( m_engine, NAME2, "test2.txt" );
+        final Attachment att2 = new Attachment( m_engine, NAME2, "test2.txt" );
 
-            m_provider.putAttachmentData( att2, new FileInputStream(in) );
+        m_provider.putAttachmentData( att2, new FileInputStream(in) );
 
-            List< Attachment > res = m_provider.listAllChanged( new Date(0L) );
+        final List< Attachment > res = m_provider.listAllChanged( new Date(0L) );
 
-            Assertions.assertEquals( 2, res.size(), "list size" );
+        Assertions.assertEquals( 2, res.size(), "list size" );
 
-            Attachment a2 = res.get(0);  // Most recently changed
-            Attachment a1 = res.get(1);  // Least recently changed
+        final Attachment a2 = res.get(0);  // Most recently changed
+        final Attachment a1 = res.get(1);  // Least recently changed
 
-            Assertions.assertEquals( att.getName(), a1.getName(), "a1 name" );
-            Assertions.assertEquals( att2.getName(), a2.getName(), "a2 name" );
-        }
-        finally
-        {
-            extrafile.delete();
-        }
+        Assertions.assertEquals( att.getName(), a1.getName(), "a1 name" );
+        Assertions.assertEquals( att2.getName(), a2.getName(), "a2 name" );
     }
 
     @Test
-    public void testListAllNoExtension()
-        throws Exception
-    {
-        File in = makeAttachmentFile();
-
-        Attachment att = new Attachment( m_engine, NAME1, "test1." );
-
+    public void testListAllNoExtension() throws Exception {
+        final File in = makeAttachmentFile();
+        final Attachment att = new Attachment( m_engine, NAME1, "test1." );
         m_provider.putAttachmentData( att, new FileInputStream(in) );
 
-        Thread.sleep( 200L ); // So that we get a bit of granularity.
-
-        Attachment att2 = new Attachment( m_engine, NAME2, "test2." );
+        Awaitility.await( "testListAllNoExtension" ).until( () -> m_provider.listAllChanged( new Date( 0L ) ).size() > 0 );
 
+        final Attachment att2 = new Attachment( m_engine, NAME2, "test2." );
         m_provider.putAttachmentData( att2, new FileInputStream(in) );
 
-        List< Attachment > res = m_provider.listAllChanged( new Date(0L) );
+        final List< Attachment > res = m_provider.listAllChanged( new Date( 0L ) );
 
         Assertions.assertEquals( 2, res.size(), "list size" );
 
-        Attachment a2 = res.get(0);  // Most recently changed
-        Attachment a1 = res.get(1);  // Least recently changed
+        final Attachment a2 = res.get(0);  // Most recently changed
+        final Attachment a1 = res.get(1);  // Least recently changed
 
         Assertions.assertEquals( att.getName(), a1.getName(), "a1 name" );
         Assertions.assertEquals( att2.getName(), a2.getName(), "a2 name" );