-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ignore issue when coming from the same class #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.kohsuke.accmod.impl; | ||
|
||
public class RestrictedElementImpl implements RestrictedElement { | ||
private final boolean isInTheInspectedModule; | ||
private final String keyName; | ||
|
||
public RestrictedElementImpl(boolean isInTheInspectedModule, String keyName) { | ||
this.isInTheInspectedModule = isInTheInspectedModule; | ||
this.keyName = keyName; | ||
} | ||
|
||
public boolean isInTheInspectedModule() { | ||
return isInTheInspectedModule; | ||
} | ||
|
||
@Override | ||
public boolean isSameClass(Location location) { | ||
String locationClassName = location.getClassName(); | ||
if (locationClassName.contains("$")) { | ||
locationClassName = locationClassName.split("\\$")[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or String locationClassName = location.getClassName().replaceFirst("[$].+$", ""); |
||
} | ||
if(keyToClassName(keyName).equals(locationClassName)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this going to work with all cases of nested classes? Like class Outer
@Restricted(DoNotUse.class)
static class Middle {
static class Inner {
static {new Middle();}
}
}
} ? |
||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private static String keyToClassName(String key) { | ||
if (key.contains(".")) { | ||
key = key.split("\\.")[0]; | ||
} | ||
return key.replace('/', '.'); | ||
} | ||
|
||
|
||
public String toString() { | ||
return keyName; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.kohsuke.accmod.impl; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally I do not trust mocks and would recommend a new IT. |
||
|
||
public class RestrictedElementImplTest { | ||
|
||
@Test | ||
public void basic() throws Exception { | ||
|
||
assertThat(new RestrictedElementImpl(false, "hudson/util/TimeUnit2") | ||
.isSameClass(createLocation("hudson.util.TimeUnit2$4"))).isTrue(); | ||
|
||
assertThat(new RestrictedElementImpl(false, "hudson/util/TimeUnit2.someMethod();") | ||
.isSameClass(createLocation("hudson.util.TimeUnit2$4"))).isTrue(); | ||
|
||
assertThat(new RestrictedElementImpl(false, "hudson/util/XStream2.addCriticalField(Ljava/lang/Class;Ljava/lang/String;)V") | ||
.isSameClass(createLocation("jenkins.model.Jenkins"))).isFalse(); | ||
|
||
assertThat(new RestrictedElementImpl(false, "jenkins/model/Jenkins.expandVariablesForDirectory(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; ") | ||
.isSameClass(createLocation("jenkins.model.Jenkins$DescriptorImpl"))).isTrue(); | ||
} | ||
|
||
private Location createLocation(String value) { | ||
Location location = mock(Location.class); | ||
when(location.getClassName()).thenReturn(value); | ||
return location; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe factor into a
private
helper method.