我已尝试使用Hsqldb和H2进行单元测试,但遇到了序列生成器的问题.字段声明如下所示.
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="keyword_seq")
@SequenceGenerator(name="keyword_seq",sequenceName="KEYWORD_ID_SEQ", allocationSize=1)
@Column(name="IM_KEYWORD_ID")
private Long keywordId;
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下配置在Hsqldb中测试此表中的简单插入时,它会给出错误
<prop key="hibernate.connection.driver_class">org.hsqldb.jdbc.JDBCDriver</prop>
<prop key="hibernate.connection.url">jdbc:hsqldb:mem:testdb;sql.syntax_ora=true</prop>
Run Code Online (Sandbox Code Playgroud)
错误:
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: KEYWORD_ID_SEQ.NEXTVAL
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
使用H2:
<prop key="hibernate.connection.driver_class">org.h2.Driver</prop>
<prop key="hibernate.connection.url">jdbc:h2:~/test</prop>
Run Code Online (Sandbox Code Playgroud)
错误是:
Hibernate: select KEYWORD_ID_SEQ.nextval from dual
3085 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 42001, SQLState: 42001
3088 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Syntax error in SQL statement "SELECT KEYWORD_ID_SEQ.NEXTVAL FROM[*] DUAL "; expected "identifier"; SQL statement:
Run Code Online (Sandbox Code Playgroud)
任何想法,如何解决这个问题?
我有一个方法可以将String记录列表解析为对象并返回List对象.所以我的方法签名是这样的.
public List<ParsedObject> parse(String[] records);
但我也想返回,其他指标,如未成功解析的字符串记录数.现在我感到困惑,如何返回此指标.一种选择是创建另一个包装类,该类包含已解析记录列表和存储这些度量标准的成员.
但是我经常面对这种情况,这样我最终会创建许多包装类.
不确定我是否解释得很好.这里有什么建议?
我正在使用以下命令从文件中删除重复项.
awk -F"," '!x[$1]++' test.csv
Run Code Online (Sandbox Code Playgroud)
如何让它忽略第1列的情况?
我试过awk -F"," '{IGNORECASE = 1} !x[$1]++' test.csv但它似乎没有用.
我在SO上查了几个问题,这似乎表明不允许使用两个连续连字符(例如my--website.com),但是当我在http://www.register.com/index上搜索相同的域名时. rcmx,它很乐意接受这个名字,同时拒绝像我的#website.com这样的无效域名.
使用Regex验证URL /域?(导轨)
这是我第一次使用AOP,所以这可能是一个新手问题。
public class MyAspect implements AspectI {
public void method1() throws AsyncApiException {
System.out.println("In Method1. calling method 2");
method2();
}
@RetryOnInvalidSessionId
public void method2() throws AsyncApiException {
System.out.println("In Method2, throwing exception");
throw new AsyncApiException("method2", AsyncExceptionCode.InvalidSessionId);
}
public void login() {
System.out.println("Logging");
}
Run Code Online (Sandbox Code Playgroud)
InvalidSessionHandler看起来像这样。
@Aspect
public class InvalidSessionIdHandler implements Ordered {
@Around("@annotation(com.pkg.RetryOnInvalidSessionId)")
public void reLoginAll(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Hijacked call: " + joinPoint.getSignature().getName() + " Proceeding");
try {
joinPoint.proceed();
} catch (Throwable e) {
if (e instanceof AsyncApiException) {
AsyncApiException …Run Code Online (Sandbox Code Playgroud) 我有一个控制器,提供下载文件的功能.
@ResponseBody
public void downloadRecycleResults(String batchName, HttpServletResponse response) throws Exception {
File finalResultFile = null;
// code here generates and initializes finalResultFile for batchName
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + finalResultFile.getName());
IOUtils.copy(new FileReader(finalResultFile), response.getOutputStream());
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何编写测试,我可以验证写入的内容response.我已经使用了ArgumentCaptor很多但不知何故它似乎不适合这里.
controller.downloadRecycleResults("batchName", mock(HttpServletResponse.class));
verify(response).getOutputStream(); // but how to capture content?
Run Code Online (Sandbox Code Playgroud) 最近我和我的队友讨论了Guava Optional在方法中使用可选参数的问题.
我们说方法是
List<Book> getBooks(String catalogId, Optional<String> categoryId) {
Validate.notNull(catalogId);
Validate.notNull(categoryId); // Point of conflict. Is this required?
Run Code Online (Sandbox Code Playgroud)
它接受一个catalogId和一个可选项 categoryId,它返回该目录中列出的书籍,如果类别也被传递,则只返回该类别中的书籍.
冲突点是,验证Optional<String> categoryId空检查.我认为不应该对它进行空检查,因为它是一个可选参数.该函数的调用者可以传递null或Optional.<String>absent()与getBooks功能应做处理这两种情况if(categoryId==null && categoryId.isPresent())在执行.
我的意见是,Optional对于可选参数,只是使方法的合同更加清晰.通过查看方法签名,可以告诉该参数是可选的,并且不需要读取javadoc.但是Optional.absent()当他不想使用那个可选参数时,他不应该被迫通过.
我的队友有不同的看法.他想对它进行空检查,因此强制调用者总是通过Optional.<String>absent().他的观点是我们为什么要传递null可选.而且getBooks("catalog123", Optional.absent())看起来更可读getBooks("catalog123", null).
此方法位于我们的一个库包中,由我们拥有的多个包使用.
您Optional对此方案的使用有何建议?
谢谢
我正在使用SuperCSV将CSV记录解析为Object.我的CSV文件最后有额外的列,我只想处理前X列.所以我String[]为前X列和CellProcessor[]相同大小定义了映射.但它似乎不起作用,并抛出异常,即单元格数量的处理器应与列数完全相同.
有人可以告诉我,如果我错过了什么.即使我不想要它们,我是否需要定义映射数组以具有与五个完全相同的列?
public CsvToBeanParser(Reader reader, Class<T> type, CsvPreference preference, CellProcessor[] cellProcessors, String[] mapping, boolean skipHeader)
throws IOException {
this.beanReader = new CsvBeanReader(reader, preference);
this.mapping = mapping;
if (skipHeader) {
beanReader.getHeader(true);
}
this.cellProcessors = cellProcessors;
this.type = type;
}
/**
* Parse and return record.
*
* @return
* @throws Exception
* if there is any parsing error
*/
public T getItem() throws Exception {
try {
return (T) beanReader.read(type, mapping, cellProcessors);
} catch (Exception e) { …Run Code Online (Sandbox Code Playgroud) 在我的测试类中定义的常量很少,并且有一些逻辑依赖于这些常量的值.
就我而言,我从输入CSV文件创建多个批次.每个批次的大小以常量定义.我正在创建一个小的输入文件并将其传递给测试用例.但由于文件很小,它只创建一个批次.
一个明显的解决方案是创建一个更大的文件并将其传递给测试用例.但我必须创建一个像15MB这样的大文件.其他方法是,如果我可以调整常量以在测试用例中具有较小的值,这肯定需要一个hacky方法.
我觉得第一个解决方案是正确的,但有任何建议吗?
我想以类型安全的方式分配两个uint64_t数的最小差异和uint32_t的最大数值限制.有办法吗?
uint32_t max32 = std::numeric_limits<uint32_t>::max();
uint64_t a, b;
uint32_t minValue = std::min(max32, b - a);
^^^^^^ (Parameter mismatch warning)
Run Code Online (Sandbox Code Playgroud)
基本上.如果a和b之间的差值大于uint32_t的最大值,我想使用最大值,否则我想使用差值.我需要minValue在只接受uint32_t的API中使用.