DiskLruCache implements Closeable { private void validateKey(String key) { Matcher matcher = LEGAL_KEY_PATTERN.matcher(key); if (!matcher.matches()) { throw new IllegalArgumentException("keys must match regex [a-z0-9_-]{1,64}: \"" + key + "\""); } } private  DiskLruCache(File directory, int appVersion, int valueCount, long maxSize); static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize); synchronized Snapshot get(String key); Editor edit(String key); File getDirectory(); synchronized long getMaxSize(); synchronized void setMaxSize(long maxSize); synchronized long size(); synchronized boolean remove(String key); synchronized boolean isClosed(); synchronized void flush(); synchronized void close(); void delete();  }
@Test public void validateKey() throws Exception { String key = null; try { key = "has_space "; cache.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,64}: \"" + key + "\""); } try { key = "has_CR\r"; cache.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,64}: \"" + key + "\""); } try { key = "has_LF\n"; cache.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,64}: \"" + key + "\""); } try { key = "has_invalid/"; cache.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,64}: \"" + key + "\""); } try { key = "has_invalid\u2603"; cache.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,64}: \"" + key + "\""); } try { key = "this_is_way_too_long_this_is_way_too_long_this_is_way_too_long_this_is_way_too_long"; cache.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,64}: \"" + key + "\""); } key = "0123456789012345678901234567890123456789012345678901234567890123"; cache.edit(key).abort(); key = "abcdefghijklmnopqrstuvwxyz_0123456789"; cache.edit(key).abort(); key = "-20384573948576"; cache.edit(key).abort(); }