Skip to content

Commit

Permalink
#728 XeMemory
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 8, 2017
1 parent 1227105 commit a6de71d
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/main/java/org/takes/rs/xe/XeMemory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2014-2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.rs.xe;

import lombok.EqualsAndHashCode;
import org.xembly.Directive;
import org.xembly.Directives;

/**
* Xembly source to report memory usage.
*
* <p>Add this Xembly source to your page like this:
*
* <pre> new RsXembly(
* new XsStylesheet("/xsl/home.xsl"),
* new XsAppend(
* "page",
* new XsMemory()
* )
* )</pre>
*
* <p>And expect this attribute in the XML:
*
* <pre>&lt;?xml version="1.0"?&gt;
* &lt;?xml-stylesheet href="/xsl/home.xsl" type="text/xsl"?&gt;
* &lt;page&gt;
* &lt;memory&gt;free="1344" max="2048" total="334"&lt;/memory&gt;
* &lt;/page&gt;
* </pre>
*
* <p>The class is immutable and thread-safe.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 1.2
*/
@EqualsAndHashCode(callSuper = true)
public final class XeMemory extends XeWrap {

/**
* Ctor.
*/
public XeMemory() {
this("memory");
}

/**
* Ctor.
* @param node Node name
*/
public XeMemory(final CharSequence node) {
super(
new XeSource() {
@Override
public Iterable<Directive> toXembly() {
return new Directives().add(node.toString())
.attr(
"total",
XeMemory.mbs(Runtime.getRuntime().totalMemory())
)
.attr(
"free",
XeMemory.mbs(Runtime.getRuntime().freeMemory())
)
.attr(
"max",
XeMemory.mbs(Runtime.getRuntime().maxMemory())
);
}
}
);
}

/**
* Format memory.
* @param bytes Bytes
* @return Mbytes
*/
private static long mbs(final long bytes) {
// @checkstyle MagicNumber (1 line)
return bytes >> 20;
}

}
19 changes: 19 additions & 0 deletions src/main/java/org/takes/rs/xe/XeMillis.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@
/**
* Xembly source to create "millis" element at the root.
*
* <p>Add this Xembly source to your page like this:
*
* <pre> new RsXembly(
* new XsStylesheet("/xsl/home.xsl"),
* new XsAppend(
* "page",
* new XsMillis()
* )
* )</pre>
*
* <p>And expect this attribute in the XML:
*
* <pre>&lt;?xml version="1.0"?&gt;
* &lt;?xml-stylesheet href="/xsl/home.xsl" type="text/xsl"?&gt;
* &lt;page&gt;
* &lt;millis&gt;3.675&lt;/millis&gt;
* &lt;/page&gt;
* </pre>
*
* <p>The class is immutable and thread-safe.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/org/takes/rs/xe/XeMemoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2014-2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.rs.xe;

import com.jcabi.matchers.XhtmlMatchers;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.hamcrest.MatcherAssert;
import org.junit.Test;

/**
* Test case for {@link XeMemory}.
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 1.2
*/
public final class XeMemoryTest {

/**
* XeMemory can build XML response.
* @throws IOException If some problem inside
*/
@Test
public void buildsXmlResponse() throws IOException {
MatcherAssert.assertThat(
IOUtils.toString(
new RsXembly(
new XeAppend(
"root",
new XeMemory()
)
).body()
),
XhtmlMatchers.hasXPaths(
"/root/memory[@total and @max and @free]"
)
);
}

}

0 comments on commit a6de71d

Please sign in to comment.