Skip to content

Commit

Permalink
[#25] Process keys map in the order it appears
Browse files Browse the repository at this point in the history
ensures that keys are evaluated in the order that they appear in the keys map file, rather than in hash code order.
  • Loading branch information
Kortanul authored and slawekjaranowski committed Nov 12, 2017
1 parent fcc6266 commit 32f5cad
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 53 deletions.
88 changes: 42 additions & 46 deletions src/main/java/com/github/s4u/plugins/KeysMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
package com.github.s4u.plugins;

import com.google.common.base.Strings;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.maven.artifact.Artifact;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.codehaus.plexus.component.annotations.Component;
Expand All @@ -27,7 +32,6 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
* @author Slawomir Jaranowski.
Expand All @@ -40,55 +44,19 @@ public class KeysMap {

private final List<ArtifactInfo> keysMapList = new ArrayList<>();

/**
* Properties.load recognize ':' as key value separator.
* This reader adds backlash before ':' char.
*/
class Reader extends InputStream {

private final InputStream inputStream;
private Character backSpace;

Reader(InputStream inputStream) {
this.inputStream = inputStream;
}

@Override
public int read() throws IOException {
public void load(String locale) throws ResourceNotFoundException, IOException {
if (!Strings.isNullOrEmpty(locale) && !Strings.isNullOrEmpty(locale.trim())) {
final Map<String, String> propertyMap;

int c;
if (backSpace == null) {
c = inputStream.read();
} else {
c = backSpace;
backSpace = null;
return c;
try (final InputStream inputStream = resourceManager.getResourceAsInputStream(locale)) {
propertyMap = loadProps(inputStream);
}

if (c == ':') {
backSpace = ':';
return '\\';
}
return c;
processProps(propertyMap);
}
}


public void load(String locale) throws ResourceNotFoundException, IOException {

if (Strings.isNullOrEmpty(locale) || Strings.isNullOrEmpty(locale.trim())) {
return;
}

InputStream inputStream = resourceManager.getResourceAsInputStream(locale);

Properties properties = new Properties();
properties.load(new Reader(inputStream));
processProps(properties);
}

public boolean isValidKey(Artifact artifact, PGPPublicKey key) {

if (keysMapList.isEmpty()) {
return true;
}
Expand All @@ -102,14 +70,42 @@ public boolean isValidKey(Artifact artifact, PGPPublicKey key) {
return false;
}

private void processProps(Properties properties) {
private Map<String, String> loadProps(final InputStream inputStream)
throws IOException {
final Map<String, String> propertyMap = new LinkedHashMap<>();
final BufferedReader propertiesReader =
new BufferedReader(new InputStreamReader(inputStream));
String currentLine;

while ((currentLine = propertiesReader.readLine()) != null) {
if (!isCommentLine(currentLine)) {
final String[] parts = currentLine.split("=");

if (parts.length != 2) {
throw new IllegalArgumentException(
"Property line is malformed: " + currentLine);
}

propertyMap.put(parts[0], parts[1]);
}
}

return propertyMap;
}

private void processProps(Map<String, String> properties) {
for (Entry<String, String> property : properties.entrySet()) {
ArtifactInfo artifactInfo =
createArtifactInfo(property.getKey(), property.getValue());

for (String propKey : properties.stringPropertyNames()) {
ArtifactInfo artifactInfo = createArtifactInfo(propKey, properties.getProperty(propKey));
keysMapList.add(artifactInfo);
}
}

private boolean isCommentLine(final String line) {
return !line.isEmpty() && line.charAt(0) == '#';
}

private ArtifactInfo createArtifactInfo(String strArtifact, String strKeys) {
return new ArtifactInfo(strArtifact, new KeyInfo(strKeys));
}
Expand Down
34 changes: 27 additions & 7 deletions src/test/java/com/github/s4u/plugins/KeysMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void setUp() throws ComponentLookupException, PlexusContainerException {
}

@AfterMethod
public void terraDown() {
public void tearDown() {
keysMap = null;
}

Expand All @@ -55,35 +55,55 @@ public void isComponentSet() {
@Test
public void nullLocationTest() throws Exception {
keysMap.load(null);

assertTrue(keysMap.isValidKey(getArtifact("test.group", "test", "1.1.1"), null));
}

@Test
public void emptyLocationTest() throws Exception {
keysMap.load("");

assertTrue(keysMap.isValidKey(getArtifact("test.group", "test", "1.1.1"), null));
}


@Test
public void validKeyFromMap1() throws Exception {

keysMap.load("/keysMap1.properties");
assertTrue(keysMap.isValidKey(getArtifact("junit", "junit", "4.12"), getPGPgpPublicKey(0x123456789abcdef0L)));

assertTrue(
keysMap.isValidKey(
getArtifact("junit", "junit", "4.12"),
getPGPgpPublicKey(0x123456789abcdef0L)));
}

@Test
public void validKeyFromMap2() throws Exception {

keysMap.load("/keysMap1.properties");
assertTrue(keysMap.isValidKey(getArtifact("test.test", "test", "1.2.3"), getPGPgpPublicKey(0x123456789abcdef0L)));

assertTrue(
keysMap.isValidKey(
getArtifact("test.test", "test", "1.2.3"),
getPGPgpPublicKey(0x123456789abcdef0L)));
}

@Test
public void invalidKeyFromMap() throws Exception {

keysMap.load("/keysMap1.properties");
assertFalse(keysMap.isValidKey(getArtifact("junit", "junit", "4.11"), getPGPgpPublicKey(0x123456789abcdef0L)));

assertFalse(
keysMap.isValidKey(
getArtifact("junit", "junit", "4.11"),
getPGPgpPublicKey(0x123456789abcdef0L)));
}

@Test
public void keysProcessedInEncounterOrder() throws Exception {
keysMap.load("/keysMap2.properties");

assertTrue(
keysMap.isValidKey(
getArtifact("test", "test-package", "1.0.0"),
getPGPgpPublicKey(0xA6ADFC93EF34893EL)));
}
}
17 changes: 17 additions & 0 deletions src/test/resources/keysMap2.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2015 Slawomir Jaranowski
#
# 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.
#
test:*:1.0.0=0xA6ADFC93EF34893E
test:test-package:*=0xA6ADFC93EF34893F

0 comments on commit 32f5cad

Please sign in to comment.