OsMemoryUtil { long getOsTotalFreeMemorySizeFromStrings(final List<String> lines, final Set<String> memKeysToCombine) { long totalFree = 0; int count = 0; for (final String line : lines) { for (final String keyName : memKeysToCombine) { if (line.startsWith(keyName)) { count++; final long size = parseMemoryLine(line); if (size == 0) { return 0; } totalFree += size; } } } final int length = memKeysToCombine.size(); if (count != length) { final String errMsg = String .format("Expect %d keys in the meminfo file. Got %d. content: %s", length, count, lines); logger.error(errMsg); totalFree = 0; } return totalFree; }    }
@Test public void getOsTotalFreeMemorySizeMissingEntry() { final List<String> lines = Arrays.asList("MemFree: 1 kB", "Foo: 10 kB"); final long size = this.util.getOsTotalFreeMemorySizeFromStrings(lines, OsMemoryUtil.MEM_KEYS); assertEquals(0, size); }
@Test public void getOsTotalFreeMemorySizeWrongEntry() { final List<String> lines = Collections.singletonList("MemFree: foo kB"); final long size = this.util.getOsTotalFreeMemorySizeFromStrings(lines, OsMemoryUtil.MEM_KEYS); assertEquals(0, size); }