/hudson-test-framework/src/main/java/org/jvnet/hudson/test/SingleFileSCM.java

http://github.com/hudson/hudson · Java · 81 lines · 38 code · 9 blank · 34 comment · 0 complexity · 031f9a399927fd096f683c5a6d90e0ad MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package org.jvnet.hudson.test;
  25. import hudson.scm.NullSCM;
  26. import hudson.scm.SCM;
  27. import hudson.model.AbstractBuild;
  28. import hudson.model.BuildListener;
  29. import hudson.Launcher;
  30. import hudson.FilePath;
  31. import java.io.File;
  32. import java.io.IOException;
  33. import java.io.OutputStream;
  34. import java.io.UnsupportedEncodingException;
  35. import java.net.URL;
  36. import org.apache.commons.io.IOUtils;
  37. /**
  38. * {@link SCM} useful for testing that puts just one file in the workspace.
  39. *
  40. * @author Kohsuke Kawaguchi
  41. */
  42. public class SingleFileSCM extends NullSCM {
  43. private final String path;
  44. private final byte[] contents;
  45. public SingleFileSCM(String path, byte[] contents) {
  46. this.path = path;
  47. this.contents = contents;
  48. }
  49. public SingleFileSCM(String path, String contents) throws UnsupportedEncodingException {
  50. this.path = path;
  51. this.contents = contents.getBytes("UTF-8");
  52. }
  53. /**
  54. * When a check out is requested, serve the contents of the URL and place it with the given path name.
  55. */
  56. public SingleFileSCM(String path, URL resource) throws IOException {
  57. this.path = path;
  58. this.contents = IOUtils.toByteArray(resource.openStream());
  59. }
  60. @Override
  61. public boolean checkout(AbstractBuild build, Launcher launcher, FilePath workspace, BuildListener listener, File changeLogFile) throws IOException, InterruptedException {
  62. listener.getLogger().println("Staging "+path);
  63. OutputStream os = workspace.child(path).write();
  64. IOUtils.write(contents, os);
  65. os.close();
  66. return true;
  67. }
  68. /**
  69. * Don't write 'this', so that subtypes can be implemented as anonymous class.
  70. */
  71. private Object writeReplace() { return new Object(); }
  72. }