From e993ba063f2218c2d8046db781f7df10436385b3 Mon Sep 17 00:00:00 2001 From: Lasse Lindqvist Date: Fri, 20 May 2022 08:48:51 +0300 Subject: [PATCH] Issue #65: Fix get_library_information when called multiple times Also extends tests to cover this behaviour. --- .../remoteserver/servlet/ServerMethods.java | 6 ++++- .../examplelib/FullDynamic.java | 26 +++++++++++++------ .../examplelib/FullDynamicTest.java | 26 +++++++++++++++++++ .../examplelib/SimpleRemoteServerServlet.java | 22 ++++++++++++++++ 4 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 src/test/java/org/robotframework/examplelib/FullDynamicTest.java create mode 100644 src/test/java/org/robotframework/examplelib/SimpleRemoteServerServlet.java diff --git a/src/main/java/org/robotframework/remoteserver/servlet/ServerMethods.java b/src/main/java/org/robotframework/remoteserver/servlet/ServerMethods.java index f9da940..dde84e5 100644 --- a/src/main/java/org/robotframework/remoteserver/servlet/ServerMethods.java +++ b/src/main/java/org/robotframework/remoteserver/servlet/ServerMethods.java @@ -42,6 +42,8 @@ public ServerMethods(RemoteServerServlet servlet) { log = LogFactory.getLog(ServerMethods.class); this.servlet = servlet; } + + private static final String STOP_REMOTE_SERVER = "stop_remote_server"; /** * Get an array containing the names of the keywords that the library @@ -54,7 +56,9 @@ public List get_keyword_names() { List names = servlet.getLibrary().getKeywordNames(); if (names == null || names.size() == 0) throw new RuntimeException("No keywords found in the test library"); - names.add("stop_remote_server"); + if (!names.contains(STOP_REMOTE_SERVER)) { + names.add(STOP_REMOTE_SERVER); + } return names; } catch (Throwable e) { log.warn("", e); diff --git a/src/test/java/org/robotframework/examplelib/FullDynamic.java b/src/test/java/org/robotframework/examplelib/FullDynamic.java index 62d294e..97def56 100644 --- a/src/test/java/org/robotframework/examplelib/FullDynamic.java +++ b/src/test/java/org/robotframework/examplelib/FullDynamic.java @@ -1,10 +1,15 @@ package org.robotframework.examplelib; import org.robotframework.javalib.library.AnnotationLibrary; +import org.robotframework.javalib.library.RobotFrameworkDynamicAPI; import java.util.List; +import java.util.Map; -public class FullDynamic { +public class FullDynamic implements RobotFrameworkDynamicAPI +{ + private final AnnotationLibrary lib; + public FullDynamic() { lib = new AnnotationLibrary(); lib.addKeywordPattern("org/robotframework/examplelib/impl/**.class"); @@ -18,14 +23,16 @@ public List getKeywordNames() { * AnnotationLibrary re-throws all exceptions as RuntimeExceptions. Unwrap * it to obtain the original exception. */ - public Object runKeyword(String keywordName, List args) throws Throwable { - try { - return lib.runKeyword(keywordName, args, null); - } catch (RuntimeException e) { - throw e.getCause(); - } + @Override + public Object runKeyword(String keywordName, List args) { + return lib.runKeyword(keywordName, args, null); } + @Override + public Object runKeyword(String keywordName, List args, Map kwargs) { + return lib.runKeyword(keywordName, args, kwargs); + } + public List getKeywordArguments(String keywordName) { return lib.getKeywordArguments(keywordName); } @@ -37,6 +44,9 @@ public String getKeywordDocumentation(String keywordName) { return lib.getKeywordDocumentation(keywordName); } - private AnnotationLibrary lib; + + public AnnotationLibrary getLib() { + return lib; + } } diff --git a/src/test/java/org/robotframework/examplelib/FullDynamicTest.java b/src/test/java/org/robotframework/examplelib/FullDynamicTest.java new file mode 100644 index 0000000..06cd89c --- /dev/null +++ b/src/test/java/org/robotframework/examplelib/FullDynamicTest.java @@ -0,0 +1,26 @@ +package org.robotframework.examplelib; + +import java.io.IOException; +import java.util.Map; + +import javax.servlet.ServletException; + +import org.robotframework.remoteserver.servlet.ServerMethods; +import org.testng.Assert; +import org.testng.annotations.Test; +public class FullDynamicTest { + + + @Test + public void testCallingTwice() throws IOException, ServletException { + FullDynamic dynamic = new FullDynamic(); + SimpleRemoteServerServlet servlet = new SimpleRemoteServerServlet(dynamic.getLib()); + ServerMethods methods = new ServerMethods(servlet); + + Map map1 = methods.get_library_information(); + Map map2 = methods.get_library_information(); + // The exact count we don't care about. + // We just want the information to stay consistent. + Assert.assertEquals(map1.size(), map2.size()); + } +} diff --git a/src/test/java/org/robotframework/examplelib/SimpleRemoteServerServlet.java b/src/test/java/org/robotframework/examplelib/SimpleRemoteServerServlet.java new file mode 100644 index 0000000..1fc1290 --- /dev/null +++ b/src/test/java/org/robotframework/examplelib/SimpleRemoteServerServlet.java @@ -0,0 +1,22 @@ +package org.robotframework.examplelib; + +import org.robotframework.javalib.library.AnnotationLibrary; +import org.robotframework.remoteserver.library.RemoteLibrary; +import org.robotframework.remoteserver.library.RemoteLibraryFactory; +import org.robotframework.remoteserver.servlet.RemoteServerServlet; + +public class SimpleRemoteServerServlet extends RemoteServerServlet { + + private static final long serialVersionUID = -2729050484180284488L; + private final RemoteLibrary library; + + public SimpleRemoteServerServlet(AnnotationLibrary library) { + RemoteLibraryFactory libraryFactory = createLibraryFactory(); + RemoteLibrary remoteLibrary = libraryFactory.createRemoteLibrary(library); + this.library = remoteLibrary; + } + + public RemoteLibrary getLibrary() { + return this.library; + } +}