Added: devicemap/trunk/clients/1.0/java/client/src/test/java/org/apache/devicemap/DeviceMapClientUrlTestOptional.java URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/java/client/src/test/java/org/apache/devicemap/DeviceMapClientUrlTestOptional.java?rev=1717136&view=auto ============================================================================== --- devicemap/trunk/clients/1.0/java/client/src/test/java/org/apache/devicemap/DeviceMapClientUrlTestOptional.java (added) +++ devicemap/trunk/clients/1.0/java/client/src/test/java/org/apache/devicemap/DeviceMapClientUrlTestOptional.java Sun Nov 29 23:36:18 2015 @@ -0,0 +1,82 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ +package org.apache.devicemap; + +import java.io.FileNotFoundException; + +import org.apache.devicemap.data.Device; +import org.junit.Assert; +import org.junit.Test; +import org.apache.devicemap.loader.LoaderOption; + +public class DeviceMapClientUrlTestOptional { + + @Test + public void deviceMapClientUrlTest() throws Exception { + DeviceMapClient client = new DeviceMapClient(); + + client.initDeviceData(LoaderOption.URL); + + String test = "Mozilla/5.0 (Linux; U; Android 2.2; en; HTC Aria A6380 Build/ERE27) AppleWebKit/540.13+ (KHTML, like Gecko) Version/3.1 Mobile Safari/524.15.0"; + + Device device = client.classifyDevice(test); + + Assert.assertEquals("test ua not htc aria", "HTC Aria", device.getId()); + } + + @Test + public void deviceMapClientCustomUrlTest() throws Exception { + DeviceMapClient client = new DeviceMapClient(); + + client.initDeviceData(LoaderOption.URL, "http://devicemap-vm.apache.org/data/latest/"); + + String test = "Mozilla/5.0 (Linux; U; Android 2.2; en; HTC Aria A6380 Build/ERE27) AppleWebKit/540.13+ (KHTML, like Gecko) Version/3.1 Mobile Safari/524.15.0"; + + Device device = client.classifyDevice(test); + + Assert.assertEquals("test ua not htc aria", "HTC Aria", device.getId()); + } + + @Test + public void deviceMapClientUrlFailureTest() throws Exception { + DeviceMapClient client = new DeviceMapClient(); + + try { + client.initDeviceData(LoaderOption.URL,"http://example.com/fail"); + Assert.fail("FileNotFoundException expected with invalid URL path"); + } catch(FileNotFoundException fnf) { + //pass + } catch(Exception ex) { + Assert.fail("FileNotFoundException expected with invalid URL path, got: "+ex.toString()); + } + } + + @Test + public void deviceMapFactoryUrlTest() throws Exception { + String test = "Mozilla/5.0 (Linux; U; Android 2.2; en; HTC Aria A6380 Build/ERE27) AppleWebKit/540.13+ (KHTML, like Gecko) Version/3.1 Mobile Safari/524.15.0"; + + Device device = DeviceMapFactory.getClient(LoaderOption.URL).classifyDevice(test); + + Assert.assertEquals("test ua not htc aria", "HTC Aria", device.getId()); + + device = DeviceMapFactory.getClient(LoaderOption.URL).classifyDevice(test); + + Assert.assertEquals("2nd test ua not htc aria", "HTC Aria", device.getId()); + } +}
Added: devicemap/trunk/clients/1.0/java/client/src/test/java/org/apache/devicemap/loader/parser/XMLParserTest.java URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/1.0/java/client/src/test/java/org/apache/devicemap/loader/parser/XMLParserTest.java?rev=1717136&view=auto ============================================================================== --- devicemap/trunk/clients/1.0/java/client/src/test/java/org/apache/devicemap/loader/parser/XMLParserTest.java (added) +++ devicemap/trunk/clients/1.0/java/client/src/test/java/org/apache/devicemap/loader/parser/XMLParserTest.java Sun Nov 29 23:36:18 2015 @@ -0,0 +1,51 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ +package org.apache.devicemap.loader.parser; + +import java.io.ByteArrayInputStream; +import java.io.InputStreamReader; +import org.junit.Assert; +import org.junit.Test; + +public class XMLParserTest { + + @Test + public void parserTest() throws Exception { + final String firstTag = "<tag attr=\"att"rv\">"; + final String xml = firstTag + + "</tag>\n" + + "<!-- <comment>\n" + + "something here -->" + + "<real>"value"&here<</real>" + + "<!-- bye "; + + XMLParser parser = new XMLParser(new InputStreamReader(new ByteArrayInputStream(xml.getBytes("UTF-8")))); + + String tag = parser.getNextTag(); + + Assert.assertEquals("First tag isnt <tag>", firstTag, tag); + Assert.assertEquals("<tag> attribute is correct", "att\"rv", XMLParser.getAttribute(tag, "attr")); + Assert.assertEquals("2ns tag isnt </tag>", "</tag>", parser.getNextTag()); + Assert.assertEquals("3rd tag isnt <real>", "<real>", parser.getNextTag()); + Assert.assertEquals("3rd tag value is incorrect", "\"value\"&here<", parser.getTagValue()); + Assert.assertEquals("4th tag isnt </real>", "</real>", parser.getNextTag()); + Assert.assertEquals("Parser didnt return empty", "", parser.getNextTag()); + Assert.assertEquals("Parser didnt return empty second time", "", parser.getNextTag()); + } +}
