Github user afs commented on a diff in the pull request:
https://github.com/apache/jena/pull/255#discussion_r118848329
--- Diff:
jena-fuseki2/jena-fuseki-embedded/src/test/java/org/apache/jena/fuseki/embedded/FusekiTestAuth.java
---
@@ -0,0 +1,182 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.fuseki.embedded;
+
+import static org.apache.jena.fuseki.server.FusekiEnv.choosePort;
+
+import java.util.Objects;
+
+import org.apache.jena.atlas.web.HttpException;
+import org.apache.jena.fuseki.FusekiException;
+import org.apache.jena.sparql.core.DatasetGraph ;
+import org.apache.jena.sparql.core.DatasetGraphFactory;
+import org.apache.jena.web.HttpSC;
+import org.eclipse.jetty.security.*;
+import org.eclipse.jetty.security.authentication.BasicAuthenticator;
+import org.eclipse.jetty.util.security.Constraint;
+import org.eclipse.jetty.util.security.Credential;
+import org.eclipse.jetty.util.security.Password;
+import org.junit.Assert;
+
+/**
+ * Testing HTTP athentication.
+ * <p>
+ * {@code FusekiAuth} provides helper code for before/after (any of
suite/class/test).
+ * The pattern of usage is:
+ * <pre>
+ *
+ * @BeforeClass
+ * public static void beforeClassAuth() {
+ * SecurityHandler sh = FusekiAuth.makeSimpleSecurityHandler("/*",
"USER", "PASSWORD");
+ * FusekiAuth.setupServer(true, sh);
+ * }
+ *
+ * @AfterClass
+ * public static void afterClassAuth() {
+ * FusekiAuth.teardownServer();
+ * // Clear up any pooled connections.
+ * HttpOp.setDefaultHttpClient(HttpOp.createPoolingHttpClient());
+ * }
+ *
+ * @Test
+ * public void myAuthTest() {
+ * BasicCredentialsProvider credsProvider = new
BasicCredentialsProvider();
+ * Credentials credentials = new UsernamePasswordCredentials("USER",
"PASSWORD");
+ * credsProvider.setCredentials(AuthScope.ANY, credentials);
+ * HttpClient client =
HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
+ * try (TypedInputStream in =
HttpOp.execHttpGet(ServerCtl.urlDataset(), "* /*", client, null)) {}
+ * }
+ *
+ * @Test
+ * public void myAuthTestRejected() {
+ * BasicCredentialsProvider credsProvider = new
BasicCredentialsProvider();
+ * Credentials credentials = new UsernamePasswordCredentials("USER",
"PASSWORD");
+ * credsProvider.setCredentials(AuthScope.ANY, credentials);
+ * HttpClient client =
HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
+ * try (TypedInputStream in =
HttpOp.execHttpGet(ServerCtl.urlDataset(), "* /*", client, null)) {}
+ * catch (HttpException ex) {
+ * throw assertAuthHttpException(ex);
+ * }
+ * }
+ * </pre>
+ *
+ * {@code @BeforeClass} can be {@code @Before} but server stop-start is
expensive so a
+ * large test suite may end up quite slow.
+ */
+public class FusekiTestAuth {
+ private static int currentPort = choosePort() ;
+
+ public static int port() {
+ return currentPort ;
+ }
+
+ static boolean CLEAR_DSG_DIRECTLY = true ;
+ static private DatasetGraph dsgTesting ;
+
+ // Abstraction that runs a SPARQL server for tests.
+ public static final String urlRoot() { return
"http://localhost:"+port()+"/" ; }
+ public static final String datasetPath() { return "/dataset" ; }
+ public static final String urlDataset() { return
"http://localhost:"+port()+datasetPath() ; }
+ public static final DatasetGraph getDataset() { return dsgTesting ; }
+
+ public static final String serviceUpdate() { return
"http://localhost:"+port()+datasetPath()+"/update" ; }
+ public static final String serviceQuery() { return
"http://localhost:"+port()+datasetPath()+"/query" ; }
+ public static final String serviceGSP() { return
"http://localhost:"+port()+datasetPath()+"/data" ; }
+
+ private static FusekiEmbeddedServer server ;
+
+ public static void setupServer(boolean updateable, SecurityHandler sh)
{
+ // Clean datasets.
+ dsgTesting = DatasetGraphFactory.createTxnMem();
+ server = FusekiEmbeddedServer.create()
+ .add(datasetPath(), dsgTesting)
--- End diff --
Speed but also for clear-up. No files get left on disk to interfere with
other tests; especially if the server takes awhile to stop properly and release
all its resources.
We could add `void setupServer(boolean updateable, DatasetGraph dsg,
SecurityHandler sh)` for the general case.
`FusekiTestServer` is also a in-memory transactional dataset and there it
is much more difficult to inject a specific implementation because the
lifecycle is driven by JUnit.
The example on the JIRA shows it is not necessary to use these helper
classes. They are helpful when a lot of small tests are run, ensuring a
balance between careful clean-up and speed.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---