Search in sources :

Example 81 with RuntimeEnvironment

use of org.opensolaris.opengrok.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.

the class PageConfigTest method testGetResourceFileList.

/**
 * Testing the root of /xref for authorization filtering.
 */
@Test
public void testGetResourceFileList() {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    // backup original values
    String oldSourceRootPath = env.getSourceRootPath();
    AuthorizationFramework oldAuthorizationFramework = env.getAuthorizationFramework();
    Map<String, Project> oldProjects = env.getProjects();
    // Set up the source root directory containing some projects.
    env.setSourceRoot(repository.getSourceRoot());
    env.setProjectsEnabled(true);
    // Enable projects.
    for (String file : new File(repository.getSourceRoot()).list()) {
        Project proj = new Project(file);
        proj.setIndexed(true);
        env.getProjects().put(file, proj);
    }
    HttpServletRequest req = createRequest("/source", "/xref", "");
    PageConfig cfg = PageConfig.get(req);
    List<String> allFiles = new ArrayList<>(cfg.getResourceFileList());
    /**
     * Check if there are some files (the "5" here is just a sufficient
     * value for now which won't break any future repository tests) without
     * any authorization.
     */
    assertTrue(allFiles.size() > 5);
    assertTrue(allFiles.contains("git"));
    assertTrue(allFiles.contains("mercurial"));
    /**
     * Now set up the same projects with authorization plugin enabling only
     * some of them.
     * <pre>
     *  - disabling "git"
     *  - disabling "mercurial"
     * </pre>
     */
    env.setAuthorizationFramework(new AuthorizationFramework());
    env.getAuthorizationFramework().reload();
    env.getAuthorizationFramework().getStack().add(new AuthorizationPlugin(AuthControlFlag.REQUIRED, new TestPlugin() {

        @Override
        public boolean isAllowed(HttpServletRequest request, Project project) {
            return !project.getName().startsWith("git") && !project.getName().startsWith("mercurial");
        }
    }));
    req = createRequest("/source", "/xref", "");
    cfg = PageConfig.get(req);
    List<String> filteredFiles = new ArrayList<>(cfg.getResourceFileList());
    // list subtraction - retains only disabled files
    allFiles.removeAll(filteredFiles);
    assertEquals(2, allFiles.size());
    assertTrue(allFiles.contains("git"));
    assertTrue(allFiles.contains("mercurial"));
    // restore original values
    env.setAuthorizationFramework(oldAuthorizationFramework);
    env.setSourceRoot(oldSourceRootPath);
    env.setProjects(oldProjects);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Project(org.opensolaris.opengrok.configuration.Project) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) AuthorizationFramework(org.opensolaris.opengrok.authorization.AuthorizationFramework) ArrayList(java.util.ArrayList) AuthorizationPlugin(org.opensolaris.opengrok.authorization.AuthorizationPlugin) TestPlugin(org.opensolaris.opengrok.authorization.TestPlugin) File(java.io.File) Test(org.junit.Test)

Aggregations

RuntimeEnvironment (org.opensolaris.opengrok.configuration.RuntimeEnvironment)81 File (java.io.File)31 Project (org.opensolaris.opengrok.configuration.Project)27 IOException (java.io.IOException)23 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)17 TestRepository (org.opensolaris.opengrok.util.TestRepository)13 ForbiddenSymlinkException (org.opensolaris.opengrok.util.ForbiddenSymlinkException)12 BeforeClass (org.junit.BeforeClass)11 Ctags (org.opensolaris.opengrok.analysis.Ctags)6 BufferedReader (java.io.BufferedReader)5 FileNotFoundException (java.io.FileNotFoundException)5 InputStreamReader (java.io.InputStreamReader)5 ParseException (java.text.ParseException)5 DateFormat (java.text.DateFormat)4 Date (java.util.Date)4 RepositoryInfo (org.opensolaris.opengrok.history.RepositoryInfo)4 OutputStreamWriter (java.io.OutputStreamWriter)3 ConnectException (java.net.ConnectException)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3