Skip to content

Commit

Permalink
Merge 053685d into a024d74
Browse files Browse the repository at this point in the history
  • Loading branch information
ScienJus committed Mar 3, 2016
2 parents a024d74 + 053685d commit 5c9ab94
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pippo-controller/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
<artifactId>pippo-core</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ro.pippo.core.Param;
import ro.pippo.core.ParamPattern;
import ro.pippo.core.ParameterValue;
import ro.pippo.core.PippoRuntimeException;
import ro.pippo.core.route.RouteContext;
Expand Down Expand Up @@ -172,6 +173,7 @@ protected Method findMethod(Class<? extends Controller> controllerClass, String
@SuppressWarnings("unchecked")
protected Object[] prepareMethodArgs(RouteContext routeContext) {
Class<?>[] types = method.getParameterTypes();
Parameter[] parameters = method.getParameters();

if (types.length == 0) {
return new Object[]{};
Expand Down Expand Up @@ -199,7 +201,12 @@ protected Object[] prepareMethodArgs(RouteContext routeContext) {
args[i] = value.toCollection((Class<? extends Collection>) type, genericType, null);
}
} else {
args[i] = value.to(type);
ParamPattern pattern = parameters[i].getAnnotation(ParamPattern.class);
if (pattern != null) {
args[i] = value.to(type, pattern.value());
} else {
args[i] = value.to(type);
}
}
}
}
Expand Down
53 changes: 53 additions & 0 deletions pippo-controller/src/test/java/ControllerHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2014 the original author or authors.
*
* 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.
*/
import org.junit.Test;
import ro.pippo.controller.Controller;
import ro.pippo.controller.DefaultControllerHandler;
import ro.pippo.core.Param;
import ro.pippo.core.ParamPattern;

import static org.junit.Assert.assertNotNull;

/**
* @author ScienJus
*/
public class ControllerHandlerTest {

@Test
public void testDateParam() throws Exception {
DefaultControllerHandler handler = new DefaultControllerHandler(DateController.class, "testDateParam");
assertNotNull(handler.getMethod());
}

@Test
public void testDateParamWithPattern() throws Exception {
DefaultControllerHandler handler = new DefaultControllerHandler(DateController.class, "testDateParamWithPattern");
assertNotNull(handler.getMethod());
}

static class DateController extends Controller {

public void testDateParam(@Param("date") java.util.Date since) {
System.out.println(since);
}

// yyyy-MM-dd HH:mm is not supported from in java.sql.Date/Time/Timestamp, just for test
public void testDateParamWithPattern(@ParamPattern("yyyy-MM-dd HH:mm") @Param("date") java.util.Date since) {
System.out.println(since);
}

}
}
16 changes: 16 additions & 0 deletions pippo-core/src/main/java/ro/pippo/core/ParameterValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ public Date toDate(Date defaultValue, String pattern) {
}
}

private Date toDateFromUnixTimestamp() {
if (isNull() || StringUtils.isNullOrEmpty(values[0])) {
return null;
}
try {
return new Date(Long.parseLong(values[0]));
} catch (NumberFormatException e) {
throw new PippoRuntimeException(e);
}
}

public java.sql.Date toSqlDate() {
return toSqlDate(null);
}
Expand Down Expand Up @@ -516,6 +527,11 @@ private Object toObject(Class<?> type, String pattern) {
if (type == Timestamp.class) {
return toSqlTimestamp();
}
// support java.util.Date
if (Date.class.isAssignableFrom(type)) {
//default from unix timestamp like System.currentTimeMillis()
return toDateFromUnixTimestamp();
}
} else {
if (Date.class.isAssignableFrom(type)) {
Date date = toDate(pattern);
Expand Down

0 comments on commit 5c9ab94

Please sign in to comment.