下面列出了org.hamcrest.collection.IsEmptyCollection#org.cactoos.map.MapOf 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void convertsMapToProperties() {
new Assertion<>(
"Must convert map to properties",
new PropertiesOf(
new Sticky<>(
new MapOf<Integer, String>(
new MapEntry<>(0, "hello, world"),
new MapEntry<>(1, "how are you?")
)
)
),
new ScalarHasValue<>(
new MatcherOf<Properties>(
props -> {
return props.getProperty("0").endsWith(", world");
}
)
)
).affirm();
}
@Test
public void sensesChangesInMap() throws Exception {
final AtomicInteger size = new AtomicInteger(2);
final PropertiesOf props = new PropertiesOf(
new MapOf<>(
() -> new Repeated<>(
size.incrementAndGet(), () -> new MapEntry<>(
new SecureRandom().nextInt(),
1
)
)
)
);
new Assertion<>(
"Must sense the changes in the underlying map",
props.value().size(),
new IsNot<>(new IsEqual<>(props.value().size()))
).affirm();
}
/**
* Ctor.
* @param source Source directory
* @param target Target dir
*/
public App(final Path source, final Path target) {
this(
source, target,
new MapOf<String, Object>(
new MapEntry<>("LCOM", true),
new MapEntry<>("LCOM2", true),
new MapEntry<>("LCOM3", true),
new MapEntry<>("LCOM4", true),
new MapEntry<>("LCOM5", true),
new MapEntry<>("SCOM", true),
new MapEntry<>("NHD", true),
new MapEntry<>("MMAC", true),
new MapEntry<>("OCC", true),
new MapEntry<>("PCC", true),
new MapEntry<>("TCC", true),
new MapEntry<>("LCC", true),
new MapEntry<>("CCM", true),
new MapEntry<>("MWE", true)
)
);
}
/**
* Ctor.
* @param kvps Key-value pairs
*/
public HashDict(final Iterable<Kvp> kvps) {
this(
kvps,
new MapOf<>(
new Mapped<>(
input -> new MapEntry<>(input.key(), input.value()),
kvps
)
)
);
}
/**
* Ctor.
* @param entries The map with properties
* @since 0.23
*/
public PropertiesOf(final Iterable<Map.Entry<?, ?>> entries) {
this(
new MapOf<>(
new Mapped<Map.Entry<?, ?>, Map.Entry<String, String>>(
input -> new MapEntry<>(
input.getKey().toString(), input.getValue().toString()
),
entries
)
)
);
}
/**
* Finds the best fallback for the given exception type and apply it to
* the exception or throw the original error if no fallback found.
* @param exp The original exception
* @return Result of the most suitable fallback
* @throws Exception The original exception if no fallback found
*/
@SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes")
private T fallback(final Throwable exp) throws Exception {
final Sorted<Map.Entry<FallbackFrom<T>, Integer>> candidates =
new Sorted<>(
Comparator.comparing(Map.Entry::getValue),
new Filtered<>(
entry -> new Not(
new Equals<>(
entry::getValue,
() -> Integer.MIN_VALUE
)
).value(),
new MapOf<>(
fbk -> fbk,
fbk -> fbk.support(exp.getClass()),
this.fallbacks
).entrySet().iterator()
)
);
if (candidates.hasNext()) {
return candidates.next().getKey().apply(exp);
} else {
throw new Exception(
"No fallback found - throw the original exception",
exp
);
}
}
@Test
void buildsConnections() {
final Map<String, Node> byname = new MapOf<>(
Node::name,
node -> node,
new XmlGraph(
new Skeleton(new FakeBase(XmlGraphTest.CLASS_NAME))
).nodes()
);
final Node one = byname.get(XmlGraphTest.METHOD_ONE);
final Node two = byname.get(XmlGraphTest.METHOD_TWO);
final Node three = byname.get(XmlGraphTest.METHOD_THREE);
final Node four = byname.get(XmlGraphTest.METHOD_FOUR);
final Node five = byname.get(XmlGraphTest.METHOD_FIVE);
new Assertion<>(
"Must build nodes connections when called",
one.connections(),
new HasValues<>(two)
).affirm();
new Assertion<>(
"Must build nodes connections when called or calling",
two.connections(),
new HasValues<>(one, four)
).affirm();
new Assertion<>(
"Must build nodes connections when neither called nor calling",
three.connections(),
new IsEmptyCollection<>()
).affirm();
new Assertion<>(
"Must build nodes connections when calling",
four.connections(),
new HasValues<>(two)
).affirm();
new Assertion<>(
"Must build nodes connections when throwing",
five.connections(),
new IsEmptyCollection<>()
).affirm();
}
@Test
public void addContact() throws Exception {
try (
Servers servers = new Servers(
new ServerH2(
new ScriptSql(
new ResourceOf(
new Joined(
"/",
"com/github/fabriciofx/cactoos/jdbc/phonebook",
"phonebook-h2.sql"
)
)
)
),
new ServerPgsql(
new ScriptSql(
new ResourceOf(
new Joined(
"/",
"com/github/fabriciofx/cactoos/jdbc/phonebook",
"phonebook-pgsql.sql"
)
)
)
)
)
) {
for (final Session session : servers.sessions()) {
final Phonebook phonebook = new PhonebookSql(session);
final Contact contact = phonebook.contact(
new MapOf<String, String>(
new MapEntry<>("name", "Donald Knuth")
)
);
contact.phones().add(
new MapOf<String, String>(
new MapEntry<>("number", "99991234"),
new MapEntry<>("carrier", "TIM")
)
);
contact.phones().add(
new MapOf<String, String>(
new MapEntry<>("number", "98812564"),
new MapEntry<>("carrier", "Oi")
)
);
MatcherAssert.assertThat(
XhtmlMatchers.xhtml(
contact.about()
),
XhtmlMatchers.hasXPaths(
"/contact/name[text()='Donald Knuth']",
"/contact/phones/phone/number[text()='99991234']",
"/contact/phones/phone/carrier[text()='TIM']",
"/contact/phones/phone/number[text()='98812564']",
"/contact/phones/phone/carrier[text()='Oi']"
)
);
}
}
}
@Test
public void renameContact() throws Exception {
try (
Servers servers = new Servers(
new ServerH2(
new ScriptSql(
new ResourceOf(
new Joined(
"/",
"com/github/fabriciofx/cactoos/jdbc/phonebook",
"phonebook-h2.sql"
)
)
)
),
new ServerPgsql(
new ScriptSql(
new ResourceOf(
new Joined(
"/",
"com/github/fabriciofx/cactoos/jdbc/phonebook",
"phonebook-pgsql.sql"
)
)
)
)
)
) {
for (final Session session : servers.sessions()) {
final Phonebook phonebook = new PhonebookSql(session);
final Contact contact = phonebook.filter("maria").iterator()
.next();
contact.update(
new MapOf<String, String>(
new MapEntry<>("name", "Maria Lima")
)
);
MatcherAssert.assertThat(
XhtmlMatchers.xhtml(
new PhonebookSql(session)
.filter("maria")
.iterator()
.next()
.about()
),
XhtmlMatchers.hasXPaths(
"/contact/name[text()='Maria Lima']"
)
);
}
}
}
@Test
public void commit() throws Exception {
final Transacted transacted = new Transacted(
new SessionNoAuth(
new SourceH2("safedb")
)
);
new ScriptSql(
new ResourceOf(
"com/github/fabriciofx/cactoos/jdbc/phonebook/phonebook-h2.sql"
)
).run(transacted);
MatcherAssert.assertThat(
"Can't perform a transaction commit",
XhtmlMatchers.xhtml(
new ResultAsValue<>(
new StatementTransaction<>(
transacted,
() -> {
final Phonebook phonebook = new PhonebookSql(
transacted
);
final Contact contact = phonebook.contact(
new MapOf<String, String>(
new MapEntry<>("name", "Albert Einstein")
)
);
contact.phones().add(
new MapOf<String, String>(
new MapEntry<>("number", "99991234"),
new MapEntry<>("carrier", "TIM")
)
);
contact.phones().add(
new MapOf<String, String>(
new MapEntry<>("number", "98812564"),
new MapEntry<>("carrier", "Oi")
)
);
return contact.about();
}
)
).value()
),
XhtmlMatchers.hasXPaths(
"/contact/name[text()='Albert Einstein']",
"/contact/phones/phone/number[text()='99991234']",
"/contact/phones/phone/carrier[text()='TIM']",
"/contact/phones/phone/number[text()='98812564']",
"/contact/phones/phone/carrier[text()='Oi']"
)
);
}
@Test
public void rollback() throws Exception {
final Transacted transacted = new Transacted(
new SessionNoAuth(
new SourceH2("unsafedb")
)
);
new ScriptSql(
new ResourceOf(
"com/github/fabriciofx/cactoos/jdbc/phonebook/phonebook-h2.sql"
)
).run(transacted);
final Phonebook phonebook = new PhonebookSql(transacted);
final String name = "Frank Miller";
try {
new StatementTransaction<>(
transacted,
() -> {
final Contact contact = phonebook.contact(
new MapOf<String, String>(
new MapEntry<>("name", name)
)
);
contact.phones().add(
new MapOf<String, String>(
new MapEntry<>("number", "99991234"),
new MapEntry<>("carrier", "TIM")
)
);
throw new IllegalStateException("Rollback");
}
).result();
} catch (final IllegalStateException ex) {
}
MatcherAssert.assertThat(
"Can't perform a transaction rollback",
StreamSupport.stream(
phonebook.filter(name).spliterator(),
false
).count(),
Matchers.equalTo(0L)
);
}
private static Map<String, Object> args() {
return new MapOf<String, Object>(
new MapEntry<>("a", 1), new MapEntry<>("b", 2)
);
}
/**
* Ctor.
* @param name Name of the metric
* @param args Params for XSL
* @param mean Mean
* @param sigma Sigma
* @checkstyle ParameterNumberCheck (10 lines)
*/
ReportData(final String name, final Map<String, Object> args, final double mean,
final double sigma) {
this.metr = name;
this.args = new MapOf<String, Object>(new HashMap<>(args));
this.man = mean;
this.sig = sigma;
}