@Theory public void theoryNonDirectoriesHaveFileWithSameContents(String path) throws Exception { Resource res = getResource(path); assumeThat(res, not(directory())); byte[] test = {42, 29, 32, 120, 69, 0, 1}; try (OutputStream ostream = res.out()) { ostream.write(test); } byte[] result = new byte[test.length]; try (InputStream istream = new FileInputStream(res.file())) { istream.read(result); assertThat(istream.read(), is(-1)); } assertThat(result, equalTo(test)); }
private boolean nullsOk() { Theory annotation = testMethod.getMethod().getAnnotation( Theory.class); if (annotation == null) { return false; } return annotation.nullsAccepted(); }
@Theory public void theoryChildrenKnowTheirParents(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); Collection<Resource> children = res.list(); assumeThat(children, not(empty())); // Make sure this resource has children for (Resource child : children) { Resource parent = child.parent(); assertThat(parent, equalTo(res)); } }
@Theory public void theoryDoubleClose(String path) throws Exception { final Resource res = getResource(path); assumeThat(res, is(resource())); OutputStream os = res.out(); os.close(); os.close(); }
@Theory public void theoryMultipleOutputStreamsAreSafe(String path) throws Exception { final Resource res = getResource(path); assumeThat(res, is(resource())); final byte[] thread1Content = "This is the content for thread 1".getBytes(); final byte[] thread2Content = "Thread 2 has this content".getBytes(); try (OutputStream out1 = res.out()) { try (OutputStream out2 = res.out()) { for (int i = 0; i < thread1Content.length || i < thread2Content.length; i++) { if (i < thread1Content.length) { out1.write(thread1Content[i]); } if (i < thread2Content.length) { out2.write(thread2Content[i]); } } } } final byte[] resultContent; try (InputStream in = res.in()) { resultContent = IOUtils.toByteArray(in); } // 2 streams being written to concurrently should result in the resource containing // what was written to one of the two streams. assertThat(resultContent, anyOf(equalTo(thread1Content), equalTo(thread2Content))); }
@Theory public void headerExchange(NetworkLayerFactory serverFactory, NetworkLayerFactory clientFactory) throws Exception { Random entropy = new Random(); .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink())) .filter(new ConnectionHeadersFilterLayer(clientExpectedHeaders, new ConnectionHeadersFilterLayer.Listener() { .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink())) .filter(new ConnectionHeadersFilterLayer(serverExpectedHeaders, new ConnectionHeadersFilterLayer.Listener() { data.flip(); server.get().send(data); client.get().awaitByteContent(is(expected)); assertThat(client.get().asByteArray(), is(expected)); assertThat(serverActualHeaders.get(1000, TimeUnit.MICROSECONDS), is(serverExpectedHeaders)); assertThat(clientActualHeaders.get(1000, TimeUnit.MICROSECONDS), is(clientExpectedHeaders));
@Theory public void theoryAddingFileToDirectoryAddsResource(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); File dir = res.dir(); File file = new File(dir, "newFileCreatedDirectly"); assumeTrue(file.createNewFile()); Resource child = getResource(Paths.path(res.path(), "newFileCreatedDirectly")); Collection<Resource> children = res.list(); assertThat(child, is(defined())); assertThat(children, hasItem(child)); }
@Theory public void hashCodeMustFollowValues( ValueProducer values, Factory<PrimitiveCollection> factory ) assumeTrue( values.isApplicable( factory ) ); try ( PrimitiveCollection a = factory.newInstance() ) int o = a.hashCode(); assertThat( "0 elm hashcode equal", o, is( i ) ); assertThat( "1 elm hashcode equal", n, is( j ) ); assertThat( "2 elm hashcode equal", m, is( k ) ); assertThat( "3 elm hashcode distinct", l, not( isOneOf( i, j, k, m, n, o ) ) ); assertThat( "2 elm hashcode distinct", k, not( isOneOf( i, j, l, n, o ) ) );
@Theory public void theoryDirectoriesHaveNoIstreams(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); exception.expect(IllegalStateException.class); res.in().close(); }
@Theory public void clientRefuses(NetworkLayerFactory serverFactory, NetworkLayerFactory clientFactory) throws Exception { ProtocolStack<IOBufferMatcher> client = ProtocolStack .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink())) .filter(new ConnectionHeadersFilterLayer(Collections.<String, String>emptyMap(), new ConnectionHeadersFilterLayer.Listener() { .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink())) .filter(new ConnectionHeadersFilterLayer(Collections.<String, String>emptyMap(), new ConnectionHeadersFilterLayer.Listener() { allOf(instanceOf(ConnectionRefusalException.class), not(instanceOf(PermanentConnectionRefusalException.class)))); server.get().awaitClose(); assertThat(server.get().getCloseCause(),
@Theory public void theoryRenamedResourcesAreEquivalent(String path) throws Exception { final Resource res = getResource(path); assumeThat(res, resource()); final byte[] expectedContent; try (InputStream in = res.in()) { expectedContent = IOUtils.toByteArray(in); } final Resource target = getUndefined(); assertThat(res.renameTo(target), is(true)); assertThat(target, resource()); final byte[] resultContent; try (InputStream in = target.in()) { resultContent = IOUtils.toByteArray(in); } assertThat(resultContent, equalTo(expectedContent)); }
@Theory public void theoryParentsKnowTheirChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); Resource parent = res.parent(); assumeThat(path, parent, notNullValue()); // Make sure this resource has a parent Collection<Resource> result = parent.list(); assertThat(path, result, hasItem(res)); // this assumed equals was written! }
@Theory public void theoryLeavesHaveEmptyListOfChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(resource())); Collection<Resource> result = res.list(); assertThat(result, empty()); }
@Theory public void testSingleton(KeyData data) { assumeThat(data, notNullValue()); long key = data.getKey(); // key to use long low = data.getLow(); // unused low key long high = data.getAfter(1); // unused high key long[] rawKeys = {key}; SortedKeyIndex keys = SortedKeyIndex.wrap(rawKeys, 1); assertThat(keys.size(), equalTo(1)); assertThat(keys.size(), equalTo(1)); assertThat(keys.keySet(), hasSize(1)); assertThat(keys.tryGetIndex(key), equalTo(0)); assertThat(keys.tryGetIndex(low), lessThan(0)); assertThat(keys.tryGetIndex(high), lessThan(0)); assertThat(keys.getKeyList(), contains(key)); }
@Theory public void theoryParentIsDirectory(String path) throws Exception { Resource res = getResource(path); Resource parent = res.parent(); assumeThat(path + " not root", parent, notNullValue()); if (res.getType() != Type.UNDEFINED) { assertThat(path + " directory", parent, is(directory())); } }
@Theory public void theoryRecursiveDelete(String path) throws Exception { final Resource res = getResource(path); assumeThat(res, is(directory())); assumeThat(res, is(directory())); Collection<Resource> result = res.list(); assumeThat(result.size(), greaterThan(0)); assertTrue(res.delete()); }
@Theory public void theoryDirectoriesHaveChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); Collection<Resource> result = res.list(); assertThat(result, notNullValue()); }
@Theory public void serverRejectsClient(NetworkLayerFactory serverFactory, NetworkLayerFactory clientFactory) throws Exception { Logger.getLogger(name.getMethodName()).log( clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink())) .filter(new SSLEngineFilterLayer(clientEngine, null)) .build(new IOBufferMatcherLayer()); serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink())) .filter(new SSLEngineFilterLayer(serverEngine, new SSLEngineFilterLayer.Listener() { @Override Logger.getLogger(name.getMethodName()).log(Level.INFO, "Waiting for server close"); serverMatcher.awaitClose(); assertThat(clientMatcher.getCloseCause(), instanceOf(ClosedChannelException.class)); assertThat(serverMatcher.getCloseCause(), instanceOf(ConnectionRefusalException.class)); Logger.getLogger(name.getMethodName()).log(Level.INFO, "Done");