Skip to content

Commit 3425168

Browse files
authored
Initial refactor (#225)
Remove public accessors to the following classes: * Command * Kind * QueryType * SupportCommand Refactor ProjectModelFactory::withQueryType to withSafeQueryType and always set quertyType to QueryType.SAFE
1 parent ea66616 commit 3425168

File tree

10 files changed

+119
-228
lines changed

10 files changed

+119
-228
lines changed

plugins/codemodder-plugin-maven/src/main/java/io/codemodder/plugins/maven/MavenProvider.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import io.codemodder.plugins.maven.operator.POMScanner;
1414
import io.codemodder.plugins.maven.operator.ProjectModel;
1515
import io.codemodder.plugins.maven.operator.ProjectModelFactory;
16-
import io.codemodder.plugins.maven.operator.QueryType;
1716
import java.io.File;
1817
import java.io.IOException;
1918
import java.io.UncheckedIOException;
@@ -336,8 +335,7 @@ private CodeTFChangesetEntry getChanges(
336335
private Collection<DependencyGAV> getDependenciesFrom(final Path pomFile, final Path projectDir)
337336
throws DocumentException, IOException, URISyntaxException, XMLStreamException {
338337
ProjectModelFactory projectModelFactory =
339-
POMScanner.legacyScanFrom(pomFile.toFile(), projectDir.toFile())
340-
.withQueryType(QueryType.SAFE);
338+
POMScanner.legacyScanFrom(pomFile.toFile(), projectDir.toFile()).withSafeQueryType();
341339

342340
try {
343341
projectModelFactory =

plugins/codemodder-plugin-maven/src/main/java/io/codemodder/plugins/maven/operator/Command.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import javax.xml.stream.XMLStreamException;
66

77
/** Represents a Command in a Chain of Responsibility Pattern */
8-
public interface Command {
8+
interface Command {
99
/**
1010
* Given a context, performs an operation
1111
*

plugins/codemodder-plugin-maven/src/main/java/io/codemodder/plugins/maven/operator/Kind.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Represents a Kind of Version Pinning in a Maven File, representing the `-source` and `-target`
55
* flags as well as the newer `-release` flag of `javac`.
66
*/
7-
public enum Kind {
7+
enum Kind {
88
SOURCE,
99
TARGET,
1010
RELEASE

plugins/codemodder-plugin-maven/src/main/java/io/codemodder/plugins/maven/operator/ProjectModelFactory.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,9 @@ public ProjectModelFactory withOverrideIfAlreadyExists(boolean overrideIfAlready
8686
return this;
8787
}
8888

89-
/**
90-
* Fluent Setter
91-
*
92-
* @param queryType query type
93-
*/
94-
public ProjectModelFactory withQueryType(QueryType queryType) {
95-
this.queryType = queryType;
89+
/** Fluent Setter */
90+
public ProjectModelFactory withSafeQueryType() {
91+
this.queryType = QueryType.SAFE;
9692
return this;
9793
}
9894

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.codemodder.plugins.maven.operator;
22

3-
public enum QueryType {
3+
enum QueryType {
44
NONE,
55
SAFE
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package io.codemodder.plugins.maven.operator;
22

33
/** Tag Interface to the chain to allow it to figure out whether things were modified. */
4-
public interface SupportCommand {}
4+
interface SupportCommand {}

plugins/codemodder-plugin-maven/src/test/java/io/codemodder/plugins/maven/operator/POMOperatorDependencyQueryTest.java

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,22 @@ final class POMOperatorDependencyQueryTest {
2323
@Test
2424
void testBasicQuery()
2525
throws DocumentException, IOException, URISyntaxException, XMLStreamException {
26-
for (QueryType queryType : QueryType.values()) {
27-
if (queryType != QueryType.NONE) {
28-
ProjectModelFactory context = ProjectModelFactory.load(getClass().getResource("pom-1.xml"));
29-
context.withQueryType(queryType);
26+
ProjectModelFactory context = ProjectModelFactory.load(getClass().getResource("pom-1.xml"));
27+
context.withSafeQueryType();
3028

31-
Collection<Dependency> dependencies = POMOperator.queryDependency(context.build());
29+
Collection<Dependency> dependencies = POMOperator.queryDependency(context.build());
3230

33-
LOGGER.debug("Dependencies found: {}", dependencies);
31+
LOGGER.debug("Dependencies found: {}", dependencies);
3432

35-
assertTrue("Dependencies are not empty", dependencies != null && !dependencies.isEmpty());
36-
}
37-
}
33+
assertTrue("Dependencies are not empty", dependencies != null && !dependencies.isEmpty());
3834
}
3935

4036
@Test
4137
void testFailedSafeQuery()
4238
throws DocumentException, IOException, URISyntaxException, XMLStreamException {
4339
ProjectModelFactory context =
4440
ProjectModelFactory.load(getClass().getResource("pom-broken.xml"));
45-
context.withQueryType(QueryType.SAFE);
41+
context.withSafeQueryType();
4642

4743
Collection<Dependency> dependencies = POMOperator.queryDependency(context.build());
4844

@@ -66,7 +62,7 @@ void testAllQueryTypes()
6662
}
6763

6864
ProjectModelFactory context = ProjectModelFactory.load(getClass().getResource(pomFile));
69-
context.withQueryType(QueryType.SAFE);
65+
context.withSafeQueryType();
7066

7167
Collection<Dependency> dependencies =
7268
POMOperator.queryDependency(context.build(), commandListOverride);
@@ -79,54 +75,46 @@ void testAllQueryTypes()
7975
@Test
8076
void testTemporaryDirectory()
8177
throws IOException, DocumentException, URISyntaxException, XMLStreamException {
82-
for (QueryType queryType : QueryType.values()) {
83-
if (queryType != QueryType.NONE) {
84-
File tempDirectory = new File("/tmp/mvn-repo-" + System.currentTimeMillis() + ".dir");
8578

86-
LOGGER.info("Using queryType: " + queryType + " at " + tempDirectory);
79+
File tempDirectory = new File("/tmp/mvn-repo-" + System.currentTimeMillis() + ".dir");
8780

88-
assertFalse("Temp Directory does not exist initially", tempDirectory.exists());
89-
assertEquals(
90-
"There must be no files",
91-
tempDirectory.list() != null
92-
? (int) Files.list(tempDirectory.toPath()).filter(Files::isDirectory).count()
93-
: 0,
94-
0);
81+
assertFalse("Temp Directory does not exist initially", tempDirectory.exists());
82+
assertEquals(
83+
"There must be no files",
84+
tempDirectory.list() != null
85+
? (int) Files.list(tempDirectory.toPath()).filter(Files::isDirectory).count()
86+
: 0,
87+
0);
9588

96-
ProjectModelFactory context = ProjectModelFactory.load(getClass().getResource("pom-1.xml"));
97-
context.withQueryType(queryType);
98-
context.withRepositoryPath(tempDirectory);
89+
ProjectModelFactory context = ProjectModelFactory.load(getClass().getResource("pom-1.xml"));
90+
context.withSafeQueryType();
91+
context.withRepositoryPath(tempDirectory);
9992

100-
Collection<Dependency> dependencies = POMOperator.queryDependency(context.build());
93+
Collection<Dependency> dependencies = POMOperator.queryDependency(context.build());
10194

102-
LOGGER.debug("Dependencies found: " + dependencies);
95+
LOGGER.debug("Dependencies found: " + dependencies);
10396

104-
assertTrue("Dependencies are not empty", dependencies != null && !dependencies.isEmpty());
97+
assertTrue("Dependencies are not empty", dependencies != null && !dependencies.isEmpty());
10598

106-
assertTrue("Temp Directory ends up existing", tempDirectory.exists());
107-
assertTrue("Temp Directory is a directory", tempDirectory.isDirectory());
108-
}
109-
}
99+
assertTrue("Temp Directory ends up existing", tempDirectory.exists());
100+
assertTrue("Temp Directory is a directory", tempDirectory.isDirectory());
110101
}
111102

112103
@Test
113104
void testTemporaryDirectoryAndFullyOffline()
114105
throws IOException, DocumentException, URISyntaxException, XMLStreamException {
115-
for (QueryType queryType : QueryType.values()) {
116-
if (queryType != QueryType.NONE && queryType == QueryType.SAFE) {
117-
File tempDirectory = Files.createTempDirectory("mvn-repo").toFile();
118106

119-
ProjectModelFactory context = ProjectModelFactory.load(getClass().getResource("pom-1.xml"));
120-
context.withQueryType(queryType);
121-
context.withRepositoryPath(tempDirectory);
107+
File tempDirectory = Files.createTempDirectory("mvn-repo").toFile();
122108

123-
Collection<Dependency> dependencies = POMOperator.queryDependency(context.build());
109+
ProjectModelFactory context = ProjectModelFactory.load(getClass().getResource("pom-1.xml"));
110+
context.withSafeQueryType();
111+
context.withRepositoryPath(tempDirectory);
124112

125-
LOGGER.debug("Dependencies found: " + dependencies);
113+
Collection<Dependency> dependencies = POMOperator.queryDependency(context.build());
126114

127-
assertTrue("Dependencies are not empty", !dependencies.isEmpty());
128-
}
129-
}
115+
LOGGER.debug("Dependencies found: " + dependencies);
116+
117+
assertTrue("Dependencies are not empty", !dependencies.isEmpty());
130118
}
131119

132120
@Test
@@ -163,7 +151,7 @@ void testOnSyntheticDependency() throws Exception {
163151
.getBytes());
164152

165153
ProjectModelFactory context = ProjectModelFactory.load(tempPom.toFile());
166-
context.withQueryType(QueryType.SAFE);
154+
context.withSafeQueryType();
167155
context.withRepositoryPath(tempDirectory);
168156

169157
Collection<Dependency> dependencies = POMOperator.queryDependency(context.build());
@@ -251,7 +239,7 @@ void testOnCompositeSyntheticDependency() throws Exception {
251239
Files.write(tempPom, pomContent.getBytes());
252240

253241
ProjectModelFactory context = ProjectModelFactory.load(tempPom.toFile());
254-
context.withQueryType(QueryType.SAFE);
242+
context.withSafeQueryType();
255243
context.withRepositoryPath(tempDirectory);
256244

257245
Collection<Dependency> dependencies = POMOperator.queryDependency(context.build());
@@ -319,7 +307,7 @@ void testOnCompositeSyntheticDependencyIncompleteButWithParser() throws Exceptio
319307
Files.write(tempPom, pomContent.getBytes());
320308

321309
ProjectModelFactory context = ProjectModelFactory.load(tempPom.toFile());
322-
context.withQueryType(QueryType.SAFE);
310+
context.withSafeQueryType();
323311
context.withRepositoryPath(tempDirectory);
324312

325313
List<Command> commandList = getCommandListFor("QueryByParsing");
@@ -365,7 +353,7 @@ void testOfflineQueryResolution() throws Exception {
365353
File pomFile = new File(getClass().getResource("nested/child/pom/pom-3-child.xml").getFile());
366354

367355
ProjectModelFactory context = ProjectModelFactory.load(pomFile);
368-
context.withQueryType(QueryType.SAFE);
356+
context.withSafeQueryType();
369357
context.withRepositoryPath(tempDirectory);
370358

371359
Collection<Dependency> dependencies = POMOperator.queryDependency(context.build());

plugins/codemodder-plugin-maven/src/test/java/io/codemodder/plugins/maven/operator/POMOperatorMultipomTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void validateDepsFrom(ProjectModel context) throws Exception {
143143
File pomFile = resultFiles.entrySet().iterator().next().getValue();
144144

145145
ProjectModelFactory factory = ProjectModelFactory.load(pomFile);
146-
factory.withQueryType(QueryType.SAFE);
146+
factory.withSafeQueryType();
147147
ProjectModel projectModel = factory.build();
148148

149149
Collection<Dependency> dependencies = POMOperator.queryDependency(projectModel);

plugins/codemodder-plugin-maven/src/test/java/io/codemodder/plugins/maven/operator/POMOperatorTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ void testWithMultipleDependencies() throws Exception {
8787
}
8888

8989
Collection<Dependency> resolvedDeps =
90-
POMOperator.queryDependency(
91-
ProjectModelFactory.load(testPom).withQueryType(QueryType.SAFE).build());
90+
POMOperator.queryDependency(ProjectModelFactory.load(testPom).withSafeQueryType().build());
9291

9392
String testPomContents =
9493
new String(Files.readAllBytes(testPom.toPath()), Charset.defaultCharset());

0 commit comments

Comments
 (0)