Skip to content

Commit

Permalink
Merge pull request #364 from gdgib/G2-1512-CaseInsensitiveEnvironments
Browse files Browse the repository at this point in the history
G2-1512 Support case insensitive environment maps
  • Loading branch information
gdgib authored Jan 26, 2024
2 parents 32e3fbc + 3ad9cea commit ef37cfd
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 12 deletions.
13 changes: 12 additions & 1 deletion ax-command/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<artifactId>ax-command</artifactId>
Expand All @@ -13,4 +15,13 @@

<name>Alexandria Command</name>
<description>Library for command line programming.</description>

<dependencies>
<dependency>
<groupId>com.g2forge.alexandria</groupId>
<artifactId>ax-adt</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.g2forge.alexandria.command.invocation.environment;

import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

import com.g2forge.alexandria.java.core.marker.ISingleton;
import com.g2forge.alexandria.java.platform.HPlatform;

public class SystemEnvironment implements IEnvironment, ISingleton {
protected static final SystemEnvironment INSTANCE = new SystemEnvironment();
Expand All @@ -20,6 +23,21 @@ public String apply(String name) {

@Override
public Map<String, String> toMap() {
final Map<String, String> systemEnvironment = getSystemEnvironment();

if (isCaseInsensitive()) {
final TreeMap<String, String> retVal = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
retVal.putAll(systemEnvironment);
return Collections.unmodifiableSortedMap(retVal);
}
return systemEnvironment;
}

protected Map<String, String> getSystemEnvironment() {
return System.getenv();
}

protected boolean isCaseInsensitive() {
return HPlatform.getPlatform().getPathSpec().isCaseSensitive();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.g2forge.alexandria.command.invocation.environment.modified;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import com.g2forge.alexandria.command.invocation.environment.IEnvironment;
import com.g2forge.alexandria.java.core.helpers.HMap;

import lombok.Builder;
import lombok.Data;
Expand Down Expand Up @@ -34,7 +33,7 @@ public IEnvironment simplify() {

@Override
public Map<String, String> toMap() {
final Map<String, String> retVal = new LinkedHashMap<>(getBase().toMap());
final Map<String, String> retVal = HMap.copy(getBase().toMap());
for (Map.Entry<String, IEnvironmentModifier> entry : getModifiers().entrySet()) {
final String variable = entry.getKey();

Expand All @@ -44,6 +43,6 @@ public Map<String, String> toMap() {
else retVal.put(variable, modified);
}

return Collections.unmodifiableMap(retVal);
return HMap.unmodifiableMap(retVal);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.g2forge.alexandria.command.main;
package com.g2forge.alexandria.command.command;

import java.io.InputStream;
import java.io.PrintStream;

import org.junit.Assert;
import org.junit.Test;

import com.g2forge.alexandria.command.command.DispatchCommand;
import com.g2forge.alexandria.command.command.IStandardCommand;
import com.g2forge.alexandria.command.exit.Exit;
import com.g2forge.alexandria.command.exit.IExit;
import com.g2forge.alexandria.command.invocation.CommandInvocation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.g2forge.alexandria.command.main;
package com.g2forge.alexandria.command.command;

import java.io.BufferedReader;
import java.io.InputStream;
Expand All @@ -8,7 +8,6 @@
import org.junit.Assert;
import org.junit.Test;

import com.g2forge.alexandria.command.command.IStandardCommand;
import com.g2forge.alexandria.command.command.IStandardCommand.TestResult;
import com.g2forge.alexandria.command.exit.IExit;
import com.g2forge.alexandria.command.invocation.CommandInvocation;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.g2forge.alexandria.command.invocation.environment;

import java.util.Map;
import java.util.Objects;

import org.junit.Test;

import com.g2forge.alexandria.adt.associative.map.MapBuilder;
import com.g2forge.alexandria.test.HAssert;

public class TestSystemEnvironment {
@Test
public void caseInsensitive() {
final String key = "Key", value = "Value";
final Map<String, String> inputMap = new MapBuilder<>(key, value).build();
final Map<String, String> outputMap = new SystemEnvironment() {
@Override
protected Map<String, String> getSystemEnvironment() {
return inputMap;
}

@Override
protected boolean isCaseInsensitive() {
return true;
}
}.toMap();
HAssert.assertEquals(value, outputMap.get(key));
HAssert.assertEquals(value, outputMap.get(key.toUpperCase()));
HAssert.assertEquals(value, outputMap.get(key.toLowerCase()));
}

@Test
public void caseSensitive() {
final String key = "Key", value = "Value";
final Map<String, String> inputMap = new MapBuilder<>(key, value).build();
final Map<String, String> outputMap = new SystemEnvironment() {
@Override
protected Map<String, String> getSystemEnvironment() {
return inputMap;
}

@Override
protected boolean isCaseInsensitive() {
return false;
}
}.toMap();
HAssert.assertEquals(value, outputMap.get(key));
HAssert.assertFalse(Objects.equals(value, outputMap.get(key.toUpperCase())) && Objects.equals(value, outputMap.get(key.toLowerCase())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.g2forge.alexandria.command.invocation.environment.modified;

import java.util.Map;
import java.util.SortedMap;

import org.junit.Test;

import com.g2forge.alexandria.adt.associative.map.MapBuilder;
import com.g2forge.alexandria.command.invocation.environment.MapEnvironment;
import com.g2forge.alexandria.command.invocation.environment.SystemEnvironment;
import com.g2forge.alexandria.test.HAssert;

public class TestModifiedEnvironment {
@Test
public void unspecified() {
final String key = "Key", value = "Value";
final ModifiedEnvironment environment = ModifiedEnvironment.builder().base(new MapEnvironment(new MapBuilder<>(key, value).build())).modifier(key, EnvironmentModifier.Unspecified).build();
HAssert.assertNull(environment.apply(key));
HAssert.assertTrue(environment.toMap().isEmpty());
}

@Test
public void inherit() {
final String key = "Key", value = "Value";
final ModifiedEnvironment environment = ModifiedEnvironment.builder().base(new MapEnvironment(new MapBuilder<>(key, value).build())).modifier(key, EnvironmentModifier.Inherit).build();
HAssert.assertEquals(value, environment.apply(key));
HAssert.assertEquals(1, environment.toMap().size());
}

@Test
public void value() {
final String key = "Key", value = "Value", other = "Other";
final ModifiedEnvironment environment = ModifiedEnvironment.builder().base(new MapEnvironment(new MapBuilder<>(key, value).build())).modifier(key, new EnvironmentValue(other)).build();
HAssert.assertEquals(other, environment.apply(key));
HAssert.assertEquals(1, environment.toMap().size());
}

@Test
public void caseInsensitive() {
final String key = "Key", value = "Value";
final Map<String, String> inputMap = new MapBuilder<>(key, value).build();
final Map<String, String> outputMap = ModifiedEnvironment.builder().base(new SystemEnvironment() {
@Override
protected Map<String, String> getSystemEnvironment() {
return inputMap;
}

@Override
protected boolean isCaseInsensitive() {
return true;
}
}).build().toMap();
HAssert.assertEquals(value, outputMap.get(key));
HAssert.assertEquals(value, outputMap.get(key.toUpperCase()));
HAssert.assertEquals(value, outputMap.get(key.toLowerCase()));
HAssert.assertInstanceOf(SortedMap.class, outputMap);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.g2forge.alexandria.adt.associative.map;
package com.g2forge.alexandria.java.core.helpers;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.function.Function;

import com.g2forge.alexandria.java.core.marker.Helpers;
Expand Down Expand Up @@ -33,4 +35,19 @@ public static <K, V> Map<K, V> merge(Map<K, V>... maps) {
}
return retVal;
}

public static <T> Map<String, T> copy(Map<String, T> map) {
if (map instanceof SortedMap) {
final SortedMap<String, T> cast = (SortedMap<String, T>) map;
final TreeMap<String, T> retVal = new TreeMap<>(cast.comparator());
retVal.putAll(map);
return retVal;
}
return new LinkedHashMap<>(map);
}

public static <K, V> Map<K, V> unmodifiableMap(Map<K, V> map) {
if (map instanceof SortedMap) return Collections.unmodifiableSortedMap((SortedMap<K, V>) map);
return Collections.unmodifiableMap(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.function.Predicate;
import java.util.stream.Stream;

import com.g2forge.alexandria.adt.associative.map.HMap;
import com.g2forge.alexandria.java.core.helpers.HMap;
import com.g2forge.alexandria.java.core.helpers.HTree;
import com.g2forge.alexandria.java.reflect.JavaProtection;
import com.g2forge.alexandria.java.reflect.JavaScope;
Expand Down

0 comments on commit ef37cfd

Please sign in to comment.