Mercurial > hg > release > thermostat-1.4
changeset 1516:c880893faba9
Add maven module for testing prepared statement parsing.
Reviewed-by: omajid
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2014-September/011072.html
author | Severin Gehwolf <sgehwolf@redhat.com> |
---|---|
date | Mon, 29 Sep 2014 14:20:48 +0200 |
parents | 1744e42fc8b5 |
children | 0cd7a5d5f925 |
files | storage/pom.xml storage/testutils/pom.xml storage/testutils/src/main/java/com/redhat/thermostat/storage/internal/statement/DescriptorParserImplFactory.java storage/testutils/src/main/java/com/redhat/thermostat/storage/internal/statement/TestParser.java storage/testutils/src/main/java/com/redhat/thermostat/storage/testutils/ParserType.java storage/testutils/src/main/java/com/redhat/thermostat/storage/testutils/StatementDescriptorTester.java storage/testutils/src/test/java/com/redhat/thermostat/storage/testutils/StatementDescriptorTesterTest.java vm-cpu/common/pom.xml vm-cpu/common/src/test/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatDAOTest.java |
diffstat | 9 files changed, 440 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/storage/pom.xml Wed Sep 17 11:36:58 2014 +0200 +++ b/storage/pom.xml Mon Sep 29 14:20:48 2014 +0200 @@ -62,6 +62,7 @@ <module>core</module> <module>cli</module> <module>mongo</module> + <module>testutils</module> </modules> </project>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/testutils/pom.xml Mon Sep 29 14:20:48 2014 +0200 @@ -0,0 +1,73 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Copyright 2012-2014 Red Hat, Inc. + + This file is part of Thermostat. + + Thermostat is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 2, or (at your + option) any later version. + + Thermostat is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Thermostat; see the file COPYING. If not see + <http://www.gnu.org/licenses/>. + + Linking this code with other modules is making a combined work + based on this code. Thus, the terms and conditions of the GNU + General Public License cover the whole combination. + + As a special exception, the copyright holders of this code give + you permission to link this code with independent modules to + produce an executable, regardless of the license terms of these + independent modules, and to copy and distribute the resulting + executable under terms of your choice, provided that you also + meet, for each linked independent module, the terms and conditions + of the license of that module. An independent module is a module + which is not derived from or based on this code. If you modify + this code, you may extend this exception to your version of the + library, but you are not obligated to do so. If you do not wish + to do so, delete this exception statement from your version. + +--> +<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> + + <parent> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-storage</artifactId> + <version>1.1.0-SNAPSHOT</version> + </parent> + + <artifactId>thermostat-storage-testutils</artifactId> + <!-- Intentionally not a bundle. This module + provides utilities for testing statement + descriptors outside of storage-core --> + <packaging>jar</packaging> + + <name>Thermostat Storage Test Utils</name> + + <dependencies> + <!-- Uses classes from storage-core --> + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-storage-core</artifactId> + <version>${project.version}</version> + </dependency> + <!-- Intentionally not scope "test" since this utility uses mockito for + stubbing in src/main/java. However, this test utility + should only be used as a dep with scope "test". --> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + </dependency> + </dependencies> + +</project> +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/testutils/src/main/java/com/redhat/thermostat/storage/internal/statement/DescriptorParserImplFactory.java Mon Sep 29 14:20:48 2014 +0200 @@ -0,0 +1,84 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.internal.statement; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.redhat.thermostat.storage.core.Add; +import com.redhat.thermostat.storage.core.AggregateQuery; +import com.redhat.thermostat.storage.core.AggregateQuery.AggregateFunction; +import com.redhat.thermostat.storage.core.BackingStorage; +import com.redhat.thermostat.storage.core.Category; +import com.redhat.thermostat.storage.core.Query; +import com.redhat.thermostat.storage.core.Remove; +import com.redhat.thermostat.storage.core.Replace; +import com.redhat.thermostat.storage.core.StatementDescriptor; +import com.redhat.thermostat.storage.core.Update; +import com.redhat.thermostat.storage.model.Pojo; +import com.redhat.thermostat.storage.testutils.ParserType; + +class DescriptorParserImplFactory { + + <T extends Pojo> StatementDescriptorParser<T> getParser(ParserType type, StatementDescriptor<T> desc) { + switch (type) { + case BASIC: + return new BasicDescriptorParser<>(getBasicBackingStorage(), desc); + case SEMANTIC: + return new SemanticsEnabledDescriptorParser<>(getSemanticBackingStorage(), desc); + default: + throw new IllegalStateException("Not implemented"); + } + } + + private BackingStorage getBasicBackingStorage() { + return mock(BackingStorage.class); + } + + @SuppressWarnings("unchecked") + private BackingStorage getSemanticBackingStorage() { + BackingStorage storage = mock(BackingStorage.class); + when(storage.createAdd(any(Category.class))).thenReturn(mock(Add.class)); + when(storage.createUpdate(any(Category.class))).thenReturn(mock(Update.class)); + when(storage.createRemove(any(Category.class))).thenReturn(mock(Remove.class)); + when(storage.createReplace(any(Category.class))).thenReturn(mock(Replace.class)); + when(storage.createQuery(any(Category.class))).thenReturn(mock(Query.class)); + when(storage.createAggregateQuery(any(AggregateFunction.class), any(Category.class))).thenReturn(mock(AggregateQuery.class)); + return storage; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/testutils/src/main/java/com/redhat/thermostat/storage/internal/statement/TestParser.java Mon Sep 29 14:20:48 2014 +0200 @@ -0,0 +1,68 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.internal.statement; + +import com.redhat.thermostat.storage.core.DescriptorParsingException; +import com.redhat.thermostat.storage.core.ParsedStatement; +import com.redhat.thermostat.storage.core.StatementDescriptor; +import com.redhat.thermostat.storage.model.Pojo; +import com.redhat.thermostat.storage.testutils.ParserType; +import com.redhat.thermostat.storage.testutils.StatementDescriptorTester; + +/** + * Do NOT use this class directly. Use {@link StatementDescriptorTester} instead. + */ +public final class TestParser<T extends Pojo> implements StatementDescriptorParser<T> { + + private final StatementDescriptor<T> desc; + private final DescriptorParserImplFactory factory; + private final ParserType type; + + public TestParser(StatementDescriptor<T> desc, ParserType type) { + this.desc = desc; + this.factory = new DescriptorParserImplFactory(); + this.type = type; + } + + @Override + public ParsedStatement<T> parse() throws DescriptorParsingException { + StatementDescriptorParser<T> actualParser = factory.getParser(type, desc); + actualParser.parse(); + return null; // Always return null. Testing utility only. + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/testutils/src/main/java/com/redhat/thermostat/storage/testutils/ParserType.java Mon Sep 29 14:20:48 2014 +0200 @@ -0,0 +1,42 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.testutils; + +public enum ParserType { + BASIC, + SEMANTIC +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/testutils/src/main/java/com/redhat/thermostat/storage/testutils/StatementDescriptorTester.java Mon Sep 29 14:20:48 2014 +0200 @@ -0,0 +1,62 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.testutils; + +import com.redhat.thermostat.storage.core.DescriptorParsingException; +import com.redhat.thermostat.storage.core.StatementDescriptor; +import com.redhat.thermostat.storage.internal.statement.TestParser; +import com.redhat.thermostat.storage.model.Pojo; + +/** + * Unit test utility which is useful for verifying statement descriptors parse + * correctly. + * + * @param <T> The underlying {@link Pojo} implementation type. + */ +public final class StatementDescriptorTester<T extends Pojo> { + + public void testParseBasic(StatementDescriptor<T> desc) + throws DescriptorParsingException { + TestParser<T> testParser = new TestParser<>(desc, ParserType.BASIC); + testParser.parse(); + } + + public void testParseSemantic(StatementDescriptor<T> desc) throws DescriptorParsingException { + TestParser<T> testParser = new TestParser<>(desc, ParserType.SEMANTIC); + testParser.parse(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/testutils/src/test/java/com/redhat/thermostat/storage/testutils/StatementDescriptorTesterTest.java Mon Sep 29 14:20:48 2014 +0200 @@ -0,0 +1,89 @@ +/* + * Copyright 2012-2014 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.testutils; + +import org.junit.Test; + +import com.redhat.thermostat.storage.core.Category; +import com.redhat.thermostat.storage.core.DescriptorParsingException; +import com.redhat.thermostat.storage.core.Key; +import com.redhat.thermostat.storage.core.StatementDescriptor; +import com.redhat.thermostat.storage.model.Pojo; + +public class StatementDescriptorTesterTest { + + private static final Key<String> barKey = new Key<>("bar"); + private static final Category<TestPojo> testCategory = new Category<>("desc-tester-category", TestPojo.class, barKey); + + @Test(expected = DescriptorParsingException.class) + public void canDetermineBadDescriptorBasic() throws DescriptorParsingException { + String brokenDesc = "foo"; + StatementDescriptor<TestPojo> desc = new StatementDescriptor<>(testCategory, brokenDesc); + StatementDescriptorTester<TestPojo> tester = new StatementDescriptorTester<>(); + tester.testParseBasic(desc); // should throw exception + } + + public void canDetermineGoodDescriptorBasic() throws DescriptorParsingException { + // This desc has correct syntax, but is semantically incorrect. The + // basic parser does not check this though. + String goodDesc = "QUERY desc-tester-category WHERE 'foo' = ?l"; + StatementDescriptor<TestPojo> desc = new StatementDescriptor<>(testCategory, goodDesc); + StatementDescriptorTester<TestPojo> tester = new StatementDescriptorTester<>(); + tester.testParseBasic(desc); // must not throw exception + } + + @Test(expected = DescriptorParsingException.class) + public void canDetermineBadDescriptorSemantic() throws DescriptorParsingException { + // No such key 'foo' in category. Thus, DPE on parse() + String brokenDesc = "ADD desc-tester-category SET 'foo' = ?l"; + StatementDescriptor<TestPojo> desc = new StatementDescriptor<>(testCategory, brokenDesc); + StatementDescriptorTester<TestPojo> tester = new StatementDescriptorTester<>(); + tester.testParseSemantic(desc); // should throw exception + } + + @Test + public void canDetermineGoodDescriptorSemantic() throws DescriptorParsingException { + String goodDesc = "ADD desc-tester-category SET 'bar' = ?l"; + StatementDescriptor<TestPojo> desc = new StatementDescriptor<>(testCategory, goodDesc); + StatementDescriptorTester<TestPojo> tester = new StatementDescriptorTester<>(); + tester.testParseSemantic(desc); // must not throw exception + } + + static class TestPojo implements Pojo { + // empty + } +}
--- a/vm-cpu/common/pom.xml Wed Sep 17 11:36:58 2014 +0200 +++ b/vm-cpu/common/pom.xml Mon Sep 29 14:20:48 2014 +0200 @@ -124,5 +124,11 @@ <version>${project.version}</version> <scope>test</scope> </dependency> + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-storage-testutils</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> </dependencies> </project>
--- a/vm-cpu/common/src/test/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatDAOTest.java Wed Sep 17 11:36:58 2014 +0200 +++ b/vm-cpu/common/src/test/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatDAOTest.java Mon Sep 29 14:20:48 2014 +0200 @@ -38,6 +38,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -60,6 +61,7 @@ import com.redhat.thermostat.storage.core.StatementExecutionException; import com.redhat.thermostat.storage.core.Storage; import com.redhat.thermostat.storage.core.VmRef; +import com.redhat.thermostat.storage.testutils.StatementDescriptorTester; import com.redhat.thermostat.vm.cpu.common.VmCpuStatDAO; import com.redhat.thermostat.vm.cpu.common.model.VmCpuStat; @@ -84,6 +86,19 @@ "'cpuLoad' = ?d"; assertEquals(addCpuStat, VmCpuStatDAOImpl.DESC_ADD_VM_CPU_STAT); } + + @Test + public void canParseDescriptor() { + StatementDescriptorTester<VmCpuStat> tester = new StatementDescriptorTester<>(); + StatementDescriptor<VmCpuStat> desc = new StatementDescriptor<>(VmCpuStatDAO.vmCpuStatCategory, VmCpuStatDAOImpl.DESC_ADD_VM_CPU_STAT); + try { + tester.testParseBasic(desc); + tester.testParseSemantic(desc); + // pass + } catch (DescriptorParsingException e) { + fail(e.getMessage()); + } + } @Test public void testCategory() {