2012-05-14 64 views
0
public boolean accept(File directory, String fileName) { 
    boolean fileOK = true; 

    if (name != null) { 
     fileOK &= fileName.startsWith(name); 
    } 

    if (pattern != null) { 
     fileOK &= Pattern.matches(regex, fileName); 
    } 

    if (extension != null) { 
     fileOK &= fileName.endsWith('.' + extension); 
    } 
    return fileOK; 
} 
+0

你檢查'pattern'不爲空,然後用'regex'比較,還哪裏'name'和'來自 –

回答

1

是的!

def "file should be valid"() { 
    setup: 
     def dir = new File("/tmp") 
     def fileName = "foo.bar" 

    when: 
     boolean valid = foobar.accept(dir, fileName) 

    then: 
     valid 
} 
+2

'expect:foobar.accept(dir,fileName)'在這種情況下是更好的匹配。 –

5

下面是另一種寫作方式。我用數據驅動的方法,你必須測試多種方案(多如果該方法的)

def "should accept valid filenames"() { 
    expect: 
    foobar.accept(new File("/tmp"), fileName) 

    where: 
    fileName << ["valid_filename_1", "valid_filename_2", "valid_filename_n"] 
} 
相關問題