如何在Java 8上的maven surefire中设置单元测试的时区?
使用Java 7时,过去常常使用systemPropertyVariables以下配置,但使用Java 8时,测试只使用系统时区.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<user.timezone>UTC</user.timezone>
</systemPropertyVariables>
Run Code Online (Sandbox Code Playgroud)
为什么会这样,我该如何解决?
感觉就像java 8流和映射函数那么冗长,它们并不是真正的改进.例如,我编写了一些使用集合生成另一个修改过的集合的代码:
private List<DartField> getDartFields(Class<?> model) {
List<DartField> fields = new ArrayList<>();
for (Field field : model.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
fields.add(DartField.getDartField(field));
}
}
return fields;
}
Run Code Online (Sandbox Code Playgroud)
这似乎是java 8流及其功能的理想用例,所以我重写了它:
private List<DartField> getDartFields(Class<?> model) {
return Arrays.asList(model.getDeclaredFields())
.stream()
.filter(field -> !Modifier.isStatic(field.getModifiers()))
.map(field -> DartField.getDartField(field))
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
但我不确定我更喜欢那个.这是236个字符,而普通风格的java则是239个字符.它似乎没有或多或少可读.这是很好,你没有申报ArrayList,但需要打电话.collect(Collectors.toList())和Arrays.asList(取决于数据类型)是没有任何好转.
使用.stream()这样的东西是否有一些实际的改进,我只是没有得到,或者这只是一个有趣的方式抛出任何同事不知道函数式编程的循环?
我想如果我动态地传递过滤器或映射lambdas它会很有用,但如果你不需要这样做......
假设我有一个抛出运行时异常的方法.我正在使用a Stream来对列表中的项目调用此方法.
class ABC {
public void doStuff(MyObject myObj) {
if (...) {
throw new IllegalStateException("Fire! Fear! Foes! Awake!");
}
// do stuff...
}
public void doStuffOnList(List<MyObject> myObjs) {
try {
myObjs.stream().forEach(ABC:doStuff);
} catch(AggregateRuntimeException??? are) {
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我希望处理列表中的所有项目,并将各个项目的任何运行时异常收集到最终将抛出的"聚合"运行时异常中.
在我的真实代码中,我正在进行第三方API调用,这可能会引发运行时异常.我想确保处理所有项目并在最后报告任何错误.
我可以想出一些方法来解决这个问题,例如map()捕获并返回异常的函数(..shudder ..).但有没有一种本地方式来做到这一点?如果没有,还有另一种方法可以干净利落地实施吗?
我注意到java.time.format.DateTimeFormatter无法按预期解析.见下文:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Play {
public static void tryParse(String d,String f) {
try {
LocalDate.parse(d, DateTimeFormatter.ofPattern(f));
System.out.println("Pass");
} catch (Exception x) {System.out.println("Fail");}
}
public static void main(String[] args) {
tryParse("26-may-2015","dd-L-yyyy");
tryParse("26-May-2015","dd-L-yyyy");
tryParse("26-may-2015","dd-LLL-yyyy");
tryParse("26-May-2015","dd-LLL-yyyy");
tryParse("26-may-2015","dd-M-yyyy");
tryParse("26-May-2015","dd-M-yyyy");
tryParse("26-may-2015","dd-MMM-yyyy");
tryParse("26-May-2015","dd-MMM-yyyy");
}
}
Run Code Online (Sandbox Code Playgroud)
只有最后一次尝试tryParse("26-May-2015","dd-MMM-yyyy");将"通过".根据文档LLL应该能够解析出文本格式.也不是大写'M'与小写'm'的细微差别.
这真的很烦人,因为我无法默认解析Oracle DB默认格式化的字符串
SELECT TO_DATE(SYSDATE,'DD-MON-YYYY') AS dt FROM DUAL;
Run Code Online (Sandbox Code Playgroud)
同样,对于以下程序:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Play {
public static void output(String f) {
LocalDate d = LocalDate.now();
Locale l = …Run Code Online (Sandbox Code Playgroud) 我在java 8中看到了一个迭代集合的代码.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
numbers.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
功能是System.out::println什么?以上代码如何迭代List.
运营商的用途是::什么,我们可以在哪里使用这个运营商?
例
在这个(简化的)示例中,我可以MyInterface通过使用方法引用来创建我的对象apply,但是直接转换不起作用.
@Test
public void testInterfaceCast(){
Function<String, Integer> func = Integer::parseInt;
MyInterface legal = func::apply; // works
MyInterface illegal = func; // error
}
public interface MyInterface extends Function<String, Integer>{}
Run Code Online (Sandbox Code Playgroud)
第二个赋值给出了编译器错误:
incompatible types: Function<String,Integer> cannot be converted to MyInterface
Run Code Online (Sandbox Code Playgroud)
这个问题
我可以做一些泛型魔法,能够投射Function<T, R>到界面吗?
默认的java.time.Clock实现基于System.currentTimeMillis().正如这里所讨论的那样, 单调增加Java的时间?,不保证是单调的.
事实上,我经常遇到一种情况,系统时间会自动调整到过去几秒钟,而java时钟也会跳回来.
//now() returns 2016-01-13T22:34:05.681Z
order.setCreationTime(Instant.now());
//... something happens, order gets cancelled
//now() returns 2016-01-13T22:34:03.123Z
//which is a few seconds before the former one,
//even though the call was performed later - in any reasonable sense.
//The recorded history of events is obviously inconsistent with the real world.
order.setCancelationTime(Instant.now());
Run Code Online (Sandbox Code Playgroud)
然后,当人们不能仅依靠一个方向的时间时,就不可能执行时间敏感的事情,例如记录和分析事件历史.
上述帖子说System.nanoTime()是单调的(如果底层系统支持它).所以,如果我想将我的代码基于java.time API,我需要内部使用nanoTime的Clock来确保单向流动的时间.也许这样的事情会起作用.或者不是吗?
public class MyClock() extends java.time.Clock {
private final long adjustment;
public MyClock() {
long cpuTimeMillis = System.nanoTime()/1000000;
long systemTimeMillis = …Run Code Online (Sandbox Code Playgroud) 我最近有机会调整一些Java代码,并能够利用一些新的Java 8功能.在一个特定情况下,我需要.Name从对象列表中获取(字符串)属性列表.我所做的一个简单例子是:
// sample data: <Thing> objects have a single String property called "Name"
List<Thing> thingList =
new ArrayList<>(Arrays.asList(new Thing("Thing1"), new Thing("Thing2")));
// first pass
List<String> nameList = new ArrayList<>();
thingList.forEach(x -> nameList.add(x.getName()));
// refinement 1: use stream, map, and collect
List<String> nameList1 =
thingList.stream().map(x -> x.getName()).collect(Collectors.toList());
// refinement 2: use "Thing::getName" method reference
List<String> nameList2 =
thingList.stream().map(Thing::getName).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我很想知道这些方法将如何转化为C#,我得到了
// sample data: <Thing> objects have a single String property called "Name"
var thingList = new List<Thing> { new …Run Code Online (Sandbox Code Playgroud) 有一种方法get(sql)(我不能修改它).这个方法返回MyObjects,它必须在try catch块中,因为JqlParseException那里是可能的.我的代码是:
String sql = something;
try{
MyObject object = get(sql);
} catch(JqlParseException e){
e.printStackTrace();
} catch(RuntimeException e){
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我想删除try catch并使用Optional类,我试过:
MyObject object = Optional.ofNullable(get(sql)).orElseThrow(RuntimeException::new);
Run Code Online (Sandbox Code Playgroud)
但是那里的IDE力量也尝试了.并为:
MyObject object = Optional.ofNullable(get(sql)).orElseThrow(JqlParseException::new));
Run Code Online (Sandbox Code Playgroud)
是一个错误(在IDE中)The type JqlParseException does not define JqlParseException() that is applicable.有没有办法避免尝试catch块并使用可选的?
我认为我研究过的所有资源都强调一个流只能被消耗一次,消费是通过所谓的终端操作完成的(这对我来说非常清楚).
出于好奇我试过这个:
import java.util.stream.IntStream;
class App {
public static void main(String[] args) {
IntStream is = IntStream.of(1, 2, 3, 4);
is.map(i -> i + 1);
int sum = is.sum();
}
}
Run Code Online (Sandbox Code Playgroud)
最终会抛出一个运行时异常:
Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:229)
at java.util.stream.IntPipeline.reduce(IntPipeline.java:456)
at java.util.stream.IntPipeline.sum(IntPipeline.java:414)
at App.main(scratch.java:10)
Run Code Online (Sandbox Code Playgroud)
这是常见的,我遗漏了一些东西,但仍然想问:据我所知,这map是一个中间(和懒惰)操作,并且对Stream本身没有任何作用.只有当sum调用终端操作(这是一个急切的操作)时,才会消耗和操作流.
但为什么我要把它们连在一起呢?
有什么区别
is.map(i -> i + 1);
is.sum();
Run Code Online (Sandbox Code Playgroud)
和
is.map(i -> i + 1).sum();
Run Code Online (Sandbox Code Playgroud)
?