我正在测试一个方法,当出现问题时记录警告并返回null.
就像是:
private static final Logger log = Logger.getLogger(Clazz.class.getName());
....
if (file == null || !file.exists()) {
// if File not found
log.warn("File not found: "+file.toString());
} else if (!file.canWrite()) {
// if file is read only
log.warn("File is read-only: "+file.toString());
} else {
// all checks passed, can return an working file.
return file;
}
return null;
Run Code Online (Sandbox Code Playgroud)
我想测试junit是否发出了警告,除了返回null之外,在所有情况下(例如找不到文件,文件是只读的).
有任何想法吗?
谢谢,asaf :-)
我对Aaron的回答的实现(加上彼得的评论):
public class UnitTest {
...
@BeforeClass
public static void setUpOnce() {
appenders = new Vector<Appender>(2);
// …Run Code Online (Sandbox Code Playgroud) 在普通的java中我会使用:
public User(String name, String email) {
this.name = name;
this.email = f(email);
this.admin = false;
}
Run Code Online (Sandbox Code Playgroud)
但是,我在使用ActiveRecords的rails(3.2.3)中找不到简单的标准方法.
def initialize(attributes = {}, options = {})
@name = attributes[:name]
@email = f(attributes[:email])
@admin = false
end
Run Code Online (Sandbox Code Playgroud)
after_initialize回调通过覆盖它:
def after_initialize(attributes = {}, options = {})
...
end
Run Code Online (Sandbox Code Playgroud)
或者用宏:
after_initialize : my_own_little_init
def my_own_little_init(attributes = {}, options = {})
...
end
Run Code Online (Sandbox Code Playgroud)
但可能存在一些弃用问题.
那么,使用什么是正确/标准的方法?
我正在使用枚举来替换String我的Java应用程序中的常量(JRE 1.5).
当我将enum视为不断调用的方法中的静态名称数组时(例如,在呈现UI时),是否会影响性能?
我的代码看起来有点像这样:
public String getValue(int col) {
return ColumnValues.values()[col].toString();
}
Run Code Online (Sandbox Code Playgroud)
values()重复枚举相关的隐藏成本(例如,在paint()方法内部).int=> enum转换 - 这不是Java的方式.提取values()数组的实际价格是多少?这甚至是个问题吗?
请阅读下面的Simon Langhoff的答案,Geeks On Hugs在接受的答案评论中已经指出了这一点.Enum.values() 必须做一个防御性的副本
我正在搜索带有正则表达式的字符串数组,如下所示:
for (int j = line; j < lines.length; j++) {
if (lines[j] == null || lines[j].isEmpty()) {
continue;
}
matcher = pattern.matcher(lines[j]);
if (matcher.find(offset)) {
offset = matcher.end();
line = j;
System.out.println("found \""+matcher.group()+"\" at line "+line+" ["+matcher.start()+","+offset+"]");
return true;
}
offset = 0;
}
return false;
Run Code Online (Sandbox Code Playgroud)
请注意,在我上面的实现中,我保存line并offset进行连续搜索.
无论如何,现在我想从[line,offset] 向后搜索.
澄清:通过向后我的意思是找到以前的比赛.
例如,假设我正在搜索"dana"
"dana nama? dana kama! lama dana kama?"
Run Code Online (Sandbox Code Playgroud)
并进入第二场比赛.如果我再做matcher.find()一次,我会向前搜索并获得第3场比赛.但是我想向后搜索并进入第一场比赛.
然后上面的代码应输出如下内容:
found "dana" at line …Run Code Online (Sandbox Code Playgroud) 我想定义项目范围的接口,用于@Category注释,并配置Maven在构建整个项目时排除它们的注释测试.
在应用程序项目中,有一个我要分类的测试:
@Category(Integration.class)
@Test
public void testExternalResource() { ... }
Run Code Online (Sandbox Code Playgroud)
我已经建立了一个多模块maven项目:
/container (has a pom with <modules> element)
/parent (all other modules inherit from its pom. has no source, only pom)
/util (other modules are depending on it)
/infra
/application (depending on infra and util, inherits from parent)
Run Code Online (Sandbox Code Playgroud)
作为一个基础设施狂热者:),我想配置我的整个项目以排除任何模块中的组.所以,在父模块中我定义了:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<excludedGroups>com.mycompany.test.Integration</excludedGroups>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
我将Integration接口放在util模块中(在util/src/test/java)中,所有模块都可以看到:
package com.mycompany.test;
public interface Integration {}
Run Code Online (Sandbox Code Playgroud)
因为它是一个test-jar, …
我不禁想知道为什么我不能写那样的东西:
for (int i : 3) {
System.out.println(i);
}
Run Code Online (Sandbox Code Playgroud)
打印出来:
0
1
2
Run Code Online (Sandbox Code Playgroud)
我的意思是,3可以自动装箱Integer,也可以Iterable.
我知道,我已经选择了第一个元素0,但我认为这是常见的情况,并且它可以利用这样的ForEach构造来促进倒计时.
我有一个JComboBox,其值通过网络检索.
我正在寻找一种向用户指示该事实的方法,当用户想要查看列表时,展开下拉列表,然后才检索数据.
基本要求包括:
请注意,在用户想要查看组合的值(即展开下拉列表)之前,不会检索数据.
我使用了一个SwingWorker来保持UI响应.组合框使用JIDE's覆盖Overlayable,JIDE InfiniteProgressPanel聆听工作人员.
为什么Map<K,V>,get()使用Object参数定义:
V get(Object key)
Run Code Online (Sandbox Code Playgroud)
而不是通用的参数:
V get(K key)
Run Code Online (Sandbox Code Playgroud)
?
今天早上简单,但导致我的问题,似乎无法找到任何代码.我创建了一个数字的十六进制表示,并希望现在保存十六进制字符串的最后4个字符,然后转换回整数.
转换的代码如下所示:
int in2 = new Integer(mycard.resourceid.toString());
String hex = Integer.toHexString(in2);
Run Code Online (Sandbox Code Playgroud)
如果有人能够朝着正确的方向推动我,我将非常感激.
int in3 = new Integer(hex.length());
int in4 = new Integer (in3 - 4);
String mystring = hex.subString(in4, in3);
Run Code Online (Sandbox Code Playgroud) 在一个swing应用程序中,我正在JComponent使用自定义渲染文本Graphics.drawString().这是一个示例:
aa文本http://img525.imageshack.us/img525/4928/drawstringsample.jpg
在同一个应用程序中,我正在使用a渲染文本JTextPane.这是一个示例:
alt text http://img28.imageshack.us/img28/1134/jtextpanesample.jpg
你能注意到较低的样品有点"污迹"吗?好吧,我无法弄清楚如何让它看起来像上面的样本.
谢谢,asaf :-)
更新:
System.setProperty("awt.useSystemAAFontSettings","false")而且"lcd"也没有工作.((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)在paint()不工作putClientProperty(sun.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, Boolean.TRUE) 给 java.lang.ClassCastException: java.lang.Boolean cannot be cast to sun.swing.SwingUtilities2$AATextInfojava ×8
junit ×2
swing ×2
activerecord ×1
android ×1
antialiasing ×1
enums ×1
foreach ×1
generics ×1
jcombobox ×1
jide ×1
log4j ×1
logging ×1
map ×1
maven ×1
performance ×1
regex ×1
string ×1
unit-testing ×1