Fixing tests for the hudson plugin.
authorjlahoda
Thu, 03 Nov 2016 17:35:21 +0100
changeset 10257da1c5d9c411
parent 1024 b4b86016d699
child 1026 55d2c69abb50
Fixing tests for the hudson plugin.
remoting/server/hudson/pom.xml
remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java
remoting/server/hudson/src/test/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilderTest.java
     1.1 --- a/remoting/server/hudson/pom.xml	Sat Oct 29 12:07:58 2016 +0200
     1.2 +++ b/remoting/server/hudson/pom.xml	Thu Nov 03 17:35:21 2016 +0100
     1.3 @@ -4,7 +4,7 @@
     1.4    <parent>
     1.5      <groupId>org.jenkins-ci.plugins</groupId>
     1.6      <artifactId>plugin</artifactId>
     1.7 -    <version>1.396</version><!-- which version of Jenkins is this plugin built against? -->
     1.8 +    <version>1.424</version><!-- which version of Jenkins is this plugin built against? -->
     1.9    </parent>
    1.10  
    1.11    <groupId>org.netbeans.modules.jackpot30.hudson</groupId>
    1.12 @@ -12,6 +12,10 @@
    1.13    <version>1.0-SNAPSHOT</version>
    1.14    <packaging>hpi</packaging>
    1.15  
    1.16 +  <properties>
    1.17 +    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    1.18 +  </properties>
    1.19 +
    1.20    <!-- get every artifact through repo.jenkins-ci.org, which proxies all the artifacts that we need -->
    1.21    <repositories>
    1.22      <repository>
    1.23 @@ -27,21 +31,4 @@
    1.24      </pluginRepository>
    1.25    </pluginRepositories>
    1.26  
    1.27 -  <build>
    1.28 -    <plugins>
    1.29 -      <plugin>
    1.30 -        <groupId>org.jvnet.hudson.tools</groupId>
    1.31 -        <artifactId>maven-hpi-plugin</artifactId>
    1.32 -        <version>1.48</version>
    1.33 -        <configuration>
    1.34 -          <webResources>
    1.35 -            <resource>
    1.36 -              <directory>data</directory>
    1.37 -            </resource>
    1.38 -          </webResources>
    1.39 -        </configuration>
    1.40 -      </plugin>
    1.41 -    </plugins>
    1.42 -  </build>
    1.43 -
    1.44  </project>
     2.1 --- a/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java	Sat Oct 29 12:07:58 2016 +0200
     2.2 +++ b/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java	Thu Nov 03 17:35:21 2016 +0100
     2.3 @@ -131,12 +131,6 @@
     2.4  
     2.5      @Override
     2.6      public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
     2.7 -        boolean success = doIndex(getDescriptor().getCacheDir(), build, launcher, listener);
     2.8 -
     2.9 -        return success;
    2.10 -    }
    2.11 -
    2.12 -    protected/*tests*/ boolean doIndex(File cacheDir, AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
    2.13          IndexingTool t = findSelectedTool();
    2.14  
    2.15          if (t == null) {
    2.16 @@ -185,19 +179,6 @@
    2.17          return true;
    2.18      }
    2.19  
    2.20 -    private void dumpToFile(File target, Set<String> files) throws IOException {
    2.21 -        Writer out = new OutputStreamWriter(new FileOutputStream(target));
    2.22 -
    2.23 -        try {
    2.24 -            for (String f : files) {
    2.25 -                out.write(f);
    2.26 -                out.write("\n");
    2.27 -            }
    2.28 -        } finally {
    2.29 -            out.close();
    2.30 -        }
    2.31 -    }
    2.32 -
    2.33      public IndexingTool findSelectedTool() {
    2.34          for (IndexingTool t : getDescriptor().getIndexingTools()) {
    2.35              if (toolName.equals(t.getName())) return t;
    2.36 @@ -256,7 +237,7 @@
    2.37      }
    2.38      
    2.39      @Extension // this marker indicates Hudson that this is an implementation of an extension point.
    2.40 -    public static final class DescriptorImpl extends Descriptor<Builder> {
    2.41 +    public static class DescriptorImpl extends Descriptor<Builder> { //non-final for tests
    2.42  
    2.43          private File cacheDir;
    2.44          private String webVMOptions;
     3.1 --- a/remoting/server/hudson/src/test/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilderTest.java	Sat Oct 29 12:07:58 2016 +0200
     3.2 +++ b/remoting/server/hudson/src/test/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilderTest.java	Thu Nov 03 17:35:21 2016 +0100
     3.3 @@ -41,22 +41,28 @@
     3.4  
     3.5  import hudson.FilePath;
     3.6  import hudson.Launcher;
     3.7 +import hudson.Launcher.ProcStarter;
     3.8 +import hudson.Proc;
     3.9  import hudson.model.AbstractBuild;
    3.10  import hudson.model.BuildListener;
    3.11  import hudson.model.FreeStyleProject;
    3.12 -import hudson.scm.SubversionSCM;
    3.13 -import hudson.scm.SubversionSCM.ModuleLocation;
    3.14 -import hudson.util.ArgumentListBuilder;
    3.15 -import hudson.util.StreamTaskListener;
    3.16 +import hudson.model.Hudson;
    3.17 +import hudson.model.Result;
    3.18 +import hudson.remoting.Channel;
    3.19 +import hudson.scm.NullSCM;
    3.20 +import hudson.scm.SCM;
    3.21 +import java.io.ByteArrayInputStream;
    3.22  import java.io.File;
    3.23  import java.io.FileOutputStream;
    3.24  import java.io.IOException;
    3.25 +import java.io.InputStream;
    3.26 +import java.io.OutputStream;
    3.27 +import java.util.ArrayList;
    3.28  import java.util.Arrays;
    3.29 -import java.util.Collections;
    3.30 -import java.util.Set;
    3.31 +import java.util.List;
    3.32 +import java.util.Map;
    3.33  import org.jvnet.hudson.test.HudsonHomeLoader;
    3.34  import org.jvnet.hudson.test.HudsonTestCase;
    3.35 -import static org.junit.Assert.*;
    3.36  import org.xml.sax.SAXException;
    3.37  
    3.38  /**
    3.39 @@ -75,7 +81,7 @@
    3.40          super.setUp();
    3.41  
    3.42          checkoutDir = HudsonHomeLoader.NEW.allocate();
    3.43 -        repositoryDir = HudsonHomeLoader.NEW.allocate();
    3.44 +        toolDir = HudsonHomeLoader.NEW.allocate();
    3.45      }
    3.46  
    3.47      @Override
    3.48 @@ -84,27 +90,28 @@
    3.49  
    3.50          //XXX: some of the working directories seems to be kept by the testing infra, workarounding:
    3.51          new FilePath(checkoutDir).deleteRecursive();
    3.52 -        new FilePath(repositoryDir).deleteRecursive();
    3.53 +        new FilePath(toolDir).deleteRecursive();
    3.54          hudson.getRootPath().deleteRecursive();
    3.55      }
    3.56  
    3.57      private File checkoutDir;
    3.58 -    private File repositoryDir;
    3.59 +    private File toolDir;
    3.60  
    3.61      public void testUpdate() throws Exception {
    3.62 -        //setup svn repository:
    3.63 -        String repositoryURL = repositoryDir.toURI().toURL().toExternalForm().replace("file:/", "file:///");
    3.64 -        runSubversionAdmin("create", repositoryDir.getAbsolutePath());
    3.65 -        runSubversion("checkout", repositoryURL, ".");
    3.66 -        createFile("A.java");
    3.67 -        createFile("B.java");
    3.68 -        runSubversion("add", "A.java", "B.java");
    3.69 -        runSubversion("commit", "-m", "initial");
    3.70 -
    3.71 +        IndexBuilderImpl indexer = new IndexBuilderImpl("test", "test");
    3.72 +        File projectMarkers = new File(toolDir, "indexer/test-cluster/patterns/project-marker-test");
    3.73 +        projectMarkers.getParentFile().mkdirs();
    3.74 +        OutputStream out = new FileOutputStream(projectMarkers);
    3.75 +        try {
    3.76 +            out.write(("(.*)/nbproject/project.xml\n" +
    3.77 +                       "(.*)/share/classes\n").getBytes("UTF-8"));
    3.78 +        } finally {
    3.79 +            out.close();
    3.80 +        }
    3.81 +        IndexingTool t = new IndexingTool("test", toolDir.getAbsolutePath(), NO_PROPERTIES);
    3.82 +        Hudson.getInstance().getDescriptorByType(IndexingTool.DescriptorImpl.class).setInstallations(t);
    3.83          FreeStyleProject p = createFreeStyleProject();
    3.84 -        ModuleLocation mod1 = new ModuleLocation(repositoryURL, null);
    3.85 -        SubversionSCM scm = new SubversionSCM(Collections.singletonList(mod1), true, null, "", "", "");
    3.86 -        IndexBuilderImpl indexer = new IndexBuilderImpl("test", "test");
    3.87 +        SCM scm = new ProjectSCM();
    3.88          p.setScm(scm);
    3.89          p.getBuildersList().add(indexer);
    3.90  
    3.91 @@ -112,51 +119,10 @@
    3.92  
    3.93          assertTrue(indexer.called);
    3.94  
    3.95 -        runSubversion("remove", "B.java");
    3.96 -        createFile("C.java");
    3.97 -        runSubversion("add", "C.java");
    3.98 -        runSubversion("commit", "-m", "");
    3.99 -
   3.100 -        indexer.called = false;
   3.101 -        doRunProject(p);
   3.102 -
   3.103 -        assertTrue(indexer.called);
   3.104 -        assertEquals(Collections.singleton("C.java"), indexer.addedOrModified);
   3.105 -        assertEquals(Collections.singleton("B.java"), indexer.removed);
   3.106 -    }
   3.107 -
   3.108 -    public void DISABLEDtestCheckoutIntoSpecifiedDir() throws Exception {
   3.109 -        //setup svn repository:
   3.110 -        String repositoryURL = repositoryDir.toURI().toURL().toExternalForm().replace("file:/", "file:///");
   3.111 -        runSubversionAdmin("create", repositoryDir.getAbsolutePath());
   3.112 -        runSubversion("checkout", repositoryURL, ".");
   3.113 -        createFile("A.java");
   3.114 -        createFile("B.java");
   3.115 -        runSubversion("add", "A.java", "B.java");
   3.116 -        runSubversion("commit", "-m", "initial");
   3.117 -
   3.118 -        FreeStyleProject p = createFreeStyleProject();
   3.119 -        ModuleLocation mod1 = new ModuleLocation(repositoryURL, "repo1");
   3.120 -        SubversionSCM scm = new SubversionSCM(Collections.singletonList(mod1), true, null, "", "", "");
   3.121 -        IndexBuilderImpl indexer = new IndexBuilderImpl("test", "test");
   3.122 -        p.setScm(scm);
   3.123 -        p.getBuildersList().add(indexer);
   3.124 -
   3.125 -        doRunProject(p);
   3.126 -
   3.127 -        assertTrue(indexer.called);
   3.128 -
   3.129 -        runSubversion("remove", "B.java");
   3.130 -        createFile("C.java");
   3.131 -        runSubversion("add", "C.java");
   3.132 -        runSubversion("commit", "-m", "");
   3.133 -
   3.134 -        indexer.called = false;
   3.135 -        doRunProject(p);
   3.136 -
   3.137 -        assertTrue(indexer.called);
   3.138 -        assertEquals(Arrays.asList(""), indexer.addedOrModified);
   3.139 -        assertEquals(Arrays.asList(""), indexer.removed);
   3.140 +        assertEquals(new File(toolDir, "index.sh").getAbsolutePath(), indexer.commands.get(0));
   3.141 +        assertEquals("test0", indexer.commands.get(1));
   3.142 +        assertEquals("test", indexer.commands.get(2));
   3.143 +        assertEquals(Arrays.asList("prj1", "src/prj2"), indexer.commands.subList(5, indexer.commands.size()));
   3.144      }
   3.145  
   3.146      private void doRunProject(FreeStyleProject p) throws SAXException, IOException, InterruptedException {
   3.147 @@ -168,48 +134,79 @@
   3.148          while (p.isBuilding()) {
   3.149              Thread.sleep(100);
   3.150          }
   3.151 +
   3.152 +        assertEquals(p.getLastBuild().getLog(Integer.MAX_VALUE).toString(), Result.SUCCESS, p.getLastBuild().getResult());
   3.153      }
   3.154  
   3.155 -    private void runSubversion(String... args) throws IOException, InterruptedException {
   3.156 -        Launcher.LocalLauncher l = new Launcher.LocalLauncher(new StreamTaskListener(System.err));
   3.157 -        l.launch().cmds(new ArgumentListBuilder().add("svn").add(args))
   3.158 -                  .pwd(checkoutDir)
   3.159 -                  .start()
   3.160 -                  .join();
   3.161 +    private static final class ProjectSCM extends NullSCM {
   3.162 +
   3.163 +        @Override
   3.164 +        public boolean checkout(AbstractBuild<?, ?> build, Launcher launcher, FilePath remoteDir, BuildListener listener, File changeLogFile) throws IOException, InterruptedException {
   3.165 +            writeFile(remoteDir, "prj1/nbproject/project.xml");
   3.166 +            writeFile(remoteDir, "src/prj2/share/classes/");
   3.167 +            writeFile(remoteDir, "prj3/nothing");
   3.168 +            return true;
   3.169 +        }
   3.170 +
   3.171 +        private void writeFile(FilePath remoteDir, String path) throws IOException, InterruptedException {
   3.172 +            FilePath target = new FilePath(remoteDir, path);
   3.173 +            target.getParent().mkdirs();
   3.174 +            target.write("", "UTF-8");
   3.175 +        }
   3.176      }
   3.177 -
   3.178 -    private void runSubversionAdmin(String... args) throws IOException, InterruptedException {
   3.179 -        Launcher.LocalLauncher l = new Launcher.LocalLauncher(new StreamTaskListener(System.err));
   3.180 -        l.launch().cmds(new ArgumentListBuilder().add("svnadmin").add(args))
   3.181 -                  .pwd(repositoryDir)
   3.182 -                  .start()
   3.183 -                  .join();
   3.184 -    }
   3.185 -
   3.186 -    private void createFile(String relativePath) throws IOException {
   3.187 -        File toCreate = new File(checkoutDir, relativePath.replace('/', File.separatorChar));
   3.188 -
   3.189 -        toCreate.getParentFile().mkdirs();
   3.190 -
   3.191 -        new FileOutputStream(toCreate).close();
   3.192 -    }
   3.193 -
   3.194      private static final class IndexBuilderImpl extends IndexingBuilder {
   3.195  
   3.196          private boolean called;
   3.197 -        private Set<String> addedOrModified;
   3.198 -        private Set<String> removed;
   3.199 +        private List<String> commands;
   3.200  
   3.201          public IndexBuilderImpl(String projectName, String toolName) {
   3.202 -            super(projectName, toolName);
   3.203 +            super(projectName, toolName, "", "");
   3.204          }
   3.205  
   3.206          @Override
   3.207 -        protected boolean doIndex(File cacheDir, AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener, Set<String> addedOrModified, Set<String> removed) throws IOException, InterruptedException {
   3.208 -            this.addedOrModified = addedOrModified;
   3.209 -            this.removed = removed;
   3.210 -            called = true;
   3.211 -            return true;
   3.212 +        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
   3.213 +            Launcher testLauncher = new Launcher(listener, launcher.getChannel()) {
   3.214 +                @Override
   3.215 +                public Proc launch(ProcStarter starter) throws IOException {
   3.216 +                    called = true;
   3.217 +                    commands = new ArrayList<String>(starter.cmds());
   3.218 +                    return new Proc() {
   3.219 +                        @Override
   3.220 +                        public boolean isAlive() throws IOException, InterruptedException {
   3.221 +                            return false;
   3.222 +                        }
   3.223 +                        @Override
   3.224 +                        public void kill() throws IOException, InterruptedException {}
   3.225 +                        @Override
   3.226 +                        public int join() throws IOException, InterruptedException {
   3.227 +                            return 0;
   3.228 +                        }
   3.229 +                        @Override
   3.230 +                        public InputStream getStdout() {
   3.231 +                            return new ByteArrayInputStream(new byte[0]);
   3.232 +                        }
   3.233 +                        @Override
   3.234 +                        public InputStream getStderr() {
   3.235 +                            return new ByteArrayInputStream(new byte[0]);
   3.236 +                        }
   3.237 +                        @Override
   3.238 +                        public OutputStream getStdin() {
   3.239 +                            return listener.getLogger(); //???
   3.240 +                        }
   3.241 +                    };
   3.242 +                }
   3.243 +
   3.244 +                @Override
   3.245 +                public Channel launchChannel(String[] cmd, OutputStream out, FilePath workDir, Map<String, String> envVars) throws IOException, InterruptedException {
   3.246 +                    throw new UnsupportedOperationException();
   3.247 +                }
   3.248 +
   3.249 +                @Override
   3.250 +                public void kill(Map<String, String> modelEnvVars) throws IOException, InterruptedException {
   3.251 +                    throw new UnsupportedOperationException();
   3.252 +                }
   3.253 +            };
   3.254 +            return super.perform(build, testLauncher, listener);
   3.255          }
   3.256  
   3.257      }