/*
Copyright 2012 Christian Prause and Fraunhofer FIT
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package net.sf.collabreview.testing;
import net.sf.collabreview.core.Artifact;
import net.sf.collabreview.core.ArtifactIdentifier;
import net.sf.collabreview.core.Repository;
import net.sf.collabreview.core.users.Author;
import net.sf.collabreview.core.users.AuthorManager;
import java.util.Date;
/**
* Used by Unit tests to fill the database with some test data.
*
* @author Christian Prause (chris)
* @date 2009-11-04 17:59:35
*/
public class CreateTestDataDatabase {
public ArtifactIdentifier[] artifactIdentifiers = new ArtifactIdentifier[]{
new ArtifactIdentifier("a.java", 0, ""),
new ArtifactIdentifier("b.java", 1, ""),
new ArtifactIdentifier("a.java", 2, ""),
};
public String[] contents = new String[]{
"Hello world",
"this is a test",
"Hello world\nhere we are again"
};
public String[] authors = new String[]{
"Elrond",
"Galadriel",
"Galadriel",
"Feanor",
};
/**
* Flush the database and fill it with new data.
*
* @param repository the repository for inserting the artifacts of the test data
* @param authorManager the AuthorManager that maintains the test data users.
* @param startRev select start index of test data to insert or -1 to start with the first set
* @param endRev select up to which test data set to insert into the repository (-1 for the last)
*/
public void setUpData(Repository repository, AuthorManager authorManager, int startRev, int endRev) {
if (startRev == -1) {
startRev = 0;
}
assert startRev >= 0;
if (endRev == -1) {
endRev = artifactIdentifiers.length;
}
assert endRev >= 0 && endRev <= artifactIdentifiers.length;
repository.reset();
repository.commit();
for (String authorName : authors) {
authorManager.storeAuthor(authorManager.createAuthor(authorName));
}
authorManager.commit();
for (int i = startRev; i < endRev; i++) {
Artifact old = repository.getLatestForName(artifactIdentifiers[i].getName(), artifactIdentifiers[i].getBranch());
if (old != null) {
old.setObsoleteDate(new Date(i));
}
Author author = authorManager.getAuthor(authors[i]);
repository.addArtifact(artifactIdentifiers[i], new Date(i), contents[i], author);
}
repository.commit();
System.out.println("data inserted");
}
}