Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 UrsaJ Ltd (http://ursaj.com) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.ursaj.hfs.tools;
 
 import java.io.File;
 
 
 import static com.ursaj.hfs.HfsTestUtils.assertThrows;
 import static com.ursaj.hfs.HfsTestUtils.setField;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;

HfsUploadLibrary test cases.
 
 public class HfsUploadLibraryTest {
    
Class logger.
 
     private static final Logger log = LoggerFactory.getLogger(HfsUploadLibraryTest.class);

    
Test input validations.
 
     @Test
     public void testValidations() {
         for (int i = 0; i < 100; i++)
             assertThrows(AssertionError.class"constructor should never be called",
                 HfsUploadLibrary.class"newInstance");
     }

    
Test frame rendering.

Throws:
java.io.IOException In case of any IO exception.
 
     @Test
     public void testFrameRendering() throws IOException {
         for (int i = 0; i < 100; i++) {
             String js = "\"Some string #" + i + "\"";
 
             assertTrue(HfsUploadLibrary.renderUploadFrame(ijs).contains(
                 "hfs.Uploader.complete(" + i + ", " + js + ", window);"));
         }
     }

    
Test JS library loading.

Throws:
java.lang.Exception In case of any exception.
 
     @Test
     public void testJsLibLoading() throws Exception {
         Object lib = HfsUploadLibrary.getJsLib();
 
         assertNotNull(lib);
 
         for (int i = 0; i < 100; i++)
             assertTrue(lib == HfsUploadLibrary.getJsLib(), "Expects returned the same cached value.");
 
         // Reset cached JS library content.
         HfsTestUtils.setField(HfsUploadLibrary.class"cached"null);
 
         assertNull(HfsTestUtils.getField(HfsUploadLibrary.class"cached"String.class));
 
         // Lock intrinsic monitor for a while.
         final CountDownLatch locked = lockSynchronized(HfsUploadLibrary.class, 800);
 
         // Tries to get not-initialized JS library content in multi-threaded environment.
         HfsTestUtils.multithreaded(20, new HfsRunnable() {
            
Resolved library content.
 
             private final AtomicReference<ObjectlibContent = new AtomicReference<>();
 
             @Override
             public void run() throws IOException {
                 assertEquals(locked.getCount(), 1);
 
                 String jsLib = HfsUploadLibrary.getJsLib();
 
                 // Validate ALL concurrent results are resolved into the same object.
                assertTrue(.compareAndSet(nulljsLib) || .get() == jsLib);
            }
        });
    }

    
Test JS library missed.

Throws:
java.lang.Exception In case of any exception.
    @Test
    public void testJsLibMissed() throws Exception {
        File tmp = File.createTempFile("upload-lib""tmp");
        FileUtils.forceDelete(tmp);
        FileUtils.moveFile(libFiletmp);
        try {
            setField(HfsUploadLibrary.class"cached"null);
            assertThrows(IOException.class"Resource not found"HfsUploadLibrary.class"getJsLib");
        }
        finally {
            FileUtils.moveFile(tmplibFile);
        }
        assertTrue(libFile.exists());
        assertNotNull(HfsUploadLibrary.getJsLib());
    }

    
Lock object with intrinsic/monitor lock.

Parameters:
obj Object to catch intrinsic lock for.
timeout Timeout to release hold lock.
Returns:
Latch to release intrinsic lock.
    private CountDownLatch lockSynchronized(final Object objfinal long timeout) {
        final CountDownLatch started = new CountDownLatch(1);
        final CountDownLatch locked = new CountDownLatch(1);
        Thread t = new Thread() {
            @Override
            public void run() {
                synchronized (obj) {
                    started.countDown();
                    try {
                        locked.await(timeout.);
                    }
                    catch (InterruptedException e) {
                        .trace("Failed to wait for object intrinsic monitor unlocked."e);
                        Thread.currentThread().interrupt();
                    }
                    finally {
                        locked.countDown();
                    }
                }
            }
        };
        t.setDaemon(true);
        t.setName("Intrinsic monitor locker: " + obj);
        t.start();
        try {
            started.await();
        }
        catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            t.interrupt();
            throw new IllegalStateException("Failed to wait for object intrinsic monitor locked."e);
        }
        return locked;
    }
New to GrepCode? Check out our FAQ X