Skip to content

Commit b980958

Browse files
committed
added questions for contest
1 parent 938f36e commit b980958

File tree

8 files changed

+268
-1
lines changed

8 files changed

+268
-1
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.idea/
2+
target/
3+
.DS_Store
4+
.vscode/
5+
.log
6+
.mvn
7+
*.iml
8+
generated-src/

pom.xml

+31-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,36 @@
2020
<version>1.3</version>
2121
<scope>test</scope>
2222
</dependency>
23+
<dependency>
24+
<groupId>com.google.guava</groupId>
25+
<artifactId>guava</artifactId>
26+
<version>18.0</version>
27+
</dependency>
2328

2429
</dependencies>
25-
</project>
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-compiler-plugin</artifactId>
35+
<configuration>
36+
<source>1.8</source>
37+
<target>1.8</target>
38+
</configuration>
39+
</plugin>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-jar-plugin</artifactId>
43+
<version>2.6</version>
44+
<executions>
45+
<execution>
46+
<goals>
47+
<goal>test-jar</goal>
48+
</goals>
49+
</execution>
50+
</executions>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.oneapm.coffee.maker;
2+
3+
import static com.oneapm.coffee.maker.CoffeeType.BLACK;
4+
import static com.oneapm.coffee.maker.CoffeeType.CAPPUCCINO;
5+
import static com.oneapm.coffee.maker.CoffeeType.ESPRESSO;
6+
import static com.oneapm.coffee.maker.CoffeeType.LATTE;
7+
import static com.oneapm.coffee.maker.CoffeeType.MOCHA;
8+
9+
public class CoffeeMaker {
10+
static final String WATER = "Water";
11+
12+
public void makeCoffee(
13+
Pourable pourable,
14+
CoffeeType type
15+
) {
16+
if (type == CAPPUCCINO) {
17+
pouringCappuccinoPowder(pourable);
18+
} else if (type == BLACK) {
19+
pouringBlackPowder(pourable);
20+
} else if (type == MOCHA) {
21+
pouringMochaPowder(pourable);
22+
} else if (type == LATTE) {
23+
pouringLattePowder(pourable);
24+
} else if (type == ESPRESSO) {
25+
pouringEspressoPowder(pourable);
26+
}
27+
28+
pouringBoilingWater(pourable);
29+
}
30+
31+
private void pouringBoilingWater(Pourable pourable) {
32+
pourable.pour(WATER);
33+
}
34+
35+
private void pouringBlackPowder(Pourable pourable) {
36+
// more secret recipe here
37+
pourable.pour(BLACK.name());
38+
}
39+
40+
private void pouringMochaPowder(Pourable pourable) {
41+
// more secret recipe here
42+
pourable.pour(MOCHA.name());
43+
}
44+
45+
private void pouringLattePowder(Pourable pourable) {
46+
// more secret recipe here
47+
pourable.pour(LATTE.name());
48+
}
49+
50+
private void pouringEspressoPowder(Pourable pourable) {
51+
// more secret recipe here
52+
pourable.pour(ESPRESSO.name());
53+
}
54+
55+
private void pouringCappuccinoPowder(Pourable pourable) {
56+
// more secret recipe here
57+
pourable.pour(CAPPUCCINO.name());
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.oneapm.coffee.maker;
2+
3+
public enum CoffeeType {
4+
CAPPUCCINO,
5+
BLACK,
6+
MOCHA,
7+
LATTE,
8+
ESPRESSO
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.oneapm.coffee.maker;
2+
3+
interface Pourable {
4+
void pour(String stuff);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CREATE TABLE `sqltrace_data_entity` (
2+
`sql_trace_id` bigint(20) NOT NULL AUTO_INCREMENT,
3+
`blame_metric_name` varchar(500) NOT NULL,
4+
`uri` varchar(500) DEFAULT NULL,
5+
`sql_id` bigint(20) NOT NULL,
6+
`metric_name` varchar(500) NOT NULL,
7+
`call_count` int(11) NOT NULL,
8+
`min_time` float(10) NOT NULL,
9+
`max_time` float(10) NOT NULL,
10+
`application_id` bigint(20) NOT NULL,
11+
`parameters` text NOT NULL,
12+
`total_time` float(10) NOT NULL,
13+
`avg_time` float(10) NOT NULL,
14+
`sql_info` text NOT NULL,
15+
`record_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
16+
`agent_run_id` bigint(20) NOT NULL,
17+
`user_id` bigint(20) NOT NULL DEFAULT '0',
18+
PRIMARY KEY (`sql_trace_id`),
19+
KEY `i_appid_agentid_time_name` (`application_id`,`agent_run_id`,`record_time`,`metric_name`)
20+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.oneapm.coffee.maker;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static com.oneapm.coffee.maker.CoffeeType.BLACK;
9+
import static com.oneapm.coffee.maker.CoffeeType.CAPPUCCINO;
10+
import static com.oneapm.coffee.maker.CoffeeType.ESPRESSO;
11+
import static com.oneapm.coffee.maker.CoffeeType.LATTE;
12+
import static com.oneapm.coffee.maker.CoffeeType.MOCHA;
13+
import static org.hamcrest.MatcherAssert.assertThat;
14+
import static org.hamcrest.Matchers.contains;
15+
16+
public class CoffeeMakerTest {
17+
18+
private final List<String> contents = new ArrayList<>();
19+
20+
private final Pourable pourable = contents::add;
21+
22+
private final CoffeeMaker coffeeMaker = new CoffeeMaker();
23+
24+
@Test
25+
public void makesCappuccino() {
26+
coffeeMaker.makeCoffee(pourable, CAPPUCCINO);
27+
28+
assertThat(contents, contains(CAPPUCCINO.name(), CoffeeMaker.WATER));
29+
}
30+
31+
@Test
32+
public void makesBlack() {
33+
coffeeMaker.makeCoffee(pourable, BLACK);
34+
35+
assertThat(contents, contains(BLACK.name(), CoffeeMaker.WATER));
36+
}
37+
38+
@Test
39+
public void makesMocha() {
40+
coffeeMaker.makeCoffee(pourable, MOCHA);
41+
42+
assertThat(contents, contains(MOCHA.name(), CoffeeMaker.WATER));
43+
}
44+
45+
@Test
46+
public void makesLatte() {
47+
coffeeMaker.makeCoffee(pourable, LATTE);
48+
49+
assertThat(contents, contains(LATTE.name(), CoffeeMaker.WATER));
50+
}
51+
52+
@Test
53+
public void makesEspresso() {
54+
coffeeMaker.makeCoffee(pourable, ESPRESSO);
55+
56+
assertThat(contents, contains(ESPRESSO.name(), CoffeeMaker.WATER));
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.oneapm.tps.agent.service.domain;
2+
3+
import org.junit.Test;
4+
5+
import java.lang.reflect.Field;
6+
import java.lang.reflect.Modifier;
7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
import static com.google.common.base.CaseFormat.LOWER_CAMEL;
14+
import static com.google.common.base.CaseFormat.LOWER_UNDERSCORE;
15+
import static org.hamcrest.core.Is.is;
16+
import static org.junit.Assert.assertArrayEquals;
17+
import static org.junit.Assert.assertThat;
18+
19+
public class SqlTraceDataEntityReflectionTest {
20+
private final Map<String, String> expectedFields = new HashMap<String, String>() {
21+
{
22+
put("sql_trace_id", "bigint");
23+
put("blame_metric_name", "varchar");
24+
put("uri", "varchar");
25+
put("sql_id", "bigint");
26+
put("metric_name", "varchar");
27+
put("call_count", "int");
28+
put("min_time", "float");
29+
put("max_time", "float");
30+
put("application_id", "bigint");
31+
put("parameters", "varchar");
32+
put("total_time", "float");
33+
put("avg_time", "float");
34+
put("sql_info", "varchar");
35+
put("record_time", "bigint");
36+
put("agent_run_id", "bigint");
37+
put("user_id", "bigint");
38+
}
39+
};
40+
41+
private final Map<String, String> types = new HashMap<String, String>() {{
42+
put("long", "bigint");
43+
put("int", "int");
44+
put("float", "float");
45+
put("class java.lang.String", "varchar");
46+
}};
47+
48+
@Test
49+
public void dataSourceHasExpectedFields() throws ClassNotFoundException {
50+
Class<?> dataSourceEntity = Class.forName("com.oneapm.tps.agent.service.domain.SqltraceDataEntity");
51+
52+
List<String> actualFieldNames = new ArrayList<>();
53+
Map<String, String> actualFields = new HashMap<>();
54+
for (Field field : dataSourceEntity.getDeclaredFields()) {
55+
String nameWithUnderscore = LOWER_CAMEL.to(LOWER_UNDERSCORE, field.getName());
56+
actualFieldNames.add(nameWithUnderscore);
57+
actualFields.put(nameWithUnderscore, types.get(field.getType().toString()));
58+
59+
assertThat(field.getModifiers(), is(Modifier.PRIVATE));
60+
}
61+
62+
List<String> expectedNames = new ArrayList<>(expectedFields.keySet());
63+
Collections.sort(expectedNames);
64+
Collections.sort(actualFieldNames);
65+
66+
assertArrayEquals(expectedNames.toArray(), actualFieldNames.toArray());
67+
68+
for (String fieldName : expectedFields.keySet()) {
69+
assertThat(actualFields.containsKey(fieldName), is(true));
70+
71+
assertThat(
72+
String.format("field type of %s", fieldName),
73+
actualFields.get(fieldName),
74+
is(expectedFields.get(fieldName)));
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)