Java tutorial
/* Copyright (c) 2007 HomeAway, Inc. * All rights reserved. http://www.atomserver.org * * Licensed 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.atomserver.core; import junit.framework.TestCase; import org.apache.abdera.Abdera; import org.apache.abdera.protocol.server.ServiceContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.atomserver.EntryType; import org.atomserver.exceptions.BadRequestException; import org.atomserver.testutils.client.MockRequestContext; import org.atomserver.uri.EntryTarget; import org.atomserver.uri.FeedTarget; import org.atomserver.uri.URIHandler; import org.atomserver.utils.AtomDate; import org.atomserver.utils.conf.ConfigurationAwareClassLoader; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.Date; import java.util.Locale; public class EntryURIHelperTest extends TestCase { private static final Log log = LogFactory.getLog(EntryURIHelperTest.class); protected URIHandler handler = null; protected ServiceContext serviceContext = null; static private final String CONTEXT_NAME = "org.apache.abdera.protocol.server.ServiceContext"; protected String baseURI; protected void setUp() throws Exception { String[] configs = { "/org/atomserver/spring/propertyConfigurerBeans.xml", "/org/atomserver/spring/databaseBeans.xml", "/org/atomserver/spring/storageBeans.xml", "/org/atomserver/spring/logBeans.xml", "/org/atomserver/spring/abderaBeans.xml" }; ClassPathXmlApplicationContext springFactory = new ClassPathXmlApplicationContext(configs, false); springFactory.setClassLoader(new ConfigurationAwareClassLoader(springFactory.getClassLoader())); springFactory.refresh(); handler = ((AbstractAtomService) springFactory.getBean("org.atomserver-atomService")).getURIHandler(); serviceContext = (ServiceContext) springFactory.getBean(CONTEXT_NAME); if (serviceContext.getAbdera() == null) { serviceContext.init(new Abdera(), null); } baseURI = handler.getServiceBaseUri(); } public void testDate() { try { String ddd = "12012007"; AtomDate.parse(ddd); fail("this should fail"); } catch (IllegalArgumentException ee) { log.error(ee); } try { String ddd = "2007-12-01"; Date date = AtomDate.parse(ddd); log.debug("********************* date= " + date); } catch (IllegalArgumentException ee) { log.error(ee); fail("this should NOT fail"); } } @SuppressWarnings("deprecation") public void testDate2() { try { String iri = "http://foobar:7890/" + baseURI + "/widgets/acme/?updated-min=2007-11-30T23:59:59:000Z"; handler.getFeedTarget(new MockRequestContext(serviceContext, "GET", iri)); } catch (IllegalArgumentException ee) { log.error(ee); } try { // This date is WRONG -- should be 59.000Z !!! // NOTE: it should fail -- but it does NOT String ddd = "2007-11-30T23:59:59:000Z"; Date upmin = AtomDate.parse(ddd); log.debug("*********2007-11-30T23:59:59:000Z************ upmin= " + upmin); } catch (IllegalArgumentException ee) { log.error(ee); } String ddd2 = "2007-11-30T23:59:59.000Z"; Date upmin2 = AtomDate.parse(ddd2); log.debug("********2007-11-30T23:59:59.000Z************* upmin2= " + upmin2); log.debug("********2007-11-30T23:59:59.000Z************* upmin2 GMT= " + upmin2.toGMTString()); String iri2 = "http://stgbrss:7890/" + baseURI + "/widgets/acme?updated-min=2007-11-30T23:59:59+06:00"; FeedTarget entryURIData = handler.getFeedTarget(new MockRequestContext(serviceContext, "GET", iri2)); Date upmin = entryURIData.getUpdatedMinParam(); log.debug("********2007-11-30T23:59:59+06:00************* upmin= " + upmin); log.debug("********2007-11-30T23:59:59+06:00************* upmin GMT= " + upmin.toGMTString()); } public void testSuccess() throws Exception { checkUriData(checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.en.xml", false), "widgets", "acme", "1234", Locale.ENGLISH); checkUriData(checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234?locale=en_GB", false), "widgets", "acme", "1234", Locale.UK); assertEquals(42, checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.en.xml?start-index=42", false) .getStartIndexParam()); assertEquals(8675309, checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.en.xml?max-results=8675309", false) .getMaxResultsParam()); assertEquals(0, checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.en.xml", false).getRevision()); assertEquals(4, checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.en.xml/4?max-results=8675309", false).getRevision()); assertEquals(URIHandler.REVISION_OVERRIDE, checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.en.xml/*", false).getRevision()); assertEquals(URIHandler.REVISION_OVERRIDE, checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.en.xml/*?start-index=15", false) .getRevision()); Date now = new Date(); assertEquals(now, checkEntryIRI( "http://whatever/" + baseURI + "/widgets/acme/1234.en.xml?updated-min=" + AtomDate.format(now), false).getUpdatedMinParam()); assertEquals(Locale.US, checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234?locale=en_US", false) .getLocaleParam()); assertEquals(EntryType.full, checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.en.xml?entry-type=full", false) .getEntryTypeParam()); assertEquals(EntryType.link, checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.en.xml?entry-type=link", false) .getEntryTypeParam()); } public void testError() throws Exception { checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.xml", true); checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.en.xml?start-index=pi", true); checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.en.xml?max-results=pi", true); checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.en.xml?updated-min=NEVER", true); checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.en.xml?locale=aldsfkjasldfksdf", true); checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/1234.en.xml?entry-type=blah", true); } //>>> public void testMissingId() throws Exception { checkEntryIRI("http://whatever/" + baseURI + "/widgets/acme/.en.xml", true); } private EntryTarget checkEntryIRI(String iri, boolean expectBadUrlException) { log.debug("testing URI [" + iri + "] expecting " + (expectBadUrlException ? "failure" : "success")); try { EntryTarget entryURIData = handler.getEntryTarget(new MockRequestContext(serviceContext, "GET", iri), true); // get all of the parameters - we don't care what the values are, but these should not throw // execptions (unless we EXPECT a BadUrlException, in which case we need to know that, too!) entryURIData.getLocaleParam(); entryURIData.getStartIndexParam(); entryURIData.getMaxResultsParam(); entryURIData.getUpdatedMinParam(); entryURIData.getEntryTypeParam(); assertFalse(expectBadUrlException); return entryURIData; } catch (BadRequestException e) { assertTrue(expectBadUrlException); return null; } } private void checkUriData(EntryTarget entryURIData, String workspace, String collection, String entryId, Locale locale) { assertEquals(workspace, entryURIData.getWorkspace()); assertEquals(collection, entryURIData.getCollection()); assertEquals(entryId, entryURIData.getEntryId()); assertEquals(locale, entryURIData.getLocale()); } }