All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.test.java.com.mgnt.utils.StamTest Maven / Gradle / Ivy

Go to download

Set of various Utils: stacktrace noise filter, String to/from unicode sequence converter, simple Http client JSON parser/serializer, Silent String parsing to Integer and other numeric types, Parsing String to time intervals with support for time unit suffixes, JSON parser that provides serialization/deserialization of classes to JSON, Version comparator and Version ranges operations, Self-throttling binary reader from Http request, File reader, A utility that automatically initiates a Factory with instances of all classes that implement user provided interface. An infrastructure for writing Scheduled Task classes where time interval for task execution is provided in humanly readable format (such as "9h" for 9 hours)

There is a newer version: 1.7.0.1
Show newest version
package com.mgnt.utils;

import com.mgnt.lifecycle.management.httpclient.HttpClient;
import com.mgnt.utils.entities.TimeInterval;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.List;

/**
 * Created by michaelg on 19/01/2016.
 */
public class StamTest {
    public static void main(String[] args) {
//        testParsing();
//        testEncoderDecoder();
//        testSystemPropertyInitializer();
//        testParsingStringToTimeInterval();
//        testFormatStringToPreserveIndentaitonForHtml();
        testHttpClient();
    }

    private static void testParsing() {
        StringBuilder num = new StringBuilder("-92");
        System.out.println(TextUtils.parseStringToInt(num, 10, null, null));
    }

    public static void testEncoderDecoder() {
        String helloEncoded = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence("\\u");
        System.out.println(helloEncoded);
        String result = StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString("\\U0048\\U0065\\U006c\\U006c\\U006f");
        System.out.println(result);
        result = StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString("  \\u0048\\u0065\\u006c\\u006c \\u006f   ");
        System.out.println(result);
        result = StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString("  \\u0004\\u0065\\u006c\\u006c \\u006f   ");
        System.out.println(result);
        result = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(result);
        System.out.println(result);
    }

    private static void testSystemPropertyInitializer() {
        System.out.println("RelevantPackage: " + TextUtils.getRelevantPackage());
        TextUtils.setRelevantPackage("com.utils.");
        System.out.println("RelevantPackage: " + TextUtils.getRelevantPackage());
    }

    private static void testParsingStringToTimeInterval() {
        TimeInterval result = TextUtils.parsingStringToTimeInterval("5s");
        System.out.println(new Date() + " Waiting for " + result.toMillis());
        TimeUtils.sleepFor(result);
        System.out.println(new Date() + " wait is over");
    }

    private static void testFormatStringToPreserveIndentaitonForHtml() {
        String rawText = "     *     This is non-indented line\n" +
                "     *       This is 2 spaces indented line\n" +
                "     *         This is 4 spaces indented line\n";
        String result = TextUtils.formatStringToPreserveIndentationForHtml(rawText);
        System.out.println(result + "\n" + StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(result));
    }

    private static void testHttpClient() {
        HttpClient httpClient = new HttpClient();
        httpClient.setContentType("image/jpeg");
        try {
            FileOutputStream fos = new FileOutputStream("C:\\Michael\\Personal\\tmp\\Test.jpg");
            long startTime = System.currentTimeMillis();
            ByteBuffer buff = httpClient.sendHttpRequestForBinaryResponse("http://www.lovethispic.com/uploaded_images/116204-Pretty-Pink-Sunset.jpg", HttpClient.HttpMethod.GET);
            System.out.println("Reading file time: " + (System.currentTimeMillis() - startTime) + "ms");
            fos.write(buff.array());
            System.out.println(httpClient.sendHttpRequest("http://www.lovethispic.com/image/116204/pretty-pink-sunset", HttpClient.HttpMethod.GET));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy