Skip to content

Commit

Permalink
[FIXED JENKINS-8194]
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob_robertson committed Aug 23, 2011
1 parent da361be commit a533f37
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 121 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.341</version>
<version>1.350</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
187 changes: 72 additions & 115 deletions src/main/java/configurationslicing/logrotator/LogRotationSlicer.java
Expand Up @@ -29,69 +29,64 @@ public Count() {
}
}

public static class LogRotationDaysSliceSpec implements UnorderedStringSlicerSpec<AbstractProject<?,?>> {

protected abstract static class AbstractLogRotationSliceSpec implements UnorderedStringSlicerSpec<AbstractProject<?,?>> {
private static final String DISABLED = "(Disabled)";

public String getName() {
return "Discard Old Builds Slicer - Days";
}

public String getName(AbstractProject<?, ?> item) {
return item.getName();
}

public String getUrl() {
return "logrotationdays";
}

public List<String> getValues(AbstractProject<?, ?> item) {
String retString = null;
LogRotator logrotator = item.getLogRotator();
if(logrotator == null) {
retString=DISABLED;
if (logrotator == null) {
retString = DISABLED;
} else {
String daysToKeepStr = logrotator.getDaysToKeepStr();
if(daysToKeepStr.length() == 0) {
retString=DISABLED;
} else {
retString=daysToKeepStr;
retString = getValue(logrotator);
if (retString.length() == 0) {
retString = DISABLED;
}
}
List<String> ret = new ArrayList<String>();
ret.add(retString);
return ret;
}
abstract protected String getValue(LogRotator rotator);

public List<AbstractProject<?, ?>> getWorkDomain() {
return (List)Hudson.getInstance().getItems(AbstractProject.class);
@SuppressWarnings("unchecked")
public List<AbstractProject<?, ?>> getWorkDomain() {
return (List) Hudson.getInstance().getItems(AbstractProject.class);
}

public boolean setValues(AbstractProject<?, ?> item, Set<String> set) {
if(set.isEmpty()) return false;
if (set.isEmpty()) return false;

LogRotator logrotator = item.getLogRotator();
int days = -1,builds = -1;
int days = -1;
int builds = -1;
int artifactDaysToKeep = -1;
int artifactNumToKeep = -1;

if(logrotator!= null) {
days=logrotator.getDaysToKeep();
builds=logrotator.getNumToKeep();
}
boolean disabled = false;
for(String line : set) {
if(line.length() == 0 || line.equals(DISABLED)) {
disabled = true;
} else {
int val = Integer.parseInt(line);
if(val > days) {
days = val;
}
}
if (logrotator != null) {
days = logrotator.getDaysToKeep();
builds = logrotator.getNumToKeep();
artifactDaysToKeep = logrotator.getArtifactDaysToKeep();
artifactNumToKeep = logrotator.getArtifactNumToKeep();
}

if(disabled) days = -1;

LogRotator newlogrotator = new LogRotator(days,builds);
int newInt = -1;
String newString = null;
for(String line: set) {
newString = line;
break;
}
if (!DISABLED.equals(newString)) {
newInt = Integer.parseInt(newString);
}
days = getNewDays(days, newInt);
builds = getNewBuilds(builds, newInt);

LogRotator newlogrotator = new LogRotator(days, builds, artifactDaysToKeep, artifactNumToKeep);

if (!LogRotationSlicer.equals(newlogrotator, logrotator)) {
item.setLogRotator(newlogrotator);
Expand All @@ -106,6 +101,45 @@ public boolean setValues(AbstractProject<?, ?> item, Set<String> set) {
return false;
}
}
protected int getNewDays(int oldDays, int newValue) {
return oldDays;
}
protected int getNewBuilds(int oldBuilds, int newValue) {
return oldBuilds;
}
}

public static class LogRotationDaysSliceSpec extends AbstractLogRotationSliceSpec {
public String getName() {
return "Discard Old Builds Slicer - Days";
}
public String getUrl() {
return "logrotationdays";
}
@Override
protected String getValue(LogRotator rotator) {
return rotator.getDaysToKeepStr();
}
@Override
protected int getNewDays(int oldDays, int newValue) {
return newValue;
}
}
public static class LogRotationBuildsSliceSpec extends AbstractLogRotationSliceSpec {
public String getName() {
return "Discard Old Builds Slicer - Builds";
}
public String getUrl() {
return "logrotationbuilds";
}
@Override
protected String getValue(LogRotator rotator) {
return rotator.getNumToKeepStr();
}
@Override
protected int getNewBuilds(int oldBuilds, int newValue) {
return newValue;
}
}
public static boolean equals(LogRotator r1, LogRotator r2) {
if (r1 == r2) {
Expand All @@ -122,81 +156,4 @@ public static boolean equals(LogRotator r1, LogRotator r2) {
}
return true;
}
public static class LogRotationBuildsSliceSpec implements UnorderedStringSlicerSpec<AbstractProject<?,?>> {

private static final String DISABLED = "(Disabled)";

public String getName() {
return "Discard Old Builds Slicer - Builds";
}

public String getName(AbstractProject<?, ?> item) {
return item.getName();
}

public String getUrl() {
return "logrotationbuilds";
}

public List<String> getValues(AbstractProject<?, ?> item) {
String retString = null;
LogRotator logrotator = item.getLogRotator();
if(logrotator == null) {
retString=DISABLED;
} else {
String numToKeepStr = logrotator.getNumToKeepStr();
if(numToKeepStr.length() == 0) {
retString=DISABLED;
} else {
retString=numToKeepStr;
}
}
List<String> ret = new ArrayList<String>();
ret.add(retString);
return ret;
}

public List<AbstractProject<?, ?>> getWorkDomain() {
return (List)Hudson.getInstance().getItems(AbstractProject.class);
}

public boolean setValues(AbstractProject<?, ?> item, Set<String> set) {
if(set.isEmpty()) return false;

LogRotator logrotator = item.getLogRotator();
int days = -1,builds = -1;

if(logrotator!= null) {
days=logrotator.getDaysToKeep();
builds=logrotator.getNumToKeep();
}
boolean disabled = false;
for(String line : set) {
if(line.length() == 0 || line.equals(DISABLED)) {
disabled = true;
} else {
int val = Integer.parseInt(line);
if(val > builds) {
builds = val;
}
}
}

if(disabled) builds = -1;

LogRotator newlogrotator = new LogRotator(days,builds);
if (!LogRotationSlicer.equals(newlogrotator, logrotator)) {
item.setLogRotator(newlogrotator);
try {
item.save();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
} else {
return false;
}
}
}
}
57 changes: 52 additions & 5 deletions src/test/java/configurationslicing/LogRotationSlicerTest.java
@@ -1,11 +1,58 @@
package configurationslicing;

import configurationslicing.logrotator.LogRotationSlicer;
import java.util.HashSet;
import java.util.Set;

import org.jvnet.hudson.test.HudsonTestCase;

import hudson.model.AbstractProject;
import hudson.tasks.LogRotator;
import junit.framework.TestCase;
import configurationslicing.logrotator.LogRotationSlicer;
import configurationslicing.logrotator.LogRotationSlicer.LogRotationBuildsSliceSpec;
import configurationslicing.logrotator.LogRotationSlicer.LogRotationDaysSliceSpec;

public class LogRotationSlicerTest extends TestCase {
public class LogRotationSlicerTest extends HudsonTestCase {

@SuppressWarnings("unchecked")
public void testSetValues() throws Exception {
AbstractProject item = createFreeStyleProject();

int daysToKeep = 111;
int numToKeep = 222;
int artifactDaysToKeep = 333;
int artifactNumToKeep = 444;

LogRotator lr = new LogRotator(daysToKeep, numToKeep, artifactDaysToKeep, artifactNumToKeep);
assertEquals(lr, daysToKeep, numToKeep, artifactDaysToKeep, artifactNumToKeep);

item.setLogRotator(lr);
assertEquals(item.getLogRotator(), daysToKeep, numToKeep, artifactDaysToKeep, artifactNumToKeep);

numToKeep = 12345;
Set<String> set = new HashSet<String>();
set.add(String.valueOf(numToKeep));

LogRotationBuildsSliceSpec buildsSpec = new LogRotationBuildsSliceSpec();
buildsSpec.setValues(item, set);
assertEquals(item.getLogRotator(), daysToKeep, numToKeep, artifactDaysToKeep, artifactNumToKeep);


daysToKeep = 54321;
set = new HashSet<String>();
set.add(String.valueOf(daysToKeep));

LogRotationDaysSliceSpec daysSpec = new LogRotationDaysSliceSpec();
daysSpec.setValues(item, set);
assertEquals(item.getLogRotator(), daysToKeep, numToKeep, artifactDaysToKeep, artifactNumToKeep);
}

private void assertEquals(LogRotator lr, int daysToKeep, int numToKeep, int artifactDaysToKeep, int artifactNumToKeep) {
assertEquals(daysToKeep, lr.getDaysToKeep());
assertEquals(numToKeep, lr.getNumToKeep());
assertEquals(artifactDaysToKeep, lr.getArtifactDaysToKeep());
assertEquals(artifactNumToKeep, lr.getArtifactNumToKeep());
}

public void testLogRotatorEquals() {
doTestLogRotatorEquals(0, 0, 0, 0, true);
doTestLogRotatorEquals(-1, -1, -1, -1, true);
Expand All @@ -22,8 +69,8 @@ public void testLogRotatorEquals() {
doTestLogRotatorEquals(2, 3, 1, 1, false);
}
private void doTestLogRotatorEquals(int d1, int d2, int n1, int n2, boolean expect) {
LogRotator r1 = new LogRotator(d1, n1);
LogRotator r2 = new LogRotator(d2, n2);
LogRotator r1 = new LogRotator(d1, n1, 0, 0);
LogRotator r2 = new LogRotator(d2, n2, 0, 0);
boolean equals = LogRotationSlicer.equals(r1, r2);
assertEquals(expect, equals);
assertFalse(LogRotationSlicer.equals(r1, null));
Expand Down

0 comments on commit a533f37

Please sign in to comment.