File tree Expand file tree Collapse file tree
main/java/com/google/cloud
test/java/com/google/cloud Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -280,10 +280,12 @@ public String strValue() {
280280 public static Identity valueOf (String identityStr ) {
281281 String [] info = identityStr .split (":" );
282282 Type type = Type .valueOf (CaseFormat .LOWER_CAMEL .to (CaseFormat .UPPER_UNDERSCORE , info [0 ]));
283- if (type . equals ( Type . ALL_AUTHENTICATED_USERS ) || type . equals ( Type . ALL_USERS ) ) {
283+ if (info . length == 1 ) {
284284 return new Identity (type , null );
285+ } else if (info .length == 2 ){
286+ return new Identity (type , info [1 ]);
285287 } else {
286- return new Identity ( type , checkNotNull ( info [ 1 ]) );
288+ throw new IllegalArgumentException ( "Illegal identity string: \" " + identityStr + " \" " );
287289 }
288290 }
289291}
Original file line number Diff line number Diff line change @@ -66,6 +66,9 @@ public EnumT valueOfStrict(String constant) {
6666 * Get the enum object for the given String constant, and allow unrecognized values.
6767 */
6868 public EnumT valueOf (String constant ) {
69+ if (constant == null || constant .isEmpty ()) {
70+ throw new IllegalArgumentException ("Empty enum constants not allowed." );
71+ }
6972 EnumT value = knownValues .get (constant );
7073 if (value != null ) {
7174 return value ;
Original file line number Diff line number Diff line change @@ -142,6 +142,21 @@ public void testIdentityToAndFromPb() {
142142 compareIdentities (PROJECT_VIEWER , Identity .valueOf (PROJECT_VIEWER .strValue ()));
143143 }
144144
145+ @ Test (expected = IllegalArgumentException .class )
146+ public void testValueOfEmpty () {
147+ Identity .valueOf ("" );
148+ }
149+
150+ @ Test (expected = IllegalArgumentException .class )
151+ public void testValueOfThreePart () {
152+ Identity .valueOf ("a:b:c" );
153+ }
154+
155+ @ Test
156+ public void testUnrecognizedToString () {
157+ assertEquals ("a:b" , Identity .valueOf ("a:b" ).strValue ());
158+ }
159+
145160 private void compareIdentities (Identity expected , Identity actual ) {
146161 assertEquals (expected , actual );
147162 assertEquals (expected .getType (), actual .getType ());
You can’t perform that action at this time.
0 commit comments