PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/mercurial/test/unit/src/org/netbeans/modules/mercurial/AbstractHgTestCase.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 237 lines | 151 code | 30 blank | 56 comment | 17 complexity | e26f6e79dad9f4e376f98987fab4e894 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2008 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.mercurial;
  43. import java.io.File;
  44. import java.io.FileWriter;
  45. import java.io.IOException;
  46. import java.util.ArrayList;
  47. import java.util.Collections;
  48. import java.util.List;
  49. import java.util.Map;
  50. import java.util.concurrent.TimeoutException;
  51. import java.util.logging.Handler;
  52. import java.util.logging.Level;
  53. import java.util.logging.LogRecord;
  54. import java.util.logging.Logger;
  55. import org.netbeans.junit.NbTestCase;
  56. import org.netbeans.modules.mercurial.util.HgCommand;
  57. import org.openide.filesystems.FileObject;
  58. import org.openide.filesystems.FileUtil;
  59. /**
  60. *
  61. * @author tomas
  62. */
  63. public abstract class AbstractHgTestCase extends NbTestCase {
  64. protected static final OutputLogger NULL_LOGGER = OutputLogger.getLogger(null);
  65. public FileStatusCache getCache() {
  66. return cache;
  67. }
  68. private FileStatusCache cache;
  69. // private File workDir;
  70. // private File wc;
  71. public AbstractHgTestCase(String arg0) {
  72. super(arg0);
  73. }
  74. @Override
  75. protected Level logLevel() {
  76. return Level.FINE;
  77. }
  78. protected File getWorkTreeDir () throws IOException {
  79. return new File(getWorkDir(), "wc");
  80. }
  81. @Override
  82. protected void setUp() throws Exception {
  83. super.setUp();
  84. clearWorkDir();
  85. FileUtil.refreshFor(getWorkTreeDir());
  86. Logger.getLogger("").addHandler(versionCheckBlocker);
  87. try {
  88. Mercurial.getInstance().asyncInit();
  89. for (int i = 0; i < 20; i++) {
  90. Thread.sleep(200);
  91. if(versionCheckBlocker.versionChecked) break;
  92. }
  93. if(!versionCheckBlocker.versionChecked) throw new TimeoutException("hg version check timedout!");
  94. } finally {
  95. Logger.getLogger("").removeHandler(versionCheckBlocker);
  96. }
  97. // workDir = new File(System.getProperty("work.dir"));
  98. // FileUtil.refreshFor(workDir);
  99. try {
  100. assertTrue(getWorkTreeDir().mkdirs());
  101. HgCommand.doCreate(getWorkTreeDir(), null);
  102. new File(getWorkTreeDir(), "empty").createNewFile();
  103. } catch (IOException iOException) {
  104. throw iOException;
  105. } catch (HgException hgException) {
  106. // if(!hgException.getMessage().contains("already exists")) {
  107. // throw hgException;
  108. // }
  109. }
  110. // wc = new File(workDir, getName() + "_wc");
  111. cache = Mercurial.getInstance().getFileStatusCache();
  112. }
  113. // protected File getWC() {
  114. // return wc;
  115. // }
  116. protected void commit(File... files) throws HgException, IOException {
  117. commitIntoRepository(getWorkTreeDir(), files);
  118. }
  119. protected void commitIntoRepository (File repository, File... files) throws HgException, IOException {
  120. List<File> filesToAdd = new ArrayList<File>();
  121. FileInformation status;
  122. for (File file : files) {
  123. if(findStatus(HgCommand.getStatus(repository, Collections.singletonList(file), null, null),
  124. FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY)) {
  125. filesToAdd.add(file);
  126. }
  127. }
  128. HgCommand.doAdd(repository, filesToAdd, null);
  129. List<File> filesToCommit = new ArrayList<File>();
  130. for (File file : files) {
  131. if(file.isFile()) {
  132. filesToCommit.add(file);
  133. }
  134. }
  135. HgCommand.doCommit(repository, filesToCommit, "commit", null);
  136. for (File file : filesToCommit) {
  137. assertStatus(file, FileInformation.STATUS_VERSIONED_UPTODATE);
  138. }
  139. }
  140. protected File clone(File file) throws HgException, IOException {
  141. String path = file.getAbsolutePath() + "_cloned";
  142. HgCommand.doClone(getWorkTreeDir(), new File(path), null);
  143. return new File(path);
  144. }
  145. protected void assertStatus(File f, int status) throws HgException, IOException {
  146. FileInformation s = HgCommand.getStatus(getWorkTreeDir(), Collections.singletonList(f), null, null).get(f);
  147. if (status == FileInformation.STATUS_VERSIONED_UPTODATE) {
  148. assertEquals(s, null);
  149. } else {
  150. assertEquals(status, s.getStatus());
  151. }
  152. }
  153. protected void assertCacheStatus(File f, int status) throws HgException, IOException {
  154. assertEquals(status, cache.getStatus(f).getStatus());
  155. }
  156. protected File createFolder(String name) throws IOException {
  157. FileObject wd = FileUtil.toFileObject(getWorkTreeDir());
  158. FileObject folder = wd.createFolder(name);
  159. return FileUtil.toFile(folder);
  160. }
  161. protected File createFolder(File parent, String name) throws IOException {
  162. FileObject parentFO = FileUtil.toFileObject(parent);
  163. FileObject folder = parentFO.createFolder(name);
  164. return FileUtil.toFile(folder);
  165. }
  166. protected File createFile(File parent, String name) throws IOException {
  167. FileObject parentFO = FileUtil.toFileObject(parent);
  168. FileObject fo = parentFO.createData(name);
  169. return FileUtil.toFile(fo);
  170. }
  171. protected File createFile(String name) throws IOException {
  172. FileObject wd = FileUtil.toFileObject(getWorkTreeDir());
  173. FileObject fo = wd.createData(name);
  174. return FileUtil.toFile(fo);
  175. }
  176. protected void write(File file, String str) throws IOException {
  177. FileWriter w = null;
  178. try {
  179. w = new FileWriter(file);
  180. w.write(str);
  181. w.flush();
  182. } finally {
  183. if (w != null) {
  184. w.close();
  185. }
  186. }
  187. }
  188. private boolean findStatus(Map<File, FileInformation> statuses, int status) {
  189. for (Map.Entry<File, FileInformation> e : statuses.entrySet()) {
  190. if (e.getValue().getStatus() == status) {
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. private static class VersionCheckBlocker extends Handler {
  197. boolean versionChecked = false;
  198. public void publish(LogRecord record) {
  199. if(record.getMessage().indexOf("version: ") > -1) {
  200. versionChecked = true;
  201. }
  202. }
  203. public void flush() { }
  204. public void close() throws SecurityException { }
  205. };
  206. private static VersionCheckBlocker versionCheckBlocker = new VersionCheckBlocker();
  207. }