Util { public static String getTaskClassVersion(Config config) { try { Optional<String> appClass = Optional.ofNullable(new ApplicationConfig(config).getAppClass()); if (appClass.isPresent()) { return Optional.ofNullable(Class.forName(appClass.get()).getPackage().getImplementationVersion()) .orElse(FALLBACK_VERSION); } else { Optional<String> taskClass = new TaskConfig(config).getTaskClass(); if (taskClass.isPresent()) { return Optional.ofNullable(Class.forName(taskClass.get()).getPackage().getImplementationVersion()) .orElse(FALLBACK_VERSION); } else { LOG.warn("Unable to find app class or task class. Defaulting to {}", FALLBACK_VERSION); return FALLBACK_VERSION; } } } catch (Exception e) { LOG.warn(String.format("Ran into exception while trying to get version of app or task. Defaulting to %s", FALLBACK_VERSION), e); return FALLBACK_VERSION; } }  static String envVarEscape(String str); static String getSamzaVersion(); static String getTaskClassVersion(Config config); static InetAddress getLocalHost();  }
@Test public void testGetTaskClassVersion() { assertEquals(Util.FALLBACK_VERSION, Util.getTaskClassVersion(new MapConfig())); String appClassVersion = MyAppClass.class.getPackage().getImplementationVersion(); String expectedAppClassVersion = (appClassVersion != null) ? appClassVersion : Util.FALLBACK_VERSION; Config config = new MapConfig(ImmutableMap.of(ApplicationConfig.APP_CLASS, MyAppClass.class.getName())); assertEquals(expectedAppClassVersion, Util.getTaskClassVersion(config)); String taskClassVersion = MyTaskClass.class.getPackage().getImplementationVersion(); String expectedTaskClassVersion = (taskClassVersion != null) ? taskClassVersion : Util.FALLBACK_VERSION; config = new MapConfig(ImmutableMap.of(TaskConfig.TASK_CLASS, MyTaskClass.class.getName())); assertEquals(expectedTaskClassVersion, Util.getTaskClassVersion(config)); config = new MapConfig(ImmutableMap.of(ApplicationConfig.APP_CLASS, MyAppClass.class.getName(), TaskConfig.TASK_CLASS, "this_is_not_a_class")); assertEquals(expectedAppClassVersion, Util.getTaskClassVersion(config)); }