Skip to content

Commit

Permalink
#248: add support of {code} snippets for java stacktrace verification
Browse files Browse the repository at this point in the history
Fixed #248
  • Loading branch information
dgroup committed Nov 3, 2020
1 parent 10e41bc commit 418ff30
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 6 deletions.
51 changes: 45 additions & 6 deletions lib/lazylead/task/accuracy/stacktrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,57 @@ def passed(issue)
!frames(issue.description).select { |f| oracle?(f) || java?(f) }.empty?
end

# Detect all {noformat} frames in description field
# Detect all {noformat}, {code} frames in ticket description
def frames(description)
description.enum_for(:scan, /(?={noformat})/)
.map { Regexp.last_match.offset(0).first }
.each_slice(2).map do |f|
description[f.first, f.last - f.first + "{noformat}".size]
noformat(description).concat(code(description))
end

# Detect all noformat blocks and give all text snippets in array
# @param desc The jira ticket description
def noformat(desc)
return [] unless desc.include? "{noformat}"
desc.enum_for(:scan, /(?={noformat})/)
.map { Regexp.last_match.offset(0).first }
.each_slice(2).map do |f|
desc[f.first, f.last - f.first + "{noformat}".size]
end
end

# Detect all {code:*} blocks and give all text snippets in array
# @param desc The jira ticket description
def code(desc)
return [] unless desc.include?("{code:") || desc.include?("{code}")
words = desc.gsub(/{code/, " {code")
.gsub("}", "} ")
.gsub("Caused by:", "Caused_by:")
.split(" ")
.map(&:strip)
.reject(&:blank?)
pairs(words, "{code").map { |s| words[s.first..s.last].join("\n") }
end

# Detect indexes of pairs which are starting from particular text
# @param words is array of words
# @param text is a label for pairs
#
# paris([aa,tag,bb,cc,tag,dd], "tag") => [[1, 4]]
# paris([aa,tag,bb,cc,tag,dd,tag,ee], "tag") => [[1, 4]] # non closed
#
def pairs(words, text)
snippets = [[]]
words.each_with_index do |e, i|
next unless e.start_with? text
pair = snippets.last
pair << i if pair.size.zero? || pair.size == 1
snippets[-1] = pair
snippets << [] if pair.size == 2
end
snippets.select { |s| s.size == 2 }
end

# @return true if frame has few lines with java stack frames
def java?(frame)
allowed = ["at ", "Caused by:"]
allowed = ["at", "Caused by:", "Caused_by:"]
frame.split("\n")
.map(&:strip)
.count { |l| allowed.any? { |a| l.start_with? a } } > 3
Expand Down
87 changes: 87 additions & 0 deletions test/lazylead/task/accuracy/stacktrace_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,92 @@ class StacktraceTest < Lazylead::Test
)
)
end

test "java error is found in code section" do
assert Stacktrace.new.passed(
OpenStruct.new(
description: "
asdfasdfasdf
{code:java}texta-line1
;texta-line2{code}
asdjf;asdjfa;sdjf
as;djf;asdjf
{code}javax.servlet.ServletException: Something went wrong
at com.example.myproject.OpenSessionInViewFilter.doFilter(OpenSessionInViewFilter.java:60)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.example.myproject.ExceptionHandlerFilter.doFilter(ExceptionHandlerFilter.java:28)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.example.myproject.OutputBufferFilter.doFilter(OutputBufferFilter.java:33)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:943)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: com.example.myproject.MyProjectServletException
at com.example.myproject.MyServlet.doPost(MyServlet.java:169)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.example.myproject.OpenSessionInViewFilter.doFilter(OpenSessionInViewFilter.java:30)
... 27 more
Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [com.example.myproject.MyEntity]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2329)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2822)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:689)
at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
at $Proxy19.save(Unknown Source)
at com.example.myproject.MyEntityService.save(MyEntityService.java:59) <-- relevant call (see notes below)
at com.example.myproject.MyServlet.doPost(MyServlet.java:164)
... 32 more
Caused by: java.sql.SQLException: Violation of unique constraint MY_ENTITY_UK_1: duplicate value(s) for column(s) MY_COLUMN in statement [...]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:57)
... 54 more{code}
asdfasdfasdf{code:sql}select 1 from dual {code}what?
"
)
)
end

test "pair detected" do
assert_equal [[1, 4]],
Stacktrace.new.pairs(%w[aa tag bb cc tag dd], "tag")
end

test "proper pair detected" do
assert_equal [[1, 4]],
Stacktrace.new.pairs(%w[aa tag bb cc tag dd tag], "tag")
end
end
end

0 comments on commit 418ff30

Please sign in to comment.