• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.google.protobuf;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 
5 import protobuf_unittest.UnittestProto.RedactedFields;
6 import protobuf_unittest.UnittestProto.TestNestedMessageRedaction;
7 import java.util.ArrayList;
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 import org.junit.runners.JUnit4;
12 
13 @RunWith(JUnit4.class)
14 public final class ProtobufToStringOutputTest extends DebugFormatTest {
15   RedactedFields message;
16 
17   @Before
setupTest()18   public void setupTest() {
19     message =
20         RedactedFields.newBuilder()
21             .setOptionalUnredactedString("foo")
22             .setOptionalRedactedString("bar")
23             .setOptionalRedactedMessage(
24                 TestNestedMessageRedaction.newBuilder().setOptionalUnredactedNestedString("foobar"))
25             .build();
26   }
27 
28   @Test
toStringFormat_defaultFormat()29   public void toStringFormat_defaultFormat() {
30     assertThat(message.toString())
31         .matches(
32             "optional_redacted_string: \"bar\"\n"
33                 + "optional_unredacted_string: \"foo\"\n"
34                 + "optional_redacted_message \\{\n"
35                 + "  optional_unredacted_nested_string: \"foobar\"\n"
36                 + "\\}\n");
37   }
38 
39   @Test
toStringFormat_testDebugFormat()40   public void toStringFormat_testDebugFormat() {
41     ProtobufToStringOutput.callWithDebugFormat(
42         () ->
43             assertThat(message.toString())
44                 .matches(
45                     String.format(
46                         "%soptional_redacted_string: %s\n"
47                             + "optional_unredacted_string: \"foo\"\n"
48                             + "optional_redacted_message \\{\n"
49                             + "  %s\n"
50                             + "\\}\n",
51                         UNSTABLE_PREFIX_MULTILINE, REDACTED_REGEX, REDACTED_REGEX)));
52   }
53 
54   @Test
toStringFormat_testTextFormat()55   public void toStringFormat_testTextFormat() {
56     ProtobufToStringOutput.callWithTextFormat(
57         () -> {
58           assertThat(message.toString())
59               .matches(
60                   "optional_redacted_string: \"bar\"\n"
61                       + "optional_unredacted_string: \"foo\"\n"
62                       + "optional_redacted_message \\{\n"
63                       + "  optional_unredacted_nested_string: \"foobar\"\n"
64                       + "\\}\n");
65         });
66   }
67 
68   @Test
toStringFormat_testProtoWrapperWithDebugFormat()69   public void toStringFormat_testProtoWrapperWithDebugFormat() {
70     ProtobufToStringOutput.callWithDebugFormat(
71         () -> {
72           ArrayList<RedactedFields> list = new ArrayList<>();
73           list.add(message);
74           assertThat(list.toString())
75               .matches(
76                   String.format(
77                       "\\[%soptional_redacted_string: %s\n"
78                           + "optional_unredacted_string: \"foo\"\n"
79                           + "optional_redacted_message \\{\n"
80                           + "  %s\n"
81                           + "\\}\n"
82                           + "\\]",
83                       UNSTABLE_PREFIX_MULTILINE, REDACTED_REGEX, REDACTED_REGEX));
84         });
85   }
86 
87   @Test
toStringFormat_testProtoWrapperWithTextFormat()88   public void toStringFormat_testProtoWrapperWithTextFormat() {
89     ProtobufToStringOutput.callWithTextFormat(
90         () -> {
91           ArrayList<RedactedFields> list = new ArrayList<>();
92           list.add(message);
93           assertThat(list.toString())
94               .matches(
95                   "\\[optional_redacted_string: \"bar\"\n"
96                       + "optional_unredacted_string: \"foo\"\n"
97                       + "optional_redacted_message \\{\n"
98                       + "  optional_unredacted_nested_string: \"foobar\"\n"
99                       + "\\}\n"
100                       + "\\]");
101         });
102   }
103 }
104