IgDiskCache { private static void validateKey(String key) { Matcher matcher = LEGAL_KEY_PATTERN.matcher(key); if (!matcher.matches()) { throw new IllegalArgumentException( "keys must match regex " + STRING_KEY_PATTERN + ": \"" + key + "\""); } } IgDiskCache(File directory);  IgDiskCache(File directory, Executor serialExecutor);  IgDiskCache(File directory, long maxSizeInBytes);  IgDiskCache(File directory, long maxSizeInBytes, Executor serialExecutor);  IgDiskCache(File directory, long maxSizeInBytes, int maxCount);  IgDiskCache(File directory, long maxSizeInBytes, int maxCount, Executor serialExecutor); boolean has(String key); OptionalStream<SnapshotInputStream> get(String key); OptionalStream<EditorOutputStream> edit(String key); void remove(String key); void flush(); void close(); File getDirectory(); long getMaxSizeInBytes(); int getMaxCount(); void setMaxSizeInBytes(long maxSizeInBytes); long size(); int count(); final String getHitRateString();  }
@SuppressLint("DeadVariable") @Test public void validateKey() throws Exception { String key = null; try { key = "has_space "; mCache.edit(key); fail("Exepcting an IllegalArgumentException as the key was invalid."); } catch (IllegalArgumentException iae) { assertThat(iae.getMessage()).isEqualTo( "keys must match regex [a-z0-9_-]{1,120}: \"" + key + "\""); } try { key = "has_CR\r"; mCache.edit(key); fail("Exepcting an IllegalArgumentException as the key was invalid."); } catch (IllegalArgumentException iae) { assertThat(iae.getMessage()).isEqualTo( "keys must match regex [a-z0-9_-]{1,120}: \"" + key + "\""); } try { key = "has_LF\n"; mCache.edit(key); fail("Exepcting an IllegalArgumentException as the key was invalid."); } catch (IllegalArgumentException iae) { assertThat(iae.getMessage()).isEqualTo( "keys must match regex [a-z0-9_-]{1,120}: \"" + key + "\""); } try { key = "has_invalid/"; mCache.edit(key); fail("Exepcting an IllegalArgumentException as the key was invalid."); } catch (IllegalArgumentException iae) { assertThat(iae.getMessage()).isEqualTo( "keys must match regex [a-z0-9_-]{1,120}: \"" + key + "\""); } try { key = "has_invalid\u2603"; mCache.edit(key); fail("Exepcting an IllegalArgumentException as the key was invalid."); } catch (IllegalArgumentException iae) { assertThat(iae.getMessage()).isEqualTo( "keys must match regex [a-z0-9_-]{1,120}: \"" + key + "\""); } try { key = "this_is_way_too_long_this_is_way_too_long_this_is_way_too_long_" + "this_is_way_too_long_this_is_way_too_long_this_is_way_too_long"; mCache.edit(key); fail("Exepcting an IllegalArgumentException as the key was too long."); } catch (IllegalArgumentException iae) { assertThat(iae.getMessage()).isEqualTo( "keys must match regex [a-z0-9_-]{1,120}: \"" + key + "\""); } key = "0123456789012345678901234567890123456789012345678901234567890123456789" + "01234567890123456789012345678901234567890123456789"; abortOptionalOutputStream(mCache.edit(key)); key = "abcdefghijklmnopqrstuvwxyz_0123456789"; abortOptionalOutputStream(mCache.edit(key)); key = "-20384573948576"; abortOptionalOutputStream(mCache.edit(key)); }