如何在Java中的控制台中从输出中获取字符串并将其与期望值进行比较

时间:2018-11-27 13:53:04

标签: java

下面是代码:

InputStream in = channelExec.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = reader.readLine()) != null) {    
                System.out.println(line);               
            }                   
        } catch (JSchException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

以下是我的输出:

Context successfully set
Script started
LXKADMIN|In/OutBoundValidation|-|50149.11065.26960.11788|inbound=OFF|outbound=OFF
inbound=OFF|outbound=OFF
Script complete
STEP 1: COMPLETED
PASSED: step1

我想获取第5行“ inbound = OFF | outbound = OFF”,并检查其值是否匹配“ inbound = OFF | outbound = OFF”,那么我的测试用例将通过,否则将失败。

TCL脚本为:

tcl;
eval {
puts "Script started"

set schemaValidationStr [mql temp query bus LXKADMIN In/OutBoundValidation * select id description dump '|']
puts $schemaValidationStr

set schemaValidation [string range $schemaValidationStr 57 end]
puts $schemaValidation

puts "Script complete"
}

任何建议都会很有帮助。

4 个答案:

答案 0 :(得分:0)

不可能进行推理,因为您无法访问控制台并看到可视化的内容。相反,您应该处理控制台获取的输入,然后将其输出,在您的情况下,该输入就是line变量。

如果您足以看到inbound=OFF|outbound=OFF,则可以像这样在line中搜索该子字符串:

if(line.indexOf("inbound=OFF|outbound=OFF") != -1) {
   // you have a match
}

答案 1 :(得分:0)

我不确定我是否理解这个问题,但是您可以尝试以下方法:

public static final int CHECK_LINE_NR = 4;
public static final String CHECK_LINE_VALUE = "inbound=OFF|outbound=OFF";

InputStream in = channelExec.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            int    linenr;
            boolean line_check_passed;
            linenr = 0;
            line_check_passed = false;
            while ((line = reader.readLine()) != null) {
                linenr++;
                if (linenr == CHECK_LINE_NR)
                  line_check_passed = line.equals(CHECK_LINE_VALUE);
                System.out.println(line);               
            }                   
        } catch (JSchException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

答案 2 :(得分:0)

public static boolean check(InputStream in, int line, String expected) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    int i = 0;
    String str;

    while ((str = reader.readLine()) != null) {
        if (i < line)
            i++;
        else
            return expected.equals(str);
    }

    return false;
}

答案 3 :(得分:0)

如果您的问题只是如何验证TCL脚本返回的输出包含特定行,则可以尝试以下操作:

public static final int CHECK_LINE_NR = 4;
public static final String EXPECTED_LINE_VALUE = "inbound=OFF|outbound=OFF";

public boolean verifyScriptOutput(ChannelExec channel)
  throws IOException
{
  // Check all lines in output
  BufferedReader    reader = new BufferedReader(new InputStreamReader(channel.getInputStream()));
  String            line;
  int               linenr = 0;
  while ((line = reader.readLine()) != null)
  {
    linenr++;
    if (linenr == CHECK_LINE_NR)
      return (line.equals(EXPECTED_LINE_VALUE));
  }

  // If we get here, output has less lines
  return (false);

} // verifyScriptOutput