diff --git a/pom.xml b/pom.xml index afe8ec7..5b0cac1 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,7 @@ 1.10.19 1.6.6 2.0-alpha2 + 9.4.0.RC2 3.6.0 @@ -59,7 +60,7 @@ 2.10.4 1.6 4.3.0 - 0.7.7.201606060606 + 2.7 @@ -169,6 +170,21 @@ test + + org.eclipse.jetty + jetty-server + ${jetty.version} + test + + + + org.eclipse.jetty + jetty-servlet + ${jetty.version} + test + + + @@ -203,17 +219,15 @@ - org.jacoco - jacoco-maven-plugin - ${mvn.plugin.jacoco.version} - - - prepare-agent - - prepare-agent - - - + org.codehaus.mojo + cobertura-maven-plugin + ${mvn.plugin.cobertura.version} + + xml + 256m + + true + maven-surefire-plugin diff --git a/src/main/java/io/github/rhkiswani/javaff/exceptions/SmartException.java b/src/main/java/io/github/rhkiswani/javaff/exceptions/SmartException.java index 39ef8e7..10b9eed 100644 --- a/src/main/java/io/github/rhkiswani/javaff/exceptions/SmartException.java +++ b/src/main/java/io/github/rhkiswani/javaff/exceptions/SmartException.java @@ -58,9 +58,9 @@ public SmartException(Throwable cause) { @Override public String getMessage() { if (!ArraysUtils.isEmpty(errorMsgParams)){ - return LocaleUtil.getString(super.getMessage(), errorMsgParams); + return LocaleUtil.getString(super.getMessage(), SmartException.class, errorMsgParams); } else { - return LocaleUtil.getString(super.getMessage()); + return LocaleUtil.getString(super.getMessage(), SmartException.class, null); } } } diff --git a/src/main/java/io/github/rhkiswani/javaff/httpclient/ApacheHttpClient.java b/src/main/java/io/github/rhkiswani/javaff/httpclient/ApacheHttpClient.java index bbdf34d..d995417 100644 --- a/src/main/java/io/github/rhkiswani/javaff/httpclient/ApacheHttpClient.java +++ b/src/main/java/io/github/rhkiswani/javaff/httpclient/ApacheHttpClient.java @@ -39,7 +39,7 @@ * @since 0.0.20 * */ -class ApacheHttpClient implements HttpClient{ +public class ApacheHttpClient implements HttpClient{ @Override public String postJson(String url, String json, Map headers) throws HttpClientException { @@ -94,7 +94,8 @@ private CloseableHttpClient prepareRequest(HttpRequestBase method, Map urlParameters = new ArrayList<>(); for (String key : params.keySet()) { - urlParameters.add(new BasicNameValuePair(key, params.get(key))); + + urlParameters.add(new BasicNameValuePair(key, params.get(key))); } if (method instanceof HttpEntityEnclosingRequestBase){ ((HttpEntityEnclosingRequestBase) method).setEntity(new UrlEncodedFormEntity(urlParameters)); diff --git a/src/main/java/io/github/rhkiswani/javaff/httpclient/HttpClientWrapper.java b/src/main/java/io/github/rhkiswani/javaff/httpclient/HttpClientWrapper.java index 9be0e03..508f18a 100644 --- a/src/main/java/io/github/rhkiswani/javaff/httpclient/HttpClientWrapper.java +++ b/src/main/java/io/github/rhkiswani/javaff/httpclient/HttpClientWrapper.java @@ -48,7 +48,7 @@ public String post(String url, Map params, Map h @Override public String put(String url, Map params, Map headers) throws HttpClientException { - return httpClient.post(url, params, headers); + return httpClient.put(url, params, headers); } @Override diff --git a/src/main/java/io/github/rhkiswani/javaff/json/exceptions/JsonException.java b/src/main/java/io/github/rhkiswani/javaff/json/exceptions/JsonException.java index 6530976..24067a8 100644 --- a/src/main/java/io/github/rhkiswani/javaff/json/exceptions/JsonException.java +++ b/src/main/java/io/github/rhkiswani/javaff/json/exceptions/JsonException.java @@ -26,5 +26,4 @@ public class JsonException extends SmartException{ public JsonException(String errorMsg, Object... errorMsgParams) { super(errorMsg, errorMsgParams); } - } diff --git a/src/main/java/io/github/rhkiswani/javaff/lang/utils/StringUtils.java b/src/main/java/io/github/rhkiswani/javaff/lang/utils/StringUtils.java index 75f5e3e..1e7d37d 100644 --- a/src/main/java/io/github/rhkiswani/javaff/lang/utils/StringUtils.java +++ b/src/main/java/io/github/rhkiswani/javaff/lang/utils/StringUtils.java @@ -16,7 +16,6 @@ package io.github.rhkiswani.javaff.lang.utils; import io.github.rhkiswani.javaff.lang.ToStringHelper; -import io.github.rhkiswani.javaff.security.escape.EscapeersFactory; /** * @author Mohamed Kiswani @@ -29,10 +28,6 @@ public static String toString(Object obj) { return new ToStringHelper().toString(obj); } - public static String escape(String input) { - return (String) EscapeersFactory.getEscapeer(String.class).escape(input); - } - public static boolean isEmpty(String input){ return input == null || input.isEmpty(); } diff --git a/src/main/java/io/github/rhkiswani/javaff/locale/LocaleUtil.java b/src/main/java/io/github/rhkiswani/javaff/locale/LocaleUtil.java index 404349b..8409a26 100644 --- a/src/main/java/io/github/rhkiswani/javaff/locale/LocaleUtil.java +++ b/src/main/java/io/github/rhkiswani/javaff/locale/LocaleUtil.java @@ -49,8 +49,16 @@ private LocaleUtil(){ DEFAULT_MSGS.put(SmartException.NO_IMPLEMENTATION_FOUND, "No implementation found for {0} you need to set implementation through {1}.instance().add or add {2} to your classpath"); } - public static String getString(String key, Object... params){ + @Deprecated + /** + * use public static String getString(String key, Class targetClass, Object[] params) instead + */ + public static String getString(String key, Object[] params){ Class targetClass = ReflectionUtil.getCallerClass(1); + return getString(key, targetClass, params); + } + + public static String getString(String key, Class targetClass, Object[] params) { LocaleWorker worker = LocaleWorkersFactory.getLocalWorker(targetClass); String label = worker.getString(key, params); if (!StringUtils.isEmpty(label) && label.equals(key) && DEFAULT_MSGS.get(key) != null){ diff --git a/src/main/java/io/github/rhkiswani/javaff/log/LogWrapper.java b/src/main/java/io/github/rhkiswani/javaff/log/LogWrapper.java index 0d16921..1e7aba1 100644 --- a/src/main/java/io/github/rhkiswani/javaff/log/LogWrapper.java +++ b/src/main/java/io/github/rhkiswani/javaff/log/LogWrapper.java @@ -33,26 +33,26 @@ public LogWrapper(Log log) { @Override public void debug(String message, Object... params) { - log.debug(LocaleUtil.getString(message, params)); + log.debug(LocaleUtil.getString(message, LogWrapper.class, params)); } @Override public void info(String message, Object... params) { - log.info(LocaleUtil.getString(message, params)); + log.info(LocaleUtil.getString(message, LogWrapper.class, params)); } @Override public void warn(String message, Object... params) { - log.warn(LocaleUtil.getString(message, params)); + log.warn(LocaleUtil.getString(message, LogWrapper.class, params)); } @Override public void error(String message, Object... params) { - log.error(LocaleUtil.getString(message, params)); + log.error(LocaleUtil.getString(message, LogWrapper.class, params)); } @Override public void error(String message, Exception e, Object... params) { - log.error(LocaleUtil.getString(message, params), e); + log.error(LocaleUtil.getString(message, LogWrapper.class, params), e); } } diff --git a/src/main/java/io/github/rhkiswani/javaff/reflection/DefaultReflectionHelper.java b/src/main/java/io/github/rhkiswani/javaff/reflection/DefaultReflectionHelper.java index 142e685..354741a 100644 --- a/src/main/java/io/github/rhkiswani/javaff/reflection/DefaultReflectionHelper.java +++ b/src/main/java/io/github/rhkiswani/javaff/reflection/DefaultReflectionHelper.java @@ -20,6 +20,7 @@ import io.github.rhkiswani.javaff.reflection.exception.ReflectionException; import java.lang.reflect.Field; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -29,7 +30,13 @@ * @see io.github.rhkiswani.javaff.reflection.ReflectionHelper */ public class DefaultReflectionHelper implements ReflectionHelper{ + private static final ArrayList IGNORE_NAMES = new ArrayList<>(); + static { + IGNORE_NAMES.add("$jacocoData"); + IGNORE_NAMES.add("__cobertura_counters"); + IGNORE_NAMES.add("this$"); + } @Override public List scanFieldsByAnnotation(Class clazz, Class... annotations) throws ReflectionException { if (clazz == null){ @@ -43,8 +50,7 @@ public List scanFieldsByAnnotation(Class clazz, Class... annotations) thr for (Field field : fields) { for (Class aClass : annotations) { if (field.isAnnotationPresent(aClass)){ - if (field != null && field.getName().contains("$jacocoData")){ - // ignore jacoco:report added fields + if (isIgnored(field)){ continue; } list.add(field); @@ -80,8 +86,7 @@ public Field getField(Class clazz, String fieldName) throws ReflectionException } Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { - if (field != null && field.getName().contains("$jacocoData")){ - // ignore jacoco:report added fields + if (isIgnored(field)){ continue; } if (field.getName().equals(fieldName)){ @@ -112,11 +117,7 @@ public List getFields(Class clazz) { LinkedList list = new LinkedList(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { - if (field.getName().contains("this$")){ - continue; - } - if (field != null && field.getName().contains("$jacocoData")){ - // ignore jacoco:report added fields + if (isIgnored(field)){ continue; } field.setAccessible(true); @@ -128,6 +129,15 @@ public List getFields(Class clazz) { return list; } + private boolean isIgnored(Field field) { + for (String ignoreName : IGNORE_NAMES) { + if (field != null && field.getName().contains(ignoreName)){ + return true; + } + } + return false; + } + public V getFieldValue(T obj, String fieldName) throws ReflectionException { try { return (V) getField(obj.getClass(), fieldName).get(obj); diff --git a/src/main/java/io/github/rhkiswani/javaff/security/escape/DefaultEscapeHandler.java b/src/main/java/io/github/rhkiswani/javaff/security/escape/DefaultEscapeHandler.java deleted file mode 100644 index daa54e1..0000000 --- a/src/main/java/io/github/rhkiswani/javaff/security/escape/DefaultEscapeHandler.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2016 Mohamed Kiswani. - * - * 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 io.github.rhkiswani.javaff.security.escape; - -import io.github.rhkiswani.javaff.lang.utils.ArraysUtils; -import io.github.rhkiswani.javaff.security.escape.exception.EncodeException; - -/** - * @author Mohamed Kiswani - * @since 0.0.1 - * @see EscapeHandler - */ -public abstract class DefaultEscapeHandler implements EscapeHandler { - - protected abstract OUT encodeVal(IN in, Object... params); - - @Override - public OUT escape(IN in, Object... params) throws EncodeException { - if (in == null){ - return null; - } - if (params.length > 0){ - try { - ArraysUtils.replace(params, null, ""); - return encodeVal(in, params); - } catch (Throwable t ){ - throw new EncodeException(t); - } - - } - return encodeVal(in); - } - -} diff --git a/src/main/java/io/github/rhkiswani/javaff/security/escape/EscapeHandler.java b/src/main/java/io/github/rhkiswani/javaff/security/escape/EscapeHandler.java deleted file mode 100644 index 55275f4..0000000 --- a/src/main/java/io/github/rhkiswani/javaff/security/escape/EscapeHandler.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2016 Mohamed Kiswani. - * - * 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 io.github.rhkiswani.javaff.security.escape; - -import io.github.rhkiswani.javaff.security.escape.exception.EncodeException; - -/** - * @author Mohamed Kiswani - * @since 0.0.1 - * - */ -public interface EscapeHandler { - - O escape(I i, Object... params) throws EncodeException; - -} diff --git a/src/main/java/io/github/rhkiswani/javaff/security/escape/EscapeUtil.java b/src/main/java/io/github/rhkiswani/javaff/security/escape/EscapeUtil.java deleted file mode 100644 index 056d6f7..0000000 --- a/src/main/java/io/github/rhkiswani/javaff/security/escape/EscapeUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2016 Mohamed Kiswani. - * - * 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 io.github.rhkiswani.javaff.security.escape; - -import io.github.rhkiswani.javaff.exceptions.SmartException; -import io.github.rhkiswani.javaff.lang.exceptions.IllegalParamException; - -/** - * @author Mohamed Kiswani - * @since 0.0.1 - * - */ -public class EscapeUtil { - - private EscapeUtil(){ - - } - - public static T encodeString(String obj, Object... params){ - if (obj == null){ - throw new IllegalParamException(SmartException.NULL_VAL, "String"); - } - return (T) EscapeersFactory.getEscapeer(obj.getClass()).escape(String.valueOf(obj), params); - } -} diff --git a/src/main/java/io/github/rhkiswani/javaff/security/escape/EscapeersFactory.java b/src/main/java/io/github/rhkiswani/javaff/security/escape/EscapeersFactory.java deleted file mode 100644 index 0a0d040..0000000 --- a/src/main/java/io/github/rhkiswani/javaff/security/escape/EscapeersFactory.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2016 Mohamed Kiswani. - * - * 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 io.github.rhkiswani.javaff.security.escape; - -import io.github.rhkiswani.javaff.factory.AbstractFactory; - -/** - * @author Mohamed Kiswani - * @since 0.0.1 - * @see io.github.rhkiswani.javaff.factory.AbstractFactory - */ -public class EscapeersFactory extends AbstractFactory { - - private static EscapeersFactory instance = new EscapeersFactory(); - - private EscapeersFactory(){ - - } - - public static EscapeersFactory instance(){ - return instance; - } - - @Override - public EscapeHandler getDefault(Class targetClazz) { - return new StringEscapeHandler(); - } - - public static EscapeHandler getEscapeer(Class clazz) { - return instance.create(clazz); - } -} diff --git a/src/main/java/io/github/rhkiswani/javaff/security/escape/StringEscapeHandler.java b/src/main/java/io/github/rhkiswani/javaff/security/escape/StringEscapeHandler.java deleted file mode 100644 index 1ead644..0000000 --- a/src/main/java/io/github/rhkiswani/javaff/security/escape/StringEscapeHandler.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2016 Mohamed Kiswani. - * - * 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 io.github.rhkiswani.javaff.security.escape; - -/** - * @author Mohamed Kiswani - * @since 0.0.1 - * @see DefaultEscapeHandler - * @see EscapeHandler - */ -class StringEscapeHandler extends DefaultEscapeHandler { - - @Override - protected String encodeVal(String input, Object... params) { - StringBuilder resultStr = new StringBuilder(); - for (char ch : input.toCharArray()) { - if (isUnsafe(ch)) { - resultStr.append('%'); - resultStr.append(toHex(ch / 16)); - resultStr.append(toHex(ch % 16)); - } else { - resultStr.append(ch); - } - } - return resultStr.toString(); - } - - private static char toHex(int ch) { - return (char) (ch < 10 ? '0' + ch : 'A' + ch - 10); - } - - private static boolean isUnsafe(char ch) { - if (ch > 128 || ch < 0) - return true; - return " %$&+.,/@:;=?<>#%".indexOf(ch) >= 0; - } -} diff --git a/src/main/java/io/github/rhkiswani/javaff/security/escape/exception/EncodeException.java b/src/main/java/io/github/rhkiswani/javaff/security/escape/exception/EncodeException.java deleted file mode 100644 index 69f09f9..0000000 --- a/src/main/java/io/github/rhkiswani/javaff/security/escape/exception/EncodeException.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2016 Mohamed Kiswani. - * - * 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 io.github.rhkiswani.javaff.security.escape.exception; - -import io.github.rhkiswani.javaff.exceptions.SmartException; - -/** - * @author Mohamed Kiswani - * @since 0.0.1 - * @see io.github.rhkiswani.javaff.exceptions.SmartException - */ -public class EncodeException extends SmartException{ - - public EncodeException(String errorMsg, Object... errorMsgParams) { - super(errorMsg, errorMsgParams); - } - - public EncodeException(Throwable e) { - super(e); - } -} diff --git a/src/test/java/io/github/rhkiswani/javaff/detector/ApiMeadataTest.java b/src/test/java/io/github/rhkiswani/javaff/detector/ApiMeadataTest.java index dc149e9..f4a5e57 100644 --- a/src/test/java/io/github/rhkiswani/javaff/detector/ApiMeadataTest.java +++ b/src/test/java/io/github/rhkiswani/javaff/detector/ApiMeadataTest.java @@ -1,5 +1,6 @@ package io.github.rhkiswani.javaff.detector; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import static io.github.rhkiswani.javaff.detector.ApiDetectorUtil.*; @@ -16,6 +17,12 @@ public void setup(){ apiMetadata2 = JACKSON_API_METADATA; } + @Test(expected=IllegalAccessException.class) + public void testConstructorPrivate() throws Exception { + ApiDetectorUtil.class.newInstance(); + Assert.fail("Utility class constructor should be private"); + } + @Test public void testEquals() throws Exception { assertThat(apiMetadata.equals(apiMetadata2)).isEqualTo(false); diff --git a/src/test/java/io/github/rhkiswani/javaff/http/HttpClientTest.java b/src/test/java/io/github/rhkiswani/javaff/http/HttpClientTest.java index 7db5188..b7299c3 100644 --- a/src/test/java/io/github/rhkiswani/javaff/http/HttpClientTest.java +++ b/src/test/java/io/github/rhkiswani/javaff/http/HttpClientTest.java @@ -2,39 +2,91 @@ import io.github.rhkiswani.javaff.detector.ApiDetectorUtil; import io.github.rhkiswani.javaff.factory.exceptions.NoImplementationFoundException; +import io.github.rhkiswani.javaff.httpclient.ApacheHttpClient; import io.github.rhkiswani.javaff.httpclient.HttpClient; import io.github.rhkiswani.javaff.httpclient.HttpClientFactory; -import org.junit.Before; +import io.github.rhkiswani.javaff.json.JsonHandler; +import io.github.rhkiswani.javaff.json.JsonHandlerFactory; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import java.util.HashMap; +import java.util.Map; + import static org.assertj.core.api.Assertions.assertThat; -@RunWith(PowerMockRunner.class) -public class HttpClientTest { +public class HttpClientTest extends WebTester{ @Test - @PrepareForTest(ApiDetectorUtil.class) public void testFactory() throws Exception { // check default HttpClient httpClient = HttpClientFactory.getHttpClient(Object.class); assertThat(httpClient).isNotNull(); - PowerMockito.mockStatic(ApiDetectorUtil.class); - PowerMockito.when(ApiDetectorUtil.isApacheHttpClientAvailable()).thenReturn(false); - // check if the class have been cashed - HttpClientFactory.getHttpClient(Object.class); - assertThat(httpClient).isNotNull(); HttpClientFactory.instance().remove(Object.class); - try { HttpClientFactory.getHttpClient(Object.class); }catch (Exception e){ assertThat(e).isInstanceOf(NoImplementationFoundException.class).hasMessage("No implementation found for HttpClientFactory you need to set implementation through HttpClientFactory.instance().add or add https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient to your classpath"); } + HttpClientFactory.instance().add(Object.class, new ApacheHttpClient()); + } + + @Test + public void testPost() throws Exception{ + HttpClient httpClient = HttpClientFactory.getHttpClient(HttpClientTest.class); + Map params = prepareParams(); + String post = httpClient.post(BASE_URL, params, null); + assertValues("POST", params, post); + } + + @Test + public void testPostJson() throws Exception{ + HttpClient httpClient = HttpClientFactory.getHttpClient(HttpClientTest.class); + String post = httpClient.postJson(BASE_URL,"{\"method\":\"POST\"}", null); + assertJsonResponse(post, "POST"); + } + + @Test + public void testPut() throws Exception{ + String method = "PUT"; + HttpClient httpClient = HttpClientFactory.getHttpClient(HttpClientTest.class); + Map params = prepareParams(); + String put = httpClient.put(BASE_URL, params, null); + assertValues(method, params, put); + } + + private Map prepareParams() { + Map params = new HashMap<>(); + params.put("paramsEmail", "rhkiswani@gmail.com"); + return params; + } + + private void assertValues(String method, Map params, String post) { + JsonHandler handler = JsonHandlerFactory.getJsonHandler(HttpClientTest.class); + Response receivedRespond = handler.fromJson(post, Response.class); + + assertThat(receivedRespond.params).isEqualTo(params); + assertThat(receivedRespond.method).isEqualTo(method); + assertThat(receivedRespond.contentType).isEqualTo("application/x-www-form-urlencoded"); } + @Test + public void testPutJson() throws Exception{ + HttpClient httpClient = HttpClientFactory.getHttpClient(HttpClientTest.class); + String put = httpClient.putJson(BASE_URL,"{\"method\":\"PUT\"}", null); + assertJsonResponse(put, "PUT"); + } + + private void assertJsonResponse(String response, String method) { + JsonHandler handler = JsonHandlerFactory.getJsonHandler(HttpClientTest.class); + Response receivedRespond = handler.fromJson(response, Response.class); + + assertThat(receivedRespond.jsonParams).isEqualTo("{\"method\":\""+ method +"\"}"); + assertThat(receivedRespond.method).isEqualTo(method); + assertThat(receivedRespond.contentType).isEqualTo("application/json"); + } } diff --git a/src/test/java/io/github/rhkiswani/javaff/http/WebTester.java b/src/test/java/io/github/rhkiswani/javaff/http/WebTester.java new file mode 100644 index 0000000..ec4a833 --- /dev/null +++ b/src/test/java/io/github/rhkiswani/javaff/http/WebTester.java @@ -0,0 +1,93 @@ +package io.github.rhkiswani.javaff.http; + +import io.github.rhkiswani.javaff.json.JsonHandlerFactory; +import io.github.rhkiswani.javaff.lang.exceptions.IllegalParamException; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.handler.AbstractHandler; +import org.junit.After; +import org.junit.Before; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.BufferedReader; +import java.io.IOException; +import java.net.BindException; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +public class WebTester { + protected static final String BASE_URL = "http://localhost:9999/testHttp"; + + protected Server server = new Server(9999); + protected ServerConnector connector = null; + + @Before + public void startJetty() throws Exception { + // Create Server + server.setHandler(new MyServlet()); + // Start Server + try { + server.start(); + }catch (BindException b){ + if (b.getMessage().contains("Address already in use")){ + server.stop(); + throw b ; + } + } + } + + @After + public void stopJetty() { + try { + server.stop(); + } + catch (Exception e) { + throw new IllegalParamException(e.getMessage()); + } + } + + private class MyServlet extends AbstractHandler { + + @Override + public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException { + httpServletResponse.setContentType(request.getContentType()); + httpServletResponse.setStatus(HttpServletResponse.SC_OK); + sendWhatReceived(httpServletRequest, httpServletResponse); + request.setHandled(true); + } + + private void sendWhatReceived(HttpServletRequest req, HttpServletResponse resp) throws IOException { + Response r = new Response(); + r.method = req.getMethod(); + r.contentType = req.getContentType(); + for (String o : req.getParameterMap().keySet()) { + r.params.put(o, req.getParameter(o)); + } + Enumeration headerNames = req.getHeaderNames(); + while (headerNames.hasMoreElements()){ + String headerName = headerNames.nextElement(); + r.requestHeaders.put(headerName, req.getHeader(headerName)); + } + if (r.contentType.equalsIgnoreCase("application/json")){ + BufferedReader br = req.getReader(); + String str; + while( (str = br.readLine()) != null ){ + r.jsonParams += str; + } + } + resp.getWriter().println(JsonHandlerFactory.getJsonHandler(WebTester.class).toJson(r)); + } + } + + protected class Response { + String method; + String contentType; + Map params = new HashMap<>(); + Map requestHeaders = new HashMap<>(); + public String jsonParams = ""; + } +} diff --git a/src/test/java/io/github/rhkiswani/javaff/lang/ObjectUtilsTest.java b/src/test/java/io/github/rhkiswani/javaff/lang/ObjectUtilsTest.java index 730a81f..51bd7d2 100644 --- a/src/test/java/io/github/rhkiswani/javaff/lang/ObjectUtilsTest.java +++ b/src/test/java/io/github/rhkiswani/javaff/lang/ObjectUtilsTest.java @@ -101,17 +101,10 @@ public void testEqualsByIdAnnotation() throws Exception { assertThat(ObjectUtils.isEqual(null, e)).isEqualTo(false); } - @Test - public void testEmptyClass() throws Exception { - assertThat(ObjectUtils.isEqual(new EmptyClass(), new EmptyClass())).isEqualTo(false); - } @Test public void testHashcode() throws Exception { assertThat(ObjectUtils.toHashCode(null)).isEqualTo(-1); } - private class EmptyClass{ - - } } diff --git a/src/test/java/io/github/rhkiswani/javaff/lang/StringUtilsTest.java b/src/test/java/io/github/rhkiswani/javaff/lang/StringUtilsTest.java deleted file mode 100644 index f8d5624..0000000 --- a/src/test/java/io/github/rhkiswani/javaff/lang/StringUtilsTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package io.github.rhkiswani.javaff.lang; - -import io.github.rhkiswani.javaff.lang.utils.StringUtils; -import org.junit.Test; - -import static org.assertj.core.api.Assertions.assertThat; -public class StringUtilsTest { - - @Test - public void testStringUtils() throws Exception { - assertThat(StringUtils.escape(null)).isEqualTo(null); - assertThat(StringUtils.escape("Kiswani")).isEqualTo("Kiswani"); - assertThat(StringUtils.escape("rhkiswani@gmail.com")).isEqualTo("rhkiswani%40gmail%2Ecom"); - assertThat(StringUtils.escape("%$&+.,/@:;=?<>#%")).isEqualTo("%25%24%26%2B%2E%2C%2F%40%3A%3B%3D%3F%3C%3E%23%25"); - } - -} diff --git a/src/test/java/io/github/rhkiswani/javaff/locale/LocaleTest.java b/src/test/java/io/github/rhkiswani/javaff/locale/LocaleTest.java index 3f7c904..4c6d991 100644 --- a/src/test/java/io/github/rhkiswani/javaff/locale/LocaleTest.java +++ b/src/test/java/io/github/rhkiswani/javaff/locale/LocaleTest.java @@ -39,11 +39,11 @@ public void testDefaultWorker() throws Exception { @Test public void testLocaleUtil() throws Exception { - assertThat(LocaleUtil.getString(SmartException.EXCEEDS_LIMIT, "Array", 1000)).isEqualTo("Array MaxSize is 1,000"); - assertThat(LocaleUtil.getString(SmartException.HTTP_ERROR, "google.com")).isEqualTo("failed to connect to google.com"); - assertThat(LocaleUtil.getString("LOCALIZED_MSG", "Kiswani")).isEqualTo("this is localized msg from messages_en.properties thanks for Mr Kiswani"); - assertThat(LocaleUtil.getString(null)).isNull(); - assertThat(LocaleUtil.getString("LOCALIZED_MSG", null)).isEqualTo("this is localized msg from messages_en.properties thanks for Mr {0}"); + assertThat(LocaleUtil.getString(SmartException.EXCEEDS_LIMIT, LocaleTest.class, new Object[]{"Array", 1000})).isEqualTo("Array MaxSize is 1,000"); + assertThat(LocaleUtil.getString(SmartException.HTTP_ERROR, LocaleTest.class, new Object[]{"google.com"})).isEqualTo("failed to connect to google.com"); + assertThat(LocaleUtil.getString("LOCALIZED_MSG", LocaleTest.class, new Object[]{"Kiswani"})).isEqualTo("this is localized msg from messages_en.properties thanks for Mr Kiswani"); + assertThat(LocaleUtil.getString(null, LocaleTest.class, null)).isNull(); + assertThat(LocaleUtil.getString("LOCALIZED_MSG", LocaleTest.class, null)).isEqualTo("this is localized msg from messages_en.properties thanks for Mr {0}"); } @Test diff --git a/tools/deploy.sh b/tools/deploy.sh index e3f62fc..7daa00e 100755 --- a/tools/deploy.sh +++ b/tools/deploy.sh @@ -17,7 +17,7 @@ function getVersion(){ function prepare(){ git checkout master -# git reset --hard + git reset --hard git pull origin master getVersion }