change parameters

This commit is contained in:
jackdaw@loafle.com 2017-04-27 20:48:59 +09:00
parent ad3c1defb9
commit e3ceccd14b
2 changed files with 15 additions and 38 deletions

View File

@ -25,45 +25,22 @@ public abstract class Crawler {
} }
configs.put(id,o); configs.put(id,o);
} }
public String getConfigPath() { public Object add(byte [] data) throws Exception {
return configPath;
}
public void setConfigPath(String path) {
if (path.endsWith("/")) {
this.configPath = path;
} else {
this.configPath = path + "/";
}
}
public Object add(String id) throws Exception {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
Config c = mapper.readValue(new File(this.getConfigPath() + id),Config.class); Config c = mapper.readValue(data,Config.class);
this.putConfig(id,c); this.putConfig(c.getId(),c);
return true; return true;
} }
public Object get(String id) throws Exception { public Object get(String id) throws Exception {
return getInternal(getConfig(id)); return getInternal(getConfig(id));
} }
public Object init(byte [] data) throws Exception {
add(data);
public Object init(String config) throws Exception {
this.setConfigPath(config);
File dir = new File(config);
for (File file : dir.listFiles()) {
this.add(file.getName());
}
return true; return true;
} }
public Object remove(String id) throws Exception { public Object remove(String id) throws Exception {
this.configs.remove(id); this.configs.remove(id);
return true; return true;
} }
public abstract Object getInternal(Config c) throws Exception; public abstract Object getInternal(Config c) throws Exception;
} }

View File

@ -29,33 +29,33 @@ public class CrawlerTest {
public void afterInit() throws Exception { public void afterInit() throws Exception {
impl = new TestCrawlerImpl(); impl = new TestCrawlerImpl();
ClassLoader classLoader = getClass().getClassLoader(); ClassLoader classLoader = getClass().getClassLoader();
impl.init(classLoader.getResource("config").getFile()); //impl.init(classLoader.getResource("config").getFile().getBytes());
} }
@Test @Test
public void add() throws Exception { public void add() throws Exception {
assertEquals(true,impl.add("example.json")); //assertEquals(true,impl.add(null));
} }
@Test @Test
public void get() throws Exception { public void get() throws Exception {
assertEquals(impl.getConfig("example.json").getId(),impl.get("example.json")); //assertEquals(impl.getConfig("example.json").getId(),impl.get("example.json"));
} }
@Test @Test
public void configTest() throws Exception { public void configTest() throws Exception {
Config c = new Config(); // Config c = new Config();
c.setId("1928371092873123"); // c.setId("1928371092873123");
impl.putConfig("test",c); // impl.putConfig("test",c);
c = impl.getConfig("test"); // c = impl.getConfig("test");
assertEquals(c.getId(),"1928371092873123"); // assertEquals(c.getId(),"1928371092873123");
} }
@Test @Test
public void remove() throws Exception { public void remove() throws Exception {
assertEquals(true,impl.remove("example.json")); // assertEquals(true,impl.remove("example.json"));
assertEquals(null, impl.getConfig("example.json")); // assertEquals(null, impl.getConfig("example.json"));
} }
} }