Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.cloud.bigquery;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static java.time.temporal.ChronoUnit.MICROS;

import com.google.api.client.util.Data;
Expand Down Expand Up @@ -201,10 +200,16 @@ public double getDoubleValue() {
@SuppressWarnings("unchecked")
public boolean getBooleanValue() {
String stringValue = getStringValue();
checkState(
stringValue.equalsIgnoreCase("true") || stringValue.equalsIgnoreCase("false"),
"Field value is not of boolean type");
return Boolean.parseBoolean(stringValue);
// Validate both literals explicitly rather than delegating directly to Boolean.parseBoolean,
// which silently returns false for non-boolean strings instead of throwing
// IllegalStateException.
if ("true".equalsIgnoreCase(stringValue)) {
return true;
}
if ("false".equalsIgnoreCase(stringValue)) {
return false;
}
throw new IllegalStateException("Field value is not of boolean type");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.api.client.util.Data;
Expand Down Expand Up @@ -234,4 +235,15 @@ public void testParseCanonicalInterval() {
System.out.println(FieldValue.parseCanonicalInterval(entry.getKey()));
}
}

@Test
public void testGetBooleanValue() {
assertTrue(FieldValue.of(FieldValue.Attribute.PRIMITIVE, "true").getBooleanValue());
assertTrue(FieldValue.of(FieldValue.Attribute.PRIMITIVE, "TRUE").getBooleanValue());
assertFalse(FieldValue.of(FieldValue.Attribute.PRIMITIVE, "false").getBooleanValue());
assertFalse(FieldValue.of(FieldValue.Attribute.PRIMITIVE, "False").getBooleanValue());
assertThrows(
IllegalStateException.class,
() -> FieldValue.of(FieldValue.Attribute.PRIMITIVE, "not-a-bool").getBooleanValue());
}
}
Loading