用Java5我们可以写:
Foo[] foos = ...
for (Foo foo : foos)
Run Code Online (Sandbox Code Playgroud)
或者只是在for循环中使用Iterable.这非常方便.
但是你不能像这样为iterable编写泛型方法:
public void bar(Iterable<Foo> foos) { .. }
Run Code Online (Sandbox Code Playgroud)
并使用数组调用它,因为它不是Iterable:
Foo[] foos = { .. };
bar(foos); // compile time error
Run Code Online (Sandbox Code Playgroud)
我想知道这个设计决定背后的原因.
我有这个简单的形式:
<form id="commentForm" method="POST" action="api/comment">
<input type="text" name="name" title="Your name"/>
<textarea cols="40" rows="10" name="comment" title="Enter a comment">
</textarea>
<input type="submit" value="Post"/>
<input type="reset" value="Reset"/>
</form>
Run Code Online (Sandbox Code Playgroud)
我需要在发送到服务器之前添加两个POST参数:
var params = [
{
name: "url",
value: window.location.pathname
},
{
name: "time",
value: new Date().getTime()
}
];
Run Code Online (Sandbox Code Playgroud)
请不要修改表格.
我怎么能改写这个:
for (int i = 0; i < numberOfSpaces; i++) {
System.out.print(" ");
}
Run Code Online (Sandbox Code Playgroud)
用String.format()?
PS
我很确定这是可能的,但javadoc有点令人困惑.
长期方法在某些方面是邪恶的:
如何说服你的开发人员编写简短的方法?(武器被禁止=)
来自agiledeveloper的问题
我的小实用程序应用程序通过GUI文件选择器向用户询问输出目录.然后,经过一些处理后,它会在此输出目录中创建大量文件.
我需要检查应用程序是否具有写访问权限,以便通知用户并且不继续处理(这可能需要很长时间)
我的第一次尝试是java.io.File的canWrite()方法.但这不起作用,因为它处理目录条目本身而不是其内容.我已经看到至少一个Windows XP文件夹的实例可以重命名或删除但是不能在其中创建文件(因为权限).这实际上是我的测试用例.
我终于解决了以下解决方案
//User places the input file in a directory and selects it from the GUI
//All output files will be created in the directory that contains the input file
File fileBrowse = chooser.getSelectedFile(); //chooser is a JFileChooser
File sample = new File(fileBrowse.getParent(),"empty.txt");
try
{
/*
* Create and delete a dummy file in order to check file permissions. Maybe
* there is a safer way for this check.
*/
sample.createNewFile();
sample.delete();
} …Run Code Online (Sandbox Code Playgroud) 好的,我很沮丧!我已经狩猎了好几个小时,我仍然难过.
环境:WinXP,Eclipse Galileo 3.5(直接安装 - 无需额外插件).
所以,我有一个简单的JUnit测试.它从内部Eclipse JUnit运行配置运行良好.这个类没有依赖任何东西.为了尽可能缩小这个问题,它只包含:
@Test
public void testX() {
assertEquals("1", new Integer(1).toString());
}
Run Code Online (Sandbox Code Playgroud)
到目前为止没有汗水.现在我想采取从Ant内部运行此测试用例的超级高级步骤(最终目标是与Hudson集成).
所以,我创建了一个build.xml:
<project name="Test" default="basic">
<property name="default.target.dir" value="${basedir}/target" />
<property name="test.report.dir" value="${default.target.dir}/test-reports" />
<target name="basic">
<mkdir dir="${test.report.dir}" />
<junit fork="true" printSummary="true" showOutput="true">
<formatter type="plain" />
<classpath>
<pathelement path="${basedir}/bin "/>
</classpath>
<batchtest fork="true" todir="${test.report.dir}" >
<fileset dir="${basedir}/bin">
<include name="**/*Test.*" />
</fileset>
</batchtest>
</junit>
</target>
</project>
Run Code Online (Sandbox Code Playgroud)
$ {basedir}是工作空间中包含源,类和构建文件的Java项目名称.所有.java和build.xml都在$ {basedir}/src中..class文件位于$ {basedir}/bin中.
我已经通过Windows/Preferences/Ant/Runtime/Contributed Entries将eclipse-install-dir/plugins/org.junit4_4.5.0.v20090423/junit.jar添加到Ant运行时类路径中.ant-junit.jar在Ant Home Entries中.
那么,当我运行这个疯狂复杂的目标时会发生什么?我的报告文件包含:
Testsuite: com.xyz.test.RussianTest
Tests run: 1, Failures: 0, Errors: 1, Time …Run Code Online (Sandbox Code Playgroud) 以下类用作equals/hashCode契约的通用测试器.它是本土测试框架的一部分.
班级:
@Ignore
@RunWith(Theories.class)
public abstract class ObjectTest {
// For any non-null reference value x, x.equals(x) should return true
@Theory
public void equalsIsReflexive(Object x) {
assumeThat(x, is(not(equalTo(null))));
assertThat(x.equals(x), is(true));
}
// For any non-null reference values x and y, x.equals(y)
// should return true if and only if y.equals(x) returns true.
@Theory
public void equalsIsSymmetric(Object x, Object y) {
assumeThat(x, is(not(equalTo(null))));
assumeThat(y, is(not(equalTo(null))));
assumeThat(y.equals(x), is(true));
assertThat(x.equals(y), is(true));
}
// For any non-null reference values x, y, …Run Code Online (Sandbox Code Playgroud) 我刚刚读完了Exception Driven Programming,我想知道像ELMAH for Java 这样的东西.你知道它吗?
有趣的功能:
注意
log4j用于日志记录,它不是异常处理的集成解决方案
我经常在System.nanoTime()对中包装代码以便对其进行计时.就像是:
long start = System.nanoTime();
methodToBeTimed();
long elapsedTime = System.nanoTime() - start;
Run Code Online (Sandbox Code Playgroud)
有什么好的计时库可以解决这个问题吗?还将接受自行开发的代码.
NB
分析器不是解决方案,因为我想在单元测试中强制执行一些时间限制,所以我想以编程方式对方法进行计时.
一段时间后,每个程序员都会得到一组实用程序类.其中一些是真正的编程珍珠,它们在你的几个项目中被重用.例如,在java中:
class Separator {
private String separator;
private boolean called;
public Separator(String aSeparator) {
separator = aSeparator;
called = false;
}
@Override
public String toString() {
if (!called) {
called = true;
return "";
} else {
return separator;
}
}
}
Run Code Online (Sandbox Code Playgroud)
和:
public class JoinHelper {
public static <T> String join(T... elements) {
return joinArray(" ", elements);
}
public static <T> String join(String separator, T... elements) {
return joinArray(separator, elements);
}
private static <T> String joinArray(String sep, T[] elements) …Run Code Online (Sandbox Code Playgroud) java ×7
junit ×2
ant ×1
c# ×1
classpath ×1
file ×1
format ×1
forms ×1
hamcrest ×1
javascript ×1
jquery ×1
metrics ×1
oop ×1
performance ×1
permissions ×1
string ×1
timing ×1
unit-testing ×1