Android Open Source - qiniu-android Test File Recorder






From Project

Back to project page qiniu-android.

License

The source code is released under:

MIT License

If you think the Android project qiniu-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.qiniu.android;
/* w  w w .  j ava2s . c  o m*/
import android.test.InstrumentationTestCase;
import android.util.Log;

import com.qiniu.android.common.Config;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCancellationSignal;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UpProgressHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;
import com.qiniu.android.storage.persistent.FileRecorder;

import junit.framework.Assert;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class TestFileRecorder extends InstrumentationTestCase {
    final CountDownLatch signal = new CountDownLatch(1);
    final CountDownLatch signal2 = new CountDownLatch(1);
    boolean cancelled;
    boolean failed;
    private UploadManager uploadManager;
    private String key;
    private volatile ResponseInfo info;
    private JSONObject resp;
    private volatile UploadOptions options;

    @Override
    protected void setUp() throws Exception {
        File f = File.createTempFile("qiniutest", "b");
        String folder = f.getParent();
        FileRecorder fr = new FileRecorder(folder);
        uploadManager = new UploadManager(fr);
    }

    private void template(final int size, final double pos) throws Throwable {
        final File tempFile = TempFile.createFile(size);
        final String expectKey = "rc=" + size + "k";
        cancelled = false;
        failed = false;
        Map<String, String> params = new HashMap<String, String>();
        params.put("x:a", "test");
        params.put("x:b", "test2");
        options = new UploadOptions(params, null, false, new UpProgressHandler() {
            @Override
            public void progress(String key, double percent) {
                if (percent >= pos) {
                    cancelled = true;
                }
                Log.i("qiniutest", "progress " + percent);
            }
        }, new UpCancellationSignal() {
            @Override
            public boolean isCancelled() {
                return cancelled;
            }
        });
        runTestOnUiThread(new Runnable() { // THIS IS THE KEY TO SUCCESS
            public void run() {
                uploadManager.put(tempFile, expectKey, TestConfig.token, new UpCompletionHandler() {
                    public void complete(String k, ResponseInfo rinfo, JSONObject response) {
                        Log.i("qiniutest", k + rinfo);
                        key = k;
                        info = rinfo;
                        resp = response;
                        signal.countDown();
                    }
                }, options);
            }
        });

        try {
            signal.await(1200, TimeUnit.SECONDS); // wait for callback
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Assert.assertEquals(expectKey, key);
        Assert.assertTrue(info.isCancelled());
        Assert.assertNull(resp);

        cancelled = false;
        options = new UploadOptions(null, null, false, new UpProgressHandler() {
            @Override
            public void progress(String key, double percent) {
                if (percent < pos - Config.CHUNK_SIZE / (size * 1024.0)) {
                    failed = true;
                }
                Log.i("qiniutest", "continue progress " + percent);
            }
        }, null);

        runTestOnUiThread(new Runnable() { // THIS IS THE KEY TO SUCCESS
            public void run() {
                uploadManager.put(tempFile, expectKey, TestConfig.token, new UpCompletionHandler() {
                    public void complete(String k, ResponseInfo rinfo, JSONObject response) {
                        Log.i("qiniutest", k + rinfo);
                        key = k;
                        info = rinfo;
                        resp = response;
                        signal2.countDown();
                    }
                }, options);
            }
        });

        try {
            signal2.await(1200, TimeUnit.SECONDS); // wait for callback
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Assert.assertEquals(expectKey, key);
        Assert.assertTrue(info.isOK());
        Assert.assertTrue(!failed);
        Assert.assertNotNull(resp);

        TempFile.remove(tempFile);
    }

    public void test600k() throws Throwable {
        template(600, 0.7);
    }

    public void test700k() throws Throwable {
        template(700, 0.1);
    }

    public void test1M() throws Throwable {
        template(1024, 0.51);
    }

    public void test4M() throws Throwable {
        template(4 * 1024, 0.9);
    }

    public void test8M1K() throws Throwable {
        template(8 * 1024 + 1, 0.8);
    }
}




Java Source Code List

com.qiniu.android.Base64Test.java
com.qiniu.android.CrcTest.java
com.qiniu.android.EtagTest.java
com.qiniu.android.FormUploadTest.java
com.qiniu.android.HttpTest.java
com.qiniu.android.ResumeUploadTest.java
com.qiniu.android.TempFile.java
com.qiniu.android.TestConfig.java
com.qiniu.android.TestFileRecorder.java
com.qiniu.android.common.Config.java
com.qiniu.android.http.ByteArrayEntity.java
com.qiniu.android.http.CompletionHandler.java
com.qiniu.android.http.HttpManager.java
com.qiniu.android.http.PostArgs.java
com.qiniu.android.http.ProgressHandler.java
com.qiniu.android.http.ResponseHandler.java
com.qiniu.android.http.ResponseInfo.java
com.qiniu.android.storage.FormUploader.java
com.qiniu.android.storage.KeyGenerator.java
com.qiniu.android.storage.Recorder.java
com.qiniu.android.storage.ResumeUploader.java
com.qiniu.android.storage.UpCancellationSignal.java
com.qiniu.android.storage.UpCompletionHandler.java
com.qiniu.android.storage.UpProgressHandler.java
com.qiniu.android.storage.UploadManager.java
com.qiniu.android.storage.UploadOptions.java
com.qiniu.android.storage.persistent.FileRecorder.java
com.qiniu.android.utils.AsyncRun.java
com.qiniu.android.utils.Crc32.java
com.qiniu.android.utils.Etag.java
com.qiniu.android.utils.StringUtils.java
com.qiniu.android.utils.UriToFile.java
com.qiniu.android.utils.UrlSafeBase64.java