Author: tchemit Date: 2008-04-11 02:52:40 +0000 (Fri, 11 Apr 2008) New Revision: 446 Added: trunk/lutinvcs/lutinvcs-all/src/test/java/org/codelutin/vcs/ui/RepositoryGenerator.java Log: repository generator for tests Added: trunk/lutinvcs/lutinvcs-all/src/test/java/org/codelutin/vcs/ui/RepositoryGenerator.java =================================================================== --- trunk/lutinvcs/lutinvcs-all/src/test/java/org/codelutin/vcs/ui/RepositoryGenerator.java (rev 0) +++ trunk/lutinvcs/lutinvcs-all/src/test/java/org/codelutin/vcs/ui/RepositoryGenerator.java 2008-04-11 02:52:40 UTC (rev 446) @@ -0,0 +1,156 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.vcs.ui; + +import org.codelutin.util.FileUtil; +import org.codelutin.vcs.VCSEntries; +import org.codelutin.vcs.VCSEntry; +import org.codelutin.vcs.type.VCSEntryLocation; +import static org.codelutin.vcs.type.VCSEntryLocation.LOCAL; +import static org.codelutin.vcs.type.VCSEntryLocation.REMOTE; +import org.codelutin.vcs.type.VCSState; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.File; +import java.io.IOException; +import java.util.Random; + +/** A generator of local VCSEntry @author chemit */ +public class RepositoryGenerator { + + private static Log log = LogFactory.getLog(RepositoryGenerator.class); + + public static final VCSState[] localStates = new VCSState[]{VCSState.MODIFIED, VCSState.UNVERSIONNED, VCSState.OUT_OF_DATE_AND_MODIFIED, VCSState.REMOVED}; + + public static final VCSState[] remoteStates = new VCSState[]{VCSState.MISSING, VCSState.OUT_OF_DATE, VCSState.OUT_OF_DATE_AND_MODIFIED}; + + /** The valid chars for a file content. */ + protected static final String validFileContentChars = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\n\t\r.!:;,"; + + /** The valid chars for a filename. */ + protected static final String validFilenameChars = "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + + /** The r. */ + java.util.Random r; + + public RepositoryGenerator() { + this.r = new Random(); + } + + public void generateWorkingRepository(File root, int maxDir, int maxSubDir, int maxFile, int maxFileContent) throws IOException { + if (log.isInfoEnabled()) { + log.info(root); + } + root.mkdirs(); + + generateSubWorkingCopy(new File(root, "local"), maxDir, maxSubDir, maxFile, maxFileContent); + + generateSubWorkingCopy(new File(root, "remote"), maxDir, maxSubDir, maxFile, maxFileContent); + } + + public File generateWorkingRepositoryPath() { + return new File("/tmp/" + UITest.class.getSimpleName() + "_" + Math.abs(System.nanoTime())); + } + + public void deleteWorkingCopy(File root) { + if (log.isInfoEnabled()) { + log.info(root); + } + FileUtil.deleteRecursively(root); + } + + public void generateStates(VCSEntries entries) { + for (VCSEntry entry : entries.getEntries()) { + VCSState state; + state = entry.getRelativeLocalPath().startsWith("local") ? generateState(LOCAL) : generateState(REMOTE); + entry.setState(state); + } + } + + public VCSState generateState(VCSEntryLocation location) { + VCSState result = VCSState.UNKNOWN; + switch (location) { + case LOCAL: + result = localStates[r.nextInt(localStates.length)]; + break; + case REMOTE: + result = remoteStates[r.nextInt(remoteStates.length)]; + break; + } + return result; + } + + /** + * @param root working copy root directory + * @param maxDir max directory to generate + * @param maxSubDir max subdirectory to generate + * @param maxFile max file to generate in dir + * @param maxFileContent max file contente size + * @throws java.io.IOException if io pb while writing files' content + */ + protected void generateSubWorkingCopy(File root, int maxDir, int maxSubDir, int maxFile, int maxFileContent) throws IOException { + if (log.isInfoEnabled()) { + log.info(root); + } + generateDirectory(root, maxDir, 0, maxFile, maxFileContent); + for (int i = 0, nbDirs = r.nextInt(maxDir); i < nbDirs; i++) { + int nbSubDirs = r.nextInt(maxSubDir); + generateDirectory(new File(root, "dir_" + i), maxDir, nbSubDirs, maxFile, maxFileContent); + } + } + + protected void generateDirectory(File file, int maxDir, int nbSubDirs, int maxFile, int maxFileContent) throws IOException { + if (log.isDebugEnabled()) { + log.debug(file); + } + file.mkdirs(); + String filePrefix = "file" + file.getName().substring(3); + for (int i = 0, j = r.nextInt(maxFile); i < j; i++) { + + generateFile(new File(file, filePrefix + generateFilename(5, i)), maxFileContent); + } + if (nbSubDirs > 0) { + for (int i = 0, nbDirs = r.nextInt(maxDir); i < nbDirs; i++) { + generateDirectory(new File(file, file.getName() + "_" + i), maxDir, nbSubDirs - 1, maxFile, maxFileContent); + } + } + } + + protected void generateFile(File file, int maxFileContent) throws IOException { + if (log.isDebugEnabled()) { + log.debug(file); + } + FileUtil.writeString(file, generateContent(10 + r.nextInt(maxFileContent))); + } + + protected String generateFilename(int length, int i) { + return i + "_" + generateString0(length, validFilenameChars); + } + + protected String generateContent(int length) { + return generateString0(length, validFileContentChars); + } + + protected String generateString0(int length, String validChars) { + StringBuffer result = new StringBuffer(); + for (int i = 0; i < length; i++) { + char c = validChars.charAt(r.nextInt(validChars.length())); + result.append(c); + } + return result.toString(); + } + +}