Java源码示例:org.apache.commons.configuration2.MapConfiguration
示例1
@Override
public Configuration getConfiguration() {
final Map<String, Object> map = new HashMap<>();
map.put(GRAPH_COMPUTER, this.computer.getGraphComputerClass().getCanonicalName());
if (-1 != this.computer.getWorkers())
map.put(WORKERS, this.computer.getWorkers());
if (null != this.computer.getPersist())
map.put(PERSIST, this.computer.getPersist().name());
if (null != this.computer.getResultGraph())
map.put(RESULT, this.computer.getResultGraph().name());
if (null != this.computer.getVertices())
map.put(VERTICES, this.computer.getVertices());
if (null != this.computer.getEdges())
map.put(EDGES, this.computer.getEdges());
map.putAll(this.computer.getConfiguration());
return new MapConfiguration(map);
}
示例2
@Test
public void shouldSupportMapBasedStrategies() throws Exception {
GraphTraversalSource g = EmptyGraph.instance().traversal();
assertFalse(g.getStrategies().getStrategy(SubgraphStrategy.class).isPresent());
g = g.withStrategies(SubgraphStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{
put("vertices", __.hasLabel("person"));
put("vertexProperties", __.limit(0));
put("edges", __.hasLabel("knows"));
}})));
assertTrue(g.getStrategies().getStrategy(SubgraphStrategy.class).isPresent());
g = g.withoutStrategies(SubgraphStrategy.class);
assertFalse(g.getStrategies().getStrategy(SubgraphStrategy.class).isPresent());
//
assertFalse(g.getStrategies().getStrategy(ReadOnlyStrategy.class).isPresent());
g = g.withStrategies(ReadOnlyStrategy.instance());
assertTrue(g.getStrategies().getStrategy(ReadOnlyStrategy.class).isPresent());
g = g.withoutStrategies(ReadOnlyStrategy.class);
assertFalse(g.getStrategies().getStrategy(ReadOnlyStrategy.class).isPresent());
}
示例3
@Test
public void shouldReturnDetachedElements() {
final Graph graph = TinkerFactory.createModern();
final GraphTraversalSource g = graph.traversal().withComputer().withStrategies(HaltedTraverserStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{
put(HaltedTraverserStrategy.HALTED_TRAVERSER_FACTORY, DetachedFactory.class.getCanonicalName());
}})));
g.V().out().forEachRemaining(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
g.V().out().properties("name").forEachRemaining(vertexProperty -> assertEquals(DetachedVertexProperty.class, vertexProperty.getClass()));
g.V().out().values("name").forEachRemaining(value -> assertEquals(String.class, value.getClass()));
g.V().out().outE().forEachRemaining(edge -> assertEquals(DetachedEdge.class, edge.getClass()));
g.V().out().outE().properties("weight").forEachRemaining(property -> assertEquals(DetachedProperty.class, property.getClass()));
g.V().out().outE().values("weight").forEachRemaining(value -> assertEquals(Double.class, value.getClass()));
g.V().out().out().forEachRemaining(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
g.V().out().out().path().forEachRemaining(path -> assertEquals(DetachedPath.class, path.getClass()));
g.V().out().pageRank().forEachRemaining(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
g.V().out().pageRank().out().forEachRemaining(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
// should handle nested collections
g.V().out().fold().next().forEach(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
}
示例4
/**
* Test the modification of a configuration property stored internally as an array.
*/
@Test
public void testSetArrayValue()
{
final MapConfiguration configuration = new MapConfiguration(new HashMap<String, Object>());
configuration.getMap().put("objectArray", new Object[] {"value1", "value2", "value3"});
final ConfigurationDynaBean bean = new ConfigurationDynaBean(configuration);
bean.set("objectArray", 1, "New Value 1");
final Object value = bean.get("objectArray", 1);
assertNotNull("Returned new value 1", value);
ObjectAssert.assertInstanceOf("Returned String new value 1", String.class, value);
assertEquals("Returned correct new value 1", "New Value 1", value);
}
示例5
@Override
protected TraversalStrategy readValue(final Buffer buffer, final GraphBinaryReader context) throws IOException {
final Class<TraversalStrategy> clazz = context.readValue(buffer, Class.class, false);
final Map config = context.readValue(buffer, Map.class, false);
return new TraversalStrategyProxy(clazz, new MapConfiguration(config));
}
示例6
@Override
public Configuration getConfiguration() {
final Map<String, Object> map = new HashMap<>();
map.put(STRATEGY, PartitionStrategy.class.getCanonicalName());
map.put(INCLUDE_META_PROPERTIES, this.includeMetaProperties);
if (null != this.writePartition)
map.put(WRITE_PARTITION, this.writePartition);
if (null != this.readPartitions)
map.put(READ_PARTITIONS, this.readPartitions);
if (null != this.partitionKey)
map.put(PARTITION_KEY, this.partitionKey);
return new MapConfiguration(map);
}
示例7
@Override
public Configuration getConfiguration() {
final Map<String, Object> map = new HashMap<>();
map.put(STRATEGY, ElementIdStrategy.class.getCanonicalName());
map.put(ID_PROPERTY_KEY, this.idPropertyKey);
map.put(ID_MAKER, this.idMaker);
return new MapConfiguration(map);
}
示例8
@Override
public Configuration getConfiguration() {
final Map<String, Object> map = new HashMap<>();
map.put(STRATEGY, HaltedTraverserStrategy.class.getCanonicalName());
map.put(HALTED_TRAVERSER_FACTORY, this.haltedTraverserFactory.getCanonicalName());
return new MapConfiguration(map);
}
示例9
@Override
public Configuration getConfiguration() {
final Map<String, Object> map = new HashMap<>();
map.put(STRATEGY, SeedStrategy.class.getCanonicalName());
map.put(ID_SEED, this.seed);
return new MapConfiguration(map);
}
示例10
@Override
public Configuration getConfiguration() {
final Map<String, Object> map = new HashMap<>();
if (null != this.vertexCriterion)
map.put(VERTICES, this.vertexCriterion);
if (null != this.edgeCriterion)
map.put(EDGES, this.edgeCriterion);
if (null != this.vertexPropertyCriterion)
map.put(VERTEX_PROPERTIES, this.vertexPropertyCriterion);
map.put(CHECK_ADJACENT_VERTICES, this.checkAdjacentVertices);
return new MapConfiguration(map);
}
示例11
@Override
public Configuration getConfiguration() {
final Map<String, Object> m = new HashMap<>(2);
m.put(THROW_EXCEPTION, this.throwException);
m.put(LOG_WARNING, this.logWarning);
return new MapConfiguration(m);
}
示例12
public ProgramVertexProgramStep(final Traversal.Admin traversal, final VertexProgram vertexProgram) {
super(traversal);
this.configuration = new HashMap<>();
final MapConfiguration base = new MapConfiguration(this.configuration);
vertexProgram.storeState(base);
this.toStringOfVertexProgram = vertexProgram.toString();
this.traverserRequirements = vertexProgram.getTraverserRequirements();
}
示例13
@Override
public VertexProgram generateProgram(final Graph graph, final Memory memory) {
final MapConfiguration base = new MapConfiguration(this.configuration);
PureTraversal.storeState(base, ROOT_TRAVERSAL, TraversalHelper.getRootTraversal(this.getTraversal()).clone());
base.setProperty(STEP_ID, this.getId());
if (memory.exists(TraversalVertexProgram.HALTED_TRAVERSERS))
TraversalVertexProgram.storeHaltedTraversers(base, memory.get(TraversalVertexProgram.HALTED_TRAVERSERS));
return VertexProgram.createVertexProgram(graph, base);
}
示例14
@Override
public GraphTraversalSource traversal(final Graph graph) {
return graph.traversal().withStrategies(VertexProgramStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{
put(VertexProgramStrategy.WORKERS, RANDOM.nextInt(Runtime.getRuntime().availableProcessors()) + 1);
put(VertexProgramStrategy.GRAPH_COMPUTER, RANDOM.nextBoolean() ?
GraphComputer.class.getCanonicalName() :
TinkerGraphComputer.class.getCanonicalName());
}})));
}
示例15
@ParameterizedTest
@ArgumentsSource(RequiredParameters.class)
void shouldThrowWhenRequiredParameterOmitted(String toOmit) {
Map<String, Object> configurationWithFilteredKey = Maps.filterKeys(VALID_CONFIGURATION, key -> !toOmit.equals(key));
assertThat(configurationWithFilteredKey).doesNotContainKeys(toOmit);
assertThatThrownBy(() -> ObjectStorageBlobConfiguration.from(new MapConfiguration(configurationWithFilteredKey)))
.isInstanceOf(ConfigurationException.class);
}
示例16
@ParameterizedTest
@ArgumentsSource(RequiredParameters.class)
void shouldThrowWhenRequiredParameterEmpty(String toEmpty) {
Map<String, Object> configurationWithFilteredKey = Maps.transformEntries(VALID_CONFIGURATION, (key, value) -> {
if (toEmpty.equals(key)) {
return "";
} else {
return value;
}
});
assertThat(configurationWithFilteredKey).containsEntry(toEmpty, "");
assertThatThrownBy(() -> ObjectStorageBlobConfiguration.from(new MapConfiguration(configurationWithFilteredKey)))
.isInstanceOf(ConfigurationException.class);
}
示例17
@Test
void shouldBuildAnAESPayloadCodecForAESConfig() throws Exception {
ObjectStorageBlobConfiguration actual = ObjectStorageBlobConfiguration.from(new MapConfiguration(
ImmutableMap.<String, Object>builder()
.putAll(CONFIGURATION_WITHOUT_CODEC)
.put("objectstorage.payload.codec", PayloadCodecFactory.AES256.name())
.put("objectstorage.aes256.hexsalt", "12345123451234512345")
.put("objectstorage.aes256.password", "james is great")
.build()));
assertThat(actual.getPayloadCodecFactory()).isEqualTo(PayloadCodecFactory.AES256);
assertThat(actual.getAesSalt()).contains("12345123451234512345");
assertThat(actual.getAesPassword()).contains("james is great".toCharArray());
}
示例18
@Test
void shouldFailIfCodecKeyIsIncorrect() throws Exception {
MapConfiguration configuration = new MapConfiguration(
ImmutableMap.<String, Object>builder()
.putAll(CONFIGURATION_WITHOUT_CODEC)
.put("objectstorage.payload.codec", "aes255")
.build());
assertThatThrownBy(() -> ObjectStorageBlobConfiguration.from(configuration)).isInstanceOf(ConfigurationException.class);
}
示例19
@Test
void shouldFailForAESCodecWhenSaltKeyIsMissing() throws Exception {
MapConfiguration configuration = new MapConfiguration(
ImmutableMap.<String, Object>builder()
.putAll(CONFIGURATION_WITHOUT_CODEC)
.put("objectstorage.payload.codec", PayloadCodecFactory.AES256.name())
.put("objectstorage.aes256.password", "james is great")
.build());
assertThatThrownBy(() -> ObjectStorageBlobConfiguration.from(configuration)).isInstanceOf(IllegalStateException.class);
}
示例20
@Test
void shouldFailForAESCodecWhenSaltKeyIsEmpty() throws Exception {
MapConfiguration configuration = new MapConfiguration(
ImmutableMap.<String, Object>builder()
.putAll(CONFIGURATION_WITHOUT_CODEC)
.put("objectstorage.payload.codec", PayloadCodecFactory.AES256.name())
.put("objectstorage.aes256.hexsalt", "")
.put("objectstorage.aes256.password", "james is great")
.build());
assertThatThrownBy(() -> ObjectStorageBlobConfiguration.from(configuration)).isInstanceOf(IllegalStateException.class);
}
示例21
@Test
void shouldFailForAESCodecWhenPasswordKeyIsMissing() throws Exception {
MapConfiguration configuration = new MapConfiguration(
ImmutableMap.<String, Object>builder()
.putAll(CONFIGURATION_WITHOUT_CODEC)
.put("objectstorage.payload.codec", PayloadCodecFactory.AES256.name())
.put("objectstorage.aes256.hexsalt", "12345123451234512345")
.build());
assertThatThrownBy(() -> ObjectStorageBlobConfiguration.from(configuration)).isInstanceOf(IllegalStateException.class);
}
示例22
@Test
void shouldFailForAESCodecWhenPasswordKeyIsEmpty() throws Exception {
MapConfiguration configuration = new MapConfiguration(
ImmutableMap.<String, Object>builder()
.putAll(CONFIGURATION_WITHOUT_CODEC)
.put("objectstorage.payload.codec", PayloadCodecFactory.AES256.name())
.put("objectstorage.aes256.hexsalt", "12345123451234512345")
.put("objectstorage.aes256.password", "")
.build());
assertThatThrownBy(() -> ObjectStorageBlobConfiguration.from(configuration)).isInstanceOf(IllegalStateException.class);
}
示例23
@Test
void shouldThrowWhenUnknownProvider() throws Exception {
MapConfiguration configuration = new MapConfiguration(
ImmutableMap.<String, Object>builder()
.put("objectstorage.payload.codec", PayloadCodecFactory.DEFAULT.name())
.put("objectstorage.provider", "unknown")
.put("objectstorage.namespace", "foo")
.build());
assertThatThrownBy(() -> ObjectStorageBlobConfiguration.from(configuration))
.isInstanceOf(ConfigurationException.class)
.hasMessage("Unknown object storage provider: unknown");
}
示例24
@Test
void fromShouldParseNameSpaceWhenSpecified() throws Exception {
String bucketNameAsString = "my-bucket";
MapConfiguration configuration = new MapConfiguration(
ImmutableMap.<String, Object>builder()
.putAll(VALID_CONFIGURATION)
.put("objectstorage.namespace", bucketNameAsString)
.build());
assertThat(ObjectStorageBlobConfiguration.from(configuration).getNamespace())
.contains(BucketName.of(bucketNameAsString));
}
示例25
@Test
void fromShouldNotContainsNamespaceWhenDontSpecify() throws Exception {
MapConfiguration configuration = new MapConfiguration(
ImmutableMap.<String, Object>builder()
.putAll(VALID_CONFIGURATION)
.build());
assertThat(ObjectStorageBlobConfiguration.from(configuration).getNamespace())
.isEmpty();
}
示例26
@Test
void fromShouldParseBucketPrefixWhenSpecified() throws Exception {
String bucketPrefix = "defaultPrefix";
MapConfiguration configuration = new MapConfiguration(
ImmutableMap.<String, Object>builder()
.putAll(VALID_CONFIGURATION)
.put("objectstorage.bucketPrefix", bucketPrefix)
.build());
assertThat(ObjectStorageBlobConfiguration.from(configuration).getBucketPrefix())
.contains(bucketPrefix);
}
示例27
@Test
void fromShouldReturnEmptyWhenDontSpecifyBucketPrefix() throws Exception {
MapConfiguration configuration = new MapConfiguration(
ImmutableMap.<String, Object>builder()
.putAll(VALID_CONFIGURATION)
.build());
assertThat(ObjectStorageBlobConfiguration.from(configuration).getBucketPrefix())
.isEmpty();
}
示例28
@Override
public void registerExtendedMetrics() {
for (MetricsImporter metricsImporter : MetricsImporterLocator.locate()) {
metricsImporter.register(this);
}
MixinMetric mixinMetric = mixinMetric(createId("lookout.reg"));
mixinMetric.gauge("size", new Gauge<Integer>() {
@Override
public Integer value() {
return metrics.size();
}
});
mixinMetric.gauge("max.size", new Gauge<Integer>() {
@Override
public Integer value() {
return config.getInt(LOOKOUT_MAX_METRICS_NUMBER,
MetricConfig.DEFAULT_MAX_METRICS_NUM);
}
});
// register config info
info(createId("lookout.config"), new AutoPollFriendlyInfo<Map<String, Object>>() {
@Override
public AutoPollSuggestion autoPollSuggest() {
return AutoPollSuggestion.POLL_WHEN_UPDATED;
}
@Override
public long lastModifiedTime() {
return -1;
}
@Override
public Map<String, Object> value() {
if (config instanceof MapConfiguration) {
return ((MapConfiguration) config).getMap();
}
return null;
}
});
}
示例29
@BeforeMethod
public void setUp() throws Exception {
configuration = new HashMap<>();
context = mock(Context.class);
target = spy(new UnivariateEstimator());
target.setConfiguration(new MapConfiguration(configuration));
prices = new TreeMap<>();
prices.put(parseDate("2017-05-31"), new BigDecimal("19650.57"));
prices.put(parseDate("2017-05-30"), new BigDecimal("19677.85"));
prices.put(parseDate("2017-05-29"), new BigDecimal("19682.57"));
prices.put(parseDate("2017-05-26"), new BigDecimal("19686.84"));
prices.put(parseDate("2017-05-25"), new BigDecimal("19813.13"));
prices.put(parseDate("2017-05-24"), new BigDecimal("19742.98"));
prices.put(parseDate("2017-05-23"), new BigDecimal("19613.28"));
prices.put(parseDate("2017-05-22"), new BigDecimal("19678.28"));
prices.put(parseDate("2017-05-19"), new BigDecimal("19590.76"));
prices.put(parseDate("2017-05-18"), new BigDecimal("19553.86"));
prices.put(parseDate("2017-05-17"), new BigDecimal("19814.88"));
prices.put(parseDate("2017-05-16"), new BigDecimal("19919.82"));
prices.put(parseDate("2017-05-15"), new BigDecimal("19869.85"));
prices.put(parseDate("2017-05-12"), new BigDecimal("19883.90"));
prices.put(parseDate("2017-05-11"), new BigDecimal("19961.55"));
prices.put(parseDate("2017-05-10"), new BigDecimal("19900.09"));
prices.put(parseDate("2017-05-09"), new BigDecimal("19843.00"));
prices.put(parseDate("2017-05-08"), new BigDecimal("19895.70"));
prices.put(parseDate("2017-05-02"), new BigDecimal("19445.70"));
prices.put(parseDate("2017-05-01"), new BigDecimal("19310.52"));
prices.put(parseDate("2017-04-28"), new BigDecimal("19196.74"));
prices.put(parseDate("2017-04-27"), new BigDecimal("19251.87"));
prices.put(parseDate("2017-04-26"), new BigDecimal("19289.43"));
prices.put(parseDate("2017-04-25"), new BigDecimal("19079.33"));
prices.put(parseDate("2017-04-24"), new BigDecimal("18875.88"));
prices.put(parseDate("2017-04-21"), new BigDecimal("18620.75"));
prices.put(parseDate("2017-04-20"), new BigDecimal("18430.49"));
prices.put(parseDate("2017-04-19"), new BigDecimal("18432.20"));
prices.put(parseDate("2017-04-18"), new BigDecimal("18418.59"));
prices.put(parseDate("2017-04-17"), new BigDecimal("18355.26"));
prices.put(parseDate("2017-04-14"), new BigDecimal("18335.63"));
prices.put(parseDate("2017-04-13"), new BigDecimal("18426.84"));
prices.put(parseDate("2017-04-12"), new BigDecimal("18552.61"));
prices.put(parseDate("2017-04-11"), new BigDecimal("18747.87"));
prices.put(parseDate("2017-04-10"), new BigDecimal("18797.88"));
prices.put(parseDate("2017-04-07"), new BigDecimal("18664.63"));
prices.put(parseDate("2017-04-06"), new BigDecimal("18597.06"));
prices.put(parseDate("2017-04-05"), new BigDecimal("18861.27"));
prices.put(parseDate("2017-04-04"), new BigDecimal("18810.25"));
prices.put(parseDate("2017-04-03"), new BigDecimal("18983.23"));
}
示例30
@Test
public void testConfiguration() {
Map<String, Object> map = new HashMap<>();
target.setConfiguration(new MapConfiguration(map));
// String
assertEquals(target.getStringProperty("string", "b"), "b");
map.put(target.getClass().getName() + ".string", "a");
assertEquals(target.getStringProperty("string", "b"), "a");
map.put(target.getClass().getName() + ".string", new Object() {
@Override
public String toString() {
throw new RuntimeException("test");
}
}); // Exception
assertEquals(target.getStringProperty("string", "b"), "b");
// Int
assertEquals(target.getIntProperty("int", -123), -123);
map.put(target.getClass().getName() + ".int", -999);
assertEquals(target.getIntProperty("int", -123), -999);
map.put(target.getClass().getName() + ".int", "a"); // Exception
assertEquals(target.getIntProperty("int", -123), -123);
// Long
assertEquals(target.getLongProperty("int", -123), -123);
map.put(target.getClass().getName() + ".int", -999);
assertEquals(target.getLongProperty("int", -123), -999);
map.put(target.getClass().getName() + ".int", "a"); // Exception
assertEquals(target.getLongProperty("int", -123), -123);
// Decimal
assertEquals(target.getDecimalProperty("decimal", TEN), TEN);
map.put(target.getClass().getName() + ".decimal", "1");
assertEquals(target.getDecimalProperty("decimal", TEN), ONE);
map.put(target.getClass().getName() + ".decimal", "a"); // Exception
assertEquals(target.getDecimalProperty("decimal", TEN), TEN);
}