### Question:
AttachmentURLStreamHandler extends URLStreamHandler implements ExtendedURLStreamHandler { @Override protected URLConnection openConnection(URL url) throws IOException { AttachmentReference attachmentReference = this.attachmentReferenceResolver.resolve( getAttachmentReference(url)); return new AttachmentURLConnection(url, attachmentReference, this.documentAccessBridge); }  @Override String getProtocol();  }### Answer:
@Test void invalidAttachmentJarURL() throws Exception { URL url = new URL(null, "http: try { url.openConnection(); fail("Should have thrown an exception here"); } catch (RuntimeException expected) { assertEquals("An attachment JAR URL should start with [attachmentjar: expected.getMessage()); } }
@Test void attachmentJarURL() throws Exception { URL url = new URL(null, "attachmentjar: final AttachmentReference attachmentReference = new AttachmentReference("filename", new DocumentReference("wiki", "space", "page")); when(this.arf.resolve("Space.Page@filename")).thenReturn(attachmentReference); when(this.dab.getAttachmentContent(attachmentReference)) .thenReturn(new ByteArrayInputStream("content".getBytes())); URLConnection connection = url.openConnection(); InputStream input = null; try { connection.connect(); input = connection.getInputStream(); assertEquals("content", IOUtils.toString(input)); } finally { if (input != null) { input.close(); } } }
@Test void attachmentJarURLWithEncodedChars() throws Exception { URL url = new URL(null, "attachmentjar: url.openConnection(); verify(this.arf).resolve("some page"); }