src.test.java.com.mgnt.utils.StamTest Maven / Gradle / Ivy
package com.mgnt.utils;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import com.mgnt.lifecycle.management.httpclient.HttpClient;
import com.mgnt.lifecycle.management.httpclient.HttpClient.HttpMethod;
import com.mgnt.utils.entities.TimeInterval;
/**
* Created by michaelg on 19/01/2016.
*/
public class StamTest {
public static void main(String[] args) {
// testParsing();
// testEncoderDecoder();
// testSystemPropertyInitializer();
// testParsingStringToTimeInterval();
// testFormatStringToPreserveIndentaitonForHtml();
// testGetStacktrace();
// testTimeInterval();
// testHttpClient();
testJsonParser();
}
private static void testParsing() {
StringBuilder num = new StringBuilder("-92.89");
BigDecimal defaultVal = new BigDecimal(10);
System.out.println(TextUtils.parseStringToBigDecimal(num, defaultVal, null, null));
System.out.println(TextUtils.parseStringToBigDecimal(num, defaultVal));
num = new StringBuilder("-92L");
System.out.println(TextUtils.parseStringToBigDecimal(num, defaultVal, null, null));
System.out.println(TextUtils.parseStringToBigDecimal(num, defaultVal));
System.out.println(TextUtils.parseStringToBigDecimal(num, defaultVal));
}
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 testParseStringToTimeInterval() {
TimeInterval result = TextUtils.parseStringToTimeInterval("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 testGetStacktrace() {
String stacktrace = "com.lib.DigestiveException: Potential stomach upset was detected\r\n" +
" at our.package.util.FoodHacking.washing(FoodHacking.java:391) ~[classes:?]\r\n" +
" at our.package.CustomTool.parseFood(CustomTool.java:307) ~[classes:?]\r\n" +
" at our.package.CustomTool.initTable(CustomTool.java:300) ~[classes:?]\r\n" +
" at our.package.CustomTool.(CustomTool.java:52) ~[classes:?]\r\n" +
" at our.package.CustomToolServlet.doGetOrPost(CustomToolServlet.java:323) ~[classes:?]\r\n" +
" at our.package.CustomToolServlet.doPost(CustomToolServlet.java:261) ~[classes:?]\r\n" +
" at javax.servlet.http.HttpServlet.service(HttpServlet.java:523) ~[jboss-servlet-api.jar]\r\n" +
" at javax.servlet.http.HttpServlet.service(HttpServlet.java:590) ~[jboss-servlet-api.jar]\r\n" +
" at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74) ~[undertow-servlet.jar!/:.Final]\r\n" +
" at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129) ~[undertow-servlet.jar!/:.Final]\r\n" +
" at our.package.filter.Poison.doFilter(ldskhgoias:59) ~[our.jar:?]\r\n" +
" at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237) ~[spring-web]\r\n" +
" at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167) ~[spring-web]\r\n" +
" at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61) ~[undertow-servlet.jar!/:.Final]\r\n" +
" at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131) ~[undertow-servlet.jar!/:.Final]";
String wierdStacktrace = "com.lib.DigestiveException: Potential stomach upset was detected\r\n" +
" at deployment.our-1.0.0.ear.foodlocker-service.war//our.package.util.FoodHacking.washing(FoodHacking.java:391)\r\n" +
" at deployment.our-1.0.0.ear.foodlocker-service.war//our.package.CustomTool.parseFood(CustomTool.java:307)\r\n" +
" at deployment.our-1.0.0.ear.foodlocker-service.war//our.package.CustomTool.initTable(CustomTool.java:300)\r\n" +
" at deployment.our-1.0.0.ear.foodlocker-service.war//our.package.CustomTool.(CustomTool.java:52)\r\n" +
" at deployment.our-1.0.0.ear.foodlocker-service.war//our.package.CustomToolServlet.doGetOrPost(CustomToolServlet.java:323)\r\n" +
" at deployment.our-1.0.0.ear.foodlocker-service.war//our.package.CustomToolServlet.doPost(CustomToolServlet.java:261)\r\n" +
" at [email protected]//javax.servlet.http.HttpServlet.service(HttpServlet.java:523)\r\n" +
" at [email protected]//javax.servlet.http.HttpServlet.service(HttpServlet.java:590)\r\n" +
" at [email protected]//io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)\r\n" +
" at [email protected]//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)\r\n" +
" at deployment.our-1.0.0.ear//our.package.filter.Poison.doFilter(ldskhgoias:59)\r\n" +
" at deployment.our-1.0.0.ear//org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)\r\n" +
" at deployment.our-1.0.0.ear//org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)\r\n" +
" at [email protected]//io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)\r\n" +
" at [email protected]//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)";
// String stacktrace = "java.lang.Exception: Dumb Stacktrace\n" +
// "at com.mot.nsa.errorhandling.TestController.handler(TestController.java:35)\n" +
// "at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
// "at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n" +
// "at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n" +
// "at java.lang.reflect.Method.invoke(Method.java:498)\n" +
// "at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)\n" +
// "at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)\n" +
// "at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)\n" +
// "at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:849)\n" +
// "at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:760)\n" +
// "at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)\n" +
// "at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)\n" +
// "at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)\n" +
// "at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)\n" +
// "at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)\n" +
// "at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)\n" +
// "at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)\n" +
// "at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n" +
// "at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n" +
// "at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)\n" +
// "at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n" +
// "at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:111)\n" +
// "at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n" +
// "at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)\n" +
// "at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n" +
// "at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)\n" +
// "at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n" +
// "at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)\n" +
// "at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n" +
// "at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)\n" +
// "at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n" +
// "at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:103)\n" +
// "at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n" +
// "at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n" +
// "at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)\n" +
// "at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\n" +
// "at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)\n" +
// "at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)\n" +
// "at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)\n" +
// "at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)\n" +
// "at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)\n" +
// "at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)\n" +
// "at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)\n" +
// "at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)\n" +
// "at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)\n" +
// "at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\n" +
// "at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\n" +
// "at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\n" +
// "at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n" +
// "at java.lang.Thread.run(Thread.java:748)\n" +
// "Caused by: java.io.IOException: Test controller artificially generated exception\n" +
// "at com.mot.nsa.errorhandling.TestController.handler(TestController.java:31)\n" +
// "... 66 more";
//
// System.out.println(TextUtils.getStacktrace(stacktrace, "com.mot"));
// TextUtils.setRelevantPackage("com.mot.");
// System.out.println(TextUtils.getStacktrace(stacktrace));
System.out.println(TextUtils.getStacktrace(stacktrace, "our.package"));
System.out.println(TextUtils.getStacktrace(wierdStacktrace, "deployment.our-1.0.0.ear.foodlocker-service.war//our.package"));
}
private static void testTimeInterval() {
TimeInterval oneMilli = new TimeInterval(1L, TimeUnit.MILLISECONDS);
System.out.println("The number of nanoseconds in one millisecond is: " + oneMilli.toNanos());
}
private static void testHttpClient() {
try {
HttpClient client = new HttpClient();
// client.setConnectionUrl("https://www.yahoo.com/");
// client.setConnectionUrl("http://www.israelnationalnews.com/");
// client.setConnectTimeout("1s");
// client.setReadTimeout(1L, TimeUnit.SECONDS);
client.setConnectionUrl("http://localhost:7789/student/withDynamicSearch");
String result = client.sendHttpRequest(HttpMethod.GET);
client.setRequestProperty("accept", "application/json;charset=UTF-8");
client.setContentType("application/json");
// String result = client.sendHttpRequest(HttpMethod.POST, "{ \"username\": \"adminnpr\", \"password\": \"Adminnpr1234\"}");
System.out.println(result);
System.out.println("HTTP " + client.getLastResponseCode() + " " + client.getLastResponseMessage());
System.out.println("Response headers: " + client.getLastResponseHeaders());
} catch (IOException e) {
System.out.println(TextUtils.getStacktrace(e, "com.mgnt."));
}
}
private static void testJsonParser() {
TextUtils.setRelevantPackage("com.mgnt.");
try {
Map map = new HashMap<>();
map.put("Hello", "World");
map.put("Author", "Michael Gantman");
String json = JsonUtils.writeObjectToJsonString(map);
System.out.println(json);
map = JsonUtils.readObjectFromJsonString(json, Map.class);
System.out.println(map);
TestUser james = new TestUser();
james.setFirstName("James");
james.setLastName("Doe");
TestUser jane = new TestUser();
jane.setFirstName("Jane");
jane.setLastName("Dane");
List users = new ArrayList<>();
users.add(jane);
users.add(james);
UserList myUserList = new UserList();
myUserList.setUsers(users);
json = JsonUtils.writeObjectToJsonString(myUserList, true);
System.out.println(json);
UserList recoveredUsers = JsonUtils.readObjectFromJsonString(json, UserList.class, true);
System.out.println(recoveredUsers.getUsers());
json = JsonUtils.writeObjectToJsonString(users);
System.out.println(json);
Class> clazz = (Class>)users.getClass();
users = JsonUtils.readObjectFromJsonString(json, clazz);
System.out.println(users);
} catch (IOException e) {
System.out.println(TextUtils.getStacktrace(e));
}
}
}