Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,17 @@ public ScriptTreeModel getTreeModel() {
*
* <p>The script is added to the tree of scripts and its scripts/templates loaded, if any.
*
* <p>The call to this method has no effect if the given type or its name is null.
*
* @param type the new type of script
* @throws InvalidParameterException if a script type with same name is already registered
* @see #removeScriptType(ScriptType)
*/
public void registerScriptType(ScriptType type) {
if (type == null || type.getName() == null) {
return;
}

if (typeMap.containsKey(type.getName())) {
throw new InvalidParameterException("ScriptType already registered: " + type.getName());
}
Expand Down Expand Up @@ -623,13 +629,20 @@ public void removeScripType(ScriptType type) {
* @see #registerScriptType(ScriptType)
*/
public void removeScriptType(ScriptType type) {
if (type == null || type.getName() == null) {
return;
}

ScriptType scriptType = typeMap.remove(type.getName());
if (scriptType != null) {
getTreeModel().removeType(scriptType);
}
}

public ScriptType getScriptType(String name) {
if (name == null) {
return null;
}
return this.typeMap.get(name);
}

Expand Down
28 changes: 28 additions & 0 deletions zap/src/main/java/org/zaproxy/zap/utils/Statistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public void incCounter(String key) {
}

public void incCounter(String key, long inc) {
if (key == null) {
return;
}

stats.compute(key, (k, v) -> (v != null ? v : 0) + inc);
}

Expand All @@ -42,29 +46,49 @@ public void decCounter(String key) {
}

public void decCounter(String key, long dec) {
if (key == null) {
return;
}

stats.compute(key, (k, v) -> (v != null ? v : 0) - dec);
}

public void setHighwaterMark(String key, long value) {
if (key == null) {
return;
}

Long curValue = stats.get(key);
if (curValue == null || value > curValue) {
stats.put(key, value + 1);
}
}

public void setLowwaterMark(String key, long value) {
if (key == null) {
return;
}

Long curValue = stats.get(key);
if (curValue == null || value < curValue) {
stats.put(key, value + 1);
}
}

public Long getStat(String key) {
if (key == null) {
return null;
}

return stats.get(key);
}

public Map<String, Long> getStats(String keyPrefix) {
Map<String, Long> map = new HashMap<>();
if (keyPrefix == null) {
return map;
}

for (Entry<String, Long> stat : stats.entrySet()) {
if (stat.getKey().startsWith(keyPrefix)) {
map.put(stat.getKey(), stat.getValue());
Expand All @@ -78,6 +102,10 @@ public void clearAll() {
}

public void clear(String keyPrefix) {
if (keyPrefix == null) {
return;
}

Iterator<Entry<String, Long>> iter = stats.entrySet().iterator();
while (iter.hasNext()) {
Entry<String, Long> entry = iter.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,59 @@ void shouldReturnThreadSafeScriptTypesCollection() {
assertDoesNotThrow(it::next);
}

@Test
void shouldHandleRegistrationOfNullScriptType() {
// Given
ExtensionScript extensionScript = new ExtensionScript();
ScriptType scriptType = null;

// When / Then
assertDoesNotThrow(() -> extensionScript.registerScriptType(scriptType));
}

@Test
void shouldHandleRegistrationOfScriptTypeWithNullName() {
// Given
ExtensionScript extensionScript = new ExtensionScript();
ScriptType scriptType = new ScriptType(null, "i18n.type", null, false);

// When / Then
assertDoesNotThrow(() -> extensionScript.registerScriptType(scriptType));
}

@Test
void shouldHandleRemovalOfNullScriptType() {
// Given
ExtensionScript extensionScript = new ExtensionScript();
ScriptType scriptType = null;

// When / Then
assertDoesNotThrow(() -> extensionScript.removeScriptType(scriptType));
}

@Test
void shouldHandleRemovalOfScriptTypeWithNullName() {
// Given
ExtensionScript extensionScript = new ExtensionScript();
ScriptType scriptType = new ScriptType(null, "i18n.type", null, false);

// When / Then
assertDoesNotThrow(() -> extensionScript.removeScriptType(scriptType));
}

@Test
void shouldGetNullScriptTypeFromNullName() {
// Given
ExtensionScript extensionScript = new ExtensionScript();
String scriptTypeName = null;

// When
ScriptType type = extensionScript.getScriptType(scriptTypeName);

// Then
assertThat(type, is(nullValue()));
}

@Test
void shouldCreateScriptsCache() {
// Given
Expand Down
92 changes: 92 additions & 0 deletions zap/src/test/java/org/zaproxy/zap/utils/StatisticsUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ void shouldIncreaseCounter() throws Exception {
assertThat(statistics.getStat(STAT_KEY), is(equalTo(1L)));
}

@Test
void shouldIgnoreIncreaseCounterForNullKey() {
// Given
Statistics statistics = new Statistics();
// When
statistics.incCounter(null);
// Then
assertThat(statistics.getStat(null), is(nullValue()));
}

@Test
void shouldIncreaseExistingCounter() throws Exception {
// Given
Expand All @@ -76,6 +86,16 @@ void shouldIncreaseCounterWithGivenValue() throws Exception {
assertThat(statistics.getStat(STAT_KEY), is(equalTo(5L)));
}

@Test
void shouldIgnoreIncreaseCounterWithGivenValueForNullKey() {
// Given
Statistics statistics = new Statistics();
// When
statistics.incCounter(null, 5);
// Then
assertThat(statistics.getStat(null), is(nullValue()));
}

@Test
void shouldIncreaseExistingCounterWithGivenValue() throws Exception {
// Given
Expand Down Expand Up @@ -107,6 +127,16 @@ void shouldDecreaseCounter() throws Exception {
assertThat(statistics.getStat(STAT_KEY), is(equalTo(-1L)));
}

@Test
void shouldIgnoreDecreaseCounterForNullKey() {
// Given
Statistics statistics = new Statistics();
// When
statistics.decCounter(null);
// Then
assertThat(statistics.getStat(null), is(nullValue()));
}

@Test
void shouldDecreaseExistingCounter() throws Exception {
// Given
Expand All @@ -128,6 +158,16 @@ void shouldDecreaseCounterWithGivenValue() throws Exception {
assertThat(statistics.getStat(STAT_KEY), is(equalTo(-5L)));
}

@Test
void shouldIgnoreDecreaseCounterWithGivenValueForNullKey() {
// Given
Statistics statistics = new Statistics();
// When
statistics.decCounter(null, 5);
// Then
assertThat(statistics.getStat(null), is(nullValue()));
}

@Test
void shouldDecreaseExistingCounterWithGivenValue() throws Exception {
// Given
Expand Down Expand Up @@ -182,6 +222,19 @@ void shouldReturnNoStatsIfNoneWithGivenPrefix() throws Exception {
assertThat(stats.size(), is(equalTo(0)));
}

@Test
void shouldReturnNoStatsWithNullPrefix() {
// Given
Statistics statistics = new Statistics();
statistics.incCounter("other.stats.a");
statistics.incCounter("other.stats.b");
statistics.incCounter("other.stats.c");
// When
Map<String, Long> stats = statistics.getStats(null);
// Then
assertThat(stats.size(), is(equalTo(0)));
}

@Test
void shouldClearAllStats() throws Exception {
// Given
Expand Down Expand Up @@ -213,4 +266,43 @@ void shouldClearStatsWithSamePrefix() throws Exception {
assertThat(statistics.getStat("other.stats.a"), is(not(nullValue())));
assertThat(statistics.getStat("other.stats.b"), is(not(nullValue())));
}

@Test
void shouldIgnoreClearStatsWithNullPrefix() {
// Given
Statistics statistics = new Statistics();
statistics.incCounter("stats");
statistics.incCounter("stats.a");
statistics.incCounter("stats.b");
statistics.incCounter("other.stats.a");
statistics.incCounter("other.stats.b");
// When
statistics.clear(null);
// Then
assertThat(statistics.getStat("stats"), is(equalTo(1L)));
assertThat(statistics.getStat("stats.a"), is(equalTo(1L)));
assertThat(statistics.getStat("stats.b"), is(equalTo(1L)));
assertThat(statistics.getStat("other.stats.a"), is(equalTo(1L)));
assertThat(statistics.getStat("other.stats.b"), is(equalTo(1L)));
}

@Test
void shouldIgnoreSetHighwaterMarkForNullKey() {
// Given
Statistics statistics = new Statistics();
// When
statistics.setHighwaterMark(null, 10);
// Then
assertThat(statistics.getStat(null), is(nullValue()));
}

@Test
void shouldIgnoreSetLowwaterMarkForNullKey() {
// Given
Statistics statistics = new Statistics();
// When
statistics.setLowwaterMark(null, 10);
// Then
assertThat(statistics.getStat(null), is(nullValue()));
}
}
Loading