ConfigPropertiesUtil { @NotNull public static Map<String, String> toProperties(@NotNull ConfigPart configPart, @Nullable String valueIfUnset, @Nullable String valueIfPasswordEmpty, @Nullable String valueIfPasswordSet) { Map<String, String> ret = new HashMap<>(); ret.put(PerforceEnvironment.P4PORT, ! configPart.hasServerNameSet() || configPart.getServerName() == null ? valueIfUnset : configPart.getServerName().getDisplayName()); ret.put(PerforceEnvironment.P4TRUST, ! configPart.hasTrustTicketFileSet() || configPart.getTrustTicketFile() == null ? valueIfUnset : configPart.getTrustTicketFile().toString()); ret.put(PerforceEnvironment.P4USER, ! configPart.hasUsernameSet() || configPart.getUsername() == null ? valueIfUnset : configPart.getUsername()); ret.put(PerforceEnvironment.P4TICKETS, ! configPart.hasAuthTicketFileSet() || configPart.getAuthTicketFile() == null ? valueIfUnset : configPart.getAuthTicketFile().toString()); ret.put(PerforceEnvironment.P4SERVER_FINGERPRINT, ! configPart.hasServerFingerprintSet() || configPart.getServerFingerprint() == null ? valueIfUnset : configPart.getServerFingerprint()); ret.put(PerforceEnvironment.P4PASSWD, getPasswordValue( configPart.hasPasswordSet() && !configPart.requiresUserEnteredPassword(), configPart.getPlaintextPassword(), valueIfUnset, valueIfPasswordEmpty, valueIfPasswordSet)); ret.put(PerforceEnvironment.P4CHARSET, ! configPart.hasDefaultCharsetSet() || configPart.getDefaultCharset() == null ? valueIfUnset : configPart.getDefaultCharset()); ret.put(PerforceEnvironment.P4IGNORE, ! configPart.hasIgnoreFileNameSet() || configPart.getIgnoreFileName() == null ? valueIfUnset : configPart.getIgnoreFileName()); ret.put(PerforceEnvironment.P4CLIENT, ! configPart.hasClientnameSet() || configPart.getClientname() == null ? valueIfUnset : configPart.getClientname()); ret.put(PerforceEnvironment.P4HOST, ! configPart.hasClientHostnameSet() || configPart.getClientHostname() == null ? valueIfUnset : configPart.getClientHostname()); ret.put(PerforceEnvironment.P4LOGINSSO, ! configPart.hasLoginSsoSet() || configPart.getLoginSso() == null ? valueIfUnset : configPart.getLoginSso()); return ret; }  @NotNull static Map<String, String> toProperties(@NotNull ConfigPart configPart, @Nullable String valueIfUnset,
            @Nullable String valueIfPasswordEmpty, @Nullable String valueIfPasswordSet); @NotNull static Map<String, String> toProperties(@NotNull ServerConfig config, @Nullable String valueIfUnset,
            @Nullable String valueIfPasswordStored, @Nullable String valueIfPasswordNotStored); @NotNull static Map<String, String> toProperties(@NotNull ClientConfig config, @Nullable String valueIfUnset,
            @Nullable String valueIfPasswordStored, @Nullable String valueIfPasswordNotStored);  }
@Test void toProperties_configPartEmpty() { Map<String, String> props = ConfigPropertiesUtil.toProperties( new MockConfigPart(), "<unset>", "<ep>", "<set>" ); assertThat(props, mapContainsAll( new Pair<>("P4PORT", "<unset>"), new Pair<>("P4TRUST", "<unset>"), new Pair<>("P4USER", "<unset>"), new Pair<>("P4TICKETS", "<unset>"), new Pair<>("P4FINGERPRINT", "<unset>"), new Pair<>("P4PASSWD", "<unset>"), new Pair<>("P4CHARSET", "<unset>"), new Pair<>("P4IGNORE", "<unset>"), new Pair<>("P4CLIENT", "<unset>"), new Pair<>("P4HOST", "<unset>"), new Pair<>("P4LOGINSSO", "<unset>") )); }
@SuppressWarnings("unchecked") @Test void toProperties_configPartEmptyPassword() { Map<String, String> props = ConfigPropertiesUtil.toProperties( new MockConfigPart() .withPassword(""), "<unset>", "<ep>", "<set>" ); assertThat(props, mapContainsAll( new Pair<>("P4PORT", "<unset>"), new Pair<>("P4TRUST", "<unset>"), new Pair<>("P4USER", "<unset>"), new Pair<>("P4TICKETS", "<unset>"), new Pair<>("P4FINGERPRINT", "<unset>"), new Pair<>("P4PASSWD", "<ep>"), new Pair<>("P4CHARSET", "<unset>"), new Pair<>("P4IGNORE", "<unset>"), new Pair<>("P4CLIENT", "<unset>"), new Pair<>("P4HOST", "<unset>"), new Pair<>("P4LOGINSSO", "<unset>") )); }
@Test void toProperties_configPartSet() { File authFile = new File("auth.txt"); File trustFile = new File("trust.txt"); Map<String, String> props = ConfigPropertiesUtil.toProperties( new MockConfigPart() .withUsername("username") .withServerName("servername") .withClientname("clientname") .withConfigProblems(createError()) .withPassword("password") .withServerFingerprint("fingerprint") .withAuthTicketFile(authFile) .withTrustTicketFile(trustFile) .withClientHostname("hostname") .withDefaultCharset("charset") .withIgnoreFileName("ignore") .withLoginSso("sso") .withRequiresUserEnteredPassword(true) .withSourceName("s"), "<unset>", "<ep>", "<set>" ); assertThat(props, mapContainsAll( new Pair<>("P4PORT", portName("servername")), new Pair<>("P4TRUST", "trust.txt"), new Pair<>("P4USER", "username"), new Pair<>("P4TICKETS", "auth.txt"), new Pair<>("P4FINGERPRINT", "fingerprint"), new Pair<>("P4PASSWD", "<unset>"), new Pair<>("P4CHARSET", "charset"), new Pair<>("P4IGNORE", "ignore"), new Pair<>("P4CLIENT", "clientname"), new Pair<>("P4HOST", "hostname"), new Pair<>("P4LOGINSSO", "sso") )); }
@Test void toProperties_ServerConfig_filled() { File authFile = new File("auth.txt"); File trustFile = new File("trust.txt"); Map<String, String> props = ConfigPropertiesUtil.toProperties( ServerConfig.createFrom( new MockConfigPart() .withUsername("username") .withServerName("servername") .withClientname("clientname") .withConfigProblems(createWarning()) .withPassword("password") .withServerFingerprint("fingerprint") .withAuthTicketFile(authFile) .withTrustTicketFile(trustFile) .withClientHostname("hostname") .withDefaultCharset("charset") .withIgnoreFileName("ignore") .withLoginSso("sso") .withRequiresUserEnteredPassword(true) .withSourceName("s") ), "<unset>", "<req>", "<nr>" ); assertThat(props, mapContainsAll( new Pair<>("P4PORT", portName("servername")), new Pair<>("P4TRUST", "trust.txt"), new Pair<>("P4TICKETS", "auth.txt"), new Pair<>("P4USER", "username"), new Pair<>("P4PASSWD", "<req>"), new Pair<>("P4FINGERPRINT", "fingerprint"), new Pair<>("P4LOGINSSO", "sso") )); }
@Test void toProperties_ServerConfig_empty() { Map<String, String> props = ConfigPropertiesUtil.toProperties( ServerConfig.createFrom( new MockConfigPart() .withUsername("username") .withServerName("servername") ), "<unset>", "<stored>", "<ns>" ); assertThat(props, mapContainsAll( new Pair<>("P4PORT", portName("servername")), new Pair<>("P4USER", "username"), new Pair<>("P4PASSWD", "<ns>"), new Pair<>("P4TRUST", "<unset>"), new Pair<>("P4TICKETS", "<unset>"), new Pair<>("P4FINGERPRINT", "<unset>"), new Pair<>("P4LOGINSSO", "<unset>") )); }
@Test void toProperties_ServerConfig_emptyPassword() { Map<String, String> props = ConfigPropertiesUtil.toProperties( ServerConfig.createFrom( new MockConfigPart() .withUsername("username") .withServerName("servername") .withPassword("") ), "<unset>", "<stored>", "<ns>" ); assertThat(props, mapContainsAll( new Pair<>("P4PORT", portName("servername")), new Pair<>("P4USER", "username"), new Pair<>("P4TRUST", "<unset>"), new Pair<>("P4TICKETS", "<unset>"), new Pair<>("P4FINGERPRINT", "<unset>"), new Pair<>("P4LOGINSSO", "<unset>"), new Pair<>("P4PASSWD", "<stored>") )); }
@Test void toProperties_ClientConfig_empty() { MockConfigPart part = new MockConfigPart() .withUsername("username") .withServerName("servername") .withClientname("client"); Map<String, String> props = ConfigPropertiesUtil.toProperties( ClientConfig.createFrom(ServerConfig.createFrom(part), part), "<unset>", "<stored>", "<ns>" ); assertThat(props, mapContainsAll( new Pair<>("P4PORT", portName("servername")), new Pair<>("P4CLIENT", "client"), new Pair<>("P4USER", "username"), new Pair<>("P4PASSWD", "<ns>"), new Pair<>("P4TRUST", "<unset>"), new Pair<>("P4TICKETS", "<unset>"), new Pair<>("P4FINGERPRINT", "<unset>"), new Pair<>("P4CHARSET", "<unset>"), new Pair<>("P4IGNORE", "<unset>"), new Pair<>("P4HOST", "<unset>"), new Pair<>("P4LOGINSSO", "<unset>") )); }
@Test void toProperties_ClientConfig_filled() { File authFile = new File("auth.txt"); File trustFile = new File("trust.txt"); MockConfigPart part = new MockConfigPart() .withUsername("username") .withPassword("pass") .withServerName("servername") .withClientname("client") .withTrustTicketFile(trustFile) .withAuthTicketFile(authFile) .withServerFingerprint("abcd") .withDefaultCharset("char") .withIgnoreFileName("ignore-these") .withClientHostname("c-host") .withLoginSso("log-sso.cmd -t my_auth"); Map<String, String> props = ConfigPropertiesUtil.toProperties( ClientConfig.createFrom(ServerConfig.createFrom(part), part), "<unset>", "<stored>", "<ns>" ); assertThat(props, mapContainsAll( new Pair<>("P4PORT", portName("servername")), new Pair<>("P4CLIENT", "client"), new Pair<>("P4USER", "username"), new Pair<>("P4PASSWD", "<stored>"), new Pair<>("P4TRUST", "trust.txt"), new Pair<>("P4TICKETS", "auth.txt"), new Pair<>("P4FINGERPRINT", "abcd"), new Pair<>("P4CHARSET", "char"), new Pair<>("P4IGNORE", "ignore-these"), new Pair<>("P4HOST", "c-host"), new Pair<>("P4LOGINSSO", "log-sso.cmd -t my_auth") )); }